简体   繁体   English

python中的pong程序,无法用鼠标移动桨

[英]pong program in python, cannot move paddle with mouse

Hey all im working on a homework assignment in which i have to create a single player pong game through pygame and livewire. 大家好,我正在做一项家庭作业,我必须通过pygame和livewire创建一个单人乒乓游戏。 I have the code mostly finished but I do have one issue. 我的代码大部分已经完成,但是确实有一个问题。 The screen does not update and in doing so the paddle doesn't move and the ball doesn't bounce around. 屏幕不会更新,这样做时桨板不会移动,球也不会反弹。 i have an update method on both the paddle and the ball but for someone reason it doesn't work. 我对球拍和球都有更新方法,但是由于某些原因,它不起作用。 Here is my code Thanks! 这是我的代码,谢谢!

UPDATE: I had to redo somethings but I am now able to call on the my classes and the balls bounce around however I cannot move the paddle. 更新:我不得不重做一些事情,但是现在我可以在我的课上打电话,球反弹,但是我无法移动球拍。 I have no clue as to why this doesn't work since the self.y = games.mouse.y should update y coordinate of my paddle. 我不知道为什么这不起作用,因为self.y = games.mouse.y应该更新桨的y坐标。 Here is my re edited code however, and thanks for the help so far! 但是,这是我重新编辑的代码,感谢您的帮助!

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

I see a few issues here: 我在这里看到一些问题:

First, you've defined your "Ball" and "Paddle" classes, but you don't seem to be using them. 首先,您已经定义了“ Ball”和“ Paddle”类,但是您似乎并没有使用它们。 Instead, you're doing the following in your main() function: 而是在main()函数中执行以下操作:

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

By only defining this as a Sprite, you are only using the logic that was already defined in LiveWire's Sprite object, completely ignoring your Ball or Bar class. 通过仅将其定义为Sprite,您仅使用LiveWire的Sprite对象中已定义的逻辑,而完全忽略了Ball或Bar类。 Instead, you probably wanted to create a "Ball" and "Bar" object. 相反,您可能想创建一个“球”和“条”对象。 For example, to create a "Ball" object, you would change the above line to... 例如,要创建“球”对象,请将上面的行更改为...

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

So, you have defined a Ball class, and (using the changed code I've provided above) you would create at ball at the coordinate x=600 and y=250. 因此,您已经定义了Ball类,并(使用上面提供的更改的代码)将在坐标x = 600和y = 250处创建ball。 In order to have this ball move, the x and y coordinate would need to change. 为了使该球移动,需要更改x和y坐标。 For example, to move it to the right 20 units, the x would need to change to 620. 例如,要将其向右移动20个单位,x将需要更改为620。

So, you need to consider how the x and y coordinates change. 因此,您需要考虑x和y坐标如何变化。 Looking through the LiveWires code (speak to your teacher during the next class as to how to access the LiveWires code if you're curious) I read the following: 浏览LiveWires代码(如果您有兴趣的话,在下一堂课中与您的老师讨论如何访问LiveWires代码),我阅读以下内容:

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

So, right now, your Ball class is only inheriting from Sprite... 所以,现在,您的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)

Try changing this so that it inherits from both Sprite AND Mover... 尝试更改它,使其继承自Sprite和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)

What you're doing here is making your object not only inherit the functionality of the "Sprite" class (which handles having it draw on the screen at the coordinate provided) but also inherit the functionality of the "Mover" class (which changes the x and y values based on dx and dy). 您在这里所做的是使您的对象不仅继承“ Sprite”类的功能(该类处理以提供的坐标在屏幕上绘制对象),而且继承“ Mover”类的功能(这将更改基于dx和dy的x和y值)。

Hopefully that will give you a start into what you're trying to do. 希望这可以让您开始尝试做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM