-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace_invaders.py
More file actions
108 lines (91 loc) · 3.56 KB
/
space_invaders.py
File metadata and controls
108 lines (91 loc) · 3.56 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
import os
import time
import keyboard
import random
from threading import Thread
class SpaceInvaders:
def __init__(self):
self.width = 40
self.height = 20
self.player_pos = self.width // 2
self.player = "^"
self.enemy = "V"
self.bullet = "|"
self.enemies = [(random.randint(0, self.width-1), 0) for _ in range(5)]
self.bullets = []
self.score = 0
self.game_over = False
def draw_screen(self):
# Create empty screen
screen = [[" " for _ in range(self.width)] for _ in range(self.height)]
# Draw player
screen[-1][self.player_pos] = self.player
# Draw enemies
for ex, ey in self.enemies:
if 0 <= ey < self.height and 0 <= ex < self.width:
screen[ey][ex] = self.enemy
# Draw bullets
for bx, by in self.bullets:
if 0 <= by < self.height and 0 <= bx < self.width:
screen[by][bx] = self.bullet
# Convert to string
return "\n".join(["".join(row) for row in screen])
def move_player(self, direction):
if direction == "left" and self.player_pos > 0:
self.player_pos -= 1
elif direction == "right" and self.player_pos < self.width - 1:
self.player_pos += 1
def shoot(self):
self.bullets.append((self.player_pos, self.height - 2))
def update(self):
# Move bullets
new_bullets = []
for bx, by in self.bullets:
if by > 0:
new_bullets.append((bx, by - 1))
self.bullets = new_bullets
# Move enemies down
new_enemies = []
for ex, ey in self.enemies:
if ey < self.height - 1:
new_enemies.append((ex, ey + 1))
else:
self.game_over = True
self.enemies = new_enemies
# Check collisions
for bullet in self.bullets[:]:
for enemy in self.enemies[:]:
if bullet[0] == enemy[0] and bullet[1] == enemy[1]:
if bullet in self.bullets:
self.bullets.remove(bullet)
if enemy in self.enemies:
self.enemies.remove(enemy)
self.score += 10
# Add new enemies
if random.random() < 0.1:
self.enemies.append((random.randint(0, self.width-1), 0))
def run(self):
def input_thread():
while not self.game_over:
try:
if keyboard.is_pressed('left'):
self.move_player('left')
if keyboard.is_pressed('right'):
self.move_player('right')
if keyboard.is_pressed('space'):
self.shoot()
except:
pass
time.sleep(0.05)
Thread(target=input_thread, daemon=True).start()
while not self.game_over:
os.system('cls' if os.name == 'nt' else 'clear')
print(self.draw_screen())
print(f"\nScore: {self.score}")
print("\nUse left/right arrows to move, space to shoot")
self.update()
time.sleep(0.1)
print("\nGame Over! Final score:", self.score)
if __name__ == "__main__":
game = SpaceInvaders()
game.run()