Python 试图阻止游戏中的精灵互相纠缠

Python 试图阻止游戏中的精灵互相纠缠,python,livewires,Python,Livewires,正如标题所说的……球精灵经常会碰撞并通过桨和外星精灵鬼魂,导致它失速或卡住。试图找出一种方法来解决这个问题,使游戏运行得更好。到目前为止,这是我唯一一次尝试制作一款游戏,只是想让它更加完美 class Alien(games.Sprite): """ An alien which moves. """ image = games.load_image("crab.jpg") def __init__(self, y, speed = 2, odd

正如标题所说的……球精灵经常会碰撞并通过桨和外星精灵鬼魂,导致它失速或卡住。试图找出一种方法来解决这个问题,使游戏运行得更好。到目前为止,这是我唯一一次尝试制作一款游戏,只是想让它更加完美

    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
class外星人(games.Sprite):
"""
移动的外星人。
"""
image=games.load_image(“crab.jpg”)
定义初始(自我,y,速度=2,几率变化=250):
“”“初始化外国人。”“”
超级(外星人,自我)。\uuuu初始化\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
x=games.screen.width/2,
y=y,
dx=速度)
self.goods\u change=赔率\u change
self.timer=0
def更新(自我):
“”“确定是否需要反转方向。”“”
如果self.left<0或self.right>games.screen.width:
self.dx=-self.dx
elif random.randrange(自身概率变化)==0:
self.dx=-self.dx
self.check_collide()
如果self.timer>0:
自动定时器-=1
##如果球击中外星人,则增加点数
def检查碰撞(自):
对于自重叠精灵中的球:
如果self.timer==0:
自动定时器=100
划桨得分值+=10
ball.handle_collide()
其他:
球.手柄(2)
班级桨(游戏.精灵):
##装载桨叶雪碧
image=games.load_image(“platform.jpg”)
分数=游戏。文本(值=0,大小=75,颜色=颜色。白色,顶部=5,
右=games.screen.width-20,可碰撞=假)
def uuu init uuuu(self,他们=games.screen.height-25):
超级(桨叶,自身)。\uuuu初始(图像=桨叶,图像,角度=0,
y=他们,
x=游戏。鼠标。x,
左=20)
游戏。屏幕。添加(划桨。得分)
def更新(自我):
self.x=games.mouse.x
如果self.leftgames.screen.width:
self.x=games.screen.width
self.check_collide()
def检查碰撞(自):
对于自重叠精灵中的球:
球.手柄(2)
班级舞会(游戏、雪碧):
image=games.load_image(“ball.jpg”)
速度=2
定义初始值(self,x=100,y=70):
super(Ball,self)。\uuuu init\uuuuu(image=Ball.image,
x=x,y=y,
dx=球速度,dy=球速度)
##使球弹离墙壁
def更新(自我):
如果self.right>games.screen.width:
self.dx=-self.dx
如果你离开了