如何在背景上显示文本?

2024-09-30 22:13:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个小游戏,我需要把一些文字的背景,以显示生活。但是文本没有出现在背景上,我也不知道为什么。感谢您的帮助。所有的绘图代码都在redrawgamewindow函数中。我知道这与背景有关,也许是放在文本上,但我该怎么解决这个问题呢

import random
import math
pygame.init()
GOLD = (255, 215, 0)
def text_objects(text, font):
    textSurface = font.render(text, True, GOLD)
    return textSurface, textSurface.get_rect()
screenwidth = 500
screenheight = 500
win = pygame.display.set_mode((screenwidth, screenheight))
pygame.display.set_caption('First Game')
bkg = pygame.image.load('2.jpg')
bkg = pygame.transform.scale(bkg, (500,500))
char1 = pygame.image.load('guy.png')
char1 = pygame.transform.scale(char1, (100, 100))
walkRight = []
walkLeft = []
HitAnim = []

Howmany = 4
for i in range(1, 13):
    walkRight.append(pygame.transform.scale(pygame.image.load('R' + str(i) + '.png'), (100, 100)))
for i in range(1, 13):
    walkLeft.append(pygame.transform.scale(pygame.image.load('L' + str(i) + '.png'), (100, 100)))
rockboom = pygame.image.load('b1.png')
rockboom = pygame.transform.scale(rockboom, (100,100))
GameO = pygame.image.load('GO.jpg')
rock = pygame.image.load('b.png')
rock = pygame.transform.scale(rock, (100, 100))
clock = pygame.time.Clock()


class player(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.GameOver = False
        self.vel = 10
        self.walkCount = 0
        self.left = False
        self.right = False
        self.lives = 3
        self.hit = 0
        self.hit1 = False
    def draw(self, win):
        if self.walkCount + 1 >= 27:
            self.walkCount = 0

        if self.left == True:
            win.blit(walkLeft[self.walkCount // 3], (self.x, self.y))
            self.walkCount += 1
            pygame.display.update()
        elif self.right == True :
            win.blit(walkRight[self.walkCount // 3], (self.x, self.y))
            self.walkCount += 1
        else:
            win.blit(char1, (self.x, self.y))


class rocky():
    def __init__(self):
        self.x = random.randrange(0, 430)
        self.y = -100

    def draw(self, win):
        win.blit(rock, (self.x, self.y))
        if man.hit1 == True:
            win.blit (rockboom, (self.x, self.y))



def redrawgamewindow():
    win.blit(bkg, (0, 0))
    man.draw(win)
    rock1.draw(win)
    largeText = pygame.font.Font('freesansbold.ttf', 45)
    TextSurf, TextRect = text_objects("Lives:" + str(man.lives), largeText)
    TextRect.center = ((screenwidth / 2), (100 / 2))


    pygame.display.flip()

def collided (rockx, rocky, manx, many):
    man.hit = 0
    distance = math.sqrt((math.pow(rockx-manx, 2) + (math.pow(rocky-many, 2))))
    if distance < 75:
        man.hit += 1
        print("we be touched")
        man.hit1 = True
    else:
        man.hit1 = False


my_list= []

for number in range(Howmany):
    my_object = rocky()
    my_list.append(my_object)
rock1 = rocky()

man = player(250, 350, 100, 100)
run = True
while run:
    # Setting fps
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False
    # Getting keys pressed
    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and man.x > 0:
        man.x -= man.vel
        man.left = True
        man.right = False

    elif keys[pygame.K_RIGHT] and man.x < 500 - man.vel - man.width:
        man.x += man.vel
        man.left = False
        man.right = True
    else:
        man.left = False
        man.right = False
        man.walkCount = 0

    for rockk in my_list:
        rock1.draw(win)

    rock1.y += 5
    if rock1.y >= 500:
        rock1.x = random.randrange(0, 430)
        rock1.y = -100
    if man.hit1 == True:
        rock1.x = random.randrange(0, 430)
        rock1.y = -100

    if man.hit == 1:
        man.lives -=1
    if man.hit ==2:
        man.lives -=1
    if man.lives <= 0:
        print("so uh yeah it worked cool ")
        exit()

    collided(rock1.x, rock1.y, man.x, man.y)

    redrawgamewindow()

    win.blit(pygame.image.load('R1.png').convert_alpha(), (200, 200))

Tags: imageselffalsetrueifdeftransformload
1条回答
网友
1楼 · 发布于 2024-09-30 22:13:57

你不是在发短信。试试这个:

def redrawgamewindow():
    win.blit(bkg, (0, 0))
    man.draw(win)
    rock1.draw(win)

    largeText = pygame.font.Font('freesansbold.ttf', 45)
    TextSurf, TextRect = text_objects("Lives:" + str(man.lives), largeText)
    TextRect.center = ((screenwidth / 2), (100 / 2))
    win.blit(TextSurf,TextRect)

    pygame.display.flip()

相关问题 更多 >