其他分享
首页 > 其他分享> > 2021-04-22

2021-04-22

作者:互联网

转自https://blog.csdn.net/qq_42847252/article/details/83153696

但是这位大佬的文件不能直接运行,至于音频文件我是在网上找的,整理大佬的代码大概画了四个小时,文章末尾给了压缩包百度云链接(解压后安装对应的库就可以直接运行了),

希望你像我一样能从这些代码中有所收获。

这里还是贴一下代码

start.py

# 退出的命令没有
# 失败后应该暂停界面


import easygui as g
import sys
import choose
import traceback
# import cv2
import Game


n=0
map1=[]
map2=[]
account_mini = 0
account_list = []

# 登录函数,用户名已注册为实现
def sign_up(account_list):
    global n, map1, map2, account_mini
    fields = ('用户名:', '密码:')
    msg = '请输入用户名和密码(不输入将为游客登陆)'
    title = '登录'
    users = g.multpasswordbox(msg, title, fields)

    if users==None:
        return "取消登陆"

    elif users == ['', '']:
        button_choices=g.buttonbox('由于您未注册,系统将不会保存您的游戏缓存!!!','游客', choices=('确定','注册'))
        if button_choices=='确定':
            return 2
        elif button_choices=='注册':
            return 1

    else:
        # 将用户名读取在list1中
        list1 = []
        account = open('account.txt','r+')
        for each_line in account:
            (account_, enterkey) = each_line.split('\n')
            list1.append(account_)
        account.close()

        # 将密码读取在list2中
        list2 = []
        password = open('password.txt','r+')
        for each_line in password:
            (password_, enter_key) = each_line.split('\n')
            list2.append(password_)
        password.close()

        # 确认用户名和密码是否存在并且匹配
        for X in list1:
            if X == str(users[0]) and list2[list1.index(X)] != str(users[1]):
                g.msgbox('密码错误,请重新输入!', ok_button='确定 ')
                return 0
                
            elif X == str(users[0]) and list2[list1.index(X)] == str(users[1]):
                g.msgbox(str(users[0]) + '你好,欢迎来到扫雷!', ok_button='进入 ')
                account_mini=list1.index(X)
                return 2
                
        if str(users[0]) not in list1:
            g.msgbox('账号不存在,请注册:', ok_button='确定 ')
            return 1


# 注册函数
def function_register():

    values = []

    def _register():
        msg = '*为必填项'
        title = '账号中心'
        fields = ['*用户名', '*密码']
        return g.multenterbox(msg, title, fields, values)

    users_register = _register()

    if users_register == None:
        return "取消注册"

    else:
        while users_register[0] == '' or users_register[1] == '':
            g.msgbox('用户名或密码不能为空!', ok_button='继续填写 ')
            values = [users_register[0], users_register[1], users_register[2]]
            users_register = _register()

        # 检验用户名是否被使用
        list3 = []
        account = open('account.txt','r+')

        for each_line in account:
            (account_, enter_key) = each_line.split('\n')
            list3.append(account_)
        account.close()

        while str(users_register[0]) in list3:
            g.msgbox('该用户名已被使用!', ok_button='重新输入 ')
            users_register = _register()

        # 将账号密码分别存储在两个txt文件内
        account = open('account.txt', 'a')
        account.write(users_register[0] + '\n')
        account.close()
        password = open('password.txt', 'a')
        password.write(users_register[1] + '\n')
        password.close()
        Map1_s = open('map1_s.txt', 'a')
        Map1_s.write('1101111921910191112922011100119210111000393119100039911121109322111921121291112912922210119211191000' + '\n')
        Map1_s.close()
        Map2_s = open('map2_s.txt', 'a')
        Map2_s.write('0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' + '\n')
        Map2_s.close()
        N_s = open('N_s.txt', 'a')
        N_s.write('10' + '\n')
        N_s.close()
        # 将账号密码分别存储在两个txt文件内

        return 0


