【问题标题】:Initializing class attribute Python初始化类属性Python
【发布时间】: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


【解决方案1】:

您的 init 方法在您创建 Shape 类型的对象时运行,例如:

shape = Shape(white,x,y,w,h)

然后 init 方法将使用您指定的参数作为该方法中的参数运行。

按照原样编写代码,您永远不会执行该特定行。尝试消除 while 循环,使其不在类定义中。或者,只是为了测试它,尝试通过单独运行特定的行 shape = Shape(white, x, y, w, h) 来初始化类作为测试。

【讨论】:

    【解决方案2】:

    试试下面的代码。希望这会有所帮助..

    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()
    

    【讨论】:

      【解决方案3】:

      我建议以这种方式重写代码:只需将颜色和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_case(带下划线的小写字母)编写。另外,请使用sys.exit() 退出游戏。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-21
        • 2021-09-27
        • 1970-01-01
        • 2016-06-02
        • 2022-01-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多