-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfence.py
401 lines (326 loc) · 10.4 KB
/
fence.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
import pygame
import random
import time
pygame.init()
pygame.font.init()
display_width = 1200
display_height = 600
black = (0,0,0)
white = (255,255,255)
blue = (9,148,220)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Fence!')
clock = pygame.time.Clock()
leftImg = pygame.image.load('leftImg.png')
rightImg = pygame.image.load('rightImg.png')
stripImg = pygame.image.load('strip.png')
leftImgattack = pygame.image.load('leftImgattack.png')
leftImglunging = pygame.image.load('leftImglunging.png')
rightImgattack = pygame.image.load('rightImgattack.png')
rightImglunging = pygame.image.load('rightImglunging.png')
rightImgparry = pygame.image.load('rightImgparry.png')
leftImgparry = pygame.image.load('leftImgparry.png')
controlsImg = pygame.image.load('controls.png')
hit_time = 0
score_left = 0
score_right = 0
wins_left = 0
wins_right = 0
attack_left = False
parry_left = False
attack_right = False
parry_right = False
tick_count_attack_r = 0
tick_count_attack_l = 0
tick_count_parry_r = 0
tick_count_parry_l = 0
right_ai = False
left_ai = False
font = pygame.font.Font("freesansbold.ttf", 100)
small_font = pygame.font.Font("freesansbold.ttf",(35))
smaller_font = pygame.font.Font("freesansbold.ttf",(22))
def left_character(x,y):
global tick_count_attack_l
#decide which left sprite to display
if attack_left == False and parry_left == False:
gameDisplay.blit(leftImg,(x,y))
elif attack_left == True:
gameDisplay.blit(leftImgattack,(x,y))
elif parry_left == True:
gameDisplay.blit(leftImglunging,(x,y))
def strip():
gameDisplay.blit(stripImg,(0,0))
gameDisplay.blit(controlsImg,(0,0))
def game_loop():
global score_right,score_left,attack_right,attack_left,parry_left,\
parry_right, tick_count_attack_r,tick_count_attack_l,hit_time, \
tick_count_parry_r,tick_count_parry_l,font,wins_right,wins_left,rightImg, \
right_ai,left_ai
attack_cooldown_r = 0
attack_cooldown_l = 0
hit_left = False
hit_right = False
Game_Exit = False
xl = display_width * 0.33
xr = display_width * 0.5
xl_change = 0
xr_change = 0
touch_time = time.time()
ai_difficulty = ["off","easy","medium","hard"]
ai_toggle_R = 0
ai_toggle_L = 0
easy = []
medium = []
hard = []
while not Game_Exit:
rand_num = random.randint(0,40)
rand_attack = random.randint(0,100)
rand_num_l = random.randint(0,40)
rand_attack_l = random.randint(0,100)
duration = '%.2f' % (time.time() - touch_time)
xl_change = 0
xr_change = 0
#scoring for moving off of the strip:
if xl <= 50:
score_right += 1
xl = display_width * 0.33
xr = display_width * 0.5
if xr >= 950:
score_left += 1
xl = display_width * 0.33
xr = display_width * 0.5
#reset in event that players cross each other:
if xr - xl <= -100:
xl = display_width * 0.33
xr = display_width * .5
#events:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
#controls for the right character
if event.key == pygame.K_LEFT and attack_cooldown_r == 0:
xr_change = -25
elif event.key == pygame.K_LEFT:
#print("can't move: attack coolown is ", attack_cooldown_r)
pass
if event.key == pygame.K_RIGHT and attack_cooldown_r == 0:
xr_change = 25
if event.key == pygame.K_UP and attack_cooldown_r == 0:
attack_right = True
tick_count_attack_r += 1
attack_cooldown_r += 1
xr -= 75
if event.key == pygame.K_DOWN and tick_count_parry_r == 0:
parry_right = True
xr += 20
#controls for the left character
if event.key == pygame.K_a and attack_cooldown_l == 0:
xl_change = -25
if event.key == pygame.K_d and attack_cooldown_l == 0:
xl_change = 25
if event.key == pygame.K_w and attack_cooldown_l == 0:
attack_left = True
tick_count_attack_l += 1
attack_cooldown_l += 1
xl += 25
if event.key == pygame.K_s:
parry_left = True
xl -= 20
#reset the score
if event.key == pygame.K_o:
score_right = 0
score_left = 0
wins_right = 0
wins_left = 0
#leave the game
if event.key == pygame.K_ESCAPE:
Game_Exit = True
if event.key == pygame.K_i:
right_ai = not right_ai
if ai_toggle_R < 3:
ai_toggle_R += 1
print("right difficulty is: ", ai_difficulty[ai_toggle_R])
else:
ai_toggle_R = 0
print("right difficulty is: ", ai_difficulty[ai_toggle_R])
if event.key == pygame.K_u:
left_ai = not left_ai
if ai_toggle_L < 3:
ai_toggle_L += 1
print("left difficulty is: ", ai_difficulty[ai_toggle_L])
else:
ai_toggle_L = 0
print("left difficulty is: ", ai_difficulty[ai_toggle_L])
xr += xr_change
xl += xl_change
#ai difficulty variables
easy = [3,2,5,0]
medium = [4,3,6,1]
hard = [6,6,10,4]
left = [0,0,0,0]
right = [0,0,0,0]
#set difficulty values
if ai_toggle_R == 1:
right = easy
elif ai_toggle_R == 2:
right = medium
elif ai_toggle_R == 3:
right = hard
if ai_toggle_L == 1:
left = easy
elif ai_toggle_L == 2:
left = medium
elif ai_toggle_L == 3:
left = hard
#render the frame:
gameDisplay.fill(blue)
strip()
#AI
if ai_toggle_R > 0:
if xr-xl > 90 and right[0] > rand_num and tick_count_attack_r == 0:
xr -= 25
elif xr-xl < 190 and right[1] > rand_num and tick_count_attack_r == 0:
xr += 25
elif (xr-xl < 130 and tick_count_attack_l > 0) and (right[2] > rand_num and tick_count_parry_r == 0) and (tick_count_attack_r == 0):
#print("______parry right")
parry_right = True
xr += 20
elif xr-xl < 110 and tick_count_attack_r == 0 and rand_attack <= right[3]:
#print("_____attack right")
attack_right = True
tick_count_attack_r += 1
attack_cooldown_r += 1
xr -= 75
if ai_toggle_L > 0:
if xr-xl > 90 and left[0] > rand_num_l and tick_count_attack_l <= 0:
xl += 25
elif xr-xl < 190 and left[1] > rand_num_l and tick_count_attack_l <= 0:
xl -= 25
elif (xr-xl < 130 and tick_count_attack_r > 0) and (left[2] > rand_num_l and tick_count_parry_l == 0) and (tick_count_attack_l == 0) :
#print("parry left______")
parry_left = True
xl -= 20
elif xr-xl < 110 and tick_count_attack_l == 0 and rand_attack_l <= left[3]:
#print("attack left______")
attack_left = True
tick_count_attack_l += 1
attack_cooldown_l += 1
xl += 40
#decide which right sprite to display:
if attack_right == False and parry_right == False and tick_count_attack_r == 0 and tick_count_parry_r == 0:
gameDisplay.blit(rightImg,(xr,300))
elif attack_right == True and tick_count_attack_r == 0:
tick_count_attack_r += 1
gameDisplay.blit(rightImgattack,(xr,300))
elif 0 < tick_count_attack_r < 8:
attack_right = False
tick_count_attack_r += 1
gameDisplay.blit(rightImgattack,(xr,300))
elif tick_count_attack_r >= 8 and tick_count_attack_r < 20:
attack_right = False
tick_count_attack_r += 1
gameDisplay.blit(rightImglunging,(xr,315))
elif parry_right == True and 0 <= tick_count_parry_r < 20:
tick_count_parry_r += 1
gameDisplay.blit(rightImgparry,(xr,300))
elif parry_right == True and tick_count_parry_r >= 20:
tick_count_parry_r = 0
parry_right = False
attack_right = False
gameDisplay.blit(rightImgparry,(xr,300))
#reset the right sprite after an attack:
if tick_count_attack_r == 20:
xr += 80
tick_count_attack_r = 0
#decide which left sprite to display:
if attack_left == False and parry_left == False and tick_count_attack_l == 0:
gameDisplay.blit(leftImg,(xl,300))
elif attack_left == True and tick_count_attack_l == 0:
tick_count_attack_l += 1
gameDisplay.blit(leftImgattack,(xl,300))
elif 0 < tick_count_attack_l < 8:
attack_left = False
tick_count_attack_l += 1
gameDisplay.blit(leftImgattack,(xl,300))
elif tick_count_attack_l >= 8 and tick_count_attack_l < 20:
attack_left = False
tick_count_attack_l += 1
gameDisplay.blit(leftImglunging,(xl,315))
elif parry_left == True and 0 <= tick_count_parry_l < 20:
tick_count_parry_l += 1
gameDisplay.blit(leftImgparry,(xl,300))
elif tick_count_parry_l >= 20:
tick_count_parry_l = 0
parry_left = False
attack_left = False
gameDisplay.blit(leftImgparry,(xl,300))
#reset the left sprite after an attack:
if tick_count_attack_l == 20:
xl -= 30
tick_count_attack_l = 0
#attack cooldowns:
if 1 <= attack_cooldown_l < 30:
attack_cooldown_l += 1
elif attack_cooldown_l == 30:
attack_cooldown_l = 0
if 1 <= attack_cooldown_r < 30:
attack_cooldown_r += 1
elif attack_cooldown_r == 30:
attack_cooldown_r = 0
#hit detection:
#reset after hit:
if hit_right == True and hit_time == 30:
xl = display_width * 0.33
xr = display_width * 0.5
hit_time = 0
if hit_left == True and hit_time == 30:
xl = display_width * 0.33
xr = display_width * 0.5
hit_time = 0
#detect hit:
if tick_count_attack_l == 19 and (xr-xl) <= 85 and parry_right == False:
print("!!_touch left_!!\n")
score_left += 1
xl = display_width * 0.35
xr = display_width * 0.5
hit_time += 1
touch_time = time.time()
if tick_count_attack_r == 19 and (xr-xl) <= 45 and parry_left == False:
print("!!_touch right_!!\n")
score_right += 1
xl = display_width * 0.33
xr = display_width * 0.43
hit_right == True
hit_time += 1
touch_time = time.time()
#disable hits after a score:
if 30 > hit_time >= 1:
hit_time += 1
#convert touches to wins:
if score_left == 5:
wins_left += 1
score_left = 0
score_right = 0
if score_right == 5:
wins_right += 1
score_right = 0
score_left = 0
clock.tick(35)
show_score = font.render((str(score_left)+ " : "+ str(score_right)), False, (0,0,0))
gameDisplay.blit(show_score, (490, 100))
show_score = small_font.render((str(wins_left)), False, (0,0,0))
gameDisplay.blit(show_score, (508, 50))
show_score = small_font.render((str(wins_right)), False, (0,0,0))
gameDisplay.blit(show_score, (652, 50))
show_score = small_font.render((str(duration)), False, (0,0,0))
gameDisplay.blit(show_score, (555, 7))
show_score = smaller_font.render((str("Right AI difficulty: "+ai_difficulty[ai_toggle_R])), False, (0,0,0))
gameDisplay.blit(show_score, (900, 65))
show_score = smaller_font.render((str("Left AI difficulty: "+ai_difficulty[ai_toggle_L])), False, (0,0,0))
gameDisplay.blit(show_score, (900, 20))
pygame.display.update()
game_loop()
pygame.quit()
quit()