模拟扫雷游戏
game_main
from game_map import map
from game_intro import intro
class Engine(object):
def __init__(self):
self.moves = {}
self.count = 0
self.h_step = 0
self.v_step = 0
def play(self):
map()
print("You're now in (%s, %s)" % (self.h_step, self.v_step))
h_move = input("How much do you want to move horizontally?\n>>>>")
v_move = input("How much do you want to move vertically?\n>>>>")
while int(h_move) in [0, -1, 1] and int(v_move) in [0, -1, 1]:
next_h_step = self.h_step + int(h_move)
next_v_step = self.v_step + int(v_move)
if 0 <= next_h_step <= 3 and 0 <= next_v_step <= 3:
print("So, you're going to (%s, %s)" % (next_h_step, next_v_step))
self.h_step += int(h_move)
self.v_step += int(v_move)
self.moves[self.h_step] = self.v_step
return self.h_step, self.v_step, self.bomb(self.h_step)
else:
print("You're going out of the map, try again.")
return self.play()
else:
print("Wrong input, please try again")
return self.play()
def bomb(self, step):
bomb_steps = {0: 2, 1: 1, 2: 0, 3: 2}
x = self.moves[step]
if x == bomb_steps[step] and self.count <3:
print("Boooom!!!")
print("You just stepped on a bomb")
self.count += 1
return self.live()
else:
return self.win()
def live(self):
if self.count <3:
print("\033[31;01mNow you have %s lives left.\033[0m" % (3 - self.count))
return self.win()
else:
print("You're dead! Game over!")
exit()
def win(self):
if self.h_step == self.v_step == 3:
exit("\033[32;01mCongratulations! You win!!\033[0m")
else:
print("You're safe. Keep moving!")
return self.play()
intro()
a = Engine()
a.play()
game_intro
from game_map import map
def intro():
print("\nWelcome to MyGame!\n")
print("Here are some rules of this game:")
print("\tyou will begin at the 'B' point, i.e. (0, 0);")
print("\tthe aim is to get to point 'W', i.e. (3, 3);")
print("\teach time you can only move 0 or 1 step horizontally and vertically,")
print("\t就是说每次输入横向,以及纵向移动1个或0个单位")
print("Attention!")
print("\tThere are four bombs hidden in the map,")
print("\tyou have three lives, one step on a bomb takes one life")
print("Good luck!")
print("Now, let's do this!")
map()
game_map
def map():
print('''
(3, 3)
o -- o -- o -- W
| | | |
o -- o -- o -- o
| | | |
o -- o -- o -- o
| | | |
B -- o -- o -- o
(0, 0)
''')
Welcome to MyGame! Here are some rules of this game: you will begin at the 'B' point, i.e. (0, 0); the aim is to get to point 'W', i.e. (3, 3); each time you can only move 0 or 1 step horizontally and vertically, 就是说每次输入横向,以及纵向移动1个或0个单位 Attention! There are four bombs hidden in the map, you have three lives, one step on a bomb takes one life Good luck! Now, let's do this! (3, 3) o -- o -- o -- W | | | | o -- o -- o -- o | | | | o -- o -- o -- o | | | | B -- o -- o -- o (0, 0) (3, 3) o -- o -- o -- W | | | | o -- o -- o -- o | | | | o -- o -- o -- o | | | | B -- o -- o -- o (0, 0) You're now in (0, 0) How much do you want to move horizontally? >>>>-1 How much do you want to move vertically? >>>>0 You're going out of the map, try again. (3, 3) o -- o -- o -- W | | | | o -- o -- o -- o | | | | o -- o -- o -- o | | | | B -- o -- o -- o (0, 0) You're now in (0, 0) How much do you want to move horizontally? >>>>1 How much do you want to move vertically? >>>>1 So, you're going to (1, 1) Boooom!!! You just stepped on a bomb Now you have 2 lives left. (3, 3) o -- o -- o -- W | | | | o -- o -- o -- o | | | | o -- o -- o -- o | | | | B -- o -- o -- o (0, 0) You're now in (1, 1) How much do you want to move horizontally? >>>>-1 How much do you want to move vertically? >>>>1 So, you're going to (0, 2) Boooom!!! You just stepped on a bomb Now you have 1 lives left. (3, 3) o -- o -- o -- W | | | | o -- o -- o -- o | | | | o -- o -- o -- o | | | | B -- o -- o -- o (0, 0) You're now in (0, 2) How much do you want to move horizontally? >>>>1 How much do you want to move vertically? >>>>1 So, you're going to (1, 3) You're safe. Keep moving! (3, 3) o -- o -- o -- W | | | | o -- o -- o -- o | | | | o -- o -- o -- o | | | | B -- o -- o -- o (0, 0) You're now in (1, 3) How much do you want to move horizontally? >>>>1 How much do you want to move vertically? >>>>0 So, you're going to (2, 3) You're safe. Keep moving! (3, 3) o -- o -- o -- W | | | | o -- o -- o -- o | | | | o -- o -- o -- o | | | | B -- o -- o -- o (0, 0) You're now in (2, 3) How much do you want to move horizontally? >>>>1 How much do you want to move vertically? >>>>0 So, you're going to (3, 3) Congratulations! You win!!
2019-10-04
02:19:12
posted on 2019-10-03 21:19 petit_herisson 阅读(122) 评论(0) 收藏 举报
浙公网安备 33010602011771号