我主要写python,发出来可能不适当!
2024-06-01 21:32:55
发布于:浙江
如果你们觉得不适当的话,我可以删帖
使用时记得下载pygame十分简单
在终端
pip3 install pygame
我做了一个python+pygame扫雷游戏:如下
函数名不是很好
import pygame,random
pygame.init()
screen=pygame.display.set_mode([400,500])
screen.fill([255,255,255])
def draw9():
for x in range(1,9):
pygame.draw.line(screen,[0,0,0],[x*45,100],[x*45,500],2)
for y in range(0,9):
pygame.draw.line(screen,[0,0,0],[0,100+y*45],[400,100+y*45],2)
def lei():
LEI=[]
LEI2=[]
LEIWHERE=[]
for i in range(81):
location=[25+45*(i%9),125+(i//9)*45]
LEI.append(location)
for k in range(10):
a=random.randint(0,80)
while a in LEIWHERE:
a=random.randint(0,80)
LEI2.append(LEI[a])
pygame.draw.circle(screen,[0,0,0],LEI[a],15,0)
LEIWHERE.append(a)
return LEI2,LEI,LEIWHERE
def font(num,location):
x=location[0]
y=location[1]-10
ss=pygame.font.Font(None,50)
sf=ss.render(str(num),1,(0,0,255))
screen.blit(sf,[x,y])
def drawgai(num,a):
mo=a[1]
for k in num:
for go in mo:
if mo[k]==go:
pygame.draw.rect(screen,[129,129,129],[go[0]-25,go[1]-25,45,45],0)
else:
continue
def draw_set(num,a):
mo=a[1]
for k in num:
for go in mo:
if mo[k]==go:
pygame.draw.circle(screen,[255,0,0],[go[0],go[1]],15,0)
else:
continue
all=[]
for i in range(81):
all.append(i)
left=[0,9,18,27,36,45,54,63,72]
right=[8,17,26,35,44,53,62,71,80]
LEFT=False
RIGHT=False
a=lei()
list1=[-9,9,0]
list2=[-9,0]
list3=[9,0]
mouseX=0
mouseY=0
def jiance():
for u in range(81):
time=0
small=10
max2=10
if a[1][u] in a[0]:
continue
if u==0 or u==1 or u==2 or u==3 or u==4 or u==5 or u==6 or u==7 or u==8 or u==9:
X=list3
elif u==72 or u==73 or u==74 or u==75 or u==76 or u==77 or u==78 or u==79 or u==80:
X=list2
else:
X=list1
for q in left:
if u==q:
small=0
max2=2
LEFT=True
else:
LEFT=False
for w in right:
if w==u:
small=(-1)
max2=1
RIGHT=True
else:
RIGHT=False
if max2!=1 and small!=0:
small=(-1)
max2=2
for i in X:
for k in range(small,max2):
if a[1][u+k+i] in a[0]:
time+=1
if time==0:
time=' '
font(time,a[1][u])
remove=(-1)
ss=pygame.font.Font(None,50)
sf=ss.render(':(',1,(0,0,0))
ss1=pygame.font.Font(None,50)
sf1=ss.render(':)',1,(0,0,0))
win_ss=pygame.font.Font(None,50)
win_sf=win_ss.render('8)',1,(0,0,0))
setlei=0
LOCATION=[]
jiance()
drawgai(all,a)
draw9()
pygame.display.flip()
running=True
game=False
st=True
setlocations=[]
newall=all[:]
sort=a[2].sort()
for qw in a[2]:
newall.remove(qw)
win=False
leinum=10
while running:
leiset=win_ss.render(str(10-setlei),1,(0,0,0))
if game==False and win==False:
screen.blit(sf1,[175,50])
screen.blit(leiset,[50,50])
pygame.display.flip()
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
if event.type==pygame.MOUSEMOTION:
mouseX,mouseY=event.pos
if mouseY>100:
HOW=((mouseY-100)//45)*9+mouseX//45
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_s and game==False and setlei<10 and st==False:
screen.fill([255,255,255])
jiance()
for l in a[0]:
pygame.draw.circle(screen,[0,0,0],l,15,0)
drawgai(all,a)
draw9()
if not HOW in LOCATION:
setlocations.append(HOW)
draw_set(setlocations,a)
setlei+=1
pygame.display.flip()
if event.type==pygame.MOUSEBUTTONDOWN:
if mouseY>100 and not HOW in LOCATION and game==False:
screen.fill([255,255,255])
if st:
for i in range(-1,2):
for k in range(-18,1):
try:
all.remove(HOW+k+i)
LOCATION.append(HOW+k+i)
if HOW+k+i in a[2]:
all.append(HOW+k+i)
LOCATION.remove(HOW+k+i)
except:
pass
st=False
else:
if HOW in all:
all.remove(HOW)
if not HOW in LOCATION:
LOCATION.append(HOW)
jiance()
for l in a[0]:
pygame.draw.circle(screen,[0,0,0],l,15,0)
if HOW in setlocations:
setlocations.remove(HOW)
for ew in range(5):
try:
setlocations.remove(HOW)
setlei-=1
except:
pass
drawgai(all,a)
draw_set(setlocations,a)
draw9()
pygame.display.flip()
else:
pass
for qwe in a[2]:
if qwe in LOCATION:
game=True
screen.blit(leiset,[50,50])
screen.blit(sf,[175,50])
for l in a[0]:
pygame.draw.circle(screen,[0,0,0],l,15,0)
pygame.display.flip()
if newall==LOCATION:
pygame.draw.rect(screen,[255,255,255],[0,0,400,105],0)
screen.blit(win_sf,[175,50])
game=True
win=True
pygame.display.flip()
pygame.display.flip()
pygame.quit()
还有个小要求
加入我们!
链接
全部评论 3
有人吗
2024-06-02 来自 浙江
0我也不牛,这个我可是废了好久,有兴趣,你们也做一做
2024-06-02 来自 浙江
0牛
2024-06-02 来自 广东
0
有帮助,赞一个