【问题标题】:gameDisplay.fill(white) NameError: name 'white' is not definedgameDisplay.fill(white) NameError: name 'white' is not defined
【发布时间】:2017-06-14 00:55:36
【问题描述】:

我收到以下错误:

回溯(最近一次通话最后一次):
文件“D:\ninja_car.py”,第 32 行,在
gameDisplay.fill(白色)
NameError: name 'white' 未定义

我的代码:

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。试图将背景设置为白色,但它给出了该错误。

【问题讨论】:

  • 你在哪里定义了白色变量?

标签: python python-3.x pygame


【解决方案1】:

我没有看到变量 white 的任何实例,所以它会给你一个错误。将此代码:
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()

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 2013-04-09
    • 1970-01-01
    • 2020-11-07
    • 2017-01-09
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多