python 2.6,pygame,pong ball不会正确地进入y坐标

时间:2014-04-08 04:07:00

标签: python pygame python-2.6 pong

试图让球沿y坐标移动,它不会工作,底部有更多解释

from livewires import games, color
games.init(screen_width = 640, screen_height = 480, fps = 50)
points = games.Text(value = 0, size = 25, color = color.green,
                                bottom = games.screen.height - 5, left = 10)
games.screen.add(points)

class Paddle(games.Sprite):
    image = games.load_image("paddle.bmp")
    def __init__(self):
        super(Paddle, self).__init__(image = Paddle.image, y = games.mouse.y, right = games.screen.width)

    def update(self):
        """ Move to mouse x position. """
        self.y = games.mouse.y
        if self.top < 0:
            self.top = 0
        if self.bottom > games.screen.height:
            self.bottom = games.screen.height
        self.check_bounce()

    def check_bounce(self):
        for bouncingBall in self.overlapping_sprites:
            bouncingBall.handle_bounce()

class BouncingBall(games.Sprite):
    image = games.load_image("ball.bmp")
    def __init__(self, x, y, dx, dy):
        super(BouncingBall, self).__init__(image = BouncingBall.image, x = x, y = y, dx = dx, dy = dy)

    def update(self):
        """ Check if bottom edge has reached screen bottom. """
        if self.top > 0 or self.bottom < games.screen.height:
            self.dy = -self.dy
        if self.left < 0:
            self.dx = -self.dx
        if self.right > games.screen.width:
            self.end_game()

    def handle_bounce(self):
        global points
        points.value += 10
        points.left = 10
        self.dx = -self.dx

    def end_game(self):
        end_message = games.Message(value = "GAME OVER",
                                    size = 90,
                                    color = color.red,
                                    x = games.screen.width/2,
                                    y = games.screen.height/2,
                                    lifetime = 10 * games.screen.fps,
                                    after_death = games.screen.quit)
        games.screen.add(end_message)


def main():
    background_image = games.load_image("background.png", transparent = False)
    games.screen.background = background_image
    the_paddle = Paddle()
    games.screen.add(the_paddle)
    games.mouse.is_visible = False
    new_ball = BouncingBall(x = games.screen.width/2, y = games.screen.height/2, dx = 2, dy = 2) #<-believe it is right here im messing up
    games.screen.add(new_ball)
    games.screen.mainloop()

main()

我有一个可怕的时间让我的球正确地遵循y坐标,我相信我做错了当我创建实例new_ball,(主要功能)但我不知道大声笑,任何人都看到我做错了什么?

1 个答案:

答案 0 :(得分:2)

确保此行

if self.top > 0 or self.bottom < games.screen.height:
    self.dy = -self.dy

不会经常评估True。如果是这样,你的y速度将不断切换,球将永远不会改变y坐标。