模拟扫雷游戏

Posted petitherisson

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟扫雷游戏相关的知识,希望对你有一定的参考价值。

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

以上是关于模拟扫雷游戏的主要内容,如果未能解决你的问题,请参考以下文章

牛客~~扫雷~~~DFS+模拟

Java Swing扫雷游戏demo分享

基于C语言扫雷游戏的设计与实现

HDU 5965 扫雷 模拟 (中国大学生程序设计竞赛(合肥))

C语言实现扫雷OvO

我对扫雷的理解