-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdie_piano.py
336 lines (254 loc) · 11.2 KB
/
die_piano.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
import pygame
import sys
from pygame.sprite import Sprite
from pygame.sprite import Group
from random import *
patrons = 200
def chek_fleet_duraction(ai_settings,aliens):
for alien in aliens.sprites():
alien.rect.y += ai_settings.fleet_drop_speed
ai_settings.fleet_direction *= -1
ai_settings.alien_speed_factor += 0.01
def chek_fleet_edges(ai_settings,aliens):
for alien in aliens.sprites():
if alien.chek_edges():
chek_fleet_duraction(ai_settings,aliens)
break
bullets_die = True
def get_number_rows(ai_settings,ship_height,alien_height):
available_spase_y = ((ai_settings.screen_height - 4*alien_height) - ship_height)
number_rows = int(available_spase_y / (3*alien_height))
return number_rows
def create_alien(ai_settings,screen,aliens,alien_number,row_number):
alien = Alien(ai_settings,screen)
alien_width = alien.rect.width
available_spase_x = ai_settings.screen_width - 2*alien_width
number_aliens_x = int(available_spase_x / (2*alien_width))
alien.x = randint(0,300)
alien.rect.x = alien.x
# alien_width = alien.rect.width
alien.y = randint(0,300)
alien.rect.y = alien.y
aliens.add(alien)
print(alien.x,alien.y)
def create_piano(ai_settings,ships,screen):
for x in range(20):
ship = Ship(ai_settings,screen)
ship.rect.x = x*100
ships.add(ship)
class Settings():
def __init__(self):
self.fleet_diraction = 1
self.fleet_drop_speed = 10
self.fleet_direction = 1
self.screen_width = 300
self.bullet_speed_factor = 3
self.screen_height = 300
self.bg_color = (20,250,200)
self.ship_speed_factor = 1
self.bullet_color = 60,60,60
self.alien_speed_factor = 1
self.bullet_width = 644
self.bullet_height = 4
self.bullet_allowed = 133
class Bullet(Sprite):
def __init__(self,ai_settings,screen,ship,mouse_x,mouse_y):
super(Bullet, self).__init__()
self.screen = screen
self.rect = pygame.Rect(mouse_x,mouse_y,ai_settings.bullet_width,ai_settings.bullet_height)
self.rect.centerx = ship.rect.centerx
self.rect.top = mouse_y
self.y = float(self.rect.y)
self.color = ai_settings.bullet_color
self.speed_factor = ai_settings.bullet_speed_factor
def update(self):
self.y -= self.speed_factor
self.rect.y = self.y
def draw_bullet(self):
pygame.draw.rect(self.screen,self.color,self.rect)
class Ship(Sprite):
def __init__(self,ai_settings,screen):
super(Ship, self).__init__()
self.screen = screen
self.color = (0,0,0)
#self.image = pygame.Rect(mouse_x,mouse_y,ai_settings.bullet_width,ai_settings.bullet_height)
self.rect = pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
self.movin_right = False
self.movin_left = False
self.ai_settings = ai_settings
self.alien_speed_factor = 1
self.screen_rect = screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
self.senter = float(self.rect.centerx)
def update(self):
pass
def blitme(self):
pygame.draw.rect(self.screen,self.color,self.rect)
class Alien(Sprite):
def __init__(self,ai_settings,screen):
super(Alien, self).__init__()
self.color = (0,0,0)
self.ai_settings = ai_settings
self.screen = screen
self.image = self.rect = pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
self.rect = self.rect = pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
self.rect.x = self.rect.width
self.rect.y = self.rect.height
self.x = float(self.rect.x)
self.y = float(self.rect.y)
def blitme(self):
pygame.draw.rect(self.screen,self.color,self.rect)
def chek_edges(self):
screen_rect = self.screen.get_rect()
if self.rect.right >= screen_rect.right:
return True
if self.rect.left <= 0:
return True
def update(self):
self.x +=0
class Blocks(Sprite):
def __init__(self,ai_settings,screen):
super(Alien, self).__init__()
self.ai_settings = ai_settings
self.screen = screen
self.image = pygame.Rect(mouse_x,mouse_y,ai_settings.bullet_width,ai_settings.bullet_height)
self.rect = pygame.Rect(mouse_x,mouse_y,ai_settings.bullet_width,ai_settings.bullet_height)
self.rect.x = self.rect.width
self.rect.y = self.rect.height
self.x = float(self.rect.x)
self.y = float(self.rect.y)
def blitme(self):
self.screen.blit(self.image,self.rect)
def chek_edges(self):
screen_rect = self.screen.get_rect()
if self.rect.right >= screen_rect.right:
return True
if self.rect.left <= 0:
return True
def update(self):
self.x += (self.ai_settings.alien_speed_factor * self.ai_settings.fleet_direction)
self.rect.x = self.x
def run_game():
global bullets_die
pygame.init()
ai_settings =Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption('Alien Invasion')
ship = Ship(ai_settings,screen)
bullets = Group()
aliens = Group()
ships = Group()
ships.add(ship)
create_piano(ai_settings,ships,screen)
alien = Alien(ai_settings,screen)
alien_width = alien.rect.width
available_spase_x = ai_settings.screen_width - 2*alien_width
number_aliens_x = int(available_spase_x / (2*alien_width))
number_rows = get_number_rows(ai_settings,ship.rect.height,alien.rect.height)
alien.y = -90
alien.rect.y = alien.y
for alien_number in range(number_aliens_x):
# print(alien.rect.y)
create_alien(ai_settings,screen,aliens,alien_number,5)
# aliens.add(alien)
bg_color = (20,250,200)
while True:
pygame.mouse.set_visible(True)
# print(alien.rect.y)
# tre = randint(-1000,1000)
# if tre == 1:
# alien.y = 0
# create_alien(ai_settings,screen,aliens,alien_number,row_number)
# if tre >= 990:
# bullets_die = False
# for row_number in range(number_rows):
# for alien_number in range(number_aliens_x):
# # print(alien.rect.y)
# create_alien(ai_settings,screen,aliens,alien_number,row_number)
for event in pygame.event.get():
#for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
first = randint(0,220)
second = randint(0,220)
three = randint(0,220)
ai_settings.bg_color = (first,second,three)
if event.key == pygame.K_b:
sys.exit()
if event.key == pygame.K_RIGHT:
ship.movin_right = True
if event.key == pygame.K_SPACE:
print("IOU")
for alien in ships.sprites():
new_bullet = Bullet(ai_settings,screen,alien,0,0)
bullets.add(new_bullet)
print(len(ships))
print(len(bullets))
if event.key == pygame.K_LEFT:
ship.movin_left = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.movin_right = False
if event.key == pygame.K_LEFT:
ship.movin_left = False
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x,mouse_y = pygame.mouse.get_pos()
for alien in ships.sprites():
if alien.rect.collidepoint(mouse_x,mouse_y):
new_bullet = Bullet(ai_settings,screen,alien,mouse_x,mouse_y)
bullets.add(new_bullet)
ships.update()
bullets.update()
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
screen.fill(ai_settings.bg_color)
for bullet in bullets.sprites():
bullet.draw_bullet()
first = randint(0,200)
second = randint(0,200)
three = randint(0,200)
ai_settings.bullet_color = (first,second,three)
collisions = pygame.sprite.groupcollide(bullets,aliens,bullets_die,True)
if len(aliens) == 0:
bullets.empty()
for row_number in range(number_rows):
for alien_number in range(number_aliens_x):
# # print(alien.rect.y)
create_alien(ai_settings,screen,aliens,alien_number,row_number)
chek_fleet_edges(ai_settings,aliens)
aliens.update()
if pygame.sprite.spritecollideany(ship,aliens):
pass
pygame.display.flip()
for bullet in ships.sprites():
bullet.blitme()
first = randint(0,200)
second = randint(0,200)
three = randint(0,200)
ai_settings.bullet_color = (first,second,three)
collisions = pygame.sprite.groupcollide(bullets,aliens,bullets_die,True)
if len(aliens) == 0:
bullets.empty()
for row_number in range(number_rows):
for alien_number in range(number_aliens_x):
# # print(alien.rect.y)
create_alien(ai_settings,screen,aliens,alien_number,row_number)
pygame.display.flip()
for bullet in aliens.sprites():
bullet.blitme()
first = randint(0,200)
second = randint(0,200)
three = randint(0,200)
ai_settings.bullet_color = (first,second,three)
collisions = pygame.sprite.groupcollide(bullets,aliens,bullets_die,True)
if len(aliens) == 0:
bullets.empty()
for row_number in range(number_rows):
for alien_number in range(number_aliens_x):
# # print(alien.rect.y)
create_alien(ai_settings,screen,aliens,alien_number,row_number)
pygame.display.flip()
while True:
run_game()