【问题标题】:Pygame: Text Won't Appear Only BackgroundPygame:文本不会仅出现在背景中
【发布时间】:2013-12-30 18:16:26
【问题描述】:

我刚刚将一些我尝试过的代码作为记分板添加到我的游戏中,但我遇到了错误。只有我的背景会显示。不会出现任何文字或任何内容。这是我正在使用的代码:

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 name())那么为什么它在这里不起作用?

【问题讨论】:

  • 请提供您的问题的完整最小示例。
  • 这是唯一需要的代码。问题就在那里。我的文字没有显示。只有背景
  • 我是说它不是最小的。几乎不需要发布超过一百行来说明问题。

标签: python python-2.7 pygame


【解决方案1】:
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()

【讨论】:

  • 谢谢。那是我的错误应该仔细观察。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 2012-12-29
  • 2016-05-16
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多