-
Notifications
You must be signed in to change notification settings - Fork 1
/
project_v1.py
248 lines (228 loc) · 10 KB
/
project_v1.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
import random2
from colorama import init, Style
init() #[It generates the game state colors in windows terminal, not in Google Colab.]
# Random coordinate generator function
def Rand(start, end, length):
list = []
while len(list)<length:
res = []
for j in range(2):
res.append(random2.randint(start, end))
if res not in list:
list.append(res)
if res==[0,0] or res==[end, end]:
list.remove(res)
list = [i for i in list+obs if i in list and i not in obs]
list.sort()
return list
# Game dictionary
Game_dict={'Easy':{'Size':10, 'Life':5, 'Jump':5, 'Obstacle':10, 'Advantage':20,
'Obstacle_coordinates':[],
'Advantage_coordinates':[]},
'Medium':{'Size':20, 'Life':8, 'Jump':10,'Obstacle':15, 'Advantage':25,
'Obstacle_coordinates':[],
'Advantage_coordinates': []},
'Hard':{'Size':25, 'Life':10, 'Jump':15, 'Obstacle':20, 'Advantage':35,
'Obstacle_coordinates':[],
'Advantage_coordinates':[] }}
# Game state sign function
def Game_State_Sign(x,y,level):
Obstacle_list = Game_dict[level]['Obstacle_coordinates']
Advantage_list = Game_dict[level]['Advantage_coordinates']
Size = Game_dict[level]['Size']
for i in range(0, Size):
for j in range(0, Size):
list_ij=[i,j]
if (i==x and j==y):
print("\033[0;37;44m s ",end=' ')
elif (list_ij in Obstacle_list):
print("\033[0;37;41m * ",end=' ')
elif (list_ij in Advantage_list):
print("\033[0;37;42m + ",end=' ')
elif (i==Size-1 and j==Size-1):
print("\033[0;37;45m e ",end=' ')
else:
print("\033[0;30;47m - ",end=' ')
#print('\033[ \n') #newline with default style [It works in Google Colab, but not in windows terminal. Why!?]
print(Style.RESET_ALL,"\n") #newline with default style [It works both in windows terminal and Google Colab.]
# Game state coordinate function
def Game_State_Coordinate(x,y,level):
Obstacle_list = Game_dict[level]['Obstacle_coordinates']
Advantage_list = Game_dict[level]['Advantage_coordinates']
size = Game_dict[level]['Size']
for i in range(0, size):
for j in range(0, size):
list_ij=[i,j]
if (i==x and j==y):
print("\033[0;37;44m s ",end=' ')
elif (list_ij in Obstacle_list):
print("\033[0;37;41m {:2.0f},".format(i),j,end=' ')
elif (list_ij in Advantage_list):
print("\033[0;37;42m {:2.0f},".format(i),j,end=' ')
elif (i==size-1 and j==size-1):
print("\033[0;37;45m e ",end=' ')
else:
print("\033[0;30;47m {:2.0f},".format(i),j,end=' ')
#print('\033[ \n') #newline with default style [It works in Google Colab, but not in windows terminal. Why!?]
print(Style.RESET_ALL,"\n") #newline with default style [It works both in windows terminal and Google Colab.]
# Game state function
def Game_State(x,y,level):
print("\nYour game state with sign:\n")
Game_State_Sign(x,y,level)
print("\nYour game state with coordinate:\n")
Game_State_Coordinate(x,y,level)
print("\nYour current coordinate: {}, {}".format(x,y))
# Jump function
def Jump(command,x,y, jump):
if jump>0:
if command == '56':
x = x
y = y+2
jump = jump - 1
print("You have only",jump,"jump left!")
elif command == '54':
x = x
y = y-2
jump = jump - 1
print("You have only",jump,"jump left!")
elif command == '58':
x = x-2
y = y
jump = jump - 1
print("You have only",jump,"jump left!")
elif command == '52':
x = x+2
y = y
jump = jump - 1
print("You have only",jump,"jump left!")
else:
print("Sorry, you can't jump anymore.")
return x, y, jump
# Move function
def Move(command,x,y, jump):
commands = ['0', '1', '2', '4', '6', '8', '52', '54', '56', '58']
if command == '6':
x = x
y = y+1
elif command == '4':
x = x
y = y-1
elif command == '8':
x = x-1
y = y
elif command == '2':
x = x+1
y = y
elif command in commands[6:]:
x,y,jump = Jump(command,x,y,jump)
elif command not in commands:
print("Invalid command key\nThe valid command keys are:", end= " ")
print(*commands, sep = ", ")
return x, y, jump
# Undo function [It's mot much efficient]
def Undo(x_past,y_past):
x = x_past
y = y_past
return x,y
# Point function [ I tried to apply Game_Denoter concept. But in doing so, my Point function became complicated and was giving wrong output.]
def Points(x,y,level,life,jump,command):
Obstacle_list = Game_dict[level]['Obstacle_coordinates']
Advantage_list = Game_dict[level]['Advantage_coordinates']
size = Game_dict[level]['Size']
commands = ['0', '1', '2', '4', '6', '8', '52', '54', '56', '58']
if command in commands:
if command=='0':
Game_State(x,y,level)
life = life - 1
print("You have seen the game state again. Your life point is reduced to", life)
else:
if [x,y] in Obstacle_list:
life = life - 5
print("It's an obstacle coordinate. Your life point is reduced to",life)
elif [x,y] in Advantage_list:
life = life + 10
print("It's an advantage coordinate. Your life point is increased to",life)
else:
list_ij = []
for i in range(0, size):
for j in range(0, size):
list_ij.append([i,j])
if [x,y] not in list_ij:
life = life - 1
#print("Your current coordinate: {}, {}".format(x,y))
print("You are out of the game state boundary. You life point is reduced to",life)
return life
#main program
print("Welcome to 'Memorize your move' game!\n")
print("This is a console game where you will need to start from your starting coordinate \nand need to reach your ending coordinate. On your way to the ending coordinate, you will find obstacles and advantages.\n")
print("Game Features: \n# The game has three levels - 1) Easy, 2) Medium and 3) Hard.")
print("# For each level, you will have fixed game state, lives and jumps \n# Obstacles will reduce your point and Advantages will increase your point.")
print("# You can jump over the obstacles also. But only for fixed times. \n# You can see your present position anytime. But for each time of seeing, you will lose one of your life.")
print("# If you go out of the bound, you will lose one of your life. \n# So, you need to memorize your last position and walk or jump according to that.\n")
print("Choose your level: \nPress 1 for Easy \nPress 2 for Medium \nPress 3 for Hard")
while True:
gl = input("> ",)
if gl == '1':
Game_level = 'Easy'
break
elif gl == '2':
Game_level = 'Medium'
break
elif gl == '3':
Game_level = 'Hard'
break
else:
print("Invalid input. Try again. Choose 1, 2 or 3.")
continue
obs = []
Game_dict[Game_level]['Obstacle_coordinates'] = Rand(0, Game_dict[Game_level]['Size']-1, Game_dict[Game_level]['Obstacle'])
obs = Game_dict[Game_level]['Obstacle_coordinates']
Game_dict[Game_level]['Advantage_coordinates'] = Rand(0, Game_dict[Game_level]['Size']-1, Game_dict[Game_level]['Advantage'])
size = Game_dict[Game_level]['Size']
life = Game_dict[Game_level]['Life']
jump = Game_dict[Game_level]['Jump']
print("Welcome to level: {}".format(Game_level))
print("Here, you have {} life points".format(life), end=" ")
print("and {} jump points".format(jump))
x=0
y=0
Game_State(x,y, Game_level)
print("\nHow to Play: \nPress 8 to move up \nPress 2 to move down \nPress 4 to move left \nPress 6 to move right")
print("Press 5x to jump (double move) at a specific direction. For example:\n Press 58 to jump up; other jumps are 52, 54, 56 respectively")
print("Press 0 to see the game state and your present position.\nPress 3 to undo your coordinate change\nPress 1 to exit the game")
print("\nMemorize your present coordinate and obstacles ....")
print("\nStart making moves by pressing the command keys which are listed above.")
while (life > 0):
command = input("\ncommand: ")
if command=='0':
#Game_State(x,y, Game_level)
life = Points(x,y,Game_level,life,jump,command)
#print("Your current life:", life)
if life==0:
print("You lost with",jump,"jump points.\n Game is over.")
break
elif command=='1':
print("Game is stopped.")
break
elif command=='3': # I am not reducing life points for undo move
x,y = Undo(x_past, y_past)
#print("Your current coordinate: {}, {}".format(x,y))
#print("Your current life:", life)
else:
x_past = x
y_past = y
x,y,jump = Move(command,x,y,jump)
#print("Your current coordinate: {}, {}".format(x,y))
life = Points(x,y,Game_level,life,jump,command)
#print("Your current life:", life)
if life==0 and x==size-1 and y==size-1:
print("You have reached the end. But you have no life point left.\n Game is over.")
break
elif life==0:
print("You lost with",jump,"jump points.\n Game is over.")
break
elif (x==size-1 and y==size-1):
Game_State(x,y, Game_level)
print("You have reached the end. \nYou won with",life,"life points and",jump,"jump points. Congrats!\n Game is over.")
break
input("Press enter to exit.\n")