2024年最全python写扫雷小游戏(pygame)_pygame 扫雷
update(zhanghao,map1,‘用户信息/map1_s.txt’,N)update(zhanghao,map2,‘用户信息/map2_s.txt’,N)update(zhanghao,N,‘用户信息/N_s.txt’,N)choose.main(zhanghao,“游戏失败!#定义类容,是否抗锯齿,颜色。#更新指定文件里面的数据。#将二维数组转换成字符串。#将字放在窗口指定位置。#清除
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),[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 :
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

pygame安装方法:https://www.cnblogs.com/hongten/p/hongten\_pygame\_install.html
做了那么多年开发,自学了很多门编程语言,我很明白学习资源对于学一门新语言的重要性,这些年也收藏了不少的Python干货,对我来说这些东西确实已经用不到了,但对于准备自学Python的人来说,或许它就是一个宝藏,可以给你省去很多的时间和精力。
别在网上瞎学了,我最近也做了一些资源的更新,只要你是我的粉丝,这期福利你都可拿走。
我先来介绍一下这些东西怎么用,文末抱走。
* * *
**(1)Python所有方向的学习路线(新版)**
这是我花了几天的时间去把Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
最近我才对这些路线做了一下新的更新,知识体系更全面了。

**(2)Python学习视频**
包含了Python入门、爬虫、数据分析和web开发的学习视频,总共100多个,虽然没有那么全面,但是对于入门来说是没问题的,学完这些之后,你可以按照我上面的学习路线去网上找其他的知识资源进行进阶。

**(3)100多个练手项目**
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了,只是里面的项目比较多,水平也是参差不齐,大家可以挑自己能做的项目去练练。

**(4)200多本电子书**
这些年我也收藏了很多电子书,大概200多本,有时候带实体书不方便的话,我就会去打开电子书看看,书籍可不一定比视频教程差,尤其是权威的技术书籍。
基本上主流的和经典的都有,这里我就不放图了,版权问题,个人看看是没有问题的。
**(5)Python知识点汇总**
知识点汇总有点像学习路线,但与学习路线不同的点就在于,知识点汇总更为细致,里面包含了对具体知识点的简单说明,而我们的学习路线则更为抽象和简单,只是为了方便大家只是某个领域你应该学习哪些技术栈。

**(6)其他资料**
还有其他的一些东西,比如说我自己出的Python入门图文类教程,没有电脑的时候用手机也可以学习知识,学会了理论之后再去敲代码实践验证,还有Python中文版的库资料、MySQL和HTML标签大全等等,这些都是可以送给粉丝们的东西。

**这些都不是什么非常值钱的东西,但对于没有资源或者资源不是很好的学习者来说确实很不错,你要是用得到的话都可以直接抱走,关注过我的人都知道,这些都是可以拿到的。**
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化学习资料的朋友,可以戳这里无偿获取](https://bbs.csdn.net/topics/618317507)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
更多推荐
所有评论(0)