python - How to display game on the screen? -
i'm trying make kivy app starting menu, can't display pong game on second screen. how should refer game make visible? tried , searched can't find anything. i'm 100% sure ponggame work corectly, can't display it. great if show me how corectly.
main.py:
from kivy.app import app kivy.uix.screenmanager import screenmanager, screen, wipetransition kivy.properties import objectproperty kivy.uix.widget import widget kivy.properties import numericproperty, referencelistproperty,\ objectproperty kivy.vector import vector kivy.clock import clock kivy.uix.popup import popup kivy.uix.label import label 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 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 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 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.player1.score == 10: popup = popup(title='test popup', content=label(text='hello world'), auto_dismiss=false) return popup 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 screenthree(screen): pass class screentwo(screen): pass class screenone(screen): pass class manager(screenmanager): screen_one = objectproperty(none) screen_two = objectproperty(none) screen_three = objectproperty(none) class screensapp(app): def build(self): m = manager(transition=wipetransition()) return m if __name__ == '__main__': screensapp().run()
screen.kv:
<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 <screenone>: button: text: "screen 1 >> screen 2" on_press: root.manager.current = 'screen2' <screentwo>: def build(self): game = ponggame() game.serve_ball() clock.schedule_interval(game.update, 1.0 / 60.0) return game <screenthree>: button: text: "screen 3 >> screen 1" on_press: root.manager.current = 'screen1' <manager>: id: screen_manager screen_one: screen_one screen_two: screen_two screen_three: screen_three screenone: id: screen_one name: 'screen1' manager: screen_manager screentwo: id: screen_two name: 'screen2' manager: screen_manager screenthree: id: screen_three name: 'screen3' manager: screen_manager
well! there lot of errors in program , had make ton's of improvement. (i understand beginner)
first of all, please read complete kivy language documentation, can see directly started coding without grasping basics. may make couple of games in long run face such problems can't solved without clear concepts. , unfortunately won't able discover true power of kivy. :)
you might wanna revision of python concepts.
some improvements aren't worth mentioning important, idea when read code.
improvement 1:
an application can built if return widget on build(), or if set self.root.(but cannot make application again n again) did here:
<screentwo>: def build(self): game = ponggame() game.serve_ball() clock.schedule_interval(game.update, 1.0 / 60.0) return game
improvement 2:
as click on button play ping pong of screen game. game starts serve of ball.
on_release: root.current = 'game';game.serve_ball()
(for knowledge)
if still black screen might want check name of kivy file, either go kivy documentation or this link
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 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 __init__(self, *args, **kwargs): super(ponggame, self).__init__(*args, **kwargs) clock.schedule_interval(self.update, 1.0 / 60.0) def serve_ball(self, vel=(4, 0)): 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 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)) 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 manager(screenmanager): pass class screensapp(app): def build(self): self.load_kv('t6.kv') return manager(transition=wipetransition()) if __name__ == '__main__': screensapp().run()
here kv file.
<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 <manager>: id: screen_manager screen: name: 'home' button: text: 'play ping pong' halign: 'center' valign: 'middle' font_size: 100 text_size: self.size on_release: root.current = 'game';game.serve_ball() screen: name: 'game' ponggame: id: game
Comments
Post a Comment