python中的pong程序,不能用鼠标移动桨

时间:2013-04-15 17:25:27

标签: python pygame pong livewires

嘿所有我正在做家庭作业,我必须通过pygame和livewire创建单人乒乓球游戏。我的代码大部分都已完成,但我确实有一个问题。屏幕不会更新,并且在这样做时,球拍不会移动并且球不会反弹。我在球拍和球上有一个更新方法,但由于某些原因,它不起作用。这是我的代码谢谢!

更新:我不得不重做一些事情,但我现在可以打电话给我的班级,然后球会反弹但是我不能移动球拍。我不知道为什么这不起作用,因为self.y = games.mouse.y应该更新我的球拍的y坐标。以下是我重新编辑的代码,感谢您的帮助!

from livewires import games, color
import random

#make screen size

games.init(screen_width = 640, screen_height = 480 , fps = 50)

class Paddle(games.Sprite):

    image = games.load_image("paddle.bmp")

    def __init__(self):
        super(Paddle, self).__init__(image = Paddle.image,x = 10, y = games.mouse.y)
        self.score = games.Text(value = 0, size = 25, color = color.black,
                                top = 20, right = games.screen.width - 8)

        games.screen.add(self.score)

        def update(self):

            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_hit()

            def check_hit(self):
                countdown = 0
                if countdown == 0:
                    for ball in self.overlapping_sprites:
                        self.score.value += 1
                        ball.dat_touch()
                        countdown == 10
                else: countdown -= 1
"""




class Ball(games.Sprite):

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

    def __init__(self, x = games.screen.height/2, y = games.screen.height/2):
        super(Ball,self).__init__(image = Ball.image,
                                  x = x, y = y,
                                  dx = Ball.speed, dy = Ball.speed)

    def update(self):
        if self.right > games.screen.width:
            self.dx = -self.dx

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

        if self.left < 0:
            self.end_game()
            self.destroy()


    #def dat_touch(self):
        #self.dx = - self.dx
        #handles paddle touch
        #doesn't work = game_over
"""
      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 = 5 * games.screen.fps,
                                    after_death = games.screen.quit)
        games.screen.add(end_message)

"""




def main():
    wall_image = games.load_image("background.bmp", transparent = False)
    games.screen.background = wall_image


    the_ball = Ball()

    games.screen.add(the_ball)

    the_paddle = Paddle()
    games.screen.add(the_paddle)

    games.mouse.is_visible = False
    games.screen.event_grab = True

    games.screen.mainloop()

main()

1 个答案:

答案 0 :(得分:1)

我在这里看到一些问题:

首先,您已经定义了“Ball”和“Paddle”类,但您似乎没有使用它们。相反,您在main()函数中执行以下操作:

the_ball=games.Sprite(image=ball_image,
                      x=600,
                      y=250,
                      dx=2,
                      dy=-2)

通过仅将其定义为Sprite,您只使用已在LiveWire的Sprite对象中定义的逻辑,完全忽略您的Ball或Bar类。相反,您可能想要创建一个“Ball”和“Bar”对象。例如,要创建“Ball”对象,您可以将上面的行更改为...

the_ball=Ball(image=ball_image,
                      x=600,
                      y=250,
                      dx=2,
                      dy=-2)

所以,你已经定义了一个Ball类,并且(使用我上面提供的改变的代码)你将在坐标x = 600和y = 250处创建球。为了让这个球移动,x和y坐标需要改变。例如,要将其移动到正确的20个单位,x将需要更改为620。

因此,您需要考虑x和y坐标如何变化。浏览LiveWires代码(如果您感到好奇,请在下一堂课中与您的老师联系,了解如何访问LiveWires代码),我将阅读以下内容:

# Some [objects] will, more specifically, move at regular intervals;
# their classes should subclass Mover (which < Timer) and
# define a moved method.

所以,现在,你的Ball类只是继承自Sprite ......

class Ball(games.Sprite):

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

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

尝试更改此内容,使其继承Sprite AND Mover ...

class Ball(games.Sprite, games.Mover):

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

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

你在这里做的是使你的对象不仅继承了“Sprite”类的功能(它处理在提供的坐标上在屏幕上绘制),而且还继承了“Mover”类的功能(它根据dx和dy改变x和y值。

希望这会让你开始尝试做什么。