-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpacman.py
260 lines (219 loc) · 8.98 KB
/
pacman.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
"""
Terminal PacMan - Martin Gurasvili
-TO PLAY-
you have 3 lives
once game started use w/a/s/d - up/left/down/right and enter
if nothing is entered the player moves automatically in the facing direction within 2 seconds
score 70 points to win
▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉
▉∙ ∙ ∙ ∙ ∙ ▉▉∙ ∙ ∙ ∙ ▉▉∙ ∙ ∙ ∙ ∙ ▉
▉∙ ▉▉▉▉▉▉∙ ▉▉∙ ▉▉▉▉∙ ▉▉∙ ▉▉▉▉▉▉∙ ▉
▉∙ ▉▉∙ ∙ ∙ ∙ ∙ ▉▉▉▉∙ ∙ ∙ ∙ ∙ ▉▉∙ ▉
▉∙ ▉▉∙ ▉▉∙ ▉▉∙ ▉▉▉▉∙ ▉▉∙ ▉▉∙ ▉▉∙ ▉
▉∙ ∙ ∙ ▉▉∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ▉▉∙ ∙ ∙ ▉
▉▉▉∙ ▉▉▉▉∙ ▉▉▉▉▉▉▉▉▉▉▉▉∙ ▉▉▉▉∙ ▉▉▉
▉▉▉∙ ∙ ∙ ∙ ▉▉∙ ∙ ∙ ∙ ▉▉∙ ∙ ∙ ∙ ▉▉▉
▉▉▉∙ ▉▉▉▉∙ ▉▉∙ ∙ ∙ ∙ ▉▉∙ ▉▉▉▉∙ ▉▉▉
▉▉▉∙ ▉▉∙ ∙ ▉▉▉▉∙ ∙ ▉▉▉▉∙ ∙ ▉▉∙ ▉▉▉
▉∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ▉
▉∙ ▉▉∙ ▉▉▉▉∙ ▉▉∙ ∙ ▉▉∙ ▉▉▉▉∙ ▉▉∙ ▉
▉∙ ▉▉∙ ▉▉∙ ∙ ▉▉∙ ∙ ▉▉∙ ∙ ▉▉∙ ▉▉∙ ▉
▉∙ ▉▉∙ ▉▉∙ ▉▉▉▉▉▉▉▉▉▉▉▉∙ ▉▉∙ ▉▉∙ ▉
▉∙ ∙ ∙ ▉▉∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ▉▉∙ ∙ ∙ ▉
▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉
"""
import random
import time
from signal import signal, alarm, SIGALRM
from termcolor import colored
size = 16
lives = 3
score = "0"
direct ="N"
posx = 0
posy = 0
prev = " "
dire = 2
winscore = 70
gx = 2
gy = 1
gameover = False
maze = [[" ","██"*(size)],
["█","∙ ","∙ ","∙ ","∙ ","∙ ","██","∙ ","∙ ","∙ ","∙ ","██","∙ ","∙ ","∙ ","∙ ","∙ ","█"],
["█","∙ ","██","██","██","∙ ","██","∙ ","██","██","∙ ","██","∙ ","██","██","██","∙ ","█"],
["█","∙ ","██","∙ ","∙ ","∙ ","∙ ","∙ ","██","██","∙ ","∙ ","∙ ","∙ ","∙ ","██","∙ ","█"],
["█","∙ ","██","∙ ","██","∙ ","██","∙ ","██","██","∙ ","██","∙ ","██","∙ ","██","∙ ","█"],
["█","∙ ","∙ ","∙ ","██","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","██","∙ ","∙ ","∙ ","█"],
[" ","██","∙ ","██","██","∙ ","██","██","██","██","██","██","∙ ","██","██","∙ ","██"," "],
[" "," █","∙ ","∙ ","∙ ","∙ ","██"," "," "," "," ","██","∙ ","∙ ","∙ ","∙ ","█ "," "],
[" "," █","∙ ","██","██","∙ ","██"," "," "," "," ","██","∙ ","██","██","∙ ","█ "," "],
[" ","██","∙ ","██","∙ ","∙ ","██","██"," "," ","██","██","∙ ","∙ ","██","∙ ","██"," "],
["█","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","█"],
["█","∙ ","██","∙ ","██","██","∙ ","██","∙ ","∙ ","██","∙ ","██","██","∙ ","██","∙ ","█"],
["█","∙ ","██","∙ ","██","∙ ","∙ ","██","∙ ","∙ ","██","∙ ","∙ ","██","∙ ","██","∙ ","█"],
["█","∙ ","██","∙ ","██","∙ ","██","██","██","██","██","██","∙ ","██","∙ ","██","∙ ","█"],
["█","∙ ","∙ ","∙ ","██","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","∙ ","██","∙ ","∙ ","∙ ","█"],
[" ","██"*(size)],
]
def game_space ():
print((" "*int(size/2-1)),"Martin's - PacMan")
print((" "*int(size/1.3)),"Score",score)
print((" "*int(size-2)),(str(lives)+" ❤"))
for x in range(len(maze)):
for y in range(len(maze[x])):
if(maze[x][y] == "▶ " or maze[x][y] == "◀ " or maze[x][y] == "▲ " or maze[x][y] == "▼ "):
print(colored(maze[x][y],"yellow"),end="")
elif(maze[x][y]=="∙ "):
print(maze[x][y],end="")
elif(maze[x][y]=="◓ "):
print(colored(maze[x][y],"red"),end="")
else:
print(maze[x][y],end="")
print()
def spawn():
global direct,maze,posx,posy
good_loc = False
while good_loc == False:
x = random.randint(1,size-2)
y = random.randint(1,size-2)
if(maze[x][y]=="∙ "):
good_loc = True
try:
if(maze[x][y+1]=="∙ "):
maze[x][y] = "▶ "
direct ="R"
if(maze[x][y-1]=="∙ "):
maze[x][y] = "◀ "
direct ="L"
if(maze[x-1][y]=="∙ "):
maze[x][y] = "▲ "
direct ="U"
if(maze[x+1][y]=="∙ "):
maze[x][y] = "▼ "
direct ="D"
except:
print(" ")
posx = x
posy = y
def move():
global direct,maze,posx,posy,score,gameover
x = None
signal(SIGALRM, lambda x, y: 1/0)
try:
alarm(1)
x = str(input(">>>"))
except ZeroDivisionError:
print("")
if(x!=None):
if (x == "w"):
direct = "U"
if (x == "a"):
direct = "L"
if (x == "s"):
direct = "D"
if (x == "d"):
direct = "R"
running = True
while running:
if(direct =="L"):
maze[posx][posy] = " "
score = int(score)+1
posy-=1
if(maze[posx][posy] == " " or maze[posx][posy] == "∙ "):
maze[posx][posy] = "◀ "
else:
gameover =True
running = False
if(direct =="R"):
maze[posx][posy] = " "
score = int(score)+1
posy+=1
if(maze[posx][posy] == " " or maze[posx][posy] == "∙ "):
maze[posx][posy] = "▶ "
else:
gameover =True
running = False
break
if(direct =="U"):
maze[posx][posy] = " "
score = int(score)+1
posx-=1
if(maze[posx][posy] == " " or maze[posx][posy] == "∙ "):
maze[posx][posy] = "▲ "
else:
gameover =True
running = False
break
if(direct =="D"):
maze[posx][posy] = " "
score = int(score)+1
posx+=1
if(maze[posx][posy] == " " or maze[posx][posy] == "∙ "):
maze[posx][posy] = "▼ "
else:
gameover =True
running = False
game_space()
def ghost_ai():
global maze,gx,gy,gameover,prev,dire
running = True
while running:
if(dire == 0):
if(maze[gx][gy+1] == " " or maze[gx][gy+1]== "∙ "):
maze[gx][gy] = prev
prev = maze[gx][gy+1]
maze[gx][gy+1] = "◓ "
gy+=1
running = False
break
elif(dire == 1):
if(maze[gx][gy-1] == " " or maze[gx][gy-1]== "∙ "):
maze[gx][gy] = prev
prev = maze[gx][gy-1]
maze[gx][gy-1] = "◓ "
gy-=1
running = False
break
elif(dire == 2):
if(maze[gx+1][gy] == " " or maze[gx+1][gy]== "∙ "):
maze[gx][gy] = prev
prev = maze[gx+1][gy]
maze[gx+1][gy] = "◓ "
gx+=1
running = False
break
elif(dire == 3):
try:
if(maze[gx-1][gy] == " " or maze[gx-1][gy]== "∙ "):
maze[gx][gy] = prev
prev = maze[gx-1][gy]
maze[gx-1][gy] = "◓ "
gx-=1
running = False
break
except:
dire = random.randint(0,4)
dire = random.randint(0,4)
if __name__ == "__main__":
spawn()
game_space()
input("Press Enter To Start Game")
print(" ")
for x in range(3+1):
if score == winscore:
break
while gameover == False:
move()
ghost_ai()
if score == winscore:
print("You Won")
input("enter to exit")
break
if gameover ==True:
if(lives > 0):
print("You Died")
lives-=1
spawn()
gameover = False
if score != winscore:
print("GameOver")