python的pygame模組寫掃雷小遊戲
阿新 • • 發佈:2018-11-08
學了python後,在9月初開始比賽,比賽類容是在一個星期內(白天有課,其實只有星期一到星期五晚上和雙休有時間寫程式碼)完成指定的一個程式,程式可以選擇,我選的是掃雷,因為之前有過做五子棋的基礎,完成掃雷也不算難。
程式碼跟有緣人分享一下,資源可以加我qq(2395425694)找我要(記得評論點贊)。
雖然可能沒人看,但還是打一個小廣告:幫忙完成大一、大二的程式作業(c\c++\python\java),保證受老師表揚,而且保證不到網上抄襲,價格視程式難度而定。聯絡方式上面有。
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()
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)
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