-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·390 lines (330 loc) · 7.98 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.graphics import Rectangle, Color
from kivy.properties import ListProperty, NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock
from kivy.core.window import WindowBase, Window
from kivy.lang import Builder
from libdw import sm
from time import sleep
class Velocity_sm(sm.SM):
def __init__(self, velocity):
super(Velocity_sm, self).__init__()
self.velocity = velocity
start_state = 1
def get_next_value(self, state, inp):
next_state = state + 1
output = inp * (1 + 1.0 / next_state)
return next_state, output
class MainGame(Widget):
v = Velocity_sm(3)
v.start()
lvl = NumericProperty(v.state)
label = ObjectProperty(None)
ball = ObjectProperty(None)
ball = ObjectProperty(None)
paddle = ObjectProperty(None)
button = ObjectProperty(None)
box1 = ObjectProperty(None)
box2 = ObjectProperty(None)
rect1 = ObjectProperty(None)
rect2 = ObjectProperty(None)
rect3 = ObjectProperty(None)
rect4 = ObjectProperty(None)
rect5 = ObjectProperty(None)
rect6 = ObjectProperty(None)
rect7 = ObjectProperty(None)
rect8 = ObjectProperty(None)
rect9 = ObjectProperty(None)
rect10 = ObjectProperty(None)
rect11 = ObjectProperty(None)
rect12 = ObjectProperty(None)
rect13 = ObjectProperty(None)
rect14 = ObjectProperty(None)
rect15 = ObjectProperty(None)
# on_click callback of the button
def toggle(self):
if self.ball.velocity != Vector(0, 0):
self.ball.center = self.center[0], self.center[1] + 60
self.stop()
else:
self.start()
def stop(self):
self.ball.velocity = Vector(0, 0)
rectlist = [self.rect1, self.rect2, self.rect3, self.rect4, self.rect5, self.rect6, self.rect7, self.rect8, self.rect9, self.rect10, self.rect11, self.rect12, self.rect13, self.rect14, self.rect15]
for rect in rectlist:
rect.reset()
self.button.text = "START"
self.button.background_color = (0.1, 1, 0, 0.9)
def start(self):
self.paddle.center_x = self.center_x
self.ball.velocity = Vector(0, -1 * self.v.velocity)
self.button.text = "STOP"
self.label.color = (1,1,1,1)
self.label.text = "Level: " + str(self.v.state)
self.button.background_color = (1, 0, 0, 0.6)
def update(self, dt):
if Window.size != (1000, 900):
Window.size = (1000, 900)
self.ball.move()
self.paddle.bounce_ball(self.ball)
rectlist = [self.rect1, self.rect2, self.rect3, self.rect4, self.rect5, self.rect6, self.rect7, self.rect8, self.rect9, self.rect10, self.rect11, self.rect12, self.rect13, self.rect14, self.rect15]
counter = 0
# loop to check if targets are hit, if not proceed with bounce_ball function
for i in range(len(rectlist)):
if rectlist[i].bounce == 0:
rectlist[i].bounce_ball(self.ball)
else:
counter += 1
# every targets are hit, enter the next level
if counter == i + 1:
self.ball.center = self.center[0], self.center[1] + 60
self.stop()
self.label.text = "LEVEL PASSED!"
self.label.color = (0,1,0,1)
self.v.state, self.v.velocity = self.v.get_next_value(self.v.state, self.v.velocity)
# hit top, add y velocity as penalty
if self.ball.top >= self.height - 5:
self.ball.velocity_y *= -1
self.ball.velocity_y -= 0.3 * self.v.state**0.5
# hit left or right
if (self.ball.x <= 5) or (self.ball.right >= self.width - 5):
self.ball.velocity_x *= -1
# hit bottom bar, game over, reset level
if self.ball.y <= 160:
self.label.text = "GAME OVER"
self.label.color = (1,0,0,0.9)
self.v = Velocity_sm(3)
self.v.start()
self.ball.move()
sleep(0.2)
self.ball.center = self.center[0], self.center[1] + 60
self.stop()
def on_touch_move(self, touch):
if touch.y < self.height / 2 + 160 and touch.y >= 130:
self.paddle.center_x = touch.x
class StartButton(Button):
pass
# blank space between the targets
class Spacing(Widget):
pass
class Paddle(Widget):
def bounce_ball(self, ball):
if self.collide_widget(ball):
vx, vy = ball.velocity
offset = (ball.center_x - self.center_x) / (self.width / 10.0)
bounced = Vector(vx, vy * -1)
ball.velocity = (bounced.x + offset) * 1.1, bounced.y * 1.03
class Rect(Widget):
bounce = 0
def reset(self):
self.bounce = 0
with self.canvas:
Color(1, 1, 0)
Rectangle(pos=self.pos, size=self.size)
def bounce_ball(self, ball):
if self.collide_widget(ball):
self.bounce += 1
vx, vy = ball.velocity
offset = (ball.center_x - self.center_x) / self.width
bounced = Vector(vx, vy * -1)
vel = bounced * 1.03
ball.velocity = vel.x + offset, vel.y
with self.canvas:
Color(0, 0, 0)
Rectangle(pos=self.pos, size=self.size)
class Ball(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 MainApp(App):
def build(self):
Builder.load_string("""
<WindowBase>:
minimum_height: 1000
minimum_width: 900
<MainGame>:
canvas:
Color:
rgb: 1, 0, 0
Rectangle:
pos: 0, 155
size: self.width, 5
Color:
rgb: 0, 1, 0.7
Rectangle:
pos: 0, 0
size: 5, self.height
Color:
rgb: 0, 1, 0.7
Rectangle:
pos: root.width - 5, 0
size: 5, self.height
Color:
rgb: 1, 0.7, 0
Rectangle:
pos: 0, self.height - 5
size: self.width, 5
Color:
rgb: 1, 1, 1
Rectangle:
pos: 0, 130
size: self.width, 25
source: './assets/arrow.png'
Color:
rgb: 0.2, 0.25, 0.25
Rectangle:
pos: 0, 0
size: self.width, 130
ball: ball
paddle: paddle
button: button
label: label
box1: box1
box2: box2
rect1: rect1
rect2: rect2
rect3: rect3
rect4: rect4
rect5: rect5
rect6: rect6
rect7: rect7
rect8: rect8
rect9: rect9
rect10: rect10
rect11: rect11
rect12: rect12
rect13: rect13
rect14: rect14
rect15: rect15
Paddle:
id: paddle
size: root.width / 8.0, root.height / 20.0
center_x: root.center_x - 81
y: root.y + 160
Label:
id: label
font_size: root.width * 0.05
center_x: root.width / 4
top: root.top - root.height + 120
text: "Level: " + str(root.lvl)
StartButton:
id: button
font_size: root.width * 0.05
x: root.width * 0.6
top: 100
text: "START"
size: root.width / 4.0, 130
background_color: 0.1, 1, 0, 0.6
on_press: root.toggle()
BoxLayout:
id: box1
top: root.top
Rect:
id: rect1
Spacing:
Rect:
id: rect2
Spacing:
Rect:
id: rect3
Spacing:
Rect:
id: rect4
Spacing:
Rect:
id: rect5
Spacing:
Rect:
id: rect6
Spacing:
Rect:
id: rect7
Spacing:
Rect:
id: rect8
BoxLayout:
id: box2
top: root.top - 50
Spacing:
Rect:
id: rect9
Spacing:
Rect:
id: rect10
Spacing:
Rect:
id: rect11
Spacing:
Rect:
id: rect12
Spacing:
Rect:
id: rect13
Spacing:
Rect:
id: rect14
Spacing:
Rect:
id: rect15
Spacing:
Ball:
id: ball
center: self.parent.center
<Ball>:
size: 50, 50
canvas:
Color:
rgb: 1, 0, 1
Ellipse:
pos: self.pos
size: self.size
<Paddle>:
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos:self.pos
size:self.size
source: './assets/paddle.png'
<Rect>:
top: self.parent.top - 5
size: self.parent.width / 8.0, 40
canvas:
Color:
rgb: 1, 1, 0
Rectangle:
pos:self.pos
size:self.size
<Spacing>:
top: self.parent.top - 5
size: self.parent.width / 8.0, 40
size_hint_x: 0.5
canvas:
Color:
rgb: 0, 0, 0
Rectangle:
pos:self.pos
size:self.size
<BoxLayout>:
orientation: 'horizontal'
padding: 5
center_x: root.center_x
size: self.parent.width, 50
""")
game = MainGame()
game.box1.width = 1400
game.box2.width = 1400
game.ball.center = game.center[0], game.center[1] + 180
game.paddle.center_x = game.center_x
Clock.schedule_interval(game.update, 1 / 64.0)
return game
if __name__ == '__main__':
Window.size = (1000,900)
app = MainApp()
app.run()