def main():
    # 创建两个txt临时文件,分别用于存放用户名和密码
    account = open('account.txt', 'a')
    password = open('password_s.txt', 'a')
    Map1_s = open('map1_s.txt', 'a')
    Map2_s = open('map2_s.txt', 'a')
    N_s = open('N_s.txt', 'a')
    account.close()
    password.close()
    Map1_s.close()
    Map2_s.close()
    N_s.close()

    
    
    while True:
        
        account_list=[]
        account_s=open('account.txt','r')

        for each_line in account_s:
            (account_, enter_key) = each_line.split('\n')
            account_list.append(account_)
        account_s.close()

        choices = ['已有账号,直接登录', '开始注册']
        choice = 0
        choice = g.indexbox('登录/注册:', '请选择:', choices=choices)
        
        #登陆
        if choice == 0:
            choice = sign_up(account_list)
            
            if choice=="取消登陆":
                continue

        # 注册
        if choice == 1:
            choice = function_register()

            if choice == 0:
                # 注册成功重新切入登录页面
                account_list=[]
                account_s=open('account.txt','r')
                for each_line in account_s:
                    (account_, enter_key) = each_line.split('\n')
                account_list.append(account_)
                account_s.close()
                choice = sign_up(account_list)
            elif choice=="取消注册":
                continue

        if choice == 2:
            choice = g.indexbox('开始/继续:', '请选择:', choices=['重新开始','继续游戏'])
            if choice == 0:
                choose.main(account_mini)
            elif choice==1:
                list3 = []
                N = open('N_s.txt','r')
                for each_line in N:
                    (password_, enter_key) = each_line.split('\n')
                    list3.append(password_)
                N.close()

                n=int(list3[account_mini])

                list4=[]
                Map1 = open('map1_s.txt','r')
                for each_line in Map1:
                    (password_, enter_key) = each_line.split('\n')
                    list4.append(password_)
                Map1.close()
                map=list4[account_mini]
                for i in range(n):
                    map1.append(list(map[i*n:(i+1)*n]))

                list5=[]
                Map2 = open('map2_s.txt','r')
                for each_line in Map2:
                    (password_, enter_key) = each_line.split('\n')
                    list5.append(password_)
                Map2.close()
                map=list5[account_mini]
                for i in range(n):
                    map2.append(list(map[i*n:(i+1)*n]))

                for i in range(n):
                    for j in range(n):
                        map1[i][j]=int(map1[i][j])
                        map2[i][j]=int(map2[i][j])
                
                Game.main(n,map1,map2,account_mini)


if __name__ == "__main__":
	try:
	    main()
	except SystemExit:
	    pass
	except:
	    traceback.print_exc()
	    input()

 

 

Game.py 

import numpy as np
import pygame
import sys
import traceback
import random
import choose
from pygame.locals import *


pygame.init()
pygame.mixer.init()
button_sound = pygame.mixer.Sound("button.wav")
button_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("bomb.wav")
bomb_sound.set_volume(0.2)

#绘制地图
def draw_map(screen,N):
    screen.fill((237,237,237))
    for i in range(N):
        for j in range(N):
            pygame.draw.rect(screen,(0,0,0),[i*30,j*30,29,29],1)

#翻开数字
def draw_sort(screen,x,y,N,map1,map2):

    #定义字体跟大小
    #s_font=pygame.font.Font('汉仪细行楷-1.0.0.7.ttf',19)
    # 这是用的系统自带的楷体
    s_font=pygame.font.SysFont('arial', 19)


    #定义类容,是否抗锯齿,颜色
    if map1[x][y]==0:
        color=(86,98,166)
    elif map1[x][y]==1:
        color=(67,106,62)
    elif map1[x][y]==2:
        color=(15,170,209)
    else:
        color=(222,29,90)
    s_text=s_font.render(str(map1[x][y]),True,color)

    #将字放在窗口指定位置
    screen.blit(s_text,(x*30+9,y*30+3))
    if map2[x][y]==2:
        map2[x][y]=1
        Clean_select(screen,x,y,map2)
    map2[x][y]=1
    pygame.display.flip()

#翻开雷
def draw_bomb(screen,map1,N,account_mini):
    bomb=pygame.image.load("bomb.png").convert_alpha()
    for i in range(N):
        for j in range(N):
            if map1[i][j]>=9:
                screen.blit(bomb,(30*i+1,30*j+1))
    bomb_sound.play(0)
    pygame.display.flip()
    pygame.time.wait(5000)
    choose.main(account_mini,"You Failed !!")


#绘制键盘选中项
def draw_select(screen,x,y):
    pygame.draw.rect(screen,(0,0,0),[x*30,y*30,29,29],3)
    pygame.display.flip()


#清除选中项,标记项
def Clean_select(screen,x,y,map2):
    pygame.draw.rect(screen,(237,237,237),[x*30,y*30,29,29],3)
    pygame.draw.rect(screen,(0,0,0),[x*30,y*30,29,29],1)
    if map2[x][y]==2:
        draw_sign(screen,x,y)
    pygame.display.flip()


#绘制标记项
def draw_sign(screen,x,y):
    pygame.draw.rect(screen,(237,0,0),[x*30,y*30,29,29],3)
    pygame.display.flip()


