-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbat_fly.py
More file actions
131 lines (107 loc) · 3.43 KB
/
Copy pathbat_fly.py
File metadata and controls
131 lines (107 loc) · 3.43 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
"""Bat Fly -- Program 18 from "Marvel Super Heroes Computer Fun".
Dr. Strange-as-a-bat flies around the screen. Keys 1/2/3/4 set persistent
left/up/down/right movement. Timer starts only after BOTH axes are engaged
(diagonal motion). Game ends after TJ > 200 ticks. Score <= 7 = home safe.
Windows only for keypress (msvcrt).
python bat_fly.py
"""
import os
import sys
import time
SW, SH = 40, 24
BAT1 = " \\OO/ " # line 970 (all-others profile)
BAT2 = " _OO_ " # line 980
STEP_DELAY = 0.22
GREEN = "\033[32m"
AMBER = "\033[33m"
RESET = "\033[0m"
CLEAR = "\033[2J\033[H"
if os.name == "nt":
import msvcrt
else:
msvcrt = None
def enable_ansi():
if os.name == "nt":
os.system("")
def play():
if msvcrt is None:
print("Bat Fly needs Windows (msvcrt). Sorry!")
return
enable_ansi()
grid = [[" "] * SW for _ in range(SH)]
def put(vt, ht, s):
r, c = vt - 1, ht - 1
for ch in s:
if 0 <= r < SH and 0 <= c < SW:
grid[r][c] = ch
c += 1
def render():
sys.stdout.write(CLEAR + GREEN)
sys.stdout.write("\n".join("".join(row).rstrip() for row in grid))
sys.stdout.write(RESET)
sys.stdout.flush()
def cls():
for r in range(SH):
for c in range(SW):
grid[r][c] = " "
HH = SW // 2
VV = SH // 2
HM = VM = 0
I = 1
ER = 0
TJ = 0
def header():
s = " %d %d " % (ER, TJ)
put(1, 1, s[:28])
cls(); put(VV, HH, BAT1); header(); render()
last_step = time.time()
while True:
if msvcrt.kbhit():
raw = msvcrt.getch()
try:
k = raw.decode("ascii", errors="ignore")
except Exception:
k = ""
if k == "1": HM = -1
elif k == "2": VM = -1
elif k == "3": VM = 1
elif k == "4": HM = 1
elif k == "\x1b": # ESC to quit early
break
if time.time() - last_step >= STEP_DELAY:
last_step = time.time()
if HM != 0 and VM != 0 and TJ == 0:
TJ = 1 # line 200
header()
if TJ > 0:
TJ += 1 # line 270
if TJ > 200:
break # line 280 -> 400
old_HH = HH
put(VV, HH, " ") # line 290 erase
HH = HH + HM # 300
VV = VV + VM
hit = (old_HH == SW - 6 or old_HH == 1 or VV == SH or VV == 0)
if hit: # 390
HH = SW // 2 - 3
VV = SH // 2
ER = ER + 1
put(VV, HH, BAT1 if I == 1 else BAT2) # 350
I = 2 if I == 1 else 1 # 360-370
render()
time.sleep(0.01)
cls()
msg1 = "%d HITS" % ER
put(SH // 2, SW // 2 - len(msg1) // 2, msg1)
final = "* HOME SAFE *" if ER <= 7 else "* CRASHED *"
put(SH // 2 + 2, SW // 2 - len(final) // 2, final)
render()
sys.stdout.write("\n")
def main():
play()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.stdout.write(RESET + "\n")