Python pygame';屏幕';未定义
在Python速成课程之后,我复制了这个输入,并在运行时出错Python pygame';屏幕';未定义,python,pygame,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
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()
错误内容如下:
名称错误:未定义名称“屏幕”
我该如何解决这个问题?我相信我正确安装了pygame和pip。只需将第五行更改为:
screen = pygame.display.set_mode((1920, 1080))
您没有定义名称“screen”。使用相同的变量来引用
screen.fill()
,将其更改为resolution.fill()
是否更改了变量名称?在示例中,Wasresolution=pygame.display.set_mode((1920,1080))
实际上screen=pygame.display.set_mode((1920,1080))
在示例中..?显示表面的名称(即pygame.display.set_mode
返回)是分辨率而不是屏幕。只需将其重命名为屏幕
。