栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 教学

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 教学

第四章 项目练习 第三节 扫雷游戏

1. 下载easygui库。

python2这样下载:

pip install easygui

python3这样下载:

pip3 install easygui

2. 学习easygui库基本知识。

(1)msgbox(文字,框标题【可以不填】),消息框。

例如下列代码:

import easygui as g
g.msgbox("今天我们来做扫雷游戏",title = "猫咪扫雷游戏V1.0")

(2)choicebox(提示语,框标题【可以不填】,选项),单选框,把结果赋值给一个变量,就知道你选的是什么了。

例如下列代码:

import easygui as g
a = g.choicebox("今天天气怎么样?",title = "天气调查",choices = ["晴天","多云"])
print(a)

(3)enterbox(提示语,框标题【可以不填】),填空框,把结果赋值给一个变量,就知道你选的是什么了。

例如下列代码:

import easygui as g
a = g.enterbox("今天天气怎么样?",title = "天气调查")
print(a)

3. 写好变量、列表、导入库。

其中定义五个级别难度不同雷被选中的概率。

import random
import time
import easygui as g
ls_fu = []
ls = []
your = []
lei = ["雷","好","好","好","好"]
lei2 = ["雷","好","好","好"]
lei3 = ["雷","好","好"]
lei4 = ["雷","好"]
lei5 = ["雷","雷","好"]

4. 雷类——init函数——初始化。

把列表清空,设置难度。

class Lei():
    def __init__(self,moshi,moshima):
        global ls_fu
        global lei
        global ls
        global your
        ls.clear()
        ls_fu.clear()
        your.clear()
        self.moshi = moshi
        self.moshima = int(moshima * 1.5)

5. 雷类——init函数——遍历选雷。

根据不同的难度选不同数量的雷。

import random
import time
import easygui as g
ls_fu = []
ls = []
your = []
lei = ["雷","好","好","好","好"]
lei2 = ["雷","好","好","好"]
lei3 = ["雷","好","好"]
lei4 = ["雷","好"]
lei5 = ["雷","雷","好"]
class Lei():
    def __init__(self,moshi,moshima):
        global ls_fu
        global lei
        global ls
        global your
        ls.clear()
        ls_fu.clear()
        your.clear()
        self.moshi = moshi
        self.moshima = int(moshima * 1.5)
        for n in range(int(moshima * 1.5)):
            m = []
            p = []
            for n in range(int(moshima * 1.5)):
                if moshi == "easy":
                    a = random.choice(lei)
                elif moshi == "yiban":
                    a = random.choice(lei2)
                elif moshi == "zhongdeng":
                    a = random.choice(lei3)
                elif moshi == "jiaonan":
                    a = random.choice(lei4)
                elif moshi == "zhuanjia":
                    a = random.choice(lei5)
                else:
                    a = random.choice(lei)
                m.append(a)
                p.append("?")
            ls_fu.append(m)
            your.append(p)
            ls.append(p)

        self.__get_lei()

6. 雷类——__get_lei()函数——进行雷列表的初始化。

进行数雷,看看这个地方四周有多少个雷。

class Lei():
    def __init__(self,moshi,moshima):
        global ls_fu
        global lei
        global ls
        global your
        ls.clear()
        ls_fu.clear()
        your.clear()
        self.moshi = moshi
        self.moshima = int(moshima * 1.5)
        for n in range(int(moshima * 1.5)):
            m = []
            p = []
            for n in range(int(moshima * 1.5)):
                if moshi == "easy":
                    a = random.choice(lei)
                elif moshi == "yiban":
                    a = random.choice(lei2)
                elif moshi == "zhongdeng":
                    a = random.choice(lei3)
                elif moshi == "jiaonan":
                    a = random.choice(lei4)
                elif moshi == "zhuanjia":
                    a = random.choice(lei5)
                else:
                    a = random.choice(lei)
                m.append(a)
                p.append("?")
            ls_fu.append(m)
            your.append(p)
            ls.append(p)

        self.__get_lei()

    def __get_lei(self):
        global ls
        global ls_fu
        global your
        for i in range(len(ls_fu)):
            for j in range(len(ls_fu)):
                new_ls = []
                for k in range(i - 1, i + 2):
                    for k2 in range(j - 1, j + 2):
                        if self.is_hefa(k,k2):
                            new_ls.append(ls_fu[k - 1][k2 - 1])
                if ls_fu[i][j] == "雷":
                    ls[i][j] = "b"
                else:
                    x = new_ls.count("雷")
                    ls[i][j] = x
        for i in range(len(ls)):
            for j in range(len(ls)):
                ls[i][j] = str(ls[i][j])
        for i in range(len(ls_fu)):
            for j in range(len(ls_fu)):
                ls_fu[i][j] = str(ls_fu[i][j])
        your.clear()
        for n in range(self.moshima):
            a = []
            for i in range(self.moshima):
                a.append("?")
            your.append(a)

