试图阻止游戏中的精灵互相卡住

时间:2013-12-19 19:29:39

标签: python livewires

正如标题所说的那样......球精灵经常会碰撞并通过桨和幽灵鬼魂导致它停滞或卡住。试图找出解决这个问题的方法,以便游戏更好地运作。这是我到目前为止制作游戏的唯一尝试,只是为了更多地进行改进。

    class Alien(games.Sprite):
    """
    An alien which moves.
    """
    image = games.load_image("crab.jpg")

    def __init__(self, y, speed = 2, odds_change = 250):
        """ Initialize alien. """
        super(Alien, self).__init__(image = Alien.image,
                                   x = games.screen.width/2,
                                   y = y,
                                   dx = speed)

        self.odds_change = odds_change
        self.timer = 0

    def update(self):
        """ Determine if direction needs to be reversed. """
        if self.left < 0 or self.right > games.screen.width:
            self.dx = -self.dx
        elif random.randrange(self.odds_change) == 0:
           self.dx = -self.dx   
           self.check_collide()
        if self.timer > 0:
            self.timer -= 1


   ##if ball hits alien add points
    def check_collide(self):
        for ball in self.overlapping_sprites:
            if self.timer == 0:
                self.timer = 100
                Paddle.score.value+=10    
                ball.handle_collide()
            else:
                ball.handle_collide2()


class Paddle(games.Sprite):
##load up paddle sprite
    image=games.load_image("platform.jpg")
    score=games.Text(value=0, size=75, color = color.white, top=5,
                              right=games.screen.width - 20, is_collideable = False)
    def __init__(self, theY=games.screen.height - 25):
        super(Paddle, self).__init__(image=Paddle.image, angle = 0,
                                    y = theY,
                                    x=games.mouse.x,
                                    left=20)

        games.screen.add(Paddle.score)

    def update(self):
        self.x=games.mouse.x
        if self.left<0:
            self.x=0
        if self.right>games.screen.width:
            self.x=games.screen.width
        self.check_collide()

    def check_collide(self):
        for ball in self.overlapping_sprites:
            ball.handle_collide2()

class Ball(games.Sprite):

    image=games.load_image("ball.jpg")
    speed=2

    def __init__(self, x=100, y=70):
        super(Ball, self).__init__(image=Ball.image,
                                   x=x, y=y,
                                   dx=Ball.speed, dy=Ball.speed)

##make the ball bounce off walls
    def update(self):
        if self.right>games.screen.width:
            self.dx=-self.dx
        if self.left<0:
            self.dx=-self.dx
        if self.top<0:
            self.dy=-self.dy
        if self.bottom>games.screen.height:
            self.end_game()
            self.destroy()
##ball gets faster as score rises
    def handle_collide(self):
        if Paddle.score.value == 30:
            self.dx *= 2
            self.dy *= 2
        if Paddle.score.value == 60:
            self.dx *= 2
            self.dy *= 2
        self.dy=-self.dy
    def handle_collide2(self):
        self.dy=-self.dy

我已经问过多个我认识的人用python创建游戏,但没有一个人能想出​​更好的解决方案。

0 个答案:

没有答案
相关问题