基于Python实现的扫雷游戏实例代码

 更新时间:2014年08月01日 10:49:37   投稿:shichen2014  
这篇文章主要介绍了基于Python实现的扫雷游戏实例代码,对于Python的学习以及Python游戏开发都有一定的借鉴价值,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

本文实例借鉴mvc模式,核心数据为model,维护1个矩阵,0表无雷,1表雷,-1表已经检测过。
本例使用python的tkinter做gui,由于没考虑可用性问题,因此UI比较难看,pygame更有趣更强大更好看,做这些小游戏更合适,感兴趣的读者可以尝试一下!

具体的功能代码如下:

# -*- coding: utf-8 -*-
import random
import sys
from Tkinter import *

class Model:
  """
  核心数据类,维护一个矩阵
  """
  def __init__(self,row,col):
    self.width=col
    self.height=row
    self.items=[[0 for c in range(col)] for r in range(row)]

  def setItemValue(self,r,c,value):
    """
    设置某个位置的值为value
    """
    self.items[r][c]=value;

  def checkValue(self,r,c,value):
    """
    检测某个位置的值是否为value
    """
    if self.items[r][c]!=-1 and self.items[r][c]==value:
      self.items[r][c]=-1 #已经检测过
      return True
    else:
      return False
    
  def countValue(self,r,c,value):
    """
    统计某个位置周围8个位置中,值为value的个数
    """
    count=0
    if r-1>=0 and c-1>=0:
      if self.items[r-1][c-1]==1:count+=1
    if r-1>=0 and c>=0:
      if self.items[r-1][c]==1:count+=1
    if r-1>=0 and c+1<=self.width-1:
      if self.items[r-1][c+1]==1:count+=1
    if c-1>=0:
      if self.items[r][c-1]==1:count+=1
    if c+1<=self.width-1 :
      if self.items[r][c+1]==1:count+=1
    if r+1<=self.height-1 and c-1>=0:
      if self.items[r+1][c-1]==1:count+=1
    if r+1<=self.height-1 :
      if self.items[r+1][c]==1:count+=1
    if r+1<=self.height-1 and c+1<=self.width-1:
      if self.items[r+1][c+1]==1:count+=1
    return count

  
class Mines(Frame):
  def __init__(self,m,master=None):
    Frame.__init__(self,master)
    self.model=m
    self.initmine()
    self.grid()
    self.createWidgets()

 
  
  def createWidgets(self):
    #top=self.winfo_toplevel()
    #top.rowconfigure(self.model.height*2,weight=1)
    #top.columnconfigure(self.model.width*2,weight=1)
    self.rowconfigure(self.model.height,weight=1)
    self.columnconfigure(self.model.width,weight=1)
    self.buttongroups=[[Button(self,height=1,width=2) for i in range(self.model.width)]
              for j in range(self.model.height)]
    for r in range(self.model.width):
      for c in range(self.model.height):
        self.buttongroups[r][c].grid(row=r,column=c)
        self.buttongroups[r][c].bind('<Button-1>',self.clickevent)
        self.buttongroups[r][c]['padx']=r
        self.buttongroups[r][c]['pady']=c

  def showall(self):
    for r in range(model.height):
      for c in range(model.width):
        self.showone(r,c)

  def showone(self,r,c):
    if model.checkValue(r,c,0):
      self.buttongroups[r][c]['text']=model.countValue(r,c,1)
    else:
      self.buttongroups[r][c]['text']='Mines'

  def recureshow(self,r,c):
    if 0<=r<=self.model.height-1 and 0<=c<=self.model.width-1:
      if model.checkValue(r,c,0) and model.countValue(r,c,1)==0:
        self.buttongroups[r][c]['text']=''
        self.recureshow(r-1,c-1)
        self.recureshow(r-1,c)
        self.recureshow(r-1,c+1)
        self.recureshow(r,c-1)
        self.recureshow(r,c+1)
        self.recureshow(r+1,c-1)
        self.recureshow(r+1,c)
        self.recureshow(r+1,c+1)
      elif model.countValue(r,c,1)!=0:
        self.buttongroups[r][c]['text']=model.countValue(r,c,1)
    else:
      pass
        
      
  def clickevent(self,event):
    """
    点击事件
    case 1:是雷,所有都显示出来,游戏结束
    case 2:是周围雷数为0的,递归触发周围8个button的点击事件
    case 3:周围雷数不为0的,显示周围雷数
    """
    r=int(str(event.widget['padx']))
    c=int(str(event.widget['pady']))
    if model.checkValue(r,c,1):#是雷
      self.showall()
    else:#不是雷
      self.recureshow(r,c)
    
    
  def initmine(self):
    """
    埋雷,每行埋height/width+2个暂定
    """
    r=random.randint(1,model.height/model.width+2)
    for r in range(model.height):
      for i in range(2):
        rancol=random.randint(0,model.width-1)
        model.setItemValue(r,rancol,1)

  
  def printf(self):
    """
    打印
    """
    for r in range(model.height):
      for c in range(model.width):
        print model.items[r][c],
      print '/n'
      

