Python 游戏显示。填充(白色)名称错误:未定义名称“白色”
我得到以下错误: 回溯最近一次呼叫上次: 文件D:\ninja_car.py,第32行,在 gameDisplay.fillwhite NameError:未定义名称“white” 我的代码:Python 游戏显示。填充(白色)名称错误:未定义名称“白色”,python,python-3.x,pygame,Python,Python 3.x,Pygame,我得到以下错误: 回溯最近一次呼叫上次: 文件D:\ninja_car.py,第32行,在 gameDisplay.fillwhite NameError:未定义名称“white” 我的代码: import pygame pygame.init() display_width = 800 display_height = 600 gameDisplay = pygame.display.set_mode((display_width,display_height)) pygame.disp
import pygame
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Ninja Game')
clock = pygame.time.Clock()
carImg = pygame.image.load('car.png')
def car(x,y):
gameDisplay.blit(carImg,(x,y))
x = (display_width * 0.45)
y = (display_height * 0.8)
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill(white) # here
car(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
这里有什么问题?我正在使用Python 3.4。试图将背景设置为白色,但会出现错误 我没有看到变量white的任何实例,因此它会给您一个错误。添加此代码: 白色=255,255,255到您定义变量的位置 以下是完整的代码:
import pygame
pygame.init()
display_width = 800
display_height = 600
white = (255, 255, 255) # Here is the new code!
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Ninja Game')
clock = pygame.time.Clock()
carImg = pygame.image.load('car.png')
def car(x,y):
gameDisplay.blit(carImg,(x,y))
x = (display_width * 0.45)
y = (display_height * 0.8)
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill(white)
car(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
您在哪里定义了白色变量?