赞
踩
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
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()
![登陆界面](https://img-blog.csdn.net/20181019161117680?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyODQ3MjUy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
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)
![选择界面](https://img-blog.csdn.net/20181019161258195?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyODQ3MjUy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
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),[i30,j30,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,(x30+9,y30+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,(30i+1,30j+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),[x30,y30,29,29],3)
pygame.display.flip()
#清除选中项,标记项
def Clean_select(screen,x,y,map2):
pygame.draw.rect(screen,(237,237,237),[x30,y30,29,29],3)
pygame.draw.rect(screen,(0,0,0),[x30,y30,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),[x30,y30,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=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=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==(NN-NN//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 :
不知道你们用的什么环境,我一般都是用的Python3.6环境和pycharm解释器,没有软件,或者没有资料,没人解答问题,都可以免费领取(包括今天的代码),过几天我还会做个视频教程出来,有需要也可以领取~
给大家准备的学习资料包括但不限于:
Python 环境、pycharm编辑器/永久激活/翻译插件
python 零基础视频教程
Python 界面开发实战教程
Python 爬虫实战教程
Python 数据分析实战教程
python 游戏开发实战教程
Python 电子书100本
Python 学习路线规划
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。