-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
271 lines (217 loc) · 10.8 KB
/
game.py
File metadata and controls
271 lines (217 loc) · 10.8 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
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
265
266
267
268
269
270
271
import random
import keyboard
class Data:
character_Position = [25,6] # x , y
wallLocations = []
ScreenSize = [50,14]
totalScore = 0
Levels = ["Normal", "Hard", "Master"]
diffcultlyLevel = 0
runningGame = True
# Train Data
trains = []
trainLocations_T = []
trainLocations_R = []
trainLocations_B = []
trainLocations_L = []
def draw_screen():
print("\n"*5)
print("-"*51)
for y_ in range(1, Data.ScreenSize[1]):
row = "|"
for x_ in range(1, Data.ScreenSize[0]):
cell_Location = [x_, y_]
if (Data.character_Position in Data.trainLocations_T or
Data.character_Position in Data.trainLocations_R or
Data.character_Position in Data.trainLocations_B or
Data.character_Position in Data.trainLocations_L):
print(" ")
print("DEATH")
print(f"Diffculty: {Data.Levels[Data.diffcultlyLevel-1]}")
print(f"Score: {str(Data.totalScore)}")
print(" ")
print("-"*50)
Data.runningGame = False
return
if Data.character_Position == cell_Location:
row += "X"
elif cell_Location in Data.trainLocations_T:
row += "|" if Data.diffcultlyLevel == 3 else "v"
elif cell_Location in Data.trainLocations_R:
row += "=" if Data.diffcultlyLevel == 3 else "<"
elif cell_Location in Data.trainLocations_B:
row += "|" if Data.diffcultlyLevel == 3 else "^"
elif cell_Location in Data.trainLocations_L:
row += "=" if Data.diffcultlyLevel == 3 else ">"
elif cell_Location in Data.wallLocations:
row += "+"
else:
row += " "
print(row + "|")
print("-"*50)
print(" ")
def start():
print("")
print("████████╗██████╗░░█████╗░██╗███╗░░██╗ ████████╗███████╗██████╗░███╗░░░███╗██╗███╗░░██╗░█████╗░██╗░░░░░ \n" +
"╚══██╔══╝██╔══██╗██╔══██╗██║████╗░██║ ╚══██╔══╝██╔════╝██╔══██╗████╗░████║██║████╗░██║██╔══██╗██║░░░░░ \n" +
"░░░██║░░░██████╔╝███████║██║██╔██╗██║ ░░░██║░░░█████╗░░██████╔╝██╔████╔██║██║██╔██╗██║███████║██║░░░░░ \n" +
"░░░██║░░░██╔══██╗██╔══██║██║██║╚████║ ░░░██║░░░██╔══╝░░██╔══██╗██║╚██╔╝██║██║██║╚████║██╔══██║██║░░░░░ \n" +
"░░░██║░░░██║░░██║██║░░██║██║██║░╚███║ ░░░██║░░░███████╗██║░░██║██║░╚═╝░██║██║██║░╚███║██║░░██║███████╗ \n" +
"░░░╚═╝░░░╚═╝░░╚═╝╚═╝░░╚═╝╚═╝╚═╝░░╚══╝ ░░░╚═╝░░░╚══════╝╚═╝░░╚═╝╚═╝░░░░░╚═╝╚═╝╚═╝░░╚══╝╚═╝░░╚═╝╚══════╝")
print("")
print("How to play: \n Move with the 'ASWD' Keys to dodge the moving Trains(^<v>) \n with the the (+) wall growing")
while Data.diffcultlyLevel == 0:
print("\n")
for i in range(0, len(Data.Levels)):
print(f"Pick {i} - [ {Data.Levels[i]} ]")
print("\n")
diffcultly = input("Choose the diffcutly level: ")
try:
Data.diffcultlyLevel = int(diffcultly) if int(diffcultly) < 4 else 0
except:
pass
random_WallGrowth()
draw_screen()
for key in ['w', 's', 'a', 'd']:
keyboard.on_press_key(key, lambda e, d=key: moveSteps(e, d), suppress=True)
while Data.runningGame:
if keyboard.is_pressed('esc'):
print("Exiting Game...")
break
def moveSteps(e, d: str):
match d:
case "w":
if (Data.character_Position[1] - 1) <= 0:
return
pre = [Data.character_Position[0], Data.character_Position[1] - 1]
if pre in Data.wallLocations:
return
_y = Data.character_Position[1]
Data.character_Position = [Data.character_Position[0], _y - 1]
case "s":
if (Data.character_Position[1] + 1) >= Data.ScreenSize[1]:
return
pre = [Data.character_Position[0], Data.character_Position[1] + 1]
if pre in Data.wallLocations:
return
_y = Data.character_Position[1]
Data.character_Position = [Data.character_Position[0], _y + 1]
case "a":
if (Data.character_Position[0] - 1) <= 0:
return
pre = [Data.character_Position[0] - 1, Data.character_Position[1]]
if pre in Data.wallLocations:
return
_x = Data.character_Position[0]
Data.character_Position = [_x - 1, Data.character_Position[1]]
case "d":
if (Data.character_Position[0] + 1) >= Data.ScreenSize[0]:
return
pre = [Data.character_Position[0] + 1, Data.character_Position[1]]
if pre in Data.wallLocations:
return
_x = Data.character_Position[0]
Data.character_Position = [_x + 1, Data.character_Position[1]]
spawnTrain()
trainMovement()
random_WallGrowth()
draw_screen()
Data.totalScore += 1
def trainMovement():
Data.trainLocations_T.clear()
Data.trainLocations_L.clear()
Data.trainLocations_B.clear()
Data.trainLocations_R.clear()
newTrainSet = []
for train in Data.trains:
match train[0]:
case "T":
for b in range(0, train[1]):
G = [train[2], (train[3]-b)]
Data.trainLocations_T.append(G)
newpos = (train[3] + 1)
if newpos < (Data.ScreenSize[1] + train[1] + 2):
U = [train[0], train[1], train[2], newpos]
newTrainSet.append(U)
case "R":
for b in range(0, train[1]):
G = [train[2]-b, train[3]]
Data.trainLocations_R.append(G)
newpos = (train[2] - 1)
if newpos < (Data.ScreenSize[0] + train[1] + 2):
U = [train[0], train[1], newpos, train[3]]
newTrainSet.append(U)
case "B":
for b in range(0, train[1]):
G = [train[2], (train[3]+b)]
Data.trainLocations_B.append(G)
newpos = (train[3] - 1)
if newpos < (Data.ScreenSize[1] + train[1] + 2):
U = [train[0], train[1], train[2], newpos]
newTrainSet.append(U)
case "L":
for b in range(0, train[1]):
G = [train[2]+b, train[3]]
Data.trainLocations_L.append(G)
newpos = (train[2]+1)
if newpos < (Data.ScreenSize[0] + train[1] + 2):
U = [train[0], train[1], newpos, train[3]]
newTrainSet.append(U)
Data.trains.clear()
Data.trains = newTrainSet
def spawnTrain():
chance = random.randrange(1,101)
match Data.diffcultlyLevel:
case 1:
if chance > 66: # 33% chance to spawn Train
return
case 2:
if chance > 50: # %50 chance to spawn Train
return
data_ = ""
match (random.randrange(1,5)):
case 1: # top ^
loc = [random.randrange(1, Data.ScreenSize[0]), 1]
data_ = ["T", random.randrange(2,8), loc[0], loc[1]] # side, size, head-x-position, head-y-position
Data.trainLocations_T.append(loc)
case 2: # right
loc = [Data.ScreenSize[0], random.randrange(1, Data.ScreenSize[1])]
data_ = ["R", random.randrange(2,8), loc[0], loc[1]] # side, size, head-x-position, head-y-position
Data.trainLocations_R.append(loc)
case 3: # bottom
loc = [random.randrange(1, Data.ScreenSize[0]), Data.ScreenSize[1]]
data_ = ["B", random.randrange(2,8), loc[0], loc[1]] # side, size, head-x-position, head-y-position
Data.trainLocations_B.append(loc)
case _: # left
loc = [1, random.randrange(1, Data.ScreenSize[1])]
data_ = ["L", random.randrange(2,8), loc[0], loc[1]] # side, size, head-x-position, head-y-position
Data.trainLocations_L.append(loc)
Data.trains.append(data_)
def random_WallGrowth():
# pick starting pos
if len(Data.wallLocations) == 0:
Data.wallLocations.append(
[random.randrange(1,Data.ScreenSize[0]),
random.randrange(1,Data.ScreenSize[1])])
else:
openSpots = []
for L in Data.wallLocations:
if L[0] >= 2:
h = [(L[0]-1), L[1]]
if h not in Data.wallLocations and h not in openSpots and h != Data.character_Position:
openSpots.append(h)
if L[0] < Data.ScreenSize[0]:
h = [(L[0]+1), L[1]]
if h not in Data.wallLocations and h not in openSpots and h != Data.character_Position:
openSpots.append(h)
if L[1] >= 2:
h = [L[0], (L[1]-1)]
if h not in Data.wallLocations and h not in openSpots and h != Data.character_Position:
openSpots.append(h)
if L[1] < Data.ScreenSize[1]:
h = [L[0], (L[1]+1)]
if h not in Data.wallLocations and h not in openSpots and h != Data.character_Position:
openSpots.append(h)
randomPick = random.randrange(0, len(openSpots))
Data.wallLocations.append(openSpots[randomPick])
start()