码迷,mamicode.com
首页 > 其他好文 > 详细

模拟扫雷游戏

时间:2019-10-03 22:08:00      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:point   bom   import   wrong   ini   horizon   ret   def   就是   

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, lets do this!

                     (3, 3)
        o -- o -- o -- W
        |    |    |    |
        o -- o -- o -- o
        |    |    |    |
        o -- o -- o -- o
        |    |    |    |
        B -- o -- o -- o
     (0, 0)
    
Youre now in (0, 0)
How much do you want to move horizontally?0
How much do you want to move vertically?1
So, youre going to (0, 1)
{0: 1}
Youre safe. Keep moving!

                     (3, 3)
        o -- o -- o -- W
        |    |    |    |
        o -- o -- o -- o
        |    |    |    |
        o -- o -- o -- o
        |    |    |    |
        B -- o -- o -- o
     (0, 0)
    
Youre now in (0, 1)
How much do you want to move horizontally?1
How much do you want to move vertically?1
So, youre going to (1, 2)
{0: 1, 1: 2}
Youre safe. Keep moving!

                     (3, 3)
        o -- o -- o -- W
        |    |    |    |
        o -- o -- o -- o
        |    |    |    |
        o -- o -- o -- o
        |    |    |    |
        B -- o -- o -- o
     (0, 0)
    
Youre now in (1, 2)
How much do you want to move horizontally?0
How much do you want to move vertically?-1
So, youre going to (1, 1)
{0: 1, 1: 1}
Boooom!!!
You just stepped on a bomb
Now you have 2 live left.
Youre now in (1, 1)
How much do you want to move horizontally?1
How much do you want to move vertically?1
So, youre going to (2, 2)
{0: 1, 1: 1, 2: 2}
Youre safe. Keep moving!

                     (3, 3)
        o -- o -- o -- W
        |    |    |    |
        o -- o -- o -- o
        |    |    |    |
        o -- o -- o -- o
        |    |    |    |
        B -- o -- o -- o
     (0, 0)
    
Youre now in (2, 2)
How much do you want to move horizontally?1
How much do you want to move vertically?1
So, youre going to (3, 3)
{0: 1, 1: 1, 2: 2, 3: 3}
Youre 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

模拟扫雷游戏

标签:point   bom   import   wrong   ini   horizon   ret   def   就是   

原文地址:https://www.cnblogs.com/petitherisson/p/11620828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!