python - How to call a function from a class from another class in kivy Pong ball game -
i'm practicing kivy ponggame code given in tutorial. want know how call function - serve_ball2() in class ponggame newly created class - pongsample. in below code created class pongsample serve second ball once first ball collide paddle.
update : can call serve_ball2() pongsample serve_ball2() doesn't function intended i.e doesn't serve ball.
i've shared complete code below. in advance
pong.py:
from kivy.app import app kivy.uix.widget import widget kivy.properties import numericproperty, referencelistproperty,\ objectproperty kivy.vector import vector kivy.clock import clock, time 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 side 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
to use class add 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
and add self.ball2
bounce off paddles:
#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)
Comments
Post a Comment