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): print("You‘re now in (%s, %s)" % (self.h_step, self.v_step)) h_move = int(input("How much do you want to move horizontally?")) v_move = int(input("How much do you want to move vertically?")) if int(self.h_step) + h_move >= 0 and int(self.v_step) + v_move >= 0: next_h_step = self.h_step + h_move next_v_step = self.v_step + v_move if next_h_step - int(self.h_step) <= 1 and next_v_step - int(self.v_step) <= 1: print("So, you‘re going to (%s, %s)" % (next_h_step, next_v_step)) self.h_step += h_move self.v_step += v_move self.moves[self.h_step] = self.v_step return self.h_step, self.v_step, self.bomb(self.h_step) else: print("Wrong input, please try again") 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: print("You‘re safe. Keep moving!") map() return self.win() def live(self): if self.count <3: print("\033[31;01mNow you have %s live left.\033[0m" % (3 - self.count)) return self.win() else: print("Game over!") exit() def win(self): if self.h_step == self.v_step == 3: exit("\033[32;01mCongratulations! You win!!\033[0m") else: 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) You‘re now in (0, 0) How much do you want to move horizontally?0 How much do you want to move vertically?1 So, you‘re going to (0, 1) {0: 1} 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 (0, 1) How much do you want to move horizontally?1 How much do you want to move vertically?1 So, you‘re going to (1, 2) {0: 1, 1: 2} 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, 2) How much do you want to move horizontally?0 How much do you want to move vertically?-1 So, you‘re going to (1, 1) {0: 1, 1: 1} Boooom!!! You just stepped on a bomb Now you have 2 live left. 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 (2, 2) {0: 1, 1: 1, 2: 2} 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, 2) How much do you want to move horizontally?1 How much do you want to move vertically?1 So, you‘re going to (3, 3) {0: 1, 1: 1, 2: 2, 3: 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) Congratulations! You win!! Process finished with exit code 1
2019-10-04
02:19:12
原文地址:https://www.cnblogs.com/petitherisson/p/11620828.html
时间: 2024-11-13 15:25:03