刚刚开始使用python编码,所以我不是很擅长,但是我正在尝试进行Pacman娱乐,并且在启动时屏幕没有填充的情况下出现此错误,我也不知道为什么?我经历了它,一切看起来都很好吗?到目前为止,这是我的代码。该错误必须与简介部分有关,但我找不到它。 BLACK = (0, 0, 0)
。
import pygame as pg
import sys
from settings import *
pg.init()
pvec = pg.math.Vector2
class Main:
def __init__(self):
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
self.clock = pg.time.Clock()
self.running = True
self.state = 'startscreen'
self.cellwidth = WIDTH//28
self.cellheight = HEIGHT//38
self.load()
def run(self):
while self.running:
if self.state == 'startscreen':
self.intro_events()
self.intro_update()
self.intro_draw()
elif self.state == 'play':
self.play_events()
self.play_update()
self.play_draw()
elif self.state == 'highscore':
self.high_events()
self.high_update()
self.high_draw()
else:
self.running = False
self.clock.tick(FPS)
pg.quit()
sys.exit()
def draw_text(self, text, screen, pos, size, color, fontname, centered=False):
font = pg.font.SysFont(fontname, size)
scrtext = font.render(text, False, color)
text_size = scrtext.get_size()
if centered:
pos[0] = pos[0]-text_size[0]//2
pos[1] = pos[1] - text_size[1] // 2
screen.blit(scrtext, pos)
def draw_grid(self):
for x in range(WIDTH//self.cellwidth):
pg.draw.line(self.background, GRAY, (x*self.cellwidth, 0), (x*self.cellwidth, HEIGHT))
for x in range(HEIGHT//self.cellheight):
pg.draw.line(self.background, GRAY, (0, x*self.cellheight), (WIDTH, x*self.cellheight))
def load(self):
self.background = pg.image.load('background2.png')
self.background = pg.transform.scale(self.background, (WIDTH, HEIGHT))
# INTRO
def intro_events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.running = False
if event.type == pg.KEYDOWN and event.key == pg.K_SPACE:
self.state = 'play'
if event.type == pg.KEYDOWN and event.key == pg.K_BACKSLASH:
self.state = 'highscore'
def intro_update(self):
pass
def intro_draw(self):
self.screen.fill(BLACK)
self.draw_text('PLAY GAME', self.screen, [WIDTH//2, HEIGHT//2+150],
INTRO_TEXT_SIZE, ORANGE, INTRO_FONT, centered=True)
self.draw_text('HIGH SCORES', self.screen, [WIDTH//2, HEIGHT//2+200],
INTRO_TEXT_SIZE, ORANGE, INTRO_FONT, centered=True)
self.draw_text('PACMAN PORTAL', self.screen, [WIDTH//2, HEIGHT//2-250],
LOGO, ORANGE, LOGO_FONT, centered=True)
# HIGH SCORE
def high_events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.running = False
if event.type == pg.KEYDOWN and event.key == pg.K_BACKSLASH:
self.state = 'startscreen'
def high_update(self):
pass
def high_draw(self):
self.screen.fill(BLACK)
self.draw_text('HIGH SCORES', self.screen, [WIDTH // 2, HEIGHT // 2 - 250],
INTRO_TEXT_SIZE, ORANGE, INTRO_FONT, centered=True)
pg.display.update()
# PLAYING
def play_events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.running = False
def play_update(self):
pass
def play_draw(self):
self.screen.fill(BLACK)
self.screen.blit(self.background, (0, 0))
self.draw_text('SCORE: ', self.screen, (30, 750), 35, WHITE, INTRO_FONT, centered=False)
self.draw_grid()
pg.display.update()
您错过了更新显示的操作。注意,显示在play_draw
中更新,但在intro_draw
中不更新。我建议在主应用程序循环的末尾而不是在* pg.display.update()
方法中调用_draw
,因为在任何情况下都必须更新显示:
class Main:
# [...]
def run(self):
while self.running:
if self.state == 'startscreen':
self.intro_events()
self.intro_update()
self.intro_draw()
elif self.state == 'play':
self.play_events()
self.play_update()
self.play_draw()
elif self.state == 'highscore':
self.high_events()
self.high_update()
self.high_draw()
else:
self.running = False
pg.display.update() # <--------
self.clock.tick(FPS)