【问题标题】:Sprite Attribute Changing [closed]精灵属性更改[关闭]
【发布时间】:2020-12-28 20:39:37
【问题描述】:

我正在做一个 covid 模拟,让球相互反弹,但是当它与一个红球碰撞时,我需要它变成红色。但是,我不完全确定该怎么做。我当前的代码如下:

class Cell(pygame.sprite.Sprite):

    def __init__(self, color, speed, width, height):
        super().__init__()

        self.color = color
        self.speed = speed

        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)

        self.radius = width // 2  # 25
        center = [width // 2, height // 2]
        pygame.draw.circle(self.image, self.color, center, self.radius, width=0)

        self.rect = self.image.get_rect()
        self.rect.x = random.randint(5, 795)
        self.rect.y = random.randint(150, 700)

        self.pos = pygame.math.Vector2(self.rect.center)
        self.dir = pygame.math.Vector2(1, 0).rotate(random.randrange(360))

    def update(self):
        self.pos += self.dir * self.speed
        if self.pos.x - self.radius < 5 or self.pos.x + self.radius > 790:
            self.dir.x *= -1
        if self.pos.y - self.radius < 150 or self.pos.y + self.radius > 700:
            self.dir.y *= -1

        for other_cell in all_cells:
            if all_cells != self:
                distance_vec = self.pos - other_cell.pos
                if 0 < distance_vec.length_squared() < (self.radius * 2) ** 2:
                    self.dir.reflect_ip(distance_vec)
                    other_cell.dir.reflect_ip(distance_vec)

        self.rect.centerx = round(self.pos.x)
        self.rect.centery = round(self.pos.y)

    def changeColor(self, newColor):
        self.color = newColor
        pygame.draw.circle(self.image, self.color, center, self.radius, width=0)

class Infected(Cell):
    def __init__(self, color, speed, width, height):
        super().__init__('RED', speed, width, height)
        self.color = color
        self.pos.x = 345
        self.pos.y = 295
        centre = [self.pos.x, self.pos.y]
        pygame.draw.circle(self.image, self.color, centre, self.radius, width=0)

pygame.init()

screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Covid-19 Simualtion")

all_cells = pygame.sprite.Group()
redcell = Infected('RED', 2, 10, 10)
all_cells.add(redcell)
for _ in range(100):
    cell = Cell(GREEN1, 2, 10, 10)
    all_cells.add(cell)

此代码允许球相互反弹。另外,我想知道是否可以转换类,例如,如果我有一个属于健康类(细胞)的细胞,我可以在撞击时将它转换成感染类(细胞)。

【问题讨论】:

  • 您应该在每个帖子中问一个问题,请参阅How to Ask。对于你的第二个问题,你应该举一个例子说明你需要这种行为而不是将那种东西存储为类属性。
  • 不,你不能改变对象的类型。

标签: python pygame sprite simulation


【解决方案1】:

只有知道您之前的问题才能回答这个问题:Random systematic movement in pygame

您必须更改color 属性并重新绘制image 表面。添加方法changeColor

class Cell(pygame.sprite.Sprite):
    # [...]

    def changeColor(self, newColor):
        self.color = newColor
        pygame.draw.circle(self.image, self.color, center, self.radius, width=0)

当你想改变颜色时调用该方法。假设您有一个红色变量 RED

RED = (255, 0, 0) # just an example use your red color here

在 pygame 中,您甚至可以通过字符串而不是元组来定义颜色。但是,始终使用相同的颜色很重要:

RED = 'RED'

测试一个粒子的颜色是否为RED并改变颜色:

for other_cell in all_cells:
    if all_cells != self:
        distance_vec = self.pos - other_cell.pos
        if 0 < distance_vec.length_squared() < (self.radius * 2) ** 2:
            self.dir.reflect_ip(distance_vec)
            other_cell.dir.reflect_ip(distance_vec)

            if self.color == RED:
                other_cell.changeColor(RED)
            elif other_cell.color == RED:
                self.changeColor(RED)

【讨论】:

  • 感谢 rabid 的帮助,但是在将其与我当前的模拟代码结合后,它没有工作
  • 没有出现错误,但单元格在碰撞时没有改变颜色
  • 我已经编辑了代码和我放置它的位置,但是当单元格改变颜色时,它会创建另一个红色单元格,我无法删除原来的绿色单元格
  • 否则我试图在创建红色单元格时也删除绿色单元格,但它只是在另一个随机位置形成
  • 是的,在编辑后单元格仍然不会改变颜色。我也尝试编辑颜色变化的方法,因为它说没有中心,所以我采取了~~~round(self.pos.x)~~~和~~~round(self.pos.y)~~~正如子程序中我的中心和 thrn 的参数所说 ~~~centre = [round(self.pos.x), round(self.pos.y)]~~~ 但仍然收到错误,没有中心
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多