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

2024-04-18 12:35:23 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,所有我正在做一个家庭作业,其中我必须通过pygame和livewire创建一个单人乒乓游戏。我已经基本完成了代码,但我确实有一个问题。屏幕不会更新,这样一来,球拍就不会移动,球也不会反弹。我对球拍和球都有一个更新方法,但由于某些原因它不起作用。这是我的密码谢谢!在

更新:我不得不重做一些事情,但我现在可以调用我的课程和球反弹,但我不能移动桨。自从self.y=游戏.鼠标.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()

Tags: imageselfifinitdefwidthscreengames
1条回答
网友
1楼 · 发布于 2024-04-18 12:35:23

我在这里看到了一些问题:

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

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

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

^{pr2}$

因此,您已经定义了一个Ball类,并且(使用我在上面提供的更改过的代码)将在坐标x=600和y=250处创建atball。为了让这个球移动,需要改变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.

所以,现在,你的球类只继承了精灵。。。在

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)

尝试改变这个,让它继承了精灵和移动器。。。在

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值)。在

希望这会给你一个开始做你想做的事情。在

相关问题 更多 >