Trying to stop sprites in game from getting stuck on each other

113 views Asked by At

Just what the title says...the ball sprite will often collide and ghost through the paddle and alien sprite causing it to stall or get stuck. Trying to figure out a way to fix this so the game operates better. This is my only attempt at making a game so far just trying to polish it more.

    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

I have asked multiple people I know who create games with python but none of them could come up with a better solution.

0

There are 0 answers