初始化类属性Python

初始化类属性Python,python,class,attributes,pygame,Python,Class,Attributes,Pygame,我有一个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])

我有一个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属性,但得到以下错误。名称错误:未定义名称“颜色”。
如何初始化square属性。

无论何时创建Shape类型的对象,您的init方法都会运行,例如:

shape = Shape(white,x,y,w,h)
然后,init方法使用指定的参数运行,这些参数在该方法中作为参数传递


如果代码按原样编写,则永远不会执行该特定行。尝试删除while循环,使其不在类定义中。或者,为了进行测试,可以单独运行特定的行shape=shape(白色,x,y,w,h)来初始化类作为测试。希望这有帮助

import pygame
import time

class Shape:

    def __init__(self,color,x,y,w,h):
        self.color = color
        self.x = x
        self.y = y
        self.w = w
        self.h = h

    def draw_rect(self, gameDisplay):
        square = pygame.draw.rect(gameDisplay,self.color,
                                  [self.x,self.y,self.w,self.h]
                                  )
        while not gameExit:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()              
                    quit()

        gameDisplay.fill(white)
        pygame.display.update()
        clock.tick(60)

def main():

    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

    sobj = shape(white,0,0,25,25)
    sobj.draw_rect(gameDisplay)

if __name__ == '__main__':
    main()

我建议以这种方式重写代码:只需将颜色和一个
pygame.Rect
存储在
Shape
类中,并给它一个
draw
方法。将主循环放在
Shape
类中是没有意义的。现在,您可以创建任意多个形状,并在while循环中绘制它们

import sys
import pygame


pygame.init()
WHITE = pygame.Color('white')


class Shape:

    def __init__(self, color, x, y, w, h):
        self.color = color
        self.rect = pygame.Rect(x, y, w, h)

    def draw(self, surface):
        pygame.draw.rect(surface, self.color, self.rect)


def main():
    game_display = pygame.display.set_mode((600, 800))
    shape = Shape(WHITE, 0, 0, 25, 25)
    shape2 = Shape(pygame.Color('sienna1'), 100, 100, 25, 25)
    clock = pygame.time.Clock()
    game_exit = False

    while not game_exit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_exit = True

        game_display.fill((40, 40, 40))
        shape.draw(game_display)
        shape2.draw(game_display)
        pygame.display.update()
        clock.tick(60)


if __name__ == '__main__':
    main()
    pygame.quit()
    sys.exit()

作为旁注,Python中的变量名应该用
snake\u大小写(小写加下划线)。另外,使用
sys.exit()
退出游戏。

问题可能是
\uuuuu init\uuu
方法上方的行。你想在那里完成什么?另外,为什么类内部而非外部有
while
循环?
square=pygame.draw.rect(gameDisplay,color,[x,y,w,h])
是在定义
类时执行的,而不是在创建实例时执行的。
color
应该从哪里来?这是一段代码,我已经创建了Shape的子类,while loop就在那里