如何在Kivy Pong游戏中从另一个类调用类中的函数

1 投票
2 回答
2529 浏览
提问于 2025-04-18 11:08

我正在练习使用Kivy,参考教程中的PongGame代码。我想知道如何从新创建的类PongSample中调用PongGame类里的一个函数serve_ball2()。在下面的代码中,我创建了一个PongSample类,目的是在第一个球碰到挡板后发出第二个球。

更新:我可以从PongSample中调用serve_ball2(),但这个函数并没有按预期工作,也就是说它没有发出球。

我已经把完整的代码分享在下面了。提前谢谢你们!

Pong.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, time
from threading import Thread

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

    def bounce_ball(self, ball):
        if self.collide_widget(ball):
            vx, vy = ball.velocity
            offset = (ball.center_y - self.center_y) / (self.height / 2)
            bounced = Vector(-1 * vx, vy)
            vel = bounced * 1.1
            ball.velocity = vel.x, vel.y + offset
            PongSample().call_game()

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 PongSample(Widget):
    def call_game(self):
        print 'PongSample'
        ponggame=PongGame()
        ponggame.serve_ball2()

class PongGame(Widget):
    ball = ObjectProperty(None)
    ball2 = 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 serve_ball2(self, vel=(3, 0)):  
        print 'Serve_ball2'
        self.ball2.center = self.center
        self.ball2.velocity = vel

    def serve_down(self):
        print 'Inside Serve Down'
        self.ball.center = self.center
        self.ball.velocity = Vector(4,0).rotate(-90)

    def update(self, dt):
        self.ball.move()
        self.ball2.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
        if (self.ball2.y < self.y) or (self.ball2.top > self.top):
            self.ball2.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))

        if self.ball2.x < self.x:
            self.player2.score += 1
            self.serve_ball2(vel=(3, 0))
        if self.ball2.x > self.width:
            self.player1.score += 1
            self.serve_ball2(vel=(-3, 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:

    #: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
    ball2: pong_ball2
    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

    PongBall:
        id: pong_ball2
        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

2 个回答

1

要在你的程序中使用这个类,先在 PongApp 里加上 game.serve_ball2()

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

然后再把 self.ball2 加入到可以和球拍反弹的部分:

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

我想强调的是,这看起来没必要,而且PongSample这个东西似乎根本不需要存在。不过,按照你的要求,我觉得下面的方法应该可以实现。不过,我真的不喜欢这一点,就是在kv文件里创建了一个PongSample实例,除了给ball2服务之外没有其他用途。不过……

为什么不把serve_ball2这个函数放在PongSample类里,然后把ball2传给它呢?

举个例子:

class PongSample(Widget):

    def serve_ball2(self, ball2, vel=(3,0)):
        print 'Serve ball 2'
        ball2.center = self.center
        ball2.velocity = vel

在PongGame类里:

class PongGame(Widget):
    ball = ObjectProperty(None)
    ball2 = ObjectProperty(None)
    player1 = ObjectProperty(None)
    player2 = ObjectProperty(None)
    # add this
    sample = ObjectProperty(None)

然后在kv文件里:

# add at the top

<PongSample>:
    size: self.size
    pos: self.pos

# add the below in appropriate places within the PongGame definition

PongGame:
    sample: pong_sample

    PongSample:
        id: pong_sample # now it's linked to 'sample' in PongGame

这样在PongGame里,你可以从任何方法调用self.sample.serve_ball2(ball2)

撰写回答