#放入雷
def thunder(N,map1):
    a=0
    while(a!=N*N//6):
        x=random.randint(0,N-1)
        y=random.randint(0,N-1)
        map1[x][y]=9
        a=0
        for i in range(N):
            for j in range(N):
                if map1[i][j]==9:
                    a+=1


        
#显示0旁边的类容
def show_around(screen,x,y,N,map1,map2):
    if y+1<N and map1[x][y+1]!=9 and map2[x][y+1]!=1:
        draw_sort(screen,x,y+1,N,map1,map2)
        if map1[x][y+1]==0:
            show_around(screen,x,y+1,N,map1,map2)
    if y-1>=0 and map1[x][y-1]!=9and map2[x][y-1]!=1:
        draw_sort(screen,x,y-1,N,map1,map2)
        if map1[x][y-1]==0:
            show_around(screen,x,y-1,N,map1,map2)
    if x+1<N and map1[x+1][y]!=9and map2[x+1][y]!=1:
        draw_sort(screen,x+1,y,N,map1,map2)
        if map1[x+1][y]==0:
            show_around(screen,x+1,y,N,map1,map2)
    if x-1>=0 and map1[x-1][y]!=9and map2[x-1][y]!=1:
        draw_sort(screen,x-1,y,N,map1,map2)
        if map1[x-1][y]==0:
            show_around(screen,x-1,y,N,map1,map2)
    if x+1<N and y-1>=0 and map1[x+1][y-1]!=9and map2[x+1][y-1]!=1:
        draw_sort(screen,x+1,y-1,N,map1,map2)
        if map1[x+1][y-1]==0:
            show_around(screen,x+1,y-1,N,map1,map2)
    if x+1<N and y+1<N and map1[x+1][y+1]!=9and map2[x+1][y+1]!=1:
        draw_sort(screen,x+1,y+1,N,map1,map2)
        if map1[x+1][y+1]==0:
            show_around(screen,x+1,y+1,N,map1,map2)
    if x-1>=0 and y-1>=0 and map1[x-1][y-1]!=9and map2[x-1][y-1]!=1:
        draw_sort(screen,x-1,y-1,N,map1,map2)
        if map1[x-1][y-1]==0:
            show_around(screen,x-1,y-1,N,map1,map2)
    if x-1>=0 and y+1<N and map1[x-1][y+1]!=9and map2[x-1][y+1]!=1:
        draw_sort(screen,x-1,y+1,N,map1,map2)
        if map1[x-1][y+1]==0:
            show_around(screen,x-1,y+1,N,map1,map2)
    


#给map1赋值
def assignment(N,map1):
    for i in range(N):
        for j in range(N):
            if(map1[i][j]>=9):
                if j+1<N:
                    map1[i][j+1]+=1
                if j-1>=0:
                    map1[i][j-1]+=1
                if i+1<N:
                    map1[i+1][j]+=1
                if i-1>=0:
                    map1[i-1][j]+=1
                if i+1<N and j-1>=0:
                    map1[i+1][j-1]+=1
                if i+1<N and j+1<N:
                    map1[i+1][j+1]+=1
                if i-1>=0 and j-1>=0:
                    map1[i-1][j-1]+=1
                if i-1>=0 and j+1<N:
                    map1[i-1][j+1]+=1
    for i in range(N):
        for j in range(N):
            if(map1[i][j]>=9):
                map1[i][j]=9
#更新指定文件里面的数据
def update(account_mini,data,file,N):
    data_list=[]
    data_s=open(file,'r')
    for each_line in data_s:
        (data_, huiche_) = each_line.split('\n')
        data_list.append(data_)
    data_s.close()
    if data!=N:
        data_list[account_mini]=conversion(data,N)
    elif data==N:
        data_list[account_mini]=str(N)
    data_s=open(file,'w')
    for i in range(len(data_list)):
        data_s.write(data_list[i]+'\n')
    data_s.close()

#更新所有数据
def update2(account_mini,map1,map2,N,address='map1_s.txt'):
    update(account_mini,map1,'map1_s.txt',N)
    update(account_mini,map2,'map2_s.txt',N)
    update(account_mini,N,'N_s.txt',N)


#将二维数组转换成字符串
def conversion(map,N):
    Str=''
    for i in range(N):
        for j in range(N):
            Str+=str(map[i][j])
    return Str

#判断是否胜利
def is_win(map2,N):
    sum=0
    for i in range(N):
        sum+=map2[i].count(1)
    if sum==(N*N-N*N//6):
        return True
    return False

#主函数
def main(N,map1=[],map2=[],account_mini=0):

    if map1==[]:
        #储存“雷”状态的数组
        map1=list([])
        for i in range(N):
            map1.append([0]*N)
        #储存翻开状态的列表
        map2=list([])
        for i in range(N):
            map2.append([0]*N)
        thunder(N,map1)
        assignment(N,map1)

    key_x=0
    key_y=0

    #定义窗口
    screen = pygame.display.set_mode([30*N,30*N])
    
    #定义窗口名字
    pygame.display.set_caption("Mine clearance:(" + str(N*N//6) + ")")
    draw_map(screen,N)

    if map2!=[]:
        for i in range(N):
            for j in range(N):
                if map2[i][j]==1 and map1[i][j]<9:
                    draw_sort(screen,i,j,N,map1,map2)                    
        for i in range(N):
            for j in range(N):
                if map2[i][j]==1 and map1[i][j]>=9:
                    draw_bomb(screen,map1,N,account_mini)

    pygame.display.flip()    
    while True:
        for event in pygame.event.get():
            #点击x则关闭窗口
            if event.type ==pygame.QUIT:
                update2(account_mini,map1,map2,N)
                pygame.quit()
                sys.exit()
            
            #点击鼠标
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    Clean_select(screen,key_x,key_y,map2)
                    x,y=event.pos[0]//30,event.pos[1]//30
                    if map2[x][y]== 0:
                        button_sound.play(0)
                        if map1[x][y]== 0:
                            show_around(screen,x,y,N,map1,map2)
                            update2(account_mini,map1,map2,N)
                        if map1[x][y]<9:
                            draw_sort(screen,x,y,N,map1,map2)
                            update2(account_mini,map1,map2,N)
                        else :
                            map2[x][y]=1
                            update2(account_mini,map1,map2,N)
                            draw_bomb(screen,map1,N,account_mini)
                        if is_win(map2,N):
                            update2(account_mini,map1,map2,N)
                            choose.main(account_mini,"You Win!!")
                    
                        
                        
                elif event.button==3:
                    Clean_select(screen,key_x,key_y,map2)
                    x,y=event.pos[0]//30,event.pos[1]//30
                    if map2[x][y]==0:
                        draw_sign(screen,x,y)
                        map2[x][y]=2
                    elif map2[x][y]==2:
                        map2[x][y]=0
                        Clean_select(screen,x,y,map2)

            #点击键盘
            elif event.type==KEYDOWN:
                if (event.key==K_w or event.key==K_UP) and key_y>0:
                    Clean_select(screen,key_x,key_y,map2)
                    key_y-=1
                    draw_select(screen,key_x,key_y)
                elif (event.key==K_s or event.key==K_DOWN) and key_y<N-1:
                    Clean_select(screen,key_x,key_y,map2)
                    key_y+=1
                    draw_select(screen,key_x,key_y)
                elif (event.key==K_a or event.key==K_LEFT) and key_x>0:
                    Clean_select(screen,key_x,key_y,map2)
                    key_x-=1
                    draw_select(screen,key_x,key_y)
                elif (event.key==K_d or event.key==K_RIGHT) and key_x<N-1:
                    Clean_select(screen,key_x,key_y,map2)
                    key_x+=1
                    draw_select(screen,key_x,key_y)
                elif event.key==K_e and map2[key_x][key_y]== 0: 
                    button_sound.play(0)
                    if map1[key_x][key_y]==0:
                        show_around(screen,key_x,key_y,N,map1,map2)
                        update2(account_mini,map1,map2,N)                  
                    if map1[key_x][key_y]<9:
                        draw_sort(screen,key_x,key_y,N,map1,map2)
                        update2(account_mini,map1,map2,N)
                    else :
                        map2[key_x][key_y]=1
                        update2(account_mini,map1,map2,N)
                        draw_bomb(screen,map1,N,account_mini)
                    if is_win(map2,N):
                        choose.main(account_mini,"You Win!!")
                elif event.key==K_q:
                    if map2[key_x][key_y]==0:
                        draw_sign(screen,key_x,key_y)
                        map2[key_x][key_y]=2
                    elif map2[key_x][key_y]==2:
                        map2[key_x][key_y]=0
                        Clean_select(screen,key_x,key_y,map2)
                elif event.key==K_o:
                    for i in range(N):
                        for j in range(N):
                            if map1[i][j]<9:
                                draw_sort(screen,i,j,N,map1,map2)
                                map2[i][j]=1


































































 

 

choose.py

 

import numpy as np
import pygame
import sys
from pygame.locals import *
import traceback
import Game

pygame.init()
pygame.mixer.init()


#画出主界面
def draw_selector(position,decade,prompt):
    decade+=1
    str1="(tips: w,s,a,d is for up, down, left and right,q for tab,e for open)"
    screen = pygame.display.set_mode([450,700])
    screen.fill((237,237,237))
    pygame.draw.rect(screen,(0,0,0),[100,100,100,300],5)
    pygame.draw.rect(screen,(0,0,0),[250,100,100,300],5)
    pygame.draw.line(screen,(0,0,0),(100,200),(200,200),5)
    pygame.draw.line(screen,(0,0,0),(100,300),(200,300),5)
    pygame.draw.line(screen,(0,0,0),(250,200),(350,200),5)
    pygame.draw.line(screen,(0,0,0),(250,300),(350,300),5)
    
    pygame.draw.line(screen,(0,0,0),(120,150),(180,150),20)
    pygame.draw.line(screen,(0,0,0),(120,350),(180,350),20)
    pygame.draw.line(screen,(0,0,0),(270,150),(330,150),20)
    pygame.draw.line(screen,(0,0,0),(270,350),(330,350),20)
    pygame.draw.line(screen,(0,0,0),(270,350),(330,350),20)
    pygame.draw.line(screen,(0,0,0),(150,120),(150,180),20)
    pygame.draw.line(screen,(0,0,0),(300,120),(300,180),20)
    pygame.draw.rect(screen,(0,0,0),[100,450,250,100],5)

    '''
    #定义字体跟大小
    s_font1=pygame.font.Font('汉仪细行楷-1.0.0.7.ttf',50)
    s_font2=pygame.font.Font('汉仪细行楷-1.0.0.7.ttf',16)
    s_font3=pygame.font.Font('汉仪细行楷-1.0.0.7.ttf',34)
    '''
    # 我的字体不对,就用系统字体
    s_font1=pygame.font.SysFont('arial',50)
    s_font2=pygame.font.SysFont('arial',16)
    s_font3=pygame.font.SysFont('arial',34)
    



    #定义类容,是否抗锯齿,颜色
    s_text1=s_font1.render(str(decade),True,(0,0,0))
    s_text2=s_font1.render(str(position),True,(0,0,0))
    s_text3=s_font1.render("Start game",True,(0,0,0))
    s_text4=s_font2.render(str1,True,(0,0,0))
    s_text5=s_font3.render(prompt,True,(255,0,0))
    #将字放在窗口指定位置
    screen.blit(s_text1,(135,220))
    screen.blit(s_text2,(285,220))
    screen.blit(s_text3,(120,470))
    screen.blit(s_text4,(22,650))
    screen.blit(s_text5,(100,50))





    pygame.display.set_caption("select difficulty")
    
    pygame.display.flip()


def main(account_mini,prompt='select difficulty:'):
    position = 0
    decade = 1

    


    draw_selector(position,decade,prompt)
    while True:
        for event in pygame.event.get():
            if event.type ==pygame.QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    if 100<event.pos[0]<200 and 100<event.pos[1]<200 :
                        decade+=1
                        decade=decade%3
                        draw_selector(position,decade,prompt)
                    elif 100<event.pos[0]<200 and 300<event.pos[1]<400:
                        decade-=1
                        decade=decade%3
                        draw_selector(position,decade,prompt)
                    elif 250<event.pos[0]<350 and 100<event.pos[1]<200 :
                        position+=1
                        position=position%10
                        draw_selector(position,decade,prompt)
                    elif 250<event.pos[0]<350 and 300<event.pos[1]<400 :
                        position-=1
                        position=position%10
                        draw_selector(position,decade,prompt)
                    elif 100<event.pos[0]<350 and 450<event.pos[1]<550:
                        Game.main(N=10*(decade+1)+position, account_mini = account_mini)
                    
                   

 

 

以下是我找的资源了,对了,里面那个ttf文件,总是报错,最后我直接用的系统自带字体了,pygame对于中文不大友好,我改成了英文,有兴趣你可以去试试中文怎么搞

欢迎在评论区交流学习,这些代码还有待改进

 

链接:https://pan.baidu.com/s/1GWCXinc6T_AIdfnm54smyw 
提取码:smwy 
复制这段内容后打开百度网盘手机App,操作更方便哦

 

 

 

 

 

标签:account,04,22,screen,draw,map2,map1,pygame,2021
来源: https://blog.csdn.net/yesIdoareyou/article/details/116026243