-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace_invaders.py
More file actions
54 lines (45 loc) · 1.45 KB
/
Copy pathspace_invaders.py
File metadata and controls
54 lines (45 loc) · 1.45 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
#Noor Ansari
import random
WIDTH = 8
HEIGHT = 8
class Game:
def __init__(self):
self.player_x = 4
self.enemy_x = random.randint(1, 5)
self.enemy_y = 0
self.bullet_x = 0
self.bullet_y = 0
self.bullet_active = False
self.game_over = False
def move_player_left(self):
if self.player_x > 0:
self.player_x -= 1
def move_player_right(self):
if self.player_x < WIDTH - 1:
self.player_x += 1
def fire_bullet(self):
if not self.bullet_active:
self.bullet_x = self.player_x
self.bullet_y = 6
self.bullet_active = True
def update_bullet(self):
if self.bullet_active:
self.bullet_y -= 1
if self.bullet_y < 0:
self.bullet_active = False
elif self.bullet_x == self.enemy_x and self.bullet_y == self.enemy_y:
self.enemy_x = random.randint(1, 5)
self.enemy_y = 0
self.bullet_active = False
def update_enemy(self):
self.enemy_y += 1
if self.enemy_y == 7 and self.enemy_x == self.player_x:
self.game_over = True
def get_state(self):
return {
"player": self.player_x,
"enemy": (self.enemy_x, self.enemy_y),
"bullet": (self.bullet_x, self.bullet_y),
"bullet_active": self.bullet_active,
"game_over": self.game_over
}