pygame的“屏幕”未定义

时间:2018-06-20 06:36:12

标签: python pygame

在Python速成课程之后,我复制了此输入并以错误运行它

import os
import pygame
def play_game():
    #Creates Screen
    resolution = pygame.display.set_mode((1920, 1080))
    pygame.init()
    pygame.display.set_caption('Game')
    #Colors the background
    bg_color = (230,230,230)
    #Starts the game
    while True:
        #Sets background color
        screen.fill(bg_color)
        #Looks for user inputs
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

                #updates the screen
                pygame.display.flip()



play_game()

该错误显示为:

  

NameError:名称“屏幕”未定义

该如何解决?我相信我正确安装了pygame和pip。

2 个答案:

答案 0 :(得分:0)

您没有定义名称“屏幕”。使用相同的变量引用screen.fill()中的变量,将其更改为resolution.fill()

答案 1 :(得分:-1)

只需将第五行更改为:

screen = pygame.display.set_mode((1920, 1080))