-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpace_shooter_console.py
83 lines (71 loc) · 2.19 KB
/
Space_shooter_console.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
import curses.ascii, random, time, os
if os.name == 'nt':
import winsound
# initializations
screen = curses.initscr()
screen.nodelay(True)
screen.keypad(True)
screen.timeout(100)
curses.curs_set(0)
curses.noecho()
sh, sw = screen.getmaxyx()
ship_y, ship_x = int(sh * .95), sw // 2
ship_speed = 1
lasers = []
enemies = []
start_time = time.time()
move_time = time.time()
inWindows = True if os.name == 'nt' else False
score = 0
while True:
# controls
key = screen.getch()
if key == curses.ascii.ESC:
curses.endwin()
quit()
if key == curses.KEY_LEFT or key == ord('a') and ship_x - 2 > 0:
ship_x -= ship_speed
if key == curses.KEY_RIGHT or key == ord('d') and ship_x + 2 < sw - 1:
ship_x += ship_speed
if key == ord(' '):
lasers.append([ship_y - 2, ship_x])
if inWindows:
winsound.PlaySound('laser_sound', winsound.SND_ASYNC)
# drawing and logic
screen.clear()
screen.addstr(0,0,'Score:')
screen.addstr(0,6,str(score))
# ship drawing
screen.addch(ship_y - 1, ship_x, '#')
screen.addstr(ship_y, ship_x - 2, '#####')
# laser drawing and movement
for laser in lasers:
screen.addch(laser[0], laser[1], '*')
if laser[0] <= 2:
lasers.remove(laser)
else:
laser[0] -= 1
# enemy spawn
if time.time() - start_time >= 3:
start_time = time.time()
enemies.append([1, random.randint(2, sw - 3)])
# enemy drawing
for enemy in enemies:
screen.addch(enemy[0], enemy[1], 'O')
if enemy[0] >= sh - 2:
enemies.remove(enemy)
# enemy movement (i created this because enemy is so fast)
if time.time() - move_time >= 0.3 and len(enemies) > 0:
move_time = time.time()
for enemy in enemies:
enemy[0] += 1
# collision
for enemy in enemies:
for laser in lasers:
if enemy[0] == laser[0] and enemy[1] == laser[1]:
if inWindows:
winsound.PlaySound('explosion', winsound.SND_ASYNC)
enemies.remove(enemy)
lasers.remove(laser)
score += 1
screen.refresh()