Pygame创建一个精灵类并有多个精灵有不同的统计数据并相互冲突

时间:2017-01-26 10:22:11

标签: python pygame sprite

我试图用玩家1和玩家2创建精灵类。两者都是半径为10的圆圈,玩家1是蓝色,玩家2是绿色。我可以画出这些圈子,但我不能让它们碰撞。现在,只有玩家2移动但会直接移动玩家1圈。任何带有.rect的东西都会返回错误#34;对象没有属性.get_rect"。我不确定我是否正确地制作了精灵类。我阅读了所有pygame文档和所有stackoverflow问题,但它没有意义。这是我的班级:

class Player(pygame.sprite.Sprite):
    def __init__(self, px, py, boost, health):
        pygame.sprite.Sprite.__init__(self)
        self.px = px
        self.py = py
        self.boost = boost
        self.health = health

    def draw1(self):
        bpish = [2, 31, 211]

        player1circle = pygame.draw.circle(screen, bpish, (self.px, self.py), 10, 0)

    def draw2(self):
        neongreen = [14, 208, 0]

        player2circle = pygame.draw.circle(screen, neongreen, (self.px, self.py), 10, 0)

player1 = Player(width - 20, height / 2, 0, 100)
player2 = Player(20, height / 2, 500, 100)

这就是我希望它们碰撞的地方:

if keys_pressed[K_a] and player2.px > 10 AND NO COLLISION: # if the "a" key is pressed and player2 is not hitting the edge of the screen or player1
    if keys_pressed[K_LSHIFT] and player2.boost > 0: # and the left shift and player2 has boost available
        player2.px -= 4
        player2.boost -= 10
    else:
        player2.px -= 2
if keys_pressed[K_d] and player2.px < width - 10 AND NO COLLISION:
    if keys_pressed[K_LSHIFT] and player2.boost > 0:
        player2.px += 4
        player2.boost -= 10
    else:
        player2.px += 2
if keys_pressed[K_w] and player2.py > 10:
    if keys_pressed[K_LSHIFT] and player2.boost > 0 AND NO COLLISION:
        player2.py -= 4
        player2.boost -= 10
    else:
        player2.py -= 2
if keys_pressed[K_s] and player2.py < height -10 AND NO COLLISION:
    if keys_pressed[K_LSHIFT] and player2.boost > 0:
        player2.py += 4
        player2.boost -= 10
    else:
        player2.py += 2

if player2.boost < 500: # if player2 boost is not full, refill it by 12 points/sec
    player2.boost += 0.2 # refills player2 boost 6 points/sec

player2.draw2()
player1.draw1()
pygame.display.update()
clock.tick(60)

我尝试添加pygame.sprite.collide_rect和pygame.sprite.spritecollide以及其他一些但无济于事。

0 个答案:

没有答案