-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·171 lines (140 loc) · 5.48 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
__version__ = '0.2.8'
from math import cos, pi, sin
from kivy.animation import Animation
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.properties import (NumericProperty, ObjectProperty,
ReferenceListProperty)
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.vector import Vector
class PongPaddle(Widget):
score = NumericProperty(0)
bounce = NumericProperty(0)
def bounce_ball(self, ball, player):
if self.collide_widget(ball) and self.bounce < 0:
self.bounce = 12
if self.x < Window.width / 2.0:
factor = 1
else:
factor = -1
vx, vy = ball.velocity
offset = (ball.center_y - self.center_y) / (
self.height / 2) * 30 * pi / 180.0 * factor
bounced = Vector(-1 * vx, vy)
vel = bounced * 1.05
ball.velocity = vel.x * cos(offset) - vel.y * sin(
offset), vel.x * sin(offset) + vel.y * cos(offset)
try:
player.score += 1
except AttributeError:
pass
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, **kwargs):
super(PongGame, self).__init__()
self.restart()
def serve_ball(self, vel=(-Window.width / 140.0, 0)):
self.ball.center = self.center
self.ball.velocity = vel
self.player1.center_y = self.center_y
self.player2.center_y = self.center_y
def move_player2(self):
if self.player2.score >= 5:
self.player2.center_y = self.player2.center_y
elif self.player2.y - Window.height / 300.0 < self.y:
self.player2.y = self.y
else:
self.player2.center_y -= Window.height / 300.0
if self.player2.center_y < self.ball.center_y - Window.height / 8.0:
if self.player2.top + Window.height * 15 / 128.0 < self.top:
self.player2.center_y += Window.height * 15 / 128.0
def update(self, dt):
if self.ball.velocity == [0, 0] and self.player2.score < 5:
self.serve_ball()
if self.player2.score >= 5:
self.player1.center_y = self.player1.center_y
elif self.player1.y - Window.height / 300.0 < self.y:
self.player1.y = self.y
else:
self.player1.center_y -= Window.height / 300.0
self.player1.bounce_ball(self.ball, self.player1)
self.player2.bounce_ball(self.ball, None)
self.player1.bounce -= 1
self.player2.bounce -= 1
if (self.ball.y < self.y) or (self.ball.top > self.top):
self.ball.velocity_y *= -1
if self.ball.x < self.x:
self.player2.score += 1
if self.player2.score < 5:
self.serve_ball()
elif self.player2.score >= 5:
self.serve_ball((0, 0))
self.end()
if self.ball.x > self.width:
self.serve_ball()
self.move_player2()
self.ball.move()
def on_touch_move(self, touch):
if touch.x < self.width:
if self.player1.center_y + Window.height / 8 < self.top and self.player2.score <= 5:
self.player1.center_y += Window.height / 16
elif self.player2.score >= 5:
self.player1.center_y = self.player1.center_y
else:
self.player1.top = self.top
def end(self):
end = self.ids.end.__self__
self.remove_widget(end)
self.add_widget(end)
Animation(opacity=1., d=.5).start(end)
def restart(self):
self.player2.score = 0
self.player1.score = 0
Clock.schedule_once(self.update, 1 / 60.0)
self.ids.end.opacity = 0
self.player1.center_y = self.center_y
self.player2.center_y = self.center_y
class PongMenu(FloatLayout):
def __init__(self, **kwargs):
super(PongMenu, self).__init__(**kwargs)
self.size = (100, 100)
self.orientation = 'vertical'
self.spacing = 10
self.padding = [10, 10, 10, 10]
btn1 = Button(text='Play',
size_hint=(.3, .2),
pos_hint={
'right': .9,
'top': .8
})
btn1.bind(on_press=self.play_pong)
btn2 = Button(text='Settings',
size_hint=(.3, .2),
pos_hint={
'right': .9,
'top': .4
})
self.add_widget(btn1)
self.add_widget(btn2)
def play_pong(self, *l):
if not hasattr(self, 'play'):
self.play = play = PongGame()
Clock.schedule_interval(play.update, 1.0 / 60.0)
self.add_widget(play)
class LazyPongApp(App):
use_kivy_settings = False
def build(self):
return PongMenu()
if __name__ == '__main__':
LazyPongApp().run()