【问题标题】:Changing the unknown attributes from a parent class for a child class为子类更改父类的未知属性
【发布时间】:2020-12-26 23:03:45
【问题描述】:

我想用父类 pygame.Rect 创建一个子类 Cell,但我无法更改 Cell 类实例的属性 top 并且它只是设置为零。 我的代码是:

import pygame

light = pygame.Color('#D3D3D3')
dark = pygame.Color('#222222')

class Cell(pygame.Rect):
    def __init__(self, index, side):
        self.index = index
        self.side = side
        self.color = None       # Index is a tuple from (0, 0) to (2, 2)
        self.has_pawn = None

        # Setting coordinates
        for i in range(3):
            if index[1] == i:
                self.x = i*self.side
            if index[0] == i:
                self.y == (2-i)*self.side

        self.left = self.x
        self.top = self.y
        self.width = self.side
        self.height = self.side
        
        # self.rect = pygame.Rect(x, y, width, height)

        # Setting color
        sum = self.index[0] + self.index[1]
        if sum % 2 == 0:
            self.color = light
        if sum % 2 == 1:
            self.color = dark

        # Setting has_pawn
        if index[0] == 0 | index[1] == 2:
            self.has_pawn = True


b2 = Cell((1, 1), 5)
print(b2)

给出输出:

pygame 2.0.1 (SDL 2.0.14, Python 3.9.1)
Hello from the pygame community. https://www.pygame.org/contribute.html
<rect(5, 0, 5, 5)>
[Finished in 0.6s]

希望有人能帮我把Cell对象的属性top改一下。

【问题讨论】:

  • 你可能需要调用super().__init__(),并传入适当的参数,这样父类才能初始化这些属性。
  • 我不知道使用打印功能时使用的“适当参数”到底是什么。所以,我不知道我是否使用了 super().__init__(top) 什么的。

标签: python oop pygame


【解决方案1】:

我看不出这里有什么问题。您的self.top 为0,这就是为什么self.rect 中的y 值也为0。在下面的循环中,

# Setting coordinates
for i in range(3):
    if index[1] == i:
         self.x = i*self.side
    if index[0] == i:
         self.y == (2-i)*self.side

上一次迭代中i 的值是2,这意味着在上一次迭代中会发生这种情况。

self.y == (2-2)*5 # 0*5 = 0

这导致self.y 被设置为0,而self.top 又被设置为0。因此,得到你得到的结果也就不足为奇了。

如果你这样做

super().__init__(pygame.Rect(10, 10, 10, 10))

打印的结果实际上是&lt;rect(10, 10, 10, 10)&gt;,所以你应该专注于修复循环。

【讨论】:

    【解决方案2】:

    您可以简化代码。你根本不需要循环。 Cell 是 pygame.Rect 的子类。计算调用的坐标:

    x = index[1] * self.side
    y = (index[0]-2) * self.side
    

    使用super()委托给pygame.Rect的构造函数:

    super().__init__(x, y, self.side, self.side) 
    

    但是,我不知道-2 来自哪里。你可能想要这样的东西:

    x = offsetx + index[1] * self.side
    y = offsety + index[0] * self.side
    

    班级Cell:

    class Cell(pygame.Rect):
        def __init__(self, index, side):
            self.index = index
            self.side = side
            self.color = None       # Index is a tuple from (0, 0) to (2, 2)
            self.has_pawn = None
    
            x = index[1] * self.side
            y = (index[0]-2) * self.side
            super().__init__(x, y, self.side, self.side) 
    
            # Setting color
            sum = self.index[0] + self.index[1]
            if sum % 2 == 0:
                self.color = light
            if sum % 2 == 1:
                self.color = dark
    
            # Setting has_pawn
            if index[0] == 0 | index[1] == 2:
                self.has_pawn = True
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      相关资源
      最近更新 更多