-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
153 lines (109 loc) · 4.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from kivy.config import Config
Config.set('graphics','resizable',False)
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty, StringProperty
from kivy.vector import Vector
from kivy.clock import Clock
from random import random, randrange
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.metrics import sp
class PongPaddle(Widget):
score = NumericProperty(0)
def bounce_ball(self, ball, p_no: int):
if self.collide_widget(ball):
speedup = 1.3
stuff = [abs(self.y - ball.top),abs(self.x-ball.right),abs(self.top-ball.y),abs(self.right-ball.x)]
q = stuff.index(min(stuff))
if q % 2:
ball.velocity_x = -ball.velocity_x
else:
ball.velocity_y = -ball.velocity_y
ball.velocity_x *= speedup
ball.velocity_y *= speedup
class PongGame(Screen):
name = StringProperty('game')
ball = ObjectProperty(None)
player1 = ObjectProperty(None)
player2 = ObjectProperty(None)
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.winner = StringProperty('Player 0')
def on_enter(self):
self.serve_ball()
def serve_ball(self):
self.ball.center = self.center
self.ball.velocity = (0,0)
def _serve_ball(dt):
self.ball.velocity = Vector(random()*randrange(-1,2,2),0.3*random()*randrange(-1,2,2)).normalize() * sp(4)
Clock.schedule_once(_serve_ball,1.25)
def serve_ball_caller(self,dt):
self.serve_ball()
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
def update(self, dt):
self.ball.move()
self.player1.bounce_ball(self.ball,1)
self.player2.bounce_ball(self.ball,2)
# bounce ball off bottom or top
if (self.ball.y < self.y) or (self.ball.top > self.top):
self.ball.velocity_y *= -1
speedup = 1.1
self.ball.velocity_x *= speedup
self.ball.velocity_y *= speedup
# went of to a side to score point?
if self.ball.x < self.x - sp(30):
self.player2.score += 1
self.serve_ball()
if self.player2.score >= 11:
self.player1.score = 0
self.player2.score = 0
App.get_running_app().root.set_winner('Player 2')
App.get_running_app().root.current = 'restart'
# Clock.schedule_once(self.serve_ball_caller,1)
if self.ball.right > self.width + sp(30):
self.player1.score += 1
self.serve_ball()
if self.player1.score >= 11:
self.player1.score = 0
self.player2.score = 0
App.get_running_app().root.set_winner('Player 1')
App.get_running_app().root.current = 'restart'
# Clock.schedule_once(self.serve_ball_caller,1)
class PongMenu(Screen):
name = StringProperty('menu')
def update(self, dt):
pass
class PongRestart(Screen):
name = StringProperty('restart')
def update(self, dt):
pass
class PongManager(ScreenManager):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.transition = NoTransition()
def update(self, dt):
self.current_screen.update(dt)
def set_winner(self, winner: str):
self.pong_restart.restart_label.text = winner + ' wins!'
class PongApp(App):
def build(self):
sm = PongManager()
Clock.schedule_interval(sm.update, 1.0/60.0)
return sm
class PongBall(Widget):
# velocity of the ball on x and y axis
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
# referencelist property so we can use ball.velocity as
# a shorthand, just like e.g. w.pos for w.x and w.y
velocity = ReferenceListProperty(velocity_x, velocity_y)
# ``move`` function will move the ball one step. This
# will be called in equal intervals to animate the ball
def move(self):
self.pos = Vector(*self.velocity) + self.pos
if __name__ == '__main__':
PongApp().run()