-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
127 lines (95 loc) · 3.08 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
import pygame
import random
import sys
pygame.init()
Width = 800
Height = 600
Red = (255,0,0)
Blue = (0,0,255)
s_color = (255,85,0)
Background_Color = (11,235,205)
player_size = 50
player_pos = [Width/2, Height -1.5*player_size]
enemy_size = 50
enemy_pos = [random.randint(0,Width-player_size), 0]
enemy_list = [enemy_pos]
screen = pygame.display.set_mode((Width, Height))
game_over = False
clock = pygame.time.Clock()
speed = 10
myFont = pygame.font.SysFont("monospace", 35)
score = 0
def drop_enemies(enemy_list):
delay = random.random()
if len(enemy_list) < 10 and delay< 0.15 :
x_p = random.randint(0, Width-enemy_size)
y_p = 0
enemy_list.append([x_p, y_p])
def draw_enemies(enemy_list):
for enemy_pos in enemy_list:
pygame.draw.rect(screen, Blue, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size))
def update_enemy_position(enemy_list, score):
for idx, enemy_pos in enumerate(enemy_list):
if enemy_pos[1] >= 0 and enemy_pos[1] < Height:
enemy_pos[1] += speed
else:
enemy_list.pop(idx)
score += 1
return score
def collision_check(enemy_list, player_pos):
for enemy_pos in enemy_list:
if detect_collision(player_pos, enemy_pos):
return True
return False
def detect_collision(player_pos, enemy_pos):
p_x = player_pos[0]
p_y = player_pos[1]
e_x = enemy_pos[0]
e_y = enemy_pos[1]
if (p_x <= e_x and e_x < (p_x+player_size)) or (e_x <= p_x and p_x < (e_x + enemy_size) ):
if (e_y >= p_y and e_y < (p_y+player_size) ) or ( e_y<= p_y and p_y < (e_y + enemy_size) ):
return True
return False
def set_level(score,speed):
if score < 10:
speed = 5
elif score < 20:
speed = 8
elif score < 40:
speed = 13;
elif score <= 60:
speed = 20
else :
speed = 27
# speed = score/5 +1
return speed
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_pos[0]-= player_size
elif event.key == pygame.K_RIGHT:
player_pos[0]+= player_size
screen.fill(Background_Color)
# updating position of single enemy
# if enemy_pos[1] >= 0 and enemy_pos[1] < Height:
# enemy_pos[1] += speed
# else:
# enemy_pos[0] = random.randint(0, Width-enemy_size)
# enemy_pos[1] = 0
drop_enemies(enemy_list)
score = update_enemy_position(enemy_list, score)
speed = set_level(score, speed)
text = "Score: " + str(score)
label = myFont.render(text, 1, s_color )
screen.blit(label, (Width-200, Height-40))
if collision_check(enemy_list, player_pos) :
game_over = True
print('Final Score:',score)
break
draw_enemies(enemy_list)
pygame.draw.rect(screen, Red, (player_pos[0], player_pos[1], player_size, player_size))
clock.tick(30)
pygame.display.update()