-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
276 lines (207 loc) · 8.89 KB
/
Copy pathgame.py
File metadata and controls
276 lines (207 loc) · 8.89 KB
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
import pygame
import os
from random import*
from main import*
pygame.font.init()
pygame.mixer.init()
WIDTH, HEIGHT = 1200, 700
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Odlot!")
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)
BULLET_HIT_SOUND = pygame.mixer.Sound('Assets/Grenade+1.mp3')
BULLET_FIRE_SOUND = pygame.mixer.Sound('Assets/Gun+Silencer.mp3')
HEALTH_FONT = pygame.font.SysFont('comicsans', 25)
WINNER_FONT = pygame.font.SysFont('comicsans', 100)
#red
red_speed = 5
red_speed_shoot = 4
FPS = 60
VEL = 5
BULLET_VEL = 7
MAX_BULLETS = 3
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 40
YELLOW_HIT = pygame.USEREVENT + 1
YELLOW_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_yellow.png'))
YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)
czerowne_efekty = [
pygame.transform.rotate(pygame.transform.scale(
pygame.image.load(os.path.join('Assets', 'spaceship_red.png')), (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270),
pygame.transform.rotate(pygame.transform.scale(
pygame.image.load(os.path.join('Assets', 'wybuch.jpg')), (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)
]
SPACE = pygame.transform.scale(pygame.image.load(
os.path.join('Assets', 'space.png')), (WIDTH, HEIGHT))
def draw_window(red_ships_fleat ,yellow, red_bullets, yellow_bullets, yellow_health, POINTS ,elapsed_time,GAME_OVER):
WIN.blit(SPACE, (0, 0))
pygame.draw.rect(WIN, BLACK, BORDER)
health_text = ""
for red in red_ships_fleat:
health_text = health_text + str(red.health) + " "
red_health_text = HEALTH_FONT.render(health_text, 1, WHITE)
yellow_health_text = HEALTH_FONT.render(
"Health: " + str(yellow_health), 1, WHITE)
time_text = HEALTH_FONT.render(
"Time: "+str(elapsed_time), 1, WHITE)
points_text = HEALTH_FONT.render("Points: " + str(POINTS), 1, WHITE)
WIN.blit(time_text, ((WIDTH - time_text.get_width() - 120)/2-points_text.get_width(), 10))
WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))
WIN.blit(yellow_health_text, (10, 10))
WIN.blit(points_text, (WIDTH / 2 - points_text.get_width() / 2, 10))
WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
for red in red_ships_fleat:
if(red.explosion ==0):
WIN.blit(czerowne_efekty[0], (red.rect.x, red.rect.y))
if(red.explosion == 1):
WIN.blit(czerowne_efekty[1], (red.rect.x, red.rect.y))
for bullet in red_bullets:
pygame.draw.rect(WIN, RED, bullet)
for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)
if(GAME_OVER==1):
draw_text = WINNER_FONT.render("YOUR POINTS: "+str(POINTS), 1, WHITE)
WIN.blit(draw_text, (WIDTH / 2 - draw_text.get_width() /
2, HEIGHT / 2 - draw_text.get_height() / 2))
pygame.display.update()
if(GAME_OVER==1):
pygame.time.delay(15000)
def yellow_handle_movement(keys_pressed, yellow):
if keys_pressed[pygame.K_a] and yellow.x - VEL > 0: # LEFT
yellow.x -= VEL
if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x: # RIGHT
yellow.x += VEL
if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: # UP
yellow.y -= VEL
if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.height < HEIGHT - 15: # DOWN
yellow.y += VEL
def red_handle_movement(red_ship_fleet):
for red in red_ship_fleet:
keys_pressed = randint(0, 10) # losowe poruszanie sie statku
if keys_pressed == 1 and red.rect.x - red.speed > BORDER.x + BORDER.width: # LEFT
red.rect.x -= red.speed
if keys_pressed == 2 and red.rect.x + red.speed + red.rect.width < WIDTH: # RIGHT
red.rect.x += red.speed
if keys_pressed == 3 and red.rect.y - red.speed > 0: # UP
red.rect.y -= red.speed
if keys_pressed == 4 and red.rect.y + red.speed + red.rect.height < HEIGHT - 15: # DOWN
red.rect.y += red.speed
def handle_bullets(yellow_bullets, red_bullets, yellow,red_ship_fleet,RED_HITS):
for bullet in yellow_bullets:
bullet.x += BULLET_VEL
i=0
for red in red_ship_fleet:
if red.rect.colliderect(bullet):
pygame.event.post(pygame.event.Event(RED_HITS[i]))
try:
yellow_bullets.remove(bullet)
except:
pass
elif bullet.x > WIDTH:
try:
yellow_bullets.remove(bullet)
except:
pass
i += 1
for bullet in red_bullets:
bullet.x -= BULLET_VEL
if yellow.colliderect(bullet):
pygame.event.post(pygame.event.Event(YELLOW_HIT))
red_bullets.remove(bullet)
elif bullet.x < 0:
red_bullets.remove(bullet)
def if_game_quit(event, run):
if event.type == pygame.QUIT:
pygame.quit()
run = False
return run
def shoot_bullet(ship, bullets, max_bullets, bullet_sound,yellow_bullets):
if len(yellow_bullets) < max_bullets:
bullet = pygame.Rect(
ship.x + ship.width, ship.y + ship.height//2 - 2, 10, 5)
bullets.append(bullet)
bullet_sound.play()
def shot_red_bullet(red_ship_fleet, red_bullets, MAX_BULLETS, BULLET_FIRE_SOUND):
for Red in red_ship_fleet:
losowa_kula = randint(Red.red_speed_shoot, 18)
if (losowa_kula == 18 or losowa_kula == 17) and Red.explosion==0:
bullet = pygame.Rect(
Red.rect.x, Red.rect.y + Red.rect.height // 2 - 2, 10, 5)
red_bullets.append(bullet)
BULLET_FIRE_SOUND.play()
def red_speed_accereration(elapsed_time,red_ships_fleet):
if(elapsed_time>60 * 2):
red_ships_fleet[0].speed=20
red_ships_fleet[1].speed=6
red_ships_fleet[2].speed=6
return
elif (elapsed_time > 60 * 4):
red_ships_fleet[0].speed = 10
red_ships_fleet[1].speed = 7
red_ships_fleet[2].speed = 7
return
elif (elapsed_time > 60 * 6):
red_ships_fleet[0].speed = 12
red_ships_fleet[1].speed = 7
red_ships_fleet[2].speed = 7
return
def red_bullet_accereration(elapsed_time,red_ships_fleet):
if (elapsed_time > 60 * 2):
red_ships_fleet[0].red_speed_shoot = 6
red_ships_fleet[1].red_speed_shoot = 9
red_ships_fleet[2].red_speed_shoot = 6
return
elif (elapsed_time > 60 * 4):
red_ships_fleet[0].red_speed_shoot = 7
red_ships_fleet[1].red_speed_shoot = 10
red_ships_fleet[2].red_speed_shoot = 7
return
elif (elapsed_time > 60 * 6):
red_ships_fleet[0].red_speed_shoot = 7
red_ships_fleet[1].red_speed_shoot = 12
red_ships_fleet[2].red_speed_shoot = 7
return
def move_red(red):
position_of_comming = randint(1, 5)
if position_of_comming == 1:#gorny rog
red.rect.x = WIDTH - 10 - SPACESHIP_WIDTH / 2
red.rect.y = 10
elif position_of_comming==2:#dolny rog
red.rect.x = WIDTH - 10 - SPACESHIP_WIDTH / 2
red.rect.y = HEIGHT - 10
elif position_of_comming==3:#srodek
red.rect.x = (WIDTH + 10)/1.5 + SPACESHIP_WIDTH / 2
red.rect.y = (HEIGHT + 10)/2
elif position_of_comming==4:#gorny przedni rog
red.rect.x = (WIDTH + 10)/2 + SPACESHIP_WIDTH
red.rect.y = 10
elif position_of_comming==5:#dolny przedni rog
red.rect.x = (WIDTH + 10)/2 + SPACESHIP_WIDTH
red.rect.y = HEIGHT - 10
def handel_death_red(red_ships_fleat,POINTS):
for red in red_ships_fleat:
if red.health <= 0:
POINTS += 1
red.health = 10
return 1
return 0
def handel_explosions(red_ships_fleat,elapsed_time,explosion_start_time):
for red in red_ships_fleat:
if elapsed_time > explosion_start_time + 1 and red.explosion==1:
red.explosion = 0
move_red(red)
def make_more_enemies(elapsed_time,red_ships_fleet,unused_ships):
for i in range(1,5):
if (elapsed_time > i * 10) and (len(red_ships_fleet) == 2+i): # po 20 sekundach pojawia sie nowy statek
red_ships_fleet.append(unused_ships[i-1])
class Red:
def __init__(self,speed,red_speed_shoot, health=10 , x=700, y=300, width=SPACESHIP_WIDTH, height=SPACESHIP_HEIGHT,explosion = 0):
self.rect = pygame.Rect(x, y, width, height)
self.health = health
self.speed = speed
self.red_speed_shoot = red_speed_shoot
self.explosion = explosion