/
game.py
264 lines (227 loc) · 5.81 KB
/
game.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import os
import sys
import random
import time
import math
class Pos:
def __init__(self,x,y):
self.x = x
self.y = y
health = 100
money = 0
home = Pos(0,0)
location = Pos(0,0)
UP = "\x1b[A"
DOWN = "\x1b[B"
RIGHT = "\x1b[C"
LEFT = "\x1b[D"
INVENTORY = []
INVWEIGHT = 0
OBJECTS = {}
WEIGHT = {"W":2,"R":8}
MAXWEIGHT = 40
CURINDEX = 0
def RLE(ls):
result = []
rlist = []
for elem in ls:
if not elem in rlist:
result.append((ls.count(elem),elem))
rlist.append(elem)
return result
def getLightLevel():
return (math.sin(time.time() / 30) + 1)*80
def GetMap(x):
def GetMap2(y):
if (location.y - y)**2 + ((location.x - x)/2**0.5)**2 <= getLightLevel()**2:
if location.x == x and location.y == y:
return "@"
elif home.x == x and home.y == y:
return "#"
elif (x,y) in list(OBJECTS.keys()):
return OBJECTS[(x,y)]
else:
return " "
else:
return "*"
return GetMap2
MAP = GetMap
def dispitem(x):
if x[0] > 1:
return str(x[0]) + " " + x[1]
else:
return x[1]
def frame():
(width,height) = os.get_terminal_size()
height -=3
stats = "HP:" + str(health) + "|GOLD:" + str(money) + "|HOME:" + str(home.x) + "," + str(home.y) + "|LOCATION:" + str(location.x) + "," + str(location.y)
remcols = width - len(stats)
stats = "="*(remcols//2) + stats + "="*(remcols//2) + "="*(remcols % 2)
print(stats)
inv = "INVENTORY:"+'|'.join(list(map(lambda x: "@" + dispitem(x) if RLE(INVENTORY).index(x) == CURINDEX else dispitem(x),RLE(INVENTORY))))
remcols = width - len(inv)
inv = "="*(remcols//2) + inv + "="*(remcols//2) + "="*(remcols % 2)
print(inv)
wt = "WEIGHT:" + str(INVWEIGHT) + "|MAX WEIGHT:" + str(MAXWEIGHT)
remcols = width - len(wt)
wt = "="*(remcols//2) + wt + "="*(remcols//2) + "="*(remcols % 2)
print(wt)
for y in range(height):
for x in range(width):
print(GetMap(x)(y),end="")
def welcome():
(width,height) = os.get_terminal_size()
msg = "KEEP MOVING"
remcols = width - len(msg)
msg = '='*(remcols//2) + msg + '='*(remcols//2) + '='*(remcols % 2)
print(msg)
msg = "Written by Tuomas Laakkonen"
remcols = width - len(msg)
msg = '='*(remcols//2) + msg + '='*(remcols//2) + '='*(remcols % 2)
print(msg,flush=True)
print("\n"*(height - 6))
msg = "PRESS ANY KEY TO START"
remcols = width - len(msg)
msg = '='*(remcols//2) + msg + '='*(remcols//2) + '='*(remcols % 2)
print(msg)
def sidetox(side):
if side == "a":
return location.x - 1
elif side == "d":
return location.x + 1
else:
return location.x
def sidetoy(side):
if side == "w":
return location.y - 1
elif side == "s":
return location.y + 1
else:
return location.y
OBSTRUCTINGOBJECTS = ['T','R']
def isnotobstructed(side):
return not MAP(sidetox(side))(sidetoy(side)) in OBSTRUCTINGOBJECTS
def isonside(side,obj):
return MAP(sidetox(side))(sidetoy(side)) == obj
def action(side):
if side == "w":
...
elif side == "s":
...
elif side == "a":
...
elif side == "d":
...
if isonside(side,"T"):
global INVWEIGHT
if (INVWEIGHT + 4 * WEIGHT["W"]) <= MAXWEIGHT:
del OBJECTS[(sidetox(side),sidetoy(side))]
for _ in range(4):
INVENTORY.append("W")
INVWEIGHT += 4 * WEIGHT["W"]
if isonside(side,"R"):
if INVWEIGHT + WEIGHT["R"] <= MAXWEIGHT:
del OBJECTS[(sidetox(side),sidetoy(side))]
INVENTORY.append("R")
INVWEIGHT += WEIGHT["R"]
def drop():
if INVENTORY != [] and not (location.x,location.y) in list(OBJECTS.keys()) and CURINDEX < len(RLE(INVENTORY)):
OBJECT = RLE(INVENTORY)[CURINDEX][1]
OBJECTS[(location.x,location.y)] = OBJECT
INVENTORY.remove(OBJECT)
global INVWEIGHT
INVWEIGHT -= WEIGHT[OBJECT]
def pickup():
if (location.x,location.y) in list(OBJECTS.keys()):
OBJECT = OBJECTS[(location.x,location.y)]
global INVWEIGHT
if INVWEIGHT + WEIGHT[OBJECT] <= MAXWEIGHT:
INVENTORY.append(OBJECT)
INVWEIGHT += WEIGHT[OBJECT]
del OBJECTS[(location.x,location.y)]
def select(num):
if INVENTORY != [] and num > -1 and num < len(RLE(INVENTORY)):
global CURINDEX
CURINDEX = num
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
getch = _Getch()
(width,height) = os.get_terminal_size()
for _ in range(40):
OBJECTS[(random.randint(0,width),random.randint(0,height))] = 'T'
OBJECTS[(random.randint(0,width),random.randint(0,height))] = 'R'
print("\033c")
welcome()
getch()
print("\033c")
frame()
print(flush=True,end="")
while True:
char = getch()
if char == '\x1b':
getch()
char = '\x1b[' + getch()
if char == UP:
if isnotobstructed("w"):
location.y -= 1
elif char == DOWN:
if isnotobstructed("s"):
location.y += 1
elif char == RIGHT:
if isnotobstructed("d"):
location.x += 1
elif char == LEFT:
if isnotobstructed("a"):
location.x -= 1
elif char == "h":
home.x = location.x
home.y = location.y
elif char == "w":
action("w")
elif char == "s":
action("s")
elif char == "a":
action("a")
elif char == "d":
action("d")
elif char == "q":
drop()
elif char == "e":
pickup()
elif char.isnumeric():
select(int(char) - 1)
elif char == "z":
select(CURINDEX - 1)
elif char == "x":
select(CURINDEX + 1)
elif char == "\x03":
sys.exit(0)
print("\033c")
frame()
print(flush=True,end="")