-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
745 lines (567 loc) · 26.9 KB
/
game.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
import pygame
from network import Network
import math
import datetime
import random
import os
import ast
from delayqueue import DelayQueue
import time
import json
##################################
# General Helper functions (START)
##################################
def load_image_convert_alpha(filename):
"""Load an image with the given filename from the images directory"""
return pygame.image.load(os.path.join('images', filename)).convert_alpha()
def draw_centered(surface1, surface2, position):
"""Draw surface1 onto surface2 with center at position"""
rect = surface1.get_rect()
rect = rect.move(position[0] - rect.width // 2, position[1] - rect.height // 2)
surface2.blit(surface1, rect)
def rotate_center(image, rect, angle):
"""rotate the given image around its center & return an image & rect"""
rotate_image = pygame.transform.rotate(image, angle)
rotate_rect = rotate_image.get_rect(center=rect.center)
return rotate_image, rotate_rect
def distance(p, q):
"""Helper function to calculate distance between 2 points"""
return math.sqrt((p[0] - q[0]) ** 2 + (p[1] - q[1]) ** 2)
################################
# General Helper functions (END)
################################
class GameObject(object):
"""All game objects have a position and an image"""
def __init__(self, position, image, speed=0):
# max speed should be 6.5
self.image = image
self.position = list(position[:])
self.speed = speed
def draw_on(self, screen):
draw_centered(self.image, screen, self.position)
def size(self):
return max(self.image.get_height(), self.image.get_width())
def radius(self):
return self.image.get_width() / 2
class Game:
# defining and initializing game states
PLAYING, DYING, GAME_OVER, STARTING, WELCOME = range(5)
# defining custom events
REFRESH, START, RESTART = range(pygame.USEREVENT, pygame.USEREVENT + 3)
def __init__(self, w, h):
self.net = Network()
# self.width = w
# self.height = h
# self.player = Player(50, 50)
# self.player2 = Player(100,100)
# self.canvas = Canvas(self.width, self.height, "Testing...")
self.statusLabel = "connecting"
self.playersLabel = "0 players"
self.frame = 0
self.down = False
self.queue = DelayQueue()
"""Initialize a new game"""
pygame.mixer.init()
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
# set up a 800 x 600 window
self.width = 800
self.height = 600
self.screen = pygame.display.set_mode((self.width, self.height))
# use a black background
self.bg_color = 0, 0, 0
# get the default system font (with different sizes of 100, 50, 25)
self.big_font = pygame.font.SysFont(None, 100)
self.medium_font = pygame.font.SysFont(None, 50)
self.small_font = pygame.font.SysFont(None, 25)
# and make the game over text using the big font just loaded
self.gameover_text = self.big_font.render('GAME OVER', \
True, (255, 0, 0))
# load a spaceship image (only used to display number of lives)
self.lives_image = load_image_convert_alpha('spaceship-off.png')
# Setup a timer to refresh the display FPS times per second
self.FPS = 30
pygame.time.set_timer(self.REFRESH, 1000 // self.FPS)
# a dictionary of death distances of different rock sizes
self.death_distances = {"big": 90, "normal": 65, "small": 40}
# display the welcome screen
#self.do_welcome()
# # Go straight into game
# self.do_init()
# used to monitor missile firing time
# to prevent firing too many missiles in a short time
self.fire_time = datetime.datetime.now()
self.spaceship = Spaceship((self.width // 2, self.height // 2))
self.spaceship2 = Spaceship((self.width // 1, self.height // 4))
# Go straight into game
self.do_init()
def do_init(self):
"""This function is called in the beginning or when
the game is restarted."""
# holds the rocks
self.rocks = []
# minimum distance from spaceship when making rocks
# this changes based on difficulty as the time passes
self.min_rock_distance = 350
# starting the game
self.start()
# create 4 big rocks
for i in range(4):
self.make_rock()
# initialize the number of lives and the score
self.lives = 3
self.score = 0
# counter used to help count seconds
self.counter = 0
def make_rock(self, size="big", pos=None):
"""Make a new rock"""
# minimum margin when creating rocks
margin = 200
if pos == None:
# no position was passed. make at a random location
rand_x = random.randint(margin, self.width - margin)
rand_y = random.randint(margin, self.height - margin)
# while the co-ordinate is too close, discard it
# and generate another one
while distance((rand_x, rand_y), self.spaceship.position) < \
self.min_rock_distance:
# choose another random co-ordinate
rand_x = random.randint(0, self.width)
rand_y = random.randint(0, self.height)
temp_rock = Rock((rand_x, rand_y), size)
else:
# a position was given through arguments
temp_rock = Rock(pos, size)
# add the recently created rock the the actual rocks list
self.rocks.append(temp_rock)
def start(self):
"""Start the game by creating the spaceship object"""
# self.spaceship = Spaceship((self.width // 2, self.height // 2))
# self.spaceship2 = Spaceship((self.width // 1, self.height // 4))
self.missiles = []
# set the state to PLAYING
self.state = Game.PLAYING
def run(self):
"""Loop forever processing events"""
running = True
while running:
event = pygame.event.wait()
# player is asking to quit
if event.type == pygame.QUIT:
running = False
if event.type == pygame.K_ESCAPE:
running = False
# time to draw a new frame
elif event.type == Game.REFRESH:
if self.state != Game.WELCOME:
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
new_time = datetime.datetime.now()
if new_time - self.fire_time > \
datetime.timedelta(seconds=0.15):
# there should be a minimum of 0.15 delay between
# firing each missile
# fire a missile
self.spaceship.fire()
# record the current fire time
self.fire_time = new_time
if self.state == Game.PLAYING:
# if the game is going on
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
# when pressing "d" or "right arrow" rotate
# the spaceship clockwise by 10 degrees
self.spaceship.angle -= 10
self.spaceship.angle %= 360
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
# when pressing "d" or "right arrow" rotate
# the spaceship counter clockwise by 10 degrees
self.spaceship.angle += 10
self.spaceship.angle %= 360
if keys[pygame.K_UP] or keys[pygame.K_w]:
# if "w" or "up arrow" is pressed,
# we should accelerate
self.spaceship.is_throttle_on = True
# increase the speed
if self.spaceship.speed < 20:
self.spaceship.speed += 1
self.down = False
else:
# if the throttle key ("d" or "up")
# is not pressed, slow down
if self.spaceship.speed > 0:
self.spaceship.speed -= 1
self.spaceship.is_throttle_on = False
# IJ Start: Send Network Stuff ####################
parsed_values = self.parse_data(self, data=self.send_data())
self.update_object_positions(parsed_values)
# IJ End: Send Network Stuff ####################
# if there are any rocks, do their physics
if len(self.rocks) > 0:
self.rocks_physics()
# do the spaceship physics
self.physics()
# if there are any missiles on the screen, process them
if len(self.spaceship.active_missiles) > 0:
self.missiles_physics()
####################
self.draw()
# resume after losing a life
elif event.type == Game.START:
pygame.time.set_timer(Game.START, 0) # turn the timer off
if self.lives < 1:
self.game_over()
else:
self.rocks = []
# make 4 rocks
for i in range(4):
self.make_rock()
# start again
self.start()
# switch from game over screen to new game
elif event.type == Game.RESTART:
pygame.time.set_timer(Game.RESTART, 0) # turn the timer off
self.state = Game.STARTING
# user is clicking to start a new game
elif event.type == pygame.MOUSEBUTTONDOWN \
and (self.state ==Game.STARTING or \
self.state == Game.WELCOME):
self.do_init()
# user is pressing enter to start a new game
elif event.type == pygame.KEYDOWN \
and event.key == pygame.K_RETURN and \
(self.state == Game.STARTING or \
self.state == Game.WELCOME):
self.do_init()
else:
pass # an event type we don't handle
def update_object_positions(self, parsed_values):
"""
Update the spaceship, Missile and Rock positions
:return: none
"""
self.spaceship2.position = parsed_values['position']
self.spaceship2.angle = parsed_values['angle']
for r in range(len(self.rocks)):
positionjson = "rocksposition_" + str(r)
speedjson = "rockspeed_" + str(r)
sizejson = "rocksize_" + str(r)
directionjson = "rockdirection_" + str(r)
try:
self.rocks[r].position = parsed_values[positionjson]
self.rocks[r].speed = parsed_values[speedjson]
self.rocks[r].size = parsed_values[sizejson]
self.rocks[r].direction = parsed_values[directionjson]
except KeyError:
# Rock r doesn't exist yet
pass
for r in range(len(self.spaceship.active_missiles)):
missileposition = "missileposition_" + str(r)
missilespeed = "missilespeed_" + str(r)
missiledirection = "missiledirection_" + str(r)
try:
self.spaceship.active_missiles[r].position = parsed_values[missileposition]
self.spaceship.active_missiles[r].speed = parsed_values[missilespeed]
self.spaceship.active_missiles[r].direction = parsed_values[missiledirection]
except KeyError:
# Missile r doesn't exist yet
pass
def send_data(self):
"""
Send my current position to server
:return: byte encoded json
"""
rockdict = {}
missiledict = {}
for r in range(len(self.rocks)):
positionjson = "rocksposition_" + str(r)
speedjson = "rockspeed_" + str(r)
sizejson = "rocksize_" + str(r)
directionjson = "rockdirection_" + str(r)
rockjson = {positionjson: self.rocks[r].position, speedjson:self.rocks[r].speed, sizejson:self.rocks[r].size,
directionjson:self.rocks[r].direction}
rockdict = {**rockjson, **rockdict}
jsonmessage = {"id": str(self.net.id), "position": self.spaceship.position, "angle": self.spaceship.angle}
for r in range(len(self.spaceship.active_missiles)):
missileposition = "missileposition_" + str(r)
missilespeed = "missilespeed_" + str(r)
missiledirection = "missiledirection_" + str(r)
missilejson = {missileposition: self.spaceship.active_missiles[r].position, missilespeed:self.spaceship.active_missiles[r].speed,
missiledirection: self.spaceship.active_missiles[r].direction}
missiledict = {**missilejson, **missiledict}
# Merge the three python dictionaries
z = {**jsonmessage, **rockdict, **missiledict}
print('sending',z)
data = json.dumps(z)
# bucket synchronization
self.queue.put(data, delay=30)
try:
data = self.queue.get()
except:
print('empty')
# no output: queue was not empty, so not executed
reply = self.net.send(data.encode())
return reply
@staticmethod
def parse_data(self, data):
"""
Get data from server. player 2 position
:return: dictionary
"""
try:
data_arr = json.loads(data.decode())
listeralvalue=u'{}'.format(data_arr)
data_arr= ast.literal_eval(listeralvalue)
# If id = other player than don't update include their rock positions, but do include ship and bullet positions.
if data_arr['id'] == 0 or data_arr['id'] == b'0' or data_arr['id'] == "b'0'":
data_arr_filtered = {'position': data_arr['position'], 'angle': data_arr['angle'],
'missileposition_': data_arr['missileposition_'], 'missilespeed_': data_arr['missilespeed_'],
'missiledirection_': data_arr['missiledirection_']}
print('receiving1', data_arr_filtered)
return data_arr_filtered
else:
print('receiving2', data_arr)
return data_arr
except:
data_arr_filtered = {'position': data_arr['position'], 'angle': data_arr['angle']}
self.spaceship2.position = data_arr['position']
return data_arr_filtered
def rocks_physics(self):
"""Move the rocks if there are any"""
# if there are any rocks
if len(self.rocks) > 0:
for rock in self.rocks:
# move the rock
rock.move()
# if the rock hits the spaceship, die once
if distance(rock.position, self.spaceship.position) < \
self.death_distances[rock.size]:
self.die()
# if the rock goes out of screen and there are less than
# 10 rocks on the screen, create a new rock with the same size
elif distance(rock.position, (self.width / 2, self.height / 2)) > \
math.sqrt((self.width / 2) ** 2 + (self.height / 2) ** 2):
self.rocks.remove(rock)
if len(self.rocks) < 10:
self.make_rock(rock.size)
def missiles_physics(self):
"""Do all the physics of missiles"""
# if there are any active missiles
if len(self.spaceship.active_missiles) > 0:
for missile in self.spaceship.active_missiles:
# move the missile
missile.move()
# check the collision with each rock
for rock in self.rocks:
if rock.size == "big":
# if the missile hits a big rock, destroy it,
# make two medium sized rocks and give 20 scores
if distance(missile.position, rock.position) < 80:
self.rocks.remove(rock)
if missile in self.spaceship.active_missiles:
self.spaceship.active_missiles.remove(missile)
self.make_rock("normal", \
(rock.position[0] + 10, rock.position[1]))
self.make_rock("normal", \
(rock.position[0] - 10, rock.position[1]))
self.score += 20
elif rock.size == "normal":
# if the missile hits a medium sized rock, destroy it,
# make two small sized rocks and give 50 scores
if distance(missile.position, rock.position) < 55:
self.rocks.remove(rock)
if missile in self.spaceship.active_missiles:
self.spaceship.active_missiles.remove(missile)
self.make_rock("small", \
(rock.position[0] + 10, rock.position[1]))
self.make_rock("small", \
(rock.position[0] - 10, rock.position[1]))
self.score += 50
else:
# if the missile hits a small rock, destroy it,
# make one big rock if there are less than 10 rocks
# on the screen, and give 100 scores
if distance(missile.position, rock.position) < 30:
self.rocks.remove(rock)
if missile in self.spaceship.active_missiles:
self.spaceship.active_missiles.remove(missile)
if len(self.rocks) < 10:
self.make_rock()
self.score += 100
def physics(self):
"""Do spaceship physics here"""
if self.state == Game.PLAYING:
# call the move function of the object
self.spaceship.move()
"""Note that this is a good place to make the spaceship
bounce for example, when it hits the walls (sides of screen)
or make it not move out of screen when it reaches the borders.
Due to lack of time, I can't implement any of them, but they are
not hard to do at all."""
def game_over(self):
"""Losing a life"""
self.state = Game.GAME_OVER
pygame.time.set_timer(Game.RESTART, 0)
def die(self):
"""Losing a life"""
self.lives -= 1
self.counter = 0
self.state = Game.DYING
pygame.time.set_timer(Game.START, 0)
def draw(self):
"""Update the display"""
# everything we draw now is to a buffer that is not displayed
self.screen.fill(self.bg_color)
# if we are not on the welcome screen
if self.state != Game.WELCOME:
# draw the spaceship
self.spaceship.draw_on(self.screen)
# draw the player 2 spaceship
self.spaceship2.draw_on(self.screen)
# if there are any active missiles draw them
if len(self.spaceship.active_missiles) > 0:
for missile in self.spaceship.active_missiles:
missile.draw_on(self.screen)
# draw the rocks
if len(self.rocks) > 0:
for rock in self.rocks:
rock.draw_on(self.screen)
# if we are in game play mode
if self.state == Game.PLAYING:
# increment the counter by 1
self.counter += 1
if self.counter == 20 * self.FPS:
# time to increase difficulty (20 secs without dying)
if len(self.rocks) < 15: # keeping it sane
# add a new rock
self.make_rock()
# decrease the minimum rock creation distance
if self.min_rock_distance < 200:
self.min_rock_distance -= 50
# set the counter back to zero
self.counter = 0
# create and display the text for score
scores_text = self.medium_font.render(str(self.score), \
True, (0, 155, 0))
draw_centered(scores_text, self.screen, \
(self.width - scores_text.get_width(), scores_text.get_height() + \
10))
# if the game is over, display the game over text
if self.state == Game.GAME_OVER or self.state == Game.STARTING:
draw_centered(self.gameover_text, self.screen, \
(self.width // 2, self.height // 2))
# draw lives
for i in range(self.lives):
draw_centered(self.lives_image, self.screen, \
(self.lives_image.get_width() * i * 1.2 + 40, \
self.lives_image.get_height() // 2))
else:
# draw the welcome texts
draw_centered(self.welcome_asteroids, self.screen, \
(self.width // 2, self.height // 2 \
- self.welcome_asteroids.get_height()))
draw_centered(self.welcome_desc, self.screen, \
(self.width // 2, self.height // 2 \
+ self.welcome_desc.get_height()))
# flip buffers so that everything we have drawn gets displayed
pygame.display.flip()
class Spaceship(GameObject):
def __init__(self, position):
"""initializing an Spaceship object given it's position"""
super(Spaceship, self).__init__(position, \
load_image_convert_alpha('spaceship-off.png'))
self.image_on = load_image_convert_alpha('spaceship-on.png')
self.direction = [0, -1]
self.is_throttle_on = False
self.angle = 0
# a list to hold the missiles fired by the spaceship
# (that are active (on the screen))
self.active_missiles = []
def draw_on(self, screen):
"""Draw the spaceship on the screen"""
# select the image, based on the fact that spaceship is accelerating
# or not
if self.is_throttle_on:
new_image, rect = rotate_center(self.image_on, \
self.image_on.get_rect(), self.angle)
else:
new_image, rect = rotate_center(self.image, \
self.image.get_rect(), self.angle)
draw_centered(new_image, screen, self.position)
def move(self):
"""Do one frame's worth of updating for the object"""
# calculate the direction from the angle variable
self.direction[0] = math.sin(-math.radians(self.angle))
self.direction[1] = -math.cos(math.radians(self.angle))
# calculate the position from the direction and speed
self.position[0] += self.direction[0] * self.speed
self.position[1] += self.direction[1] * self.speed
def fire(self):
"""create a new Missile and fire it!!"""
# adjust the firing position of the missile based on the
# angle of the spaceship.
# adjust[] is used to help hold the position of the point
# from where the missile should be fired. In other words,
# for example missiles should be fired from the bottom of
# the spaceship if it's facing down.
adjust = [0, 0]
adjust[0] = math.sin(-math.radians(self.angle)) * self.image.get_width()
adjust[1] = -math.cos(math.radians(self.angle)) * self.image.get_height()
# create a new missile using the calculated adjusted position
new_missile = Missile((self.position[0] + adjust[0], \
self.position[1] + adjust[1] / 2), \
self.angle)
self.active_missiles.append(new_missile)
class Missile(GameObject):
"""Resembles a missile"""
def __init__(self, position, angle, speed=15):
super(Missile, self).__init__(position, \
load_image_convert_alpha('missile.png'))
self.angle = angle
self.direction = [0, 0]
self.speed = speed
def move(self):
"""Move the missile towards its destination"""
# calculate the direction from the angle variable
self.direction[0] = math.sin(-math.radians(self.angle))
self.direction[1] = -math.cos(math.radians(self.angle))
# calculate the position from the direction and speed
self.position[0] += self.direction[0] * self.speed
self.position[1] += self.direction[1] * self.speed
class Rock(GameObject):
"""Resembles a rock"""
def __init__(self, position, size, speed=4):
"""Initialize a Rock object, given its position and size"""
# if the size is valid
if size in {"big", "normal", "small"}:
# load the correct image from file
str_filename = "rock-" + str(size) + ".png"
super(Rock, self).__init__(position, \
load_image_convert_alpha(str_filename))
self.size = size
else:
# the size is not pre-defined
return None
self.position = list(position)
self.speed = speed
# create a random movement direction vector
if bool(random.getrandbits(1)):
rand_x = random.random() * -1
else:
rand_x = random.random()
if bool(random.getrandbits(1)):
rand_y = random.random() * -1
else:
rand_y = random.random()
self.direction = [rand_x, rand_y]
def move(self):
"""Move the rock"""
self.position[0] += self.direction[0] * self.speed
self.position[1] += self.direction[1] * self.speed
def reprJSON(self):
return dict(position=self.position, direction=self.direction, size=self.size)
class ComplexEncoder(json.JSONEncoder):
def default(self, obj):
if hasattr(obj, 'reprJSON'):
return obj.reprJSON()
else:
return json.JSONEncoder.default(self, obj)