【发布时间】:2017-09-29 19:32:56
【问题描述】:
我有以下 Pygame 程序:
import pygame
import time
pygame.init()
white = (255,255,255)
red = (255,0,0)
gameDisplay = pygame.display.set_mode((600,800))
gameExit = False
x=0
y=0
w=25
h=25
class Shape:
square = pygame.draw.rect(gameDisplay,color,[x,y,w,h])
def __init__(self,color,x,y,w,h):
self.color = color
self.x = x
self.y = y
self.w = w
self.h = h
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
shape = Shape(white,x,y,w,h)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
我想用 init 初始化 Shape 类的 square 属性,但我得到以下错误。 NameError:名称“颜色”未定义。 如何初始化 square 属性。
【问题讨论】:
-
问题可能出在您的
__init__方法上方。你想在那里完成什么?另外,为什么在课堂内而不是在课堂外有while循环? -
square = pygame.draw.rect(gameDisplay,color,[x,y,w,h])在定义class时执行,而不是在创建实例时执行。color应该从哪里来? -
这是一个sn-p代码,我创建了Shape的子类,并且有while循环
标签: python class attributes pygame