简体   繁体   English

如何在乒乓球比赛中停球

[英]How to stop the ball in Pong ball game

I'm new to Kivy and python. 我是Kivy和python的新手。 I'm practicing Kivy with the Pong Game code given in the tutorial. 我正在使用教程中提供的Pong Game代码练习Kivy。 I want to stop the ball once it collide with the paddle. 一旦球碰到桨,我想停球。 I'm able to stop the ball inside the update function by setting the velocity to zero. 我可以通过将速度设置为零来停止更新功能中的球。 But I want to stop using my own function(stop_serve). 但是我想停止使用我自己的函数(stop_serve)。 I'm able to call the stop_serve function inside the collide_widget(Proof: Logger info) of the class PongPaddle() but the ball doesn't stop. 我可以在类PongPaddle()的collide_widget(Proof:Logger info)中调用stop_serve函数,但是球不会停止。 Please, help me achieve the above. 请帮我实现以上目标。

I've shared the complete code below. 我分享了下面的完整代码。 Thanks in advance. 提前致谢。

Pong_Modified.py : Pong_Modified.py:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty,\
    ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock
from kivy.logger import Logger


class PongPaddle(Widget):
    score = NumericProperty(0)

    def bounce_ball(self, ball):
        if self.collide_widget(ball):
            PongGame().stop_serve(vel = (0,0))


class PongBall(Widget):
    velocity_x = NumericProperty(0)
    velocity_y = NumericProperty(0)
    velocity = ReferenceListProperty(velocity_x, velocity_y)

    def move(self):
        self.pos = Vector(*self.velocity) + self.pos


class PongGame(Widget):
    ball = ObjectProperty(None)
    player1 = ObjectProperty(None)
    player2 = ObjectProperty(None)

    def serve_ball(self, vel=(4, 0)):
        self.ball.center = self.center
        self.ball.velocity = vel

    def stop_serve(self,vel=(0,0)):
        Logger.info('Inside stop_serve: %s' %self.pos)
        self.ball.center = self.center
        self.ball.velocity = vel

    def update(self, dt):
        self.ball.move()

        #bounce of paddles
        self.player1.bounce_ball(self.ball)
        self.player2.bounce_ball(self.ball)

        #bounce ball off bottom or top
        if (self.ball.y < self.y) or (self.ball.top > self.top):
            self.ball.velocity_y *= -1

        #went of to a side to score point?
        if self.ball.x < self.x:
            self.player2.score += 1
            self.serve_ball(vel=(4, 0))
        if self.ball.x > self.width:
            self.player1.score += 1
            self.serve_ball(vel=(-4, 0))

    def on_touch_move(self, touch):
        if touch.x < self.width / 3:
            self.player1.center_y = touch.y
        if touch.x > self.width - self.width / 3:
            self.player2.center_y = touch.y



class PongApp(App):
    def build(self):
        game = PongGame()
        game.serve_ball()
        Clock.schedule_interval(game.update, 1.0 / 60.0)
        return game


if __name__ == '__main__':
    PongApp().run()

pong.kv : pong.kv:

    #:kivy 1.8.0

<PongBall>:
    size: 50, 50 
    canvas:
        Ellipse:
            pos: self.pos
            size: self.size          

<PongPaddle>:
    size: 25, 200
    canvas:
        Rectangle:
            pos:self.pos
            size:self.size

<PongGame>:
    ball: pong_ball
    player1: player_left
    player2: player_right

    canvas:
        Rectangle:
            pos: self.center_x-5, 0
            size: 10, self.height

    Label:
        font_size: 70  
        center_x: root.width / 4
        top: root.top - 50
        text: str(root.player1.score)

    Label:
        font_size: 70  
        center_x: root.width * 3 / 4
        top: root.top - 50
        text: str(root.player2.score)

    PongBall:
        id: pong_ball
        center: self.parent.center

    PongPaddle:
        id: player_left
        x: root.x
        center_y: root.center_y

    PongPaddle:
        id: player_right
        x: root.width-self.width
        center_y: root.center_y

PongGame().stop_serve(vel = (0,0)) PongGame()。stop_serve(vel =(0,0))

This is the python syntax to make a new PongGame instance with a different ball, so stop_serve runs fine but simply doesn't affect any of the widgets that you're actually displaying. 这是使用另一个球制作新的 PongGame实例的python语法,因此stop_serve可以正常运行,但不会影响您实际显示的任何小部件。

You instead need to use a reference to the existing PongGame. 相反,您需要使用对现有PongGame的引用。 In this case you could use self.parent.stop_serve() . 在这种情况下,您可以使用self.parent.stop_serve()

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

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