-
Notifications
You must be signed in to change notification settings - Fork 0
/
TargetHunter.py
425 lines (308 loc) · 10.8 KB
/
TargetHunter.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# -*- coding: utf-8 -*-
"""
TargetHunter game code
github.com/B4tiste/TargetHunter
@author: B4tiste
"""
from ursina import * # Import all the Ursina lib
import random
import time
import os
def clear(): return os.system('cls')
# Change this value according to your screen refresh rate (60Hz for instance):
REFRESH_RATE = 60
# Duration of a game : 30s
GAME_TIME = 30
SPEED = 5
spd = SPEED
STARTUP_POSITION = (0, 0)
SIZE_PLAYER = (1, 1)
SIZE_TARGET = (0.5, 0.5)
SIZE_BONUS = (0.3, 0.3)
SIZE_PENALTY = (0.3, 0.3)
STEP_BONUS = 0.3
STEP_PENALTY = 0.2
GAP = 0.75
DELAY = 0
score = 0
hits = 0
time.sleep(DELAY)
rem_time = GAME_TIME
cpt = 0
menu_start = 0
var = 0
wall_hit = 0
bonus_hit = 0
penalty_hit = 0
penalty_effect = 0
rdm = 0
is_bonus_on = 0
bonus_iter = 0
is_penalty_on = 0
penalty_iter = 0
is_in_menu = 0
window.borderless = False
def menu():
global menu_start
global var
global score
global hits
global is_in_menu
global is_bonus_on
is_in_menu = 1
target.scale = (0, 0)
bonus.scale = (0, 0)
penalty.scale = (0, 0)
info.size = 0.00
text_score.text = '\nFinal Score = ' + str(score) + '\nTarget hit = ' + str(
hits) + '\nYou touched ' + str(wall_hit) + ' times the side !'
best_score_check()
text_menu.x = 0
text_menu.y = 0.25
text_menu.text = 'Press SPACE to start the game'
def bonus_start():
global SIZE_BONUS
global is_bonus_on
is_bonus_on = 0
bonus.scale = SIZE_BONUS
bonus.color = color.magenta
x_rdm = random.randint(-5, 5)
y_rdm = random.randint(-3, 3)
bonus.x = x_rdm
bonus.y = y_rdm
def penalty_start():
global SIZE_PENALTY
global is_penalty_on
is_penalty_on = 0
penalty.scale = SIZE_PENALTY
penalty.color = color.black
x_rdm = random.randint(-5, 5)
y_rdm = random.randint(-3, 3)
penalty.x = x_rdm
penalty.y = y_rdm
def update():
global cpt
global score
global hits
global spd
global rem_time
global menu_start
global REFRESH_RATE
global var
global is_in_menu
global wall_hit
global is_bonus_on
global is_penalty_on
global rdm
global bonus_iter
global penalty_iter
global STEP_BONUS
global STEP_PENALTY
global bonus_hit
global penalty_hit
global penalty_effect
rdm_time_bonus = random.randint(5, int(GAME_TIME/2))
rdm_time_penalty = random.randint(1, 5)
cpt = cpt + 1
if cpt == REFRESH_RATE:
rem_time = rem_time - 1
cpt = 0
info.text = 'Score = ' + str(score) + \
'\nTime remaining : ' + str(rem_time) + 's'
if penalty_effect == 0:
if held_keys['z'] or held_keys['up arrow']: # Go Up
player.y = player.y + spd * time.dt
if held_keys['q'] or held_keys['left arrow']: # Go to the Left
player.x = player.x - spd * time.dt
if held_keys['s'] or held_keys['down arrow']: # Go Down
player.y = player.y - spd * time.dt
if held_keys['d'] or held_keys['right arrow']: # Go to the Right
player.x = player.x + spd * time.dt
if penalty_effect == 1:
if held_keys['s'] or held_keys['up arrow']: # Go Up REVERSE
player.y = player.y + spd * time.dt
if held_keys['d'] or held_keys['left arrow']: # Go to the Left REVERSE
player.x = player.x - spd * time.dt
if held_keys['z'] or held_keys['down arrow']: # Go Down REVERSE
player.y = player.y - spd * time.dt
if held_keys['q'] or held_keys['right arrow']: # Go to the Right REVERSE
player.x = player.x + spd * time.dt
if rem_time == rdm_time_bonus:
is_bonus_on = 1
if bonus_iter == 0:
bonus_iter = 1
bonus_start()
if rem_time == rdm_time_penalty:
is_penalty_on = 1
if penalty_iter == 0:
penalty_iter = 1
penalty_start()
if is_bonus_on:
if cpt % 2 == 0:
rdm = random.randint(1, 4)
if rdm == 1:
bonus.y = bonus.y + STEP_BONUS
if rdm == 2:
bonus.y = bonus.y - STEP_BONUS
if rdm == 3:
bonus.x = bonus.x - STEP_BONUS
if rdm == 4:
bonus.x = bonus.x + STEP_BONUS
if is_penalty_on:
if cpt % 2 == 0:
rdm = random.randint(1, 4)
if rdm == 1:
penalty.y = penalty.y + STEP_PENALTY
if rdm == 2:
penalty.y = penalty.y - STEP_PENALTY
if rdm == 3:
penalty.x = penalty.x - STEP_PENALTY
if rdm == 4:
penalty.x = penalty.x + STEP_PENALTY
if held_keys['escape'] or rem_time == 0:
is_bonus_on = 0
is_penalty_on = 0
penalty_effect = 0
menu()
if held_keys['l']:
exit()
# Game restart
if held_keys['space'] and is_in_menu:
is_in_menu = 0
time.sleep(0.1)
menu_start = 1
bonus_iter = 0
penalty_iter
score = 0
hits = 0
wall_hit = 0
bonus_hit = 0
penalty_hit = 0
is_bonus_on = 0
is_penalty_on = 0
penalty_effect = 0
rem_time = GAME_TIME
target.scale = SIZE_TARGET
player.position = STARTUP_POSITION
spd = SPEED
info.size = 0.03
info.color = color.white
info.text = 'Score = ' + \
str(score) + '\nTime remaining : ' + str(rem_time) + 's'
text_score.text = ''
text_menu.x = -0.65
text_menu.y = 0.45
text_menu.text = 'ESCAPE ==> MENU'
text_new_best_score.text = ''
text_current_score.text = ''
# When the player hits a target
if player.x < (target.x + GAP) and player.x > (target.x - GAP):
if player.y < (target.y + GAP) and player.y > (target.y - GAP):
if is_in_menu == 0:
x_rdm = random.randint(-5, 5)
y_rdm = random.randint(-3, 3)
target.x = x_rdm
target.y = y_rdm
score = score + 1
hits = hits + 1
info.text = 'Score = ' + \
str(score) + '\nTime remaining : ' + str(rem_time) + 's'
spd = spd + 1
# When the player hits a bonus
if player.x < (bonus.x + GAP) and player.x > (bonus.x - GAP):
if player.y < (bonus.y + GAP) and player.y > (bonus.y - GAP):
if is_bonus_on == 1 and bonus_hit == 0:
bonus_hit = 1
is_bonus_on = 0
bonus.scale = (0, 0)
bonus.color = color.dark_gray
bonus.position = (7, 4)
rem_time = rem_time + 5
# When the player hits a penalty
if player.x < (penalty.x + GAP) and player.x > (penalty.x - GAP):
if player.y < (penalty.y + GAP) and player.y > (penalty.y - GAP):
if is_penalty_on == 1 and penalty_hit == 0:
penalty_hit = 1
is_penalty_on = 0
penalty.scale = (0, 0)
penalty.color = color.dark_gray
penalty.position = (7, 4)
penalty_effect = 1
# When the player hits one of the left or right border
if player.x < -7 or player.x > 7:
if is_in_menu == 0:
score = score - 1
wall_hit = wall_hit + 1
info.text = 'Score = ' + \
str(score) + '\nTime remaining : ' + str(rem_time) + 's'
player.position = STARTUP_POSITION
spd = spd - 1
if score < 0:
score = 0
if spd < 1:
spd = 1
# When the player hits one of the upper or lower border
if player.y < -4 or player.y > 4:
if is_in_menu == 0:
score = score - 1
wall_hit = wall_hit + 1
info.text = 'Score = ' + \
str(score) + '\nTime remaining : ' + str(rem_time) + 's'
player.position = STARTUP_POSITION
spd = spd - 1
if score < 0:
score = 0
if spd < 1:
spd = 1
# When the bonus leaves the window
if bonus.x < -7 or bonus.x > 7 or bonus.y < -4 or bonus.y > 4:
bonus.x = 0
bonus.y = 0
# When the penalty leaves the window
if penalty.x < -7 or penalty.x > 7 or penalty.y < -4 or penalty.y > 4:
penalty.x = 0
penalty.y = 0
if rem_time < 6:
info.color = color.orange
def best_score_check():
global score
f_score = open('TargetHunter_score.txt', 'r')
saved_score = f_score.read()
f_score.close()
if int(saved_score) < score:
text_new_best_score.text = 'Congratulations, you beat your best score by ' + \
str(score - int(saved_score))
f_score = open('TargetHunter_score.txt', 'w')
f_score.write(str(score))
f_score.close()
f_score = open('TargetHunter_score.txt', 'r')
saved_score = f_score.read()
f_score.close()
text_current_score.text = 'Current best score = ' + str(saved_score)
# Create an instance of Ursina ~Window
app = Ursina()
# Usina use Entities, 'elements' that can be almost anything in the instance
# [entity_name] = Entity(model = 'model', color = color.COLOR_NAME, scale = (x, y), position = (x, y))
# The center of the window is the point with (x, y) = (0, 0)
player = Entity(model='quad', color=color.green,
scale=SIZE_PLAYER, position=STARTUP_POSITION)
target = Entity(model='quad', color=color.red,
scale=SIZE_TARGET, position=(1, 1))
bonus = Entity(model='quad', color=color.dark_gray,
scale=(0, 0), position=(7, 4))
penalty = Entity(model='quad', color=color.dark_gray,
scale=(0, 0), position=(7, 4))
info = Text(text='Score', origin=(0, 0), size=0.03, color=color.white)
text_score = Text(text='', origin=(0, 0), size=0.03, color=color.cyan)
text_new_best_score = Text(text='', origin=(
0, 0), size=0.03, color=color.green, x=0, y=-0.30)
text_current_score = Text(text='', origin=(
0, 0), size=0.03, color=color.green, x=0, y=-0.20)
text_menu = Text(text='ESCAPE ==> MENU', size=0.03, origin=(
0, 0), color=color.peach, font='assets/Minecraft.ttf', x=-0.65, y=0.45)
text_exit = Text(text='PRESS L TO LEAVE THE GAME', size=0.03, origin=(
0, 0), color=color.peach, font='assets/Minecraft.ttf', x=-0.65, y=0.35)
text_bonus = Text(text='Flying Bonus : +5s', size=0.03, origin=(
0, 0), color=color.peach, font='assets/Minecraft.ttf', x=-0.65, y=0.40)
# Start the instance created at l.109
app.run()