所以我创建了一个基于Youtube的俄罗斯方块游戏教程:https://www.youtube.com/watch?v=zfvxp7PgQ6c&t=2075s
但是发生了pygame.error:显示曲面退出。
我尝试在pygame.quit后面添加"break“、"sys.exit()”、"QUIT“,但都不起作用。
有谁知道怎么解决这个问题吗?代码如下:(你可以跳到def main_menu)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run == False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_piece.x -= 1
if not (valid_space(current_piece, grid)):
current_piece.x += 1
if event.key == pygame.K_RIGHT:
current_piece.x += 1
if not (valid_space(current_piece, grid)):
current_piece.x -= 1
if event.key == pygame.K_DOWN:
current_piece.y += 1
if not (valid_space(current_piece, grid)):
current_piece.y -= 1
if event.key == pygame.K_UP:
current_piece.rotation += current_piece.rotation + 1 % len(current_piece.shape)
if not (valid_space(current_piece, grid)):
current_piece.rotation -= 1
shape_pos = convert_shape_format(current_piece)
for i in range(len(shape_pos)):
x, y = shape_pos[i]
if y > -1:
grid[y][x] = current_piece.color
if change_piece:
for pos in shape_pos:
p = (pos[0], pos[1])
locked_positions[p] = current_piece.color
current_piece = next_piece
next_piece = get_shape()
change_piece = False
score += clear_rows(grid, locked_positions) * 10
draw_window(win, grid, score, last_score)
draw_next_shape(next_piece, win)
pygame.display.update()
if check_lost(locked_positions):
draw_text_middle(win, "You Lost!", 80, (255,255,255))
pygame.display.update()
pygame.time.delay(1500)
run = False
update_score(score)
def main_menu(win):
run = True
while run:
win.fill((0,0,0))
draw_text_middle(win, 'Press any key to play', 60, (255,255,255))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
main(win)
pygame.display.QUIT()
win = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption('Tetris')
main_menu(win)更新代码:
def main_menu(win):
run = True
while run:
win.fill((0,0,0))
draw_text_middle(win, 'Press any key to play', 60, (255,255,255))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
main(win)
pygame.quit()
win = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption('Tetris')
main_menu(win)发布于 2020-01-15 20:26:58
在你的main_menu循环中,你告诉它循环,而本地的布尔值运行==。这是可以的,但您应该像评论中提到的人一样,执行pygame.quit()并选择性地执行quit() (关闭窗口),而不是您现在拥有的pygame.display.quit()和sys.exit()。
根据您编写代码的方式,事件函数中的布尔运行是局部的。这意味着它不会更改您在主循环中使用的运行的值(也不会在main_menu循环中更改它)。我建议迁移到OOP并创建一个self.run布尔值,否则您需要使布尔值全局运行。
你应该在事件函数中写下这段代码,而不是你现在在顶部的代码:
对于pygame.event.get()中的事件: if event.type == pygame.QUIT: run = False pygame.quit() quit()
希望这能有所帮助!
https://stackoverflow.com/questions/59724644
复制相似问题