-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
149 lines (108 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os, time, curses, random
ROWS = 20
COLS = 60
OBSTACLE_GAP = 5
def render_grid(grid, stdscr: curses.window):
stdscr.addstr(1, 0, "┌" + "─" * COLS + "┐")
for i, row in enumerate(grid):
stdscr.addstr(i + 2, 0, "│" + "".join(row) + "│")
stdscr.addstr(ROWS + 2, 0, "└" + "─" * COLS + "┘")
def draw_line(grid, x, y1, y2):
for i, row in enumerate(grid):
for j, _ in enumerate(row):
if j == x and min(y1, y2) <= i and max(y1, y2) >= i:
grid[i][j] = "|"
def draw_obstacle(grid, x, height):
ranged_height = min(max(1, height), ROWS - 2 - OBSTACLE_GAP)
draw_line(grid, x, 0, ranged_height)
draw_line(grid, x, ranged_height + 1 + OBSTACLE_GAP, ROWS - 1)
def draw_obstacles(grid, obstacles):
for obstacle in obstacles:
draw_obstacle(grid, obstacle["x"], obstacle["height"])
def main(stdscr: curses.window, autostart=False):
curses.curs_set(0)
stdscr.nodelay(True)
stdscr.timeout(50)
stdscr.clear()
prev_time = time.time()
game_over = False
started = autostart
x = 5
y = round(ROWS / 2)
player_speed = 10
obstacle_speed = 7
obstacles = []
OBSTACLE_DISTANCE = 12
MAX_OBSTACLES = 30
for i in range(1, MAX_OBSTACLES + 1):
obstacles.append(
{
"x": i * OBSTACLE_DISTANCE,
"real_x": i * OBSTACLE_DISTANCE,
"height": random.randint(1, ROWS - 2 - OBSTACLE_GAP),
}
)
while True:
delta = time.time() - prev_time
prev_time = time.time()
grid = [[" " for _ in range(COLS)] for _ in range(ROWS)]
try:
key = stdscr.getch()
if key == ord("q"):
curses.endwin()
break
if not started:
stdscr.addstr(0, 0, " " * 40)
stdscr.addstr(0, 0, "Press S to start")
draw_obstacles(grid, obstacles)
grid[y][x] = "O"
render_grid(grid, stdscr)
if key == ord("s") or key == ord("S"):
started = True
else:
continue
if not game_over:
if key == curses.KEY_UP:
y = max(0, y - (delta * player_speed))
if key == curses.KEY_DOWN:
y = min(ROWS - 1, y + (delta * player_speed))
rounded_y = round(y)
for obstacle in obstacles:
obstacle["real_x"] -= delta * obstacle_speed
obstacle["x"] = round(obstacle["real_x"])
if obstacle["x"] == x and (
y < obstacle["height"]
or y
> min(max(1, obstacle["height"]), ROWS - 2 - OBSTACLE_GAP)
+ 1
+ OBSTACLE_GAP
):
game_over = True
obstacles = list(filter(lambda obstacle: obstacle["x"] >= 0, obstacles))
if len(obstacles) < MAX_OBSTACLES:
obstacles.append(
{
"x": MAX_OBSTACLES * OBSTACLE_DISTANCE,
"real_x": MAX_OBSTACLES * OBSTACLE_DISTANCE,
"height": random.randint(1, ROWS - 2 - OBSTACLE_GAP),
}
)
draw_obstacles(grid, obstacles)
grid[rounded_y][x] = "O"
stdscr.addstr(0, 0, " " * 40)
render_grid(grid, stdscr)
else:
stdscr.addstr(0, 0, " " * 40)
stdscr.addstr(0, 0, "GAME OVER (Press R to restart)")
if key == ord("r") or key == ord("R"):
main(stdscr, True)
break
except Exception:
pass
stdscr.refresh()
try:
curses.wrapper(main)
except KeyboardInterrupt:
os._exit(0)
except:
pass