python用tkinter開發(fā)的掃雷游戲_第1頁
python用tkinter開發(fā)的掃雷游戲_第2頁
python用tkinter開發(fā)的掃雷游戲_第3頁
python用tkinter開發(fā)的掃雷游戲_第4頁
python用tkinter開發(fā)的掃雷游戲_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第python用tkinter開發(fā)的掃雷游戲value=property(fget=get_value,fset=set_value,doc='0:非地雷1:雷')

defget_around_mine_count(self):

returnself._around_mine_count

defset_around_mine_count(self,around_mine_count):

self._around_mine_count=around_mine_count

around_mine_count=property(fget=get_around_mine_count,fset=set_around_mine_count,doc='四周地雷數(shù)量')

defget_status(self):

returnself._status

defset_status(self,value):

self._status=value

status=property(fget=get_status,fset=set_status,doc='BlockStatus')

classMineBlock:

def__init__(self):

self._block=[[Mine(i,j)foriinrange(BLOCK_WIDTH)]forjinrange(BLOCK_HEIGHT)]

#埋雷

foriinrandom.sample(range(BLOCK_WIDTH*BLOCK_HEIGHT),MINE_COUNT):

self._block[i//BLOCK_WIDTH][i%BLOCK_WIDTH].value=1

defget_block(self):

returnself._block

block=property(fget=get_block)

defgetmine(self,x,y):

returnself._block[y][x]

defopen_mine(self,x,y):

#踩到雷了

ifself._block[y][x].value:

self._block[y][x].status=bomb

returnFalse

#先把狀態(tài)改為opened

self._block[y][x].status=opened

around=_get_around(x,y)

_sum=0

fori,jinaround:

ifself._block[j][i].value:

_sum+=1

self._block[y][x].around_mine_count=_sum

#如果周圍沒有雷,那么將周圍8個(gè)未中未點(diǎn)開的遞歸算一遍

if_sum==0:

fori,jinaround:

ifself._block[j][i].around_mine_count==-1:

self.open_mine(i,j)

returnTrue

defdouble_mouse_button_down(self,x,y):

ifself._block[y][x].around_mine_count==0:

returnTrue

self._block[y][x].status=double

around=_get_around(x,y)

#周圍被標(biāo)記的雷數(shù)量

sumflag=0

fori,jin_get_around(x,y):

ifself._block[j][i].status==flag:

sumflag+=1

#周邊的雷已經(jīng)全部被標(biāo)記

result=True

ifsumflag==self._block[y][x].around_mine_count:

fori,jinaround:

ifself._block[j][i].status==normal:

ifnotself.open_mine(i,j):

result=False

else:

fori,jinaround:

ifself._block[j][i].status==normal:

self._block[j][i].status=hint

returnresult

defdouble_mouse_button_up(self,x,y):

self._block[y][x].status=opened

fori,jin_get_around(x,y):

ifself._block[j][i].status==hint:

self._block[j][i].status=normal

#返回(x,y)周圍的點(diǎn)坐標(biāo)

def_get_around(x,y):

return[(i,j)foriinrange(max(0,x-1),min(BLOCK_WIDTH-1,x+1)+1)

forjinrange(max(0,y-1),min(BLOCK_HEIGHT-1,y+1)+1)ifi!=xorj!=y]

#游戲屏幕的寬

SCREEN_WIDTH=BLOCK_WIDTH*SIZE

#游戲屏幕的高

SCREEN_HEIGHT=(BLOCK_HEIGHT+2)*SIZE

defprint_text(screen,font,x,y,text,fcolor=(255,255,255)):

imgText=font.render(text,True,fcolor)

screen.blit(imgText,(x,y))

defmain():

pygame.init()

screen=pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))

pygame.display.set_caption('掃雷')

#得分的字體

font1=pygame.font.Font('resources/a.TTF',SIZE*2)

fwidth,fheight=font1.size('999')

red=(200,40,40)

#加載資源圖片,因?yàn)橘Y源文件大小不一,所以做了統(tǒng)一的縮放處理

img0=pygame.image.load('resources/0.bmp').convert()

img0=pygame.transform.smoothscale(img0,(SIZE,SIZE))

img1=pygame.image.load('resources/1.bmp').convert()

img1=pygame.transform.smoothscale(img1,(SIZE,SIZE))

img2=pygame.image.load('resources/2.bmp').convert()

img2=pygame.transform.smoothscale(img2,(SIZE,SIZE))

img3=pygame.image.load('resources/3.bmp').convert()

img3=pygame.transform.smoothscale(img3,(SIZE,SIZE))

img4=pygame.image.load('resources/4.bmp').convert()

img4=pygame.transform.smoothscale(img4,(SIZE,SIZE))

img5=pygame.image.load('resources/5.bmp').convert()

