gpt4 book ai didi

python - PyGame:文本未出现

转载 作者:太空宇宙 更新时间:2023-11-04 03:54:02 24 4
gpt4 key购买 nike

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

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

知道为什么会这样吗?我是不是忘记导入库了,还是我的绘制方法有问题?

最佳答案

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

您还缺少一些 selfscreen.blit(text, [400,300])应该是self.screen.blit(text, [400,300]),你需要制作的所有实例textself.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

关于python - PyGame:文本未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19733801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com