Python實現生命游戲的示例代碼(tkinter版)_第1頁
Python實現生命游戲的示例代碼(tkinter版)_第2頁
Python實現生命游戲的示例代碼(tkinter版)_第3頁
Python實現生命游戲的示例代碼(tkinter版)_第4頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第Python實現生命游戲的示例代碼(tkinter版)globaltv,rect

tv=tk.Canvas(win,width=win.winfo_width(),height=win.winfo_height())

tv.pack(side="top")

foriinrange(36):

coord=40,40,760,i*20+40

tv.create_rectangle(coord)

coord=40,40,i*20+40,760

tv.create_rectangle(coord)

coord=38,38,760,760

tv.create_rectangle(coord,width=2)

coord=39,39,760,760

tv.create_rectangle(coord,width=2)

coord=38,38,762,762

tv.create_rectangle(coord,width=2)

R,XY=8,[50+i*20foriinrange(36)]

rect=[[0]*36for_inrange(36)]

fori,xinenumerate(XY):

forj,yinenumerate(XY):

rect[i][j]=tv.create_rectangle(x-R,y-R,x+R,y+R,tags=('imgButton1'))

tv.itemconfig(rect[i][j],fill='lightgray',outline='lightgray')

tv.tag_bind('imgButton1','Button-1',on_Click)

defdrawLifes():

R,XY=8,[50+i*20foriinrange(36)]

ifLife.running:

fori,xinenumerate(XY):

forj,yinenumerate(XY):

ifLife.items[i+1][j+1]:

tv.itemconfig(rect[i][j],fill='green',outline='green')

else:

tv.itemconfig(rect[i][j],fill='lightgray',outline='lightgray')

tv.update()

Life.reproduce()

ifLife.is_stable():

Life.running=False

ifsum(sum(Life.items,[])):

msgbox.showinfo('Message','生命繁殖與湮滅進入穩(wěn)定狀態(tài)?。?!')

else:

msgbox.showinfo('Message','生命全部湮滅,進入死亡狀態(tài)?。?!')

win.after(Life.runningSpeed,drawLifes)

defStartLife():

ifsum(sum(Life.items,[])):

Life.histroy=[]

Life.running=True

else:

msgbox.showinfo('Message','請點擊小方塊填入生命細胞,或者使用隨機功能!')

defBreakLife():

Life.running=notLife.running

ifLife.running:

Life.histroy.clear()

Life.add_histroy()

defRandomLife():

Life.rndinit()

Life.running=True

defClearLife():

Life.running=False

Life.histroy=[]

Life.items=[[0]*38for_inrange(38)]

forxinrange(36):

foryinrange(36):

tv.itemconfig(rect[x][y],fill='lightgray',outline='lightgray')

defon_Enter(event):

tCanvas.itemconfig(tVisit,fill='magenta')

defon_Leave(event):

tCanvas.itemconfig(tVisit,fill='blue')

defon_Release(event):

url='/boysoft2002type=blog'

webbrowser.open(url,new=0,autoraise=True)

defon_Click(event):

x,y=(event.x-40)//20,(event.y-40)//20

ifnotLife.running:

ifLife.items[x+1][y+1]:

tv.itemconfig(rect[x][y],fill='lightgray',outline='lightgray')

else:

tv.itemconfig(rect[x][y],fill='red',outline='red')

Life.items[x+1][y+1]=notLife.items[x+1][y+1]

defon_Close():

ifmsgbox.askokcancel("Quit","Doyouwanttoquit"):

Life.running=False

print(Copyright())

win.destroy()

defIntroduce():

txt='''【生命游戲】\n\n生存定律:

(1)當前細胞為湮滅狀態(tài)時,當周圍有3個存活細胞時,則迭代后該細胞變成存活狀態(tài)(模擬繁殖)。

(2)當前細胞為存活狀態(tài)時,當周圍的鄰居細胞少于2個存活時,該細胞變成湮滅狀態(tài)(數量稀少)。

(3)當前細胞為存活狀態(tài)時,當周圍有3個以上的存活細胞時,該細胞變成湮滅狀態(tài)

溫馨提示

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

評論

0/150

提交評論