PyGame:文字没有出现

时间:2013-11-01 19:14:21

标签: python pygame

我正在关注一个教程,我试图让我的文字出现在屏幕上,这是我的代码,但文字不会出现:

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()

知道为什么会这样吗?我忘了导入一个图书馆,或者我的绘图方法有什么不对吗?

1 个答案:

答案 0 :(得分:2)

看起来您将color的三个RGB值作为三个单独的值传递给render。它们应该作为单个元组传递。

你也错过了一些selfscreen.blit(text, [400,300])应为self.screen.blit(text, [400,300]),如果您希望text和{{1}都可以访问self.text,则需要将__init__的所有实例都设置为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()

结果:

enter image description here