img5=pygame.transform.smoothscale(img5,(SIZE,SIZE))

img6=pygame.image.load('resources/6.bmp').convert()

img6=pygame.transform.smoothscale(img6,(SIZE,SIZE))

img7=pygame.image.load('resources/7.bmp').convert()

img7=pygame.transform.smoothscale(img7,(SIZE,SIZE))

img8=pygame.image.load('resources/8.bmp').convert()

img8=pygame.transform.smoothscale(img8,(SIZE,SIZE))

img_blank=pygame.image.load('resources/blank.bmp').convert()

img_blank=pygame.transform.smoothscale(img_blank,(SIZE,SIZE))

img_flag=pygame.image.load('resources/flag.bmp').convert()

img_flag=pygame.transform.smoothscale(img_flag,(SIZE,SIZE))

img_ask=pygame.image.load('resources/ask.bmp').convert()

img_ask=pygame.transform.smoothscale(img_ask,(SIZE,SIZE))

img_mine=pygame.image.load('resources/mine.bmp').convert()

img_mine=pygame.transform.smoothscale(img_mine,(SIZE,SIZE))

img_blood=pygame.image.load('resources/blood.bmp').convert()

img_blood=pygame.transform.smoothscale(img_blood,(SIZE,SIZE))

img_error=pygame.image.load('resources/error.bmp').convert()

img_error=pygame.transform.smoothscale(img_error,(SIZE,SIZE))

face_size=int(SIZE*1.25)

img_face_fail=pygame.image.load('resources/face_fail.bmp').convert()

img_face_fail=pygame.transform.smoothscale(img_face_fail,(face_size,face_size))

img_face_normal=pygame.image.load('resources/face_normal.bmp').convert()

img_face_normal=pygame.transform.smoothscale(img_face_normal,(face_size,face_size))

img_face_success=pygame.image.load('resources/face_success.bmp').convert()

img_face_success=pygame.transform.smoothscale(img_face_success,(face_size,face_size))

face_pos_x=(SCREEN_WIDTH-face_size)//2

face_pos_y=(SIZE*2-face_size)//2

img_dict={0:img0,1:img1,2:img2,3:img3,4:img4,5:img5,6:img6,7:img7,8:img8}

bgcolor=(225,225,225)

block=MineBlock()

game_status=readied

#開始時(shí)間

start_time=None

#耗時(shí)

elapsed_time=0

whileTrue:

screen.fill(bgcolor)

foreventinpygame.event.get():

ifevent.type==QUIT:

sys.exit()

elifevent.type==MOUSEBUTTONDOWN:

mouse_x,mouse_y=event.pos

x=mouse_x//SIZE

y=mouse_y//SIZE-2

b1,b2,b3=pygame.mouse.get_pressed()

ifgame_status==started:

#鼠標(biāo)左右鍵同時(shí)按下,如果已經(jīng)標(biāo)記了所有雷,則打開周圍一圈;如果還未標(biāo)記完所有雷,則有一個(gè)周圍一圈被同時(shí)按下的效果

ifb1andb3:

mine=block.getmine(x,y)

ifmine.status==opened:

ifnotblock.double_mouse_button_down(x,y):

game_status=over

elifevent.type==MOUSEBUTTONUP:

ify0:

ifface_pos_x=mouse_x=face_pos_x+face_size\

andface_pos_y=mouse_y=face_pos_y+face_size:

game_status=readied

block=MineBlock()

start_time=time.time()

elapsed_time=0

continue

ifgame_status==readied:

game_status=started

start_time=time.time()

elapsed_time=0

ifgame_status==started:

mine=block.getmine(x,y)

#按鼠標(biāo)左鍵

ifb1andnotb3:

ifmine.status==normal:

ifnotblock.open_mine(x,y):

game_status=over

#按鼠標(biāo)右鍵

elifnotb1andb3:

ifmine.status==normal:

mine.status=flag

elifmine.status==flag:

mine.status=ask

elifmine.status==ask:

mine.status=normal

elifb1andb3:

ifmine.status==double:

block.double_mouse_button_up(x,y)

flag_count=0

opened_count=0

forrowinblock.block:

formineinrow:

pos=(mine.x*SIZE,(mine.y+2)*SIZE)

ifmine.status==opened:

screen.blit(img_dict[mine.around_mine_count],pos)

opened_count+=1

elifmine.status==double:

screen.blit(img_dict[mine.around_mine_count],pos)

elifmine.status==bomb:

screen.blit(img_blood,pos)

elifmine.status==flag:

screen.blit(img_flag,pos)

flag_count+=1

elifmine.status==ask:

screen.blit(img_ask,pos)

elifmine.status==hint:

screen.blit(img0,pos)

elifgame_status==overandmine.value:

screen.blit(img_mine,pos)

elifmine.value==0andmine.

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論