-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrols.py
129 lines (92 loc) · 3.6 KB
/
controls.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
import pygame, sys
from bullet import Bullet
from alien import Alien
import time
def events(screen, gun, bullets):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
#right
if event.key == pygame.K_d:
gun.mright = True
#left
elif event.key == pygame.K_a:
gun.mleft = True
elif event.key == pygame.K_SPACE:
new_bulet = Bullet(screen, gun)
bullets.add(new_bulet)
elif event.type == pygame.KEYUP:
#right
if event.key == pygame.K_d:
gun.mright = False
#left
elif event.key == pygame.K_a:
gun.mleft = False
def update(bg_color, screen, stats, sc, gun, aliens, bullets):
screen.fill(bg_color)
sc.show_scope()
for bullet in bullets.sprites():
bullet.drow_bullet()
gun.output()
aliens.draw(screen)
pygame.display.flip()
def update_bullets(screen, stats, sc, aliens, bullets):
bullets.update()
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
#print(len(bullets))
collisions = pygame.sprite.groupcollide(bullets, aliens, True, True)
if collisions:
for aliens in collisions.values():
stats.scope += 10 * len(aliens)
sc.image_score()
check_high_score(stats, sc)
#sc.image_guns()
if len(aliens) == 0:
bullets.empty()
create_army(screen, aliens)
def gun_kill(stats, screen, sc, gun, aliens, bullets):
if stats.guns_left > 0:
stats.guns_left -= 1
#sc.image_guns()
aliens.empty()
bullets.empty()
create_army(screen, aliens)
gun.create_gun()
time.sleep(2)
else:
stats.run_game = False
sys.exit()
def update_aliens(stats, screen, sc, gun, aliens, bullets):
aliens.update()
if pygame.sprite.spritecollide(gun, aliens, True):
gun_kill(stats, screen, sc, gun, aliens, bullets)
aliens_check(stats, screen, sc, gun, aliens, bullets)
def aliens_check(stats, screen, sc, gun, aliens, bullets):
screen_rect = screen.get_rect()
for alien in aliens.sprites():
if alien.rect.bottom >= screen_rect.bottom:
gun_kill(stats, screen, sc, gun, aliens, bullets)
break
def create_army(screen, aliens):
alien = Alien(screen)
alien_width = alien.rect.width
number_alien_x = int((1000-2 * alien_width) / alien_width)
alien_height = alien.rect.height
number_alien_y = int((1000 - 100 - 2*alien_height)/alien_height)
for row_number in range(number_alien_y - 3):
for alien_num in range(number_alien_x):
alien = Alien(screen)
alien.x = alien_width + alien_width * alien_num
alien.y = alien_height + alien_height * row_number
alien.rect.x = alien.x
alien.rect.y = alien.rect.height + alien.rect.height * row_number
aliens.add(alien)
def check_high_score(stats, sc):
if stats.scope > stats.high_score:
stats.high_score = stats.scope
sc.image_high_score()
with open('high_score.txt', 'w') as f:
f.write(str(stats.high_score))