PyGame: text not appearing

十年热恋 提交于 2021-01-27 13:59:01

问题


i was following a tutorial and i'm trying to get my text to appear in the screen, here is my code but the text won't appear:

from __future__ import division
import math
import sys
import pygame


class MyGame(object):
    def __init__(self):
        pygame.mixer.init()
        pygame.mixer.pre_init(44100, -16, 2, 2048)
        pygame.init()

        self.width = 800
        self.height = 600
        self.screen = pygame.display.set_mode((self.width, self.height))

        self.bg_color = 0, 0, 0

        font = pygame.font.Font(None, 100)

        text = font.render("text that should appear", True, 238, 58, 140)


        self.FPS = 30
        self.REFRESH = pygame.USEREVENT+1
        pygame.time.set_timer(self.REFRESH, 1000//self.FPS)


    def run(self):
        running = True
        while running:
            event = pygame.event.wait()

            if event.type == pygame.QUIT:
                running = False

            elif event.type == self.REFRESH:
                self.draw()

            else:
                pass 

    def draw(self):
        self.screen.fill(self.bg_color)

        screen.blit(text, [400,300])

        pygame.display.flip()


MyGame().run()
pygame.quit()
sys.exit()

any idea why is this happening? am i forgetting to import a library or is there something off with my draw method?


回答1:


It looks like you're passing the three RGB values of color in as three separate values to render. They should be passed in as a single tuple.

You're also missing some selfs. screen.blit(text, [400,300]) should be self.screen.blit(text, [400,300]), and you need to make all instances of text into self.text if you want it to be accessible in both __init__ and draw.

from __future__ import division
import math
import sys
import pygame


class MyGame(object):
    def __init__(self):
        pygame.mixer.init()
        pygame.mixer.pre_init(44100, -16, 2, 2048)
        pygame.init()

        self.width = 800
        self.height = 600
        self.screen = pygame.display.set_mode((self.width, self.height))

        self.bg_color = 0, 0, 0

        font = pygame.font.Font(None, 100)

        self.text = font.render("text that should appear", True, (238, 58, 140))


        self.FPS = 30
        self.REFRESH = pygame.USEREVENT+1
        pygame.time.set_timer(self.REFRESH, 1000//self.FPS)


    def run(self):
        running = True
        while running:
            event = pygame.event.wait()

            if event.type == pygame.QUIT:
                running = False

            elif event.type == self.REFRESH:
                self.draw()

            else:
                pass 

    def draw(self):
        self.screen.fill(self.bg_color)

        self.screen.blit(self.text, [400,300])

        pygame.display.flip()


MyGame().run()
pygame.quit()
sys.exit()

Result:

enter image description here



来源:https://stackoverflow.com/questions/19733801/pygame-text-not-appearing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!