在kivypongball-gam中如何从另一个类调用一个函数

2024-10-01 15:40:35 发布

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

我在用教程中给出的PongGame代码练习Kivy。我想知道如何从一个新创建的类PongSample调用pongame类中的函数-serve_ball2()。在下面的代码中,我创建了一个类PongSample,以便在第一个球与球拍碰撞时为第二个球提供服务。在

更新:我可以从PongSample调用serve_ball2(),但serve_ball2()不能按预期工作,即它没有发球。在

我已经分享了下面的完整代码。提前谢谢

在乒乓球公司名称:

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

在庞千伏公司名称:

^{pr2}$

Tags: fromimportselfgameifdefscorecenter
2条回答

我强调这似乎没有必要,PongSample本身似乎根本不需要存在。但是,要按照你的要求去做,我相信以下几点应该行得通。不过,有一点我真的不喜欢,那就是在kv文件中创建了一个PongSample实例,除了为ball2服务之外,没有其他目的。然而。。。在

为什么不在PongSample类中定义serve_ball2函数,并将ball2传递给它呢?在

示例:

class PongSample(Widget):

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

在乒乓球课上:

^{pr2}$

那么在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)。在

要使用您的类,请添加game.serve_ball2()to PongApp

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来弹开球拍:

^{pr2}$

相关问题 更多 >

    热门问题