**学了python后,在9月初开始比赛,比赛类容是在一个星期内(白天有课,其实只有星期一到星期五晚上和双休有时间写代码)完成指定的一个程序,程序可以选择,我选的是扫雷,因为之前有过做五子棋的基础,完成扫雷也不算难。
代码跟有缘人分享一下,然后可以运行的代码已经上传:python扫雷小游戏(欢迎批评指正)。
**
start.py:
import easygui as g
import sys
import choose
import traceback
import cv2
import Game
n=0
map1=[]
map2=[]
zhanghao=0
zhanghao_list=[]
# 登录函数,用户名已注册为实现
def DengLu(zhanghao_list):
global n,map1,map2,zhanghao
fields = ('用户名:', '密码:')
msg = '请输入用户名和密码(不输入将为游客登陆)'
title = '登录'
yonghu = g.multpasswordbox(msg, title, fields)
if yonghu==None:
return "取消登陆"
elif yonghu == ['', '']:
button_choices=g.buttonbox('由于您未注册,系统将不会保存您的游戏缓存!!!','游客', choices=('确定','注册'))
if button_choices=='确定':
return 2
elif button_choices=='注册':
return 1
else:
# 将用户名读取在list1中
list1 = []
ZhangHao = open('用户信息/zhanghao.txt','r')
for each_line in ZhangHao:
(zhanghao_, huiche_) = each_line.split('\n')
list1.append(zhanghao_)
ZhangHao.close()
# 将密码读取在list2中
list2 = []
MiMa = open('用户信息/mima_s.txt','r')
for each_line in MiMa:
(mima_, huiche_) = each_line.split('\n')
list2.append(mima_)
MiMa.close()
# 确认用户名和密码是否存在并且匹配
for X in list1:
if X == str(yonghu[0]) and list2[list1.index(X)] != str(yonghu[1]):
g.msgbox('密码错误,请重新输入!', ok_button='确定 ')
return 0
break
elif X == str(yonghu[0]) and list2[list1.index(X)] == str(yonghu[1]):
g.msgbox(str(yonghu[0]) + '你好,欢迎来到扫雷!', ok_button='进入 ')
zhanghao=list1.index(X)
return 2
break
if str(yonghu[0]) not in list1:
g.msgbox('账号不存在,请注册:', ok_button='确定 ')
return 1
# 注册函数
def ZhuCe():
values = []
def zhuce():
msg = '*为必填项'
title = '账号中心'
fields = ['*用户名', '*密码']
return g.multenterbox(msg, title, fields, values)
YongHuZhuCe = zhuce()
if YongHuZhuCe == None:
return "取消注册"
else:
while YongHuZhuCe[0] == '' or YongHuZhuCe[1] == '':
g.msgbox('用户名或密码不能为空!', ok_button='继续填写 ')
values = [YongHuZhuCe[0], YongHuZhuCe[1], YongHuZhuCe[2]]
YongHuZhuCe = zhuce()
# 检验用户名是否被占用
list3 = []
ZhangHao = open('用户信息/zhanghao.txt','r')
for each_line in ZhangHao:
(zhanghao_, huiche_) = each_line.split('\n')
list3.append(zhanghao_)
ZhangHao.close()
while str(YongHuZhuCe[0]) in list3:
g.msgbox('该用户名已被占用!', ok_button='重新输入 ')
YongHuZhuCe = zhuce()
# 将账号密码分别存储在两个txt文件内
ZhangHao = open('用户信息/zhanghao.txt', 'a')
ZhangHao.write(YongHuZhuCe[0] + '\n')
ZhangHao.close()
MiMa = open('用户信息/mima_s.txt', 'a')
MiMa.write(YongHuZhuCe[1] + '\n')
MiMa.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临时文件,分别用于存放用户名和密码
ZhangHao = open('用户信息/zhanghao.txt', 'a')
MiMa = open('用户信息/mima_s.txt', 'a')
Map1_s = open('用户信息/map1_s.txt', 'a')
Map2_s = open('用户信息/map2_s.txt', 'a')
N_s = open('用户信息/N_s.txt', 'a')
ZhangHao.close()
MiMa.close()
Map1_s.close()
Map2_s.close()
N_s.close()
while True:
zhanghao_list=[]
zhanghao_s=open('用户信息/zhanghao.txt','r')
for each_line in zhanghao_s:
(zhanghao_, huiche_) = each_line.split('\n')
zhanghao_list.append(zhanghao_)
zhanghao_s.close()
choices = ['已有账号,直接登录', '开始注册']
choice = 0
choice = g.indexbox('登录/注册:', '请选择:', choices=choices)
#登陆
if choice==0:
choice = DengLu(zhanghao_list)
if choice=="取消登陆":
continue
# 注册
if choice == 1:
choice = ZhuCe()
if choice == 0:
# 注册成功重新切入登录页面
zhanghao_list=[]
zhanghao_s=open('用户信息/zhanghao.txt','r')
for each_line in zhanghao_s:
(zhanghao_, huiche_) = each_line.split('\n')
zhanghao_list.append(zhanghao_)
zhanghao_s.close()
choice = DengLu(zhanghao_list)
elif choice=="取消注册":
continue
if choice == 2:
choice = g.indexbox('开始/继续:', '请选择:', choices=['重新开始','继续游戏'])
if choice==0:
choose.main(zhanghao)
elif choice==1:
list3 = []
N = open('用户信息/N_s.txt','r')
for each_line in N:
(mima_, huiche_) = each_line.split('\n')
list3.append(mima_)
N.close()
n=int(list3[zhanghao])
list4=[]
Map1 = open('用户信息/map1_s.txt','r')
for each_line in Map1:
(mima_, huiche_) = each_line.split('\n')
list4.append(mima_)
Map1.close()
map=list4[zhanghao]
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:
(mima_, huiche_) = each_line.split('\n')
list5.append(mima_)
Map2.close()
map=list5[zhanghao]
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,zhanghao)
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
input()
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="(提示:w,s,a,d分别为上,下,左,右,q为标记,e为翻开)"
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('font.ttf',50)
s_font2=pygame.font.Font('font.ttf',16)
s_font3=pygame.font.Font('font.ttf',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("开始游戏",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("选择难度")
pygame.display.flip()
def main(zhanghao,prompt='选择难度:'):
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,zhanghao=zhanghao)
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('font.ttf',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,zhanghao):
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(zhanghao,"游戏失败!!")
#绘制键盘选中项
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(zhanghao,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[zhanghao]=conversion(data,N)
elif data==N:
data_list[zhanghao]=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(zhanghao,map1,map2,N,address='用户信息/map1_s.txt'):
update(zhanghao,map1,'用户信息/map1_s.txt',N)
update(zhanghao,map2,'用户信息/map2_s.txt',N)
update(zhanghao,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=[],zhanghao=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("扫雷:("+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,zhanghao)
pygame.display.flip()
while True:
for event in pygame.event.get():
#点击x则关闭窗口
if event.type ==pygame.QUIT:
update2(zhanghao,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(zhanghao,map1,map2,N)
if map1[x][y]<9:
draw_sort(screen,x,y,N,map1,map2)
update2(zhanghao,map1,map2,N)
else :
map2[x][y]=1
update2(zhanghao,map1,map2,N)
draw_bomb(screen,map1,N,zhanghao)
if is_win(map2,N):
update2(zhanghao,map1,map2,N)
choose.main(zhanghao,"游戏胜利!!")
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(zhanghao,map1,map2,N)
if map1[key_x][key_y]<9:
draw_sort(screen,key_x,key_y,N,map1,map2)
update2(zhanghao,map1,map2,N)
else :
map2[key_x][key_y]=1
update2(zhanghao,map1,map2,N)
draw_bomb(screen,map1,N,zhanghao)
if is_win(map2,N):
choose.main(zhanghao,"游戏胜利!!")
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