用python写扫雷游戏实例代码分享

时间:2022-10-12 12:27:25

扫雷是一个非常经典的WIN游戏,我们教给大家用python语言来写出这个游戏,以下是全部实例代码:

?
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
#!/usr/bin/python
#coding:utf-8
 
#python 写的扫雷游戏
 
 
import sys
import random
 
class MineSweeping():
  #扫雷主程序
  def __init__(self,row = 8 ,line= 8,mineNum = 15):
    self.row = row
    self.line = line
    self.score = 0 #分数
    self.mineNum = mineNum
    self.xy_list = [[0 for i in range(self.line)] for i in range(self.row)]
 
  def initData(self):
    # 初始化状态值
    # 游戏开始的时候状态值为清零(再重新设置状态值)
    self.xy_list = [[0 for i in range(self.line)] for i in range(self.row)]
    # 设置雷的数量
    maxMine = self.mineNum
    while maxMine > 0 :
      num_x = random.randint(0,self.row-1)
      num_y = random.randint(0,self.line-1)
      if self.xy_list[num_x][num_y] == 0:
        self.xy_list[num_x][num_y] = 1
        maxMine -= 1
 
  #获取x坐标
  def get_pos(self,str_pos):
    #获取x坐标
    while 1:
      try:
        num_x = raw_input(str_pos)
        if int(num_x) in range(self.line) and num_x :
          break
        else:
          print u'输入无效值'
      except:
        pass
    return int(num_x)
 
  #进行扫雷
  def mine_clear(self,x,y):
    # 设置显示进行扫过的数目
    # 设置数字
    # 0 表示扫过的雷
    # 1 表示类
    # 2 表示扫过的类
 
    #获取坐标的数字
    pos = self.xy_list[x][y]
    if pos == 0 :
      self.xy_list[x][y] = 2
      return 0
    elif pos == 2 :
      return 2
    else:
      return 1
 
  #界面的显示
  def mineFace(self,state):
    #显示界面的内容
    #设置游戏的状态
    #1 表示运行的状态
    #2 表示输出的状态
    #3 表示游戏结束的状态
    #4 表示游戏获得了完胜
    if state == 1:
      print '+=================+'
      print '   Game start  '
      print '+=================+'
      tt = ' #'
      print '**************************'
      for i in range(self.line):
        str_t = ''
        for t in xrange(self.row):
          str_t += tt
        print "|%s|"%(str_t,)
      print '**************************'
      print 'Please input values of x,y(0-7):'
    #刷新用户界面
    if state == 2:
      tt = ' #'
      print '**************************'
      for i in range(self.line):
        str_t = ''
        for t in xrange(self.row):
          if self.xy_list[i][t] == 2:
            str_t += str(self.xy_list[i][t]).rjust(2)
          else:
            str_t += tt
        print "|%s|"%(str_t,)
      print '**************************'
    if state == 3:
      print '**************************'
      for i in range(self.line):
        str_t = ''
        for t in xrange(self.row):
          if int(self.xy_list[i][t]) != 1:
            str_t += ' 2'
          else:
            str_t += ' *'
        print "|%s|"%(str_t,)
      print '**************************'
 
    if state == 4:
      tt = ' #'
      print '**************************'
      for i in range(self.line):
        str_t = ''
        for t in xrange(self.row):
          if self.xy_list[i][t] == 2:
            str_t += str(self.xy_list[i][t]).rjust(2)
          else:
            str_t += ' @'
        print "|%s|"%(str_t,)
      print '**************************'
 
 
  def MainLoop(self):
    #创建游戏主循环
 
    #创建界面的运行
    self.mineFace(1)
    self.score = 0
    self.initData()
    #print self.xy_list
 
 
    # 进入主循环
    while 1:
      #获取坐标的位置
      x = self.get_pos(' X = ')
      y = self.get_pos(' Y = ')
      num = self.mine_clear(x,y)
      #判断是不过的了完胜
      win = True
      for i in self.xy_list:
        if 0 in i:
          win = False
          break
      if win:
        num = 4
 
      #执行刷新界面的函数
      if num == 0:
        self.mineFace(2)
        self.score += 10
      elif num == 2:
        print u'这个位置已经被排过了,证实没有雷'
      elif num == 1:
        print '+=================+'
        print '   Game over  '
        print '+=================+'
        print u'分数 : ', self.score
        self.mineFace(3)
        # 是不是进行下一句
        next = raw_input(u'是够进行下一局:Y or N ')
        if next.upper().startswith('Y'):
          print u'下一局开始'
          self.nextGame()
        else:
          print '>>> Game exit'
          break
      else:
        self.score += 10
        print u'恭喜您获得的完全的胜利'
        print u'分数 : ', self.score
        self.mineFace(4)
        next = raw_input(u'是够进行下一局:Y or N ')
        if next.upper().startswith('Y'):
          print u'下一局开始'
          self.nextGame()
        else:
          print '>>> Game exit'
          break
 
  # 下一局初始化信息
  def nextGame(self):
    self.mineFace(1)
    self.score = 0
    self.initData()
 
 
if __name__ == '__main__':
  mi = MineSweeping(10,10,20)
  mi.MainLoop()
  sys.exit()

如果大家在测试的时候还有任何疑问,可以在下方的留言区讨论,感谢大家对服务器之家的支持。