7. 清空列表、继续函数。

继续就return True就行了,清空列表就是用clear把列表清空。

import random
import time
import easygui as g
ls_fu = []
ls = []
your = []
lei = ["雷","好","好","好","好"]
lei2 = ["雷","好","好","好"]
lei3 = ["雷","好","好"]
lei4 = ["雷","好"]
lei5 = ["雷","雷","好"]
class Lei():
    def __init__(self,moshi,moshima):
        global ls_fu
        global lei
        global ls
        global your
        ls.clear()
        ls_fu.clear()
        your.clear()
        self.moshi = moshi
        self.moshima = int(moshima * 1.5)
        for n in range(int(moshima * 1.5)):
            m = []
            p = []
            for n in range(int(moshima * 1.5)):
                if moshi == "easy":
                    a = random.choice(lei)
                elif moshi == "yiban":
                    a = random.choice(lei2)
                elif moshi == "zhongdeng":
                    a = random.choice(lei3)
                elif moshi == "jiaonan":
                    a = random.choice(lei4)
                elif moshi == "zhuanjia":
                    a = random.choice(lei5)
                else:
                    a = random.choice(lei)
                m.append(a)
                p.append("?")
            ls_fu.append(m)
            your.append(p)
            ls.append(p)

        self.__get_lei()

    def __get_lei(self):
        global ls
        global ls_fu
        global your
        for i in range(len(ls_fu)):
            for j in range(len(ls_fu)):
                new_ls = []
                for k in range(i - 1, i + 2):
                    for k2 in range(j - 1, j + 2):
                        if self.is_hefa(k,k2):
                            new_ls.append(ls_fu[k - 1][k2 - 1])
                if ls_fu[i][j] == "雷":
                    ls[i][j] = "b"
                else:
                    x = new_ls.count("雷")
                    ls[i][j] = x
        for i in range(len(ls)):
            for j in range(len(ls)):
                ls[i][j] = str(ls[i][j])
        for i in range(len(ls_fu)):
            for j in range(len(ls_fu)):
                ls_fu[i][j] = str(ls_fu[i][j])
        your.clear()
        for n in range(self.moshima):
            a = []
            for i in range(self.moshima):
                a.append("?")
            your.append(a)
    def new_clear(self):
        ls_fu.clear()
        ls.clear()
        your.clear()
    
    def jixu(self):
        return True

8. 查看是否有这个位置。

数据跟长、宽进行比较即可。

import random
import time
import easygui as g
ls_fu = []
ls = []
your = []
lei = ["雷","好","好","好","好"]
lei2 = ["雷","好","好","好"]
lei3 = ["雷","好","好"]
lei4 = ["雷","好"]
lei5 = ["雷","雷","好"]
class Lei():
    def __init__(self,moshi,moshima):
        global ls_fu
        global lei
        global ls
        global your
        ls.clear()
        ls_fu.clear()
        your.clear()
        self.moshi = moshi
        self.moshima = int(moshima * 1.5)
        for n in range(int(moshima * 1.5)):
            m = []
            p = []
            for n in range(int(moshima * 1.5)):
                if moshi == "easy":
                    a = random.choice(lei)
                elif moshi == "yiban":
                    a = random.choice(lei2)
                elif moshi == "zhongdeng":
                    a = random.choice(lei3)
                elif moshi == "jiaonan":
                    a = random.choice(lei4)
                elif moshi == "zhuanjia":
                    a = random.choice(lei5)
                else:
                    a = random.choice(lei)
                m.append(a)
                p.append("?")
            ls_fu.append(m)
            your.append(p)
            ls.append(p)

        self.__get_lei()

    def __get_lei(self):
        global ls
        global ls_fu
        global your
        for i in range(len(ls_fu)):
            for j in range(len(ls_fu)):
                new_ls = []
                for k in range(i - 1, i + 2):
                    for k2 in range(j - 1, j + 2):
                        if self.is_hefa(k,k2):
                            new_ls.append(ls_fu[k - 1][k2 - 1])
                if ls_fu[i][j] == "雷":
                    ls[i][j] = "b"
                else:
                    x = new_ls.count("雷")
                    ls[i][j] = x
        for i in range(len(ls)):
            for j in range(len(ls)):
                ls[i][j] = str(ls[i][j])
        for i in range(len(ls_fu)):
            for j in range(len(ls_fu)):
                ls_fu[i][j] = str(ls_fu[i][j])
        your.clear()
        for n in range(self.moshima):
            a = []
            for i in range(self.moshima):
                a.append("?")
            your.append(a)
    def new_clear(self):
        ls_fu.clear()
        ls.clear()
        your.clear()
    
    def jixu(self):
        return True
    
    def is_hefa(self,x,y):
        if x > self.moshima or y > self.moshima or x <= 0 or y <= 0:
            return False
        else:
            return True

