我刚刚在一些代码中添加了我作为记分板尝试进入我的游戏并且遇到了错误。只显示我的背景。不会出现任何文字或任何内容。这是我正在使用的代码:
class MenuScores(MenuClass):
def __init__(self, surface, engine):
MenuClass.__init__(self, surface)
self.MoonSurvival = engine
self.text = "Name"
def name():
size = 36
screen = pygame.display.set_mode((1280, 720))
name = ""
font = pygame.font.SysFont('data/fonts/score.ttf', size)
background = pygame.image.load('data/images/bg.jpg')
while True:
# readlines returns a list; having this in
# loop allows pygame to draw recently added
# string; no need to close and open a window
namefile = open('score.txt', 'r')
names = namefile.readlines()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.unicode.isalpha():
if len(name) < 4:
if event.unicode == 3:
name += 0
else:
name += event.unicode
elif event.key == K_BACKSPACE:
name = name[:-1]
elif event.key == K_RETURN:
f = open("data/scores/score.txt", "a")
f.write(str(name) + " " + str(self.MoonSurvival.total_score) + "\n")
f.close()
name = ""
self.text = ""
elif event.type == QUIT:
return
# create a Rectangle container, where yours
# text variable will be drawn
# Rect(left, top, width, height)
textrect = Rect(0, 0, 100, size)
screen.fill((0, 0, 0))
for i in names:
# iterate through lines from text file (it is stored
# in names variable and it is a list)
# create text variable and draw it to textrect
text = font.render(i[:-1], True, (255,0,0), (0,0,0))
screen.blit(text, textrect)
# change y coordinate of textrect; in next iteration
# next line will appear below the previous line
textrect.centery += size
block = font.render(name, True, (255, 255, 255))
rect = block.get_rect()
rect.center = screen.get_rect().center
screen.blit(background, (0, 0))
screen.blit(block, rect)
pygame.display.update()
pygame.display.flip()
def draw(self):
self.renderText()
self.drawHint()
self.surface.blit(self.background, (0, 0))
# update surface
pygame.display.update()
def _handleEvents(self, event):
def pressed(key):
keys = pygame.key.get_pressed()
if keys[key]:
return True
else:
return False
if pressed(pygame.K_SPACE):
self.MoonSurvival.game_state = STATE_MENU
self.MoonSurvival.level = 1
def renderText(self):
# split texts at \n (newline)
texts = self.text.split('\n')
for i in range(len(texts)):
textSurface = self.menufont.render(texts[i], 0, (255, 0, 0))
textRect = textSurface.get_rect()
textRect.centerx = SCREEN_WIDTH / 2
textRect.centery = SCREEN_HEIGHT / 2 + i * self.menufont.size(texts[i])[1]
self.surface.blit(textSurface, textRect)
def drawHint(self):
textSurface = self.menufont.render('(Press SPACE to return to menu)', 0, (255, 0, 0))
textRect = textSurface.get_rect()
textRect.centerx = SCREEN_WIDTH / 2
textRect.centery = SCREEN_HEIGHT - 50
self.surface.blit(textSurface, textRect)
我真的不知道如何解决这个问题。当文本独立时(def名称())文本显得很好所以为什么它在这里不起作用?
答案 0 :(得分:0)
self.renderText()
self.drawHint()
self.surface.blit(self.background, (0, 0))
在这里,您要在self.surface
上绘制文字,然后在self.surface
上绘制提示,然后在self.surface
上绘制背景图片,覆盖其他所有内容。
相反,您应首先绘制背景,然后绘制其后的所有内容。
self.surface.blit(self.background, (0, 0))
self.renderText()
self.drawHint()