-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
382 lines (294 loc) · 11.7 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
from kivy.config import Config
Config.set('graphics', 'width', '1500')
Config.set('graphics', 'height', '800')
from kivy.core.window import Window
from kivy import platform
from kivy.app import App
from kivy.metrics import dp
from kivy.properties import StringProperty, BooleanProperty, ObjectProperty,NumericProperty,Clock
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
import random
from kivy.core.audio import SoundLoader
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.behaviors import CoverBehavior
from kivy.graphics import*
#current_y_loop pour augment la vitesse
Builder.load_file("menu.kv")
class MainWidget(RelativeLayout):
from transforms import transform,transform_2d,transform_perspective
from user_actions import on_keyboard_up,on_keyboard_down,keyboard_closed,on_touch_up,on_touch_down
menu_widget=ObjectProperty()
perspective_point_x=NumericProperty(0)
perspective_point_y=NumericProperty(0)
V_NB_LINES=8
V_LINES_SPACING=.4
vertical_lines=[]
H_NB_LINES=8
H_LINES_SPACING=.15
horizontal_lines=[]
SPEED=0.8
current_offset_y=0
current_y_loop=0
SPEED_X=3.5
current_speed_x=0
current_offset_x=0
NB_TILES=16
tiles= []
tiles_coordinate=[]
SHIP_WIDTH =.1
SHIP_HEIGHT=0.035
SHIP_BASE_Y=0.04
ship=None
ship_coordinates=[(0,0),(0,0),(0,0)]
state_game_over=False
state_game_has_started=False
menu_title=StringProperty("G A L A X Y")
menu_button_title=StringProperty("START")
score_txt=StringProperty()
sound_begin=None
sound_galaxy=None
sound_gameover_impact=None
sound_gameover_voice=None
sound_music1=None
sound_restart=None
def __init__(self, **kwargs):
super(MainWidget,self).__init__(**kwargs)
print(self.width,self.height)
self.init_audio()
self.init_vertical_lines()
self.init_horizontal_lines()
self.init_tiles()
self.init_ship()
self.reset_game()
if self.is_desktop():
self._keyboard = Window.request_keyboard(self.keyboard_closed, self)
self._keyboard.bind(on_key_down=self.on_keyboard_down)
self._keyboard.bind(on_key_up=self.on_keyboard_up)
Clock.schedule_interval(self.update,1.0/60.0)
self.sound_galaxy.play()
def init_audio(self):
self.sound_begin=SoundLoader.load("audio/begin.wav")
self.sound_galaxy=SoundLoader.load("audio/galaxy.wav")
self.sound_gameover_impact=SoundLoader.load("audio/gameover_impact.wav")
self.sound_gameover_voice=SoundLoader.load("audio/gameover_voice.wav")
self.sound_music1=SoundLoader.load("audio/music1.wav")
self.sound_restart=SoundLoader.load("audio/restart.wav")
self.sound_music1.volume=1
self.sound_begin.volume=.25
self.sound_galaxy.volume=.25
self.sound_gameover_impact.volume=.6
self.sound_gameover_voice.volume=.25
self.sound_restart.volume=.25
def reset_game(self):
self.current_offset_y=0
self.current_y_loop=0
self.current_speed_x=0
self.current_offset_x=0
self.tiles_coordinate=[]
self.score_txt="SCORE: "+str(self.current_y_loop)
self.pre_fill_tiles_coordinate()
self.generate_tiles_coordinate()
self.state_game_over=False
def is_desktop(self):
if platform in("linux","win","macosx"):
return True
return False
"""def on_parent(self, widget,parent):
print(self.width,self.height)
def on_size(self,*args):
pass
#self.update_vertical_lines()
#self.update_horizontal_lines()
#print(self.width,self.height)
#self.perspective_point_x=self.width/2
#self.perspective_point_y=self.height*(3/4)
def on_perspective_point_x(self,widget,value):
#print("PX",value)
pass
def on_perspective_point_y(self,widget,value):
#print("PY",value)
pass"""
def init_ship(self):
with self.canvas:
Color(0,0,0)
self.ship=Triangle()
def update_ship(self):
center_x=self.width/2
base_y=self.SHIP_BASE_Y*self.height
half_width=self.SHIP_WIDTH*self.width/2
ship_height=self.SHIP_HEIGHT*self.height
self.ship_coordinates[0]=(center_x-half_width, base_y)
self.ship_coordinates[1]=(center_x, base_y+ship_height)
self.ship_coordinates[2]=(center_x+ half_width,base_y)
x1,y1=self.transform(*self.ship_coordinates[0])
x2,y2=self.transform(*self.ship_coordinates[1])
x3,y3=self.transform(*self.ship_coordinates[2])
self.ship.points=[x1,y1,x2,y2,x3,y3]
def check_ship_collisions(self):
for i in range (0,len(self.tiles_coordinate)):
ti_x,ti_y=self.tiles_coordinate[i]
if ti_y > self.current_y_loop +1:
return False
if self.check_ship_colision_with_tile(ti_x,ti_y):
return True
return False
def check_ship_colision_with_tile(self,ti_x,ti_y):
xmin,ymin=self.get_tile_coordinates(ti_x,ti_y)
xmax,ymax=self.get_tile_coordinates(ti_x+1,ti_y+1)
for i in range (0,3):
px,py=self.ship_coordinates[i]
if xmin <= px <= xmax and ymin <= py <= ymax:
return True
return False
def init_tiles(self):
with self.canvas:
Color(1,1,1)
for i in range (len(self.tiles_coordinate),self.NB_TILES):
self.tiles.append(Quad())
def pre_fill_tiles_coordinate(self):
for i in range (0,10):
self.tiles_coordinate.append((0,i))
def generate_tiles_coordinate(self):
last_x = 0
last_y = 0
# supprimer les coordonnées sorties de l'écran
# ti_y < self.current_y_loop
for i in range(len(self.tiles_coordinate)-1, -1, -1):
if self.tiles_coordinate[i][1] < self.current_y_loop:
del self.tiles_coordinate[i]
if len(self.tiles_coordinate) > 0:
last_coordinate = self.tiles_coordinate[-1]
last_x = last_coordinate[0]
last_y = last_coordinate[1]+1
for i in range(len(self.tiles_coordinate), self.NB_TILES):
r = random.randint(0, 2)
# 0 -> en avant
# 1 -> droite
# 2 -> gauche
start_index = -int(self.V_NB_LINES / 2) + 1
end_index = start_index + self.V_NB_LINES - 2
if last_x <= start_index:
r = 1
if last_x >= end_index:
r = 2
self.tiles_coordinate.append((last_x, last_y))
if r == 1:
last_x += 1
self.tiles_coordinate.append((last_x, last_y))
last_y += 1
self.tiles_coordinate.append((last_x, last_y))
elif r == 2:
last_x -= 1
self.tiles_coordinate.append((last_x, last_y))
last_y += 1
self.tiles_coordinate.append((last_x, last_y))
last_y += 1
def init_vertical_lines(self):
with self.canvas:
Color(1,1,1)
#self.line=Line(points=[100,0,100,100])
#V_NB_LINES=7
#V_LINES_SPACING=.1
for i in range (0,self.V_NB_LINES):
self.vertical_lines.append(Line())
def get_line_x_from_index(self,index):
central_line_x = self.perspective_point_x
spacing = self.V_LINES_SPACING * self.width
offset = index-0.5
line_x=central_line_x+offset*spacing+self.current_offset_x
return line_x
def get_line_y_from_index(self,index):
spacing_y=self.H_LINES_SPACING*self.height
line_y = index*spacing_y - self.current_offset_y
return line_y
def get_tile_coordinates(self, ti_x, ti_y):
ti_y = ti_y - self.current_y_loop
x = self.get_line_x_from_index(ti_x)
y = self.get_line_y_from_index(ti_y)
return x, y
def update_tiles(self):
for i in range(0, self.NB_TILES):
tile = self.tiles[i]
tile_coordinates = self.tiles_coordinate[i]
xmin, ymin = self.get_tile_coordinates(tile_coordinates[0], tile_coordinates[1])
xmax, ymax = self.get_tile_coordinates(tile_coordinates[0]+1, tile_coordinates[1]+1)
# 2 3
#
# 1 4
x1, y1 = self.transform(xmin, ymin)
x2, y2 = self.transform(xmin, ymax)
x3, y3 = self.transform(xmax, ymax)
x4, y4 = self.transform(xmax, ymin)
tile.points = [x1, y1, x2, y2, x3, y3, x4, y4]
def update_vertical_lines(self):
#self.line.points=[self.perspective_point_x,0,self.perspective_point_x,100]
start_index=-int(self.V_NB_LINES/2)+1
for i in range(start_index, start_index+self.V_NB_LINES):
line_x = self.get_line_x_from_index(i)
x1,y1=self.transform(line_x,0)
x2,y2=self.transform(line_x,self.height)
self.vertical_lines[i].points = [x1, y1, x2, y2]
def init_horizontal_lines(self):
with self.canvas:
Color(1,1,1)
for i in range (0,self.H_NB_LINES):
self.horizontal_lines.append(Line())
def update_horizontal_lines(self):
start_index=-int(self.V_NB_LINES/2)+1
end_index= start_index+self.V_NB_LINES -1
xmin=self.get_line_x_from_index(start_index)
xmax=self.get_line_x_from_index(end_index)
for i in range(0, self.H_NB_LINES):
line_y = self.get_line_y_from_index(i)
x1,y1=self.transform(xmin,line_y)
x2,y2=self.transform(xmax,line_y)
self.horizontal_lines[i].points = [x1, y1, x2, y2]
def update(self,dt):
time_factor=dt*60
self.update_vertical_lines()
self.update_horizontal_lines()
self.update_tiles()
self.update_ship()
if not self.state_game_over and self.state_game_has_started:
speed_y=self.SPEED*self.height/100
self.current_offset_y += speed_y*time_factor
spacing_y=self.H_LINES_SPACING*self.height
while self.current_offset_y>=spacing_y:
self.current_offset_y-=spacing_y
self.current_y_loop+=1
self.score_txt="SCORE: "+str(self.current_y_loop)
self.generate_tiles_coordinate()
speed_x=self.current_speed_x*self.width/100
self.current_offset_x+=speed_x*time_factor
if not self.check_ship_collisions() and not self.state_game_over:
self.state_game_over=True
self.menu_title="G A M E O V E R"
self.menu_button_title="RESTART"
self.menu_widget.opacity=1
self.sound_music1.stop()
self.sound_gameover_impact.play()
Clock.schedule_once(self.play_voice_game_over,3)
print ("GAME OVER")
def play_voice_game_over(self,dt):
if self.state_game_over:
self.sound_gameover_voice.play()
def on_menu_button_pressed(self):
print("button")
if self.state_game_over:
self.sound_restart.play()
else:
self.sound_begin.play()
self.sound_music1.play()
self.reset_game()
self.state_game_has_started=True
self.menu_widget.opacity=0
class GalaxyApp(App):
pass
GalaxyApp().run()