-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
124 lines (110 loc) · 4.12 KB
/
main.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
import random
import time
import argparse
import curses
from start import game
def main(nlines=10,
ncols=30,
begin_y=0,
begin_x=0,
difficulty=2): # sourcery no-metrics
difficulty_levels = [[20, 0.5], [15, 0.3], [
5, 0.2
]] # number of obstables per line, move speed of obstacles and spaceship
obstacle_n, speed = difficulty_levels[difficulty]
try:
tmp = game(nlines, ncols, begin_y, begin_x)
win = tmp.draw_window()
score, ship, obstacles = tmp.game_init()
win.addstr(ship[0], ship[1], '*')
except:
return
# game running
while True:
# press ESC to end the game
key = win.getch()
curses.flushinp()
if key == 27:
break
# create random obstacles
if len(obstacles) == nlines:
obstacles.pop()
score += 1
col_idx = random.sample(
range(begin_x, begin_x + ncols),
k=random.randint(0, ncols // obstacle_n)) # col index of obstacle
obstacles.appendleft(col_idx)
# show obstacles
for line_idx in range(len(obstacles)):
for col_idx in obstacles[line_idx]:
win.addstr(begin_y + line_idx, col_idx, '-')
# move the ship
ship[1] = ship[1] + (key == curses.KEY_RIGHT) - (key
== curses.KEY_LEFT)
# margin case when ship is out of display
if ship[1] > begin_x + ncols - 1:
ship[1] = begin_x + ncols - 1
if ship[1] < begin_x:
ship[1] = begin_x
# show ship
win.addstr(ship[0], ship[1], '*')
# ship hits the obstacle
if (len(obstacles) == nlines and ship[1] in obstacles[-2]) or (
len(obstacles) == nlines - 1 and ship[1] in obstacles[-1]):
# change the obstacle shape to 'x'
win.addstr(begin_y + nlines - 2, ship[1], 'x')
win.addstr(ship[0], begin_y + ncols + 5, 'Score: ' + str(score))
win.refresh()
time.sleep(1)
break
# show current score
win.addstr(ship[0], begin_y + ncols + 5, 'Score: ' + str(score))
win.refresh()
win.clear()
time.sleep(speed)
curses.endwin()
print('Final score: ' + str(score))
if __name__ == '__main__':
"""[Play the spaceship dodging game in terminal]
Args:
nlines (int, optional): [height of canvas]. Defaults to 10.
ncols (int, optional): [width of canvas]. Defaults to 30.
begin_y (int, optional): [position of upper margin of canvas]. Defaults to 0.
begin_x (int, optional): [position of left margin of canvas]. Defaults to 0.
difficulty (int, optional): [difficulty level: control the number of obstacles/move speed]. Choices = [0,1,2]. Defaults to 2 .
"""
parser = argparse.ArgumentParser(
description="Play the spaceship dodging game in terminal")
parser.add_argument("--canvas_height",
nargs='?',
default=10,
type=int,
help="height of canvas")
parser.add_argument("--canvas_width",
nargs='?',
default=30,
type=int,
help="width of canvas")
parser.add_argument("--begin_y",
nargs='?',
default=0,
type=int,
help="position of upper margin of canvas")
parser.add_argument("--begin_x",
nargs='?',
default=0,
type=int,
help="position of left margin of canvas")
parser.add_argument(
"--diff_level",
nargs='?',
default=1,
type=int,
choices=[0, 1, 2],
help=
"difficulty level: control the number of obstacles/move speed. Choices = [0,1,2]. Defaults to 2"
)
args = parser.parse_args()
main(nlines=args.canvas_height,
ncols=args.canvas_width,
difficulty=args.diff_level)