9. 转化模式码函数。

根据不同的难度选不同长度的列表。

import random
import time
import easygui as g
ls_fu = []
ls = []
your = []
lei = ["雷","好","好","好","好"]
lei2 = ["雷","好","好","好"]
lei3 = ["雷","好","好"]
lei4 = ["雷","好"]
lei5 = ["雷","雷","好"]
class Lei():
    def __init__(self,moshi,moshima):
        global ls_fu
        global lei
        global ls
        global your
        ls.clear()
        ls_fu.clear()
        your.clear()
        self.moshi = moshi
        self.moshima = int(moshima * 1.5)
        for n in range(int(moshima * 1.5)):
            m = []
            p = []
            for n in range(int(moshima * 1.5)):
                if moshi == "easy":
                    a = random.choice(lei)
                elif moshi == "yiban":
                    a = random.choice(lei2)
                elif moshi == "zhongdeng":
                    a = random.choice(lei3)
                elif moshi == "jiaonan":
                    a = random.choice(lei4)
                elif moshi == "zhuanjia":
                    a = random.choice(lei5)
                else:
                    a = random.choice(lei)
                m.append(a)
                p.append("?")
            ls_fu.append(m)
            your.append(p)
            ls.append(p)

        self.__get_lei()

    def __get_lei(self):
        global ls
        global ls_fu
        global your
        for i in range(len(ls_fu)):
            for j in range(len(ls_fu)):
                new_ls = []
                for k in range(i - 1, i + 2):
                    for k2 in range(j - 1, j + 2):
                        if self.is_hefa(k,k2):
                            new_ls.append(ls_fu[k - 1][k2 - 1])
                if ls_fu[i][j] == "雷":
                    ls[i][j] = "b"
                else:
                    x = new_ls.count("雷")
                    ls[i][j] = x
        for i in range(len(ls)):
            for j in range(len(ls)):
                ls[i][j] = str(ls[i][j])
        for i in range(len(ls_fu)):
            for j in range(len(ls_fu)):
                ls_fu[i][j] = str(ls_fu[i][j])
        your.clear()
        for n in range(self.moshima):
            a = []
            for i in range(self.moshima):
                a.append("?")
            your.append(a)
    def new_clear(self):
        ls_fu.clear()
        ls.clear()
        your.clear()
    
    def jixu(self):
        return True
    
    def is_hefa(self,x,y):
        if x > self.moshima or y > self.moshima or x <= 0 or y <= 0:
            return False
        else:
            return True

def turn_moshima(moshi):
    if moshi == "easy":
        return 4
    elif moshi == "yiban":
        return 6
    elif moshi == "zhongdeng":
        return 8
    elif moshi == "jiaonan":
        return 10
    elif moshi == "zhuanjia":
        return 12
    else:
        return 4

10. 主函数。

自行设计主函数。

11. 样例代码:

