昊學小昊
僅70行代碼就完成的小遊戲,效果是怎麼樣的呢?我們先一起來看看下面的視訊: 看着還不錯,跟記憶中的貪吃蛇差别不大,(其實我就是按自己的感覺做出來的),很靈活,而且顔色還多種多樣,上邊框還有記錄着分數,随着分數的增加蛇的速度也會變快,撞到自己或者牆壁都會gameover,一句話就是“麻雀雖小五髒俱全”,70行代碼就能寫出這個小遊戲,那麼具體代碼是怎麼的呢? 這裡我主要定義了3類函數,一類是畫畫的函數,一類是随機生成果實的函數,還有一類是蛇移動的函數。代碼如下:
def guoshi():#随機生成一個果實 global list3,list1 list3=[randint(0,19),randint(0,19)] a=list3 in list1 if a==True:return guoshi() else:return list3
def drew(list):#畫蛇和果實 global list3 m=0 for i in list: color=m%11 m+=1 if m==len(list):cv.create_oval(i[0]*step-5,i[1]*step-5,i[0]*step+step+5,i[1]*step+step+5,fill=list4[color],width=8) else:cv.create_rectangle(i[0]*step,i[1]*step,i[0]*step+step,i[1]*step+step,fill=list4[color],width=0.1) cv.create_rectangle(list3[0]*step,list3[1]*step,list3[0]*step+step,list3[1]*step+step,fill='red',width=0.1)
def yidon():#移動 global n top.title("得分:%d"%(n*100))#在上邊框上顯示得分 list2 = list1[-1] c = list2[0] r = list2[1] if direction == 'w':r = r - 1 elif direction == 's':r = r + 1 elif direction == 'a':c = c - 1 elif direction == 'd':c = c + 1 list00=[c, r] a=list00 in list1 if (c == -1 or c == 20 or r == -1 or r == 20 or a==True): print("Gameover!Your have gotten %d goals"%(n*100)) tk.messagebox.showinfo('消息',"遊戲結束!\n\n你共獲得%d分"%(n*100)) sleep(2) exit(top) if list00==list3:guoshi();n+=1 else:del list1[0] list1.append(list00)
寫這個遊戲時,考慮到貪吃蛇的運動狀态其實有兩種,一種是玩家不進行操作時,蛇會一直往前移動,直至撞到障礙物,另一種是玩家按下方向鍵時,蛇會對應的轉動方向。小昊學習到的知識還不多,暫時隻想到用兩個函數來分别控制這兩種運動狀态,定義的函數代碼如下:
def move1():#蛇自動前進的函數 global c,r,list1,n yidon() cv.delete("all") drew(list1) top.after(800-2*n,move1)def move(event):#蛇被動轉彎的函數 global list1,direction,times,c,r times+=1 if times!=1:direction=event.char yidon() cv.delete("all") drew(list1)
因為定義的這兩個函數之間,互不幹擾,是以這個小遊戲就出現了一點小意外,或者說是小bug,不過我覺得這是一個很有意思的bug:當你長按方向鍵時,小蛇會加速前進。這樣,在遊戲開始、蛇很小而且速度慢時,可以加速覓食,既節約了時間又收獲了刺激。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SY0IzM1EWZhFTZ0IGO5IWN2QWM3czMyQWM2M2NkRjN08CX0JXZ252bj91Ztl2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
完整的代碼如下,可直接複制粘貼:
import tkinter as tkimport tkinter.messageboxfrom time import sleepfrom random import randintfrom sys import exittop=tk.Tk()top.geometry("500x500+700+200")#top.overrideredirect(True)#去除邊框cv=tk.Canvas(top,width=500,height=500,bg='black')cv.pack()step=25list1=[[0,1],[1,1]]direction='d'times=0;n=0list4=['#0000FF','#4169E1','#1E90FF','#00BFFF','#008BBB','#00FFFF','#87CEEB','#00CED1','#87CEFA','#40E0D0','#9370DB']def guoshi():#随機生成一個果實 global list3,list1 list3=[randint(0,19),randint(0,19)] a=list3 in list1 if a==True:return guoshi() else:return list3def drew(list):#畫蛇和果實 global list3 m=0 for i in list: color=m%11 m+=1 if m==len(list):cv.create_oval(i[0]*step-5,i[1]*step-5,i[0]*step+step+5,i[1]*step+step+5,fill=list4[color],width=8) else:cv.create_rectangle(i[0]*step,i[1]*step,i[0]*step+step,i[1]*step+step,fill=list4[color],width=0.1) cv.create_rectangle(list3[0]*step,list3[1]*step,list3[0]*step+step,list3[1]*step+step,fill='red',width=0.1) cv.create_text(250, 250, text='英文鍵盤w,s,a,d為方向鍵\n\n\n\n\n\n擷取更多内容請檢視微信公衆号:昊學小昊', fill='grey', font=('楷體', 14))#這行代碼是小昊在打廣告,推廣自己,(憨笑)如果不想它運作可以在最前面加一個‘#’井号。def yidon():#移動 global n top.title("得分:%d"%(n*100))#在上邊框上顯示得分 list2 = list1[-1] c = list2[0] r = list2[1] if direction == 'w':r = r - 1 elif direction == 's':r = r + 1 elif direction == 'a':c = c - 1 elif direction == 'd':c = c + 1 list00=[c, r] a=list00 in list1 if (c == -1 or c == 20 or r == -1 or r == 20 or a==True): print("Gameover!Your have gotten %d goals"%(n*100)) tk.messagebox.showinfo('消息',"遊戲結束!\n\n你共獲得%d分"%(n*100)) sleep(2) exit(top) if list00==list3:guoshi();n+=1 else:del list1[0] list1.append(list00)def move1(): global c,r,list1,n yidon() cv.delete("all") drew(list1) top.after(800-2*n,move1)def move(event): global list1,direction,times,c,r times+=1 if times!=1:direction=event.char yidon() cv.delete("all") drew(list1)if __name__=="__main__": guoshi() drew(list1) top.after(100,move1) top.bind("",move) top.mainloop()
70行代碼,不多不少剛剛好
,如果你還想要更多内容,比如該遊戲的.exe檔案,可進入公衆号回複 010 擷取資源哦。注:該遊戲隻接受英文鍵盤輸入的w,a,s,d,既遊戲開始時應先調整為英文輸入法。
喜歡就點個在看再走吧