def new(self):
  """
  重新开始游戏
  """
  pass

if __name__=='__main__':
  model=Model(10,10)
  root=Tk()
  
  #menu
  menu = Menu(root)
  root.config(menu=menu)
  filemenu = Menu(menu)
  menu.add_cascade(label="File", menu=filemenu)
  filemenu.add_command(label="New",command=new)
  filemenu.add_separator()
  filemenu.add_command(label="Exit", command=root.quit)

  #Mines
  m=Mines(model,root)
  #m.printf()
  root.mainloop()

相关文章

  • Python数据传输黏包问题

    Python数据传输黏包问题

    这篇文章主要介绍了Python数据传输黏包问题,黏包指数据与数据之间没有明确的分界线,导致不能正确的读取数据,更多相关内容需要的小伙伴可以参考一下
    2022-04-04
  • python使用电子邮件模块smtplib的方法

    python使用电子邮件模块smtplib的方法

    这篇文章主要介绍了python使用电子邮件模块smtplib的方法,需要的朋友可以参考下
    2016-08-08
  • python命令行模式的用法及流程

    python命令行模式的用法及流程

    在本篇文章里小编给大家整理的是一篇关于python命令行模式的用法及流程相关内容,有兴趣的朋友们可以跟着学习下。
    2021-09-09
  • python smtplib发送带附件邮件小程序

    python smtplib发送带附件邮件小程序

    这篇文章主要为大家详细介绍了python smtplib发送带附件邮件小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • python对两个数组进行合并排列处理的两种方法

    python对两个数组进行合并排列处理的两种方法

    最近遇到数组合并问题,以此记录解决方法,供大家参考学习,下面这篇文章主要给大家介绍了关于python对两个数组进行合并排列处理的两种方法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-05-05
  • Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能示例

    Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能示例

    这篇文章主要介绍了Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能,涉及Python基于re模块的正则匹配及selenium模块的页面抓取等相关操作技巧,需要的朋友可以参考下
    2018-07-07
  • Python yield 的使用浅析

    Python yield 的使用浅析

    这篇文章主要为大家详细介绍了Python yield的使用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • Python多线程编程threading模块使用最佳实践及常见问题解析

    Python多线程编程threading模块使用最佳实践及常见问题解析

    这篇文章主要为大家介绍了Python多线程编程threading模块使用最佳实践及常见问题解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • Python中的Django基本命令实例详解

    Python中的Django基本命令实例详解

    这篇文章主要介绍了Python之Django基本命令 ,需要的朋友可以参考下
    2018-07-07
  • python破解WiFi教程代码,Python蹭网原理讲解

    python破解WiFi教程代码,Python蹭网原理讲解

    用Python生成一个简单的密码本,一般是有数字、字母和符号组成,这里用到的思路主要是穷举法。通过使用pywifi?模块,根据密码本暴力破解WiFi。本文只是从技术的角度来阐述学习Pywifi库!并不建议大家做任何破坏性的操作和任何不当的行为!
    2023-01-01

最新评论

?


http://www.vxiaotou.com