-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGalaga.py
More file actions
112 lines (87 loc) · 2.46 KB
/
Copy pathGalaga.py
File metadata and controls
112 lines (87 loc) · 2.46 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
import random
import pgzrun
WIDTH = 1200
HEIGHT = 600
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
ship = Actor("galaga")
bug = Actor("bug")
ship.pos = (WIDTH//2, HEIGHT-60)
speed = 5
game_over = False
#defining the list of bullets and enemies
bullets = []
enemies = []
score = 0
direction = 1
ship.countdown = 90
for x in range (8):
for y in range (4):
enemies.append(Actor("bug"))
enemies[-1].x = 100 + 50 * x
enemies[-1].y = 80 + 50 * y
def display_score():
screen.draw.text(str(score), (50, 30))
def on_key_down(key):
if key == keys.SPACE:
bullets.append(Actor("bullet"))
bullets[-1].x = ship.x
bullets[-1].y = ship.y - 50
def update():
global score, game_over, direction
moveDown = False
if keyboard.left:
ship.x -= speed
if ship.x <= 0:
ship.x = 0
elif keyboard.right:
ship.x += speed
if ship.x >= WIDTH:
ship.x = WIDTH
for bullet in bullets:
if bullet.y <= 0:
bullets.remove(bullet)
else:
bullet.y -= 10
if len(enemies) > 0 and (enemies[-1].x > WIDTH-80 or enemies[0].x < 80):
moveDown = True
direction = direction * -1
for enemy in enemies:
enemy.x += 4*direction
if moveDown == True:
enemy.y += 100
if enemy.y > HEIGHT:
enemies.remove(enemy)
if enemy.colliderect(ship):
for enemy in enemies:
enemies.remove(enemy)
if enemy.y >= HEIGHT:
enemy.y = -100
enemy.x = random.randint(50, WIDTH-50)
for bullet in bullets:
if enemy.colliderect(bullet):
score += 100
enemies.remove(enemy)
bullets.remove(bullet)
if len(enemies) == 0:
handle_game_over()
def draw():
global score
screen.clear()
screen.fill(BLUE)
for bullet in bullets:
bullet.draw()
for enemy in enemies:
enemy.draw()
ship.draw()
if game_over:
screen.fill("black")
screen.draw.text("Game Over", (500, 300), fontsize = 60)
screen.draw.text("Final Score: {}".format(score), (550, 350), fontsize = 30)
if game_over == False:
display_score()
def handle_game_over():
global game_over
game_over = True
pgzrun.go()
SC