色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

python語言實現貪吃蛇游戲

瀏覽:2日期:2022-07-05 16:27:10

本文實例為大家分享了python實現貪吃蛇游戲的具體代碼,供大家參考,具體內容如下

新手自學python(簡易貪吃蛇代碼)環境python3.7

剛剛大學畢業進入工作崗位,發現同事基本都會寫py腳本,于是自學了一下,并寫了一個簡單的貪吃蛇代碼,我覺得寫的還是比較容易看懂,適合新手接觸python。

# -*- coding: utf-8 -*-import tkinter as tk # 使用Tkinter前需要先導入import tkinter.messageboximport pickleimport randomimport time# 第1步,實例化object,建立窗口windowwindow = tk.Tk() # 第2步,給窗口的可視化起名字window.title(’Greedy snake’)# 第3步,設定窗口的大小(長 * 寬)# window.geometry(’1004x504’) # 這里的乘是小x# 第5步,創建一個主frame,長在主window窗口上frame = tk.Frame(window, bg = ’blue’, bd = 2, relief = tk.FLAT)frame.pack(side = ’left’)#當前框架被選中,意思是鍵盤觸發,只對這個框架有效frame.focus_set()Labellist = [] #存放所有方塊的labelBlocklist = [] #存放背景方塊的值 1:被占用 0:空閑Snakelist = [] #存放snake的坐標height = 15width = 20#snack前進方向left = 0right = 1up = 2down =3pause = 0start = 1class App(tk.Frame): def __init__(self,master):self.window = mastertk.Frame.__init__(self)master.bind(’<Up>’,self.Up)master.bind(’<Left>’,self.Left)master.bind(’<Right>’,self.Right)master.bind(’<Down>’,self.Down)master.bind(’<p>’,self.Pause)master.bind(’<s>’,self.Start)master.bind(’<r>’,self.Restart)self.Init_snake() #初始化界面方法 self.time = 1000self.Onetime() def Up(self, event):if self.Istart: self.direction = up def Down(self, event):if self.Istart: self.direction = down def Left(self, event):if self.Istart: self.direction = left def Right(self, event):if self.Istart: self.direction = right def Init_snake(self):del Labellist[:]del Blocklist[:]del Snakelist[:] #初始化背景方塊LabelRowList = []BlockRowlist = []c = r = 0for k in range(width*height): LN=tk.Label(frame,text = ’ ’, bg = ’black’, fg = ’white’, relief = tk.FLAT, bd = 4) LN.grid(row=r,column=c,sticky=tk.N+tk.E+tk.S+tk.W) LabelRowList.append(LN) BlockRowlist.append(0) c=c+1if c>=20:r=r+1c=0Labellist.append(LabelRowList)Blocklist.append(BlockRowlist)LabelRowList = []BlockRowlist = []#初始化snakeself.Istart = 0self.direction = left self.direction_last = left self.overflag = 0 #snake head的初始位置self.x = 7self.y = 8#snake tail的初始位置self.x_tail = 7self.y_tail = 10Snakelist.append((7,8))Snakelist.append((7,9))Snakelist.append((7,10))self.snakelen = len(Snakelist) Blocklist[self.x][self.y] = 1Blocklist[self.x][self.y+1] = 1Blocklist[self.x][self.y+2] = 1Labellist[self.x][self.y].config(bg = ’green’, relief = tk.RAISED)Labellist[self.x][self.y+1].config(bg = ’white’, relief = tk.RAISED)Labellist[self.x][self.y+2].config(bg = ’white’, relief = tk.RAISED)#初始化foodself.food_x = random.randint(0,14)self.food_y = random.randint(0,19)while Blocklist[self.food_x][self.food_y] == 1: self.food_x = random.randint(0,14) self.food_y = random.randint(0,19) Blocklist[self.food_x][self.food_y] = 1Labellist[self.food_x][self.food_y].config(bg = ’red’, relief = tk.RIDGE) def Pause(self, event):self.Istart = pause def Start(self, event):self.Istart = start def Restart(self, event):self.Init_snake() def Onetime(self): #每1000ms做一次界面刷新if self.Istart and self.overflag == 0:if (self.direction_last == down and self.direction == up )or(self.direction_last == up and self.direction == down )or(self.direction_last ==left and self.direction == right )or(self.direction_last ==right and self.direction == left ):self.direction = self.direction_last self.direction_last = self.direction x0 = self.x y0 = self.y if self.direction == left:if x0 == self.food_x and y0-1 == self.food_y: Labellist[x0][y0-1].config(bg = ’green’, relief = tk.RAISED) Labellist[x0][y0].config(bg = ’white’, relief = tk.RAISED) self.food_x = random.randint(0,14) self.food_y = random.randint(0,19) while Blocklist[self.food_x][self.food_y] == 1:self.food_x = random.randint(0,14)self.food_y = random.randint(0,19) Blocklist[self.food_x][self.food_y] = 1 Labellist[self.food_x][self.food_y].config(bg = ’red’, relief = tk.RIDGE) self.snakelen += 1 Snakelist.insert(0,(x0,y0-1)) self.x = x0 self.y = y0 - 1elif (x0>=0 and x0<height and y0-1>=0 and y0-1<width and Blocklist[x0][y0-1] == 0) or (self.x_tail == x0 and self.y_tail == y0 - 1):Blocklist[self.x_tail][self.y_tail] = 0 Labellist[self.x_tail][self.y_tail].config(bg = ’black’, relief = tk.FLAT) Blocklist[x0][y0-1] = 1 Labellist[x0][y0-1].config(bg = ’green’, relief = tk.RAISED) Labellist[x0][y0].config(bg = ’white’, relief = tk.RAISED) del Snakelist[self.snakelen - 1] Snakelist.insert(0,(x0,y0-1)) self.x = x0 self.y = y0 - 1 self.x_tail = Snakelist[self.snakelen - 1][0] self.y_tail = Snakelist[self.snakelen - 1][1]else: tk.messagebox.showinfo(title = ’snake’, message = ’game over!!!’) self.overflag = 1elif self.direction == up:if x0-1 == self.food_x and y0 == self.food_y: Labellist[x0-1][y0].config(bg = ’green’, relief = tk.RAISED) Labellist[x0][y0].config(bg = ’white’, relief = tk.RAISED) self.food_x = random.randint(0,14) self.food_y = random.randint(0,19) while Blocklist[self.food_x][self.food_y] == 1:self.food_x = random.randint(0,14)self.food_y = random.randint(0,19) Blocklist[self.food_x][self.food_y] = 1 Labellist[self.food_x][self.food_y].config(bg = ’red’, relief = tk.RIDGE) self.snakelen += 1 Snakelist.insert(0,(x0-1,y0)) self.x = x0 - 1 self.y = y0 elif (x0-1 >=0 and x0-1<height and y0>=0 and y0<width and Blocklist[x0-1][y0] == 0) or (self.x_tail == x0-1 and self.y_tail == y0): Blocklist[self.x_tail][self.y_tail] = 0 Labellist[self.x_tail][self.y_tail].config(bg = ’black’, relief = tk.FLAT) Blocklist[x0-1][y0] = 1 Labellist[x0-1][y0].config(bg = ’green’, relief = tk.RAISED) Labellist[x0][y0].config(bg = ’white’, relief = tk.RAISED) del Snakelist[self.snakelen - 1] Snakelist.insert(0,(x0 - 1,y0)) self.x = x0 - 1 self.y = y0 self.x_tail = Snakelist[self.snakelen - 1][0] self.y_tail = Snakelist[self.snakelen - 1][1]else: tk.messagebox.showinfo(title = ’snake’, message = ’game over!!!’) self.overflag = 1 elif self.direction == down:if x0+1 == self.food_x and y0 == self.food_y: Labellist[x0+1][y0].config(bg = ’green’, relief = tk.RAISED) Labellist[x0][y0].config(bg = ’white’, relief = tk.RAISED) self.food_x = random.randint(0,14) self.food_y = random.randint(0,19) while Blocklist[self.food_x][self.food_y] == 1:self.food_x = random.randint(0,14)self.food_y = random.randint(0,19) Blocklist[self.food_x][self.food_y] = 1 Labellist[self.food_x][self.food_y].config(bg = ’red’, relief = tk.RIDGE) self.snakelen += 1 Snakelist.insert(0,(x0+1,y0)) self.x = x0 + 1 self.y = y0 elif (x0+1 >=0 and x0+1 <height and y0>=0 and y0<width and Blocklist[x0+1][y0] == 0) or (self.x_tail == x0+1 and self.y_tail == y0):Blocklist[self.x_tail][self.y_tail] = 0 Labellist[self.x_tail][self.y_tail].config(bg = ’black’, relief = tk.FLAT) Blocklist[x0+1][y0] = 1 Labellist[x0+1][y0].config(bg = ’green’, relief = tk.RAISED) Labellist[x0][y0].config(bg = ’white’, relief = tk.RAISED) del Snakelist[self.snakelen - 1] Snakelist.insert(0,(x0 + 1,y0)) self.x = x0 + 1 self.y = y0 self.x_tail = Snakelist[self.snakelen - 1][0] self.y_tail = Snakelist[self.snakelen - 1][1]else: tk.messagebox.showinfo(title = ’snake’, message = ’game over!!!’) self.overflag = 1 elif self.direction == right:if x0 == self.food_x and y0+1 == self.food_y: Labellist[x0][y0+1].config(bg = ’green’, relief = tk.RAISED) Labellist[x0][y0].config(bg = ’white’, relief = tk.RAISED) self.food_x = random.randint(0,14) self.food_y = random.randint(0,19) while Blocklist[self.food_x][self.food_y] == 1:self.food_x = random.randint(0,14)self.food_y = random.randint(0,19) Blocklist[self.food_x][self.food_y] = 1 Labellist[self.food_x][self.food_y].config(bg = ’red’, relief = tk.RIDGE) self.snakelen += 1 Snakelist.insert(0,(x0,y0 + 1)) self.x = x0 self.y = y0 + 1 elif (x0>=0 and x0<height and y0+1>=0 and y0+1<width and Blocklist[x0][y0+1] == 0) or (self.x_tail == x0 and self.y_tail == y0+1): Blocklist[self.x_tail][self.y_tail] = 0 Labellist[self.x_tail][self.y_tail].config(bg = ’black’, relief = tk.FLAT) Blocklist[x0][y0+1] = 1 Labellist[x0][y0+1].config(bg = ’green’, relief = tk.RAISED) Labellist[x0][y0].config(bg = ’white’, relief = tk.RAISED) del Snakelist[self.snakelen - 1] Snakelist.insert(0,(x0,y0 + 1)) self.x = x0 self.y = y0 + 1 self.x_tail = Snakelist[self.snakelen - 1][0] self.y_tail = Snakelist[self.snakelen - 1][1]else: tk.messagebox.showinfo(title = ’snake’, message = ’game over!!!’) self.overflag = 1self.after(self.time,self.Onetime)def Start_Stop(): app.Istart = 1 - app.Istart def Restart(): app.Restart(0) #主菜單mainmenu = tk.Menu(window)window[’menu’] = mainmenu#二級菜單:gamegamemenu=tk.Menu(mainmenu)mainmenu.add_cascade(label=’游戲’,menu=gamemenu)gamemenu.add_command(label = ’開始/暫停’,command=Start_Stop)gamemenu.add_command(label = ’重置’,command=Restart)gamemenu.add_command(label = ’退出’,command=window.quit)app = App(window) window.mainloop()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 久久精品免费观看国产软件 | 亚洲国产精品a一区二区三区 | 日本久操 | 日韩欧美亚洲天堂 | 欧美日韩国产在线人成dvd | 九九视频在线观看视频6 | аⅴ资源天堂8在线 | 欧美成人a| 国产毛片基地 | 国产欧美日韩在线观看精品 | 中文字幕日本一区波多野不卡 | 国内精品久久久久影院不卡 | 日本免费毛片在线高清看 | 中文在线日韩 | 噜噜噜狠狠夜夜躁精品 | 免费伦费一区二区三区四区 | 999热成人精品国产免 | 国产亚洲精品福利片 | 国产精品久久久久久 | 亚洲在线观看网站 | 国产国语在线播放视频 | 久久国产一区二区三区 | 中国大陆一级毛片 | 97免费视频在线观看 | 欧美成人一区二区三区 | av亚洲男人天堂 | 国产亚洲精品激情一区二区三区 | 99久久九九 | 欧美成人精品福利在线视频 | 国产成人精品一区二三区 | 国产一级高清 | 99色在线视频| 免费的一级片网站 | 亚洲国产欧美在线人成精品一区二区 | 国产日韩视频在线观看 | 久久免费成人 | 欧美日韩亚洲另类 | 欧美视频在线观看免费精品欧美视频 | 欧美成人免费高清视频 | 精品视频免费在线观看 | 国产丝袜美女一区二区三区 |