import random
import time
import easygui as g
ls_fu = []
ls = []
your = []
lei = ["雷","好","好","好","好"]
lei2 = ["雷","好","好","好"]
lei3 = ["雷","好","好"]
lei4 = ["雷","好"]
lei5 = ["雷","雷","好"]
class Lei():
    def __init__(self,moshi,moshima):
        global ls_fu
        global lei
        global ls
        global your
        ls.clear()
        ls_fu.clear()
        your.clear()
        self.moshi = moshi
        self.moshima = int(moshima * 1.5)
        for n in range(int(moshima * 1.5)):
            m = []
            p = []
            for n in range(int(moshima * 1.5)):
                if moshi == "easy":
                    a = random.choice(lei)
                elif moshi == "yiban":
                    a = random.choice(lei2)
                elif moshi == "zhongdeng":
                    a = random.choice(lei3)
                elif moshi == "jiaonan":
                    a = random.choice(lei4)
                elif moshi == "zhuanjia":
                    a = random.choice(lei5)
                else:
                    a = random.choice(lei)
                m.append(a)
                p.append("?")
            ls_fu.append(m)
            your.append(p)
            ls.append(p)

        self.__get_lei()

    def __get_lei(self):
        global ls
        global ls_fu
        global your
        for i in range(len(ls_fu)):
            for j in range(len(ls_fu)):
                new_ls = []
                for k in range(i - 1, i + 2):
                    for k2 in range(j - 1, j + 2):
                        if self.is_hefa(k,k2):
                            new_ls.append(ls_fu[k - 1][k2 - 1])
                if ls_fu[i][j] == "雷":
                    ls[i][j] = "b"
                else:
                    x = new_ls.count("雷")
                    ls[i][j] = x
        for i in range(len(ls)):
            for j in range(len(ls)):
                ls[i][j] = str(ls[i][j])
        for i in range(len(ls_fu)):
            for j in range(len(ls_fu)):
                ls_fu[i][j] = str(ls_fu[i][j])
        your.clear()
        for n in range(self.moshima):
            a = []
            for i in range(self.moshima):
                a.append("?")
            your.append(a)
    def new_clear(self):
        ls_fu.clear()
        ls.clear()
        your.clear()
    
    def jixu(self):
        return True
    
    def is_hefa(self,x,y):
        if x > self.moshima or y > self.moshima or x <= 0 or y <= 0:
            return False
        else:
            return True

def turn_moshima(moshi):
    if moshi == "easy":
        return 4
    elif moshi == "yiban":
        return 6
    elif moshi == "zhongdeng":
        return 8
    elif moshi == "jiaonan":
        return 10
    elif moshi == "zhuanjia":
        return 12
    else:
        return 4
    
def main():
    global ls
    global your
    global ls_fu
    count2 = 0
    while True:
        g.msgbox('''你可以
                    1. 开启新的游戏
                    2. 继续上一次的游戏
                    3. 退出''',title = "猫咪扫雷游戏V1.0")
        choices = ["开启新的游戏","继续上一次的游戏","退出"]
        a = g.choicebox("请选择一个",title = "猫咪扫雷游戏V1.0",choices = choices)
        if a == "开启新的游戏":
            b = g.enterbox("请输入模式:easy,yiban,zhongdeng,jiaonan,zhuanjia",title = "猫咪扫雷游戏V1.0")
            a5 = time.time()
            c = turn_moshima(b)
            m = Lei(b,c)

            while True:
                count = 0
                for i in ls:
                    for j in i:
                        if j == "b":
                            count += 1
                if count2 == count:
                    a6 = time.time()
                    string = "你赢了!你用了" + str(a6 - a5) + "秒."
                    g.msgbox(string,title = "猫咪扫雷游戏V1.0")
                    break
                string = "一共有" + str(count) + "个雷."
                g.msgbox(string,title = "猫咪扫雷游戏V1.0")
                g.msgbox("游戏开始!",title = "猫咪扫雷游戏V1.0")
                g.msgbox('''你可以
                            1. 标注雷(b)
                            2. 查看一个位置
                            3. 退出''')
                choices = ["标注雷(b)","查看一个位置","退出"]
                a = g.choicebox("请选择一个:",title = "猫咪扫雷游戏V1.0",choices = choices)
                if a == "标注雷(b)":
                    b = g.enterbox("请输入行号:",title = "猫咪扫雷游戏V1.0")
                    b = int(b)
                    c = g.enterbox("请输入列号:",title = "猫咪扫雷游戏V1.0")
                    c = int(c)
                    if m.is_hefa(b,c):
                        g.msgbox("输入成功!",title = "猫咪扫雷游戏V1.0")
                        your[b - 1][c - 1] = "b"
                        string = "你的列表变更为:n"
                        for n in your:
                            for i in n:
                                string += i
                            string += "n"
                        g.msgbox(string,title = "猫咪扫雷游戏V1.0")
                        if ls[b - 1][c - 1] == "b":
                            g.msgbox("标注正确!",title = "猫咪扫雷游戏V1.0")
                            count2 += 1
                        else:
                            g.msgbox("标注错误!",title = "猫咪扫雷游戏V1.0")
                            your[b - 1][c - 1] = "?"
                    else:
                        g.msgbox("输入失败!",title = "猫咪扫雷游戏V1.0")
                elif a == "查看一个位置":
                    b = g.enterbox("请输入行号:",title = "猫咪扫雷游戏V1.0")
                    b = int(b)
                    c = g.enterbox("请输入列号:",title = "猫咪扫雷游戏V1.0")
                    c = int(c)
                    if m.is_hefa(b,c):
                        g.msgbox("输入成功!",title = "猫咪扫雷游戏V1.0")
                        if not ls[b - 1][c - 1] == "b":
                            your[b - 1][c - 1] = ls[b - 1][c - 1]
                            string = "你的列表变更为:n"
                            for n in your:
                                for i in n:
                                    string += i
                                string += "n"
                            g.msgbox(string,title = "猫咪扫雷游戏V1.0")
                        else:
                            g.msgbox("你被炸弹炸晕了,游戏结束!",title = "猫咪扫雷游戏V1.0")
                            break
                    else:
                        g.msgbox("输入失败!",title = "猫咪扫雷游戏V1.0")
                elif a == "退出":
                    g.msgbox("您退出了这局扫雷游戏!",title = "猫咪扫雷游戏V1.0")
                    break
                else:
                    g.msgbox("指令无效!",title= "猫咪扫雷游戏V1.0")
                    
        elif a == "继续上一次的游戏" and not len(ls) == 0:
            g.msgbox("继续游戏!",title = "猫咪扫雷游戏V1.0")
            while True:
                if count2 == count:
                    g.msgbox("你赢了!",title = "猫咪扫雷游戏V1.0")
                    break
                count = 0
                for i in ls:
                    for j in i:
                        if j == "b":
                            count += 1
                string = "一共有" + str(count) + "个雷."
                g.msgbox(string,title = "猫咪扫雷游戏V1.0")
                g.msgbox("游戏开始!",title = "猫咪扫雷游戏V1.0")
                g.msgbox('''你可以
                            1. 标注雷(b)
                            2. 查看一个位置
                            3. 退出''',title = "猫咪扫雷游戏V1.0")
                
                choices = ["标注雷(b)","查看一个位置","退出"]
                a = g.choicebox("请选择一个:",title = "猫咪扫雷游戏V1.0",choices = choices)
                
                if a == "标注雷(b)":
                    b = g.enterbox("请输入行号:",title = "猫咪扫雷游戏V1.0")
                    b = int(b)
                    c = g.enterbox("请输入列号:",title = "猫咪扫雷游戏V1.0")
                    c = int(c)
                    if m.is_hefa(b,c):
                        g.msgbox("输入成功!",title = "猫咪扫雷游戏V1.0")
                        your[b - 1][c - 1] = "b"
                        string = "你的列表变更为:n"
                        for n in your:
                            for i in n:
                                string += i
                            string += "n"
                        g.msgbox(string,title = "猫咪扫雷游戏V1.0")
                        if ls[b - 1][c - 1] == "b":
                            g.msgbox("标注正确!",title = "猫咪扫雷游戏V1.0")
                            count2 += 1
                        else:
                            g.msgbox("标注错误!",title = "猫咪扫雷游戏V1.0")
                            your[b - 1][c - 1] = "?"
                    else:
                        g.msgbox("输入失败!",title = "猫咪扫雷游戏V1.0")
                elif a == "查看一个位置":
                    b = g.enterbox("请输入行号:",title = "猫咪扫雷游戏V1.0")
                    b = int(b)
                    c = g.enterbox("请输入列号:",title = "猫咪扫雷游戏V1.0")
                    c = int(c)
                    if m.is_hefa(b,c):
                        g.msgbox("输入成功!",title = "猫咪扫雷游戏V1.0")
                        if not ls[b - 1][c - 1] == "b":
                            your[b - 1][c - 1] = ls[b - 1][c - 1]
                            string = "你的列表变更为:n"
                            for n in your:
                                for i in n:
                                    string += i
                                string += "n"
                            g.msgbox(string,title = "猫咪扫雷游戏V1.0")
                        else:
                            g.msgbox("你被炸弹炸晕了,游戏结束!",title = "猫咪扫雷游戏V1.0")
                            break
                    else:
                        g.msgbox("输入失败!",title = "猫咪扫雷游戏V1.0")
                elif a == "3":
                    g.msgbox("您退出了这局扫雷游戏!",title = "猫咪扫雷游戏V1.0")
                    break
                else:
                    g.msgbox("指令无效!",title = "猫咪扫雷游戏V1.0")
        elif a == "退出":
            g.msgbox("您退出了扫雷游戏,游戏结束了!",title = "猫咪扫雷游戏V1.0")
            break
        else:
            g.msgbox("指令错误!",title = "猫咪扫雷游戏V1.0")
            
if __name__ == "__main__":
    main()

这节课我们就讲到这里,下节课我们来讲讲模拟日食代码。

下次更新时间为6月8日或6月9日。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/958173.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号