微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

精灵属性改变

如何解决精灵属性改变

我正在做一个 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,width=0)

class Infected(Cell):
    def __init__(self,height):
        super().__init__('RED',height)
        self.color = color
        self.pos.x = 345
        self.pos.y = 295
        centre = [self.pos.x,self.pos.y]
        pygame.draw.circle(self.image,centre,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,10)
    all_cells.add(cell)

代码允许球相互弹开。另外,我想知道是否可以转换类,例如,如果我有一个属于健康类(单元格)的单元格,我可以在撞击时将其转换为感染类(单元格)。

解决方法

此问题只能在您了解您之前的问题的情况下回答:Random systematic movement in pygame

您必须更改 color 属性并重新绘制 image Surface。添加方法 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) # 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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。