1、python语写贪吃蛇_python语实现贪吃蛇游戏本实例为家分享了python实现贪吃蛇游戏的具体代码,供家参考,具体内容如下新学python(简易贪吃蛇代码)环境python3.7刚刚学毕业进作岗位,发现同事基本都会写py脚本,于是学了下,并写了个简单的贪吃蛇代码,我觉得写的还是较容易看懂,适合新接触python。#-*-coding:utf-8-*-import tkinter as tk#使Tkinter前需要先导import tkinter.messageboximport pickleimport randomimport time#第1步,实例化object,建窗windowwi
2、ndow=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=#存放
3、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(,self.Up)master.bind(,self.Left)master.bind(,self.Right)master.bind(,self.Down)master.bind(,self.Pause)master.bind(,self.Start)master.
4、bind(,self.Restart)self.Init_snake()#初始化界法self.time=1000self.Onetime()def Up(self,event):if self.Istart:self.direction=updef Down(self,event):if self.Istart:self.direction=downdef Left(self,event):if self.Istart:self.direction=leftdef Right(self,event):if self.Istart:self.direction=rightdef Init_sna
5、ke(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=0Labe
6、llist.append(LabelRowList)Blocklist.append(BlockRowlist)LabelRowList=BlockRowlist=#初始化snakeself.Istart=0self.direction=leftself.direction_last=leftself.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
7、)self.snakelen=len(Snakelist)Blocklistself.xself.y=1Blocklistself.xself.y+1=1Blocklistself.xself.y+2=1Labellistself.xself.y.config(bg=green,relief=tk.RAISED)Labellistself.xself.y+1.config(bg=white,relief=tk.RAISED)Labellistself.xself.y+2.config(bg=white,relief=tk.RAISED)#初始化foodself.food_x=random.ra
8、ndint(0,14)self.food_y=random.randint(0,19)while Blocklistself.food_xself.food_y=1:self.food_x=random.randint(0,14)self.food_y=random.randint(0,19)Blocklistself.food_xself.food_y=1Labellistself.food_xself.food_y.config(bg=red,relief=tk.RIDGE)def Pause(self,event):self.Istart=pausedef Start(self,even
9、t):self.Istart=startdef 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_la
10、st=right and self.direction=left):self.direction=self.direction_lastself.direction_last=self.directionx0=self.xy0=self.yif self.direction=left:if x0=self.food_x and y0-1=self.food_y:Labellistx0y0-1.config(bg=green,relief=tk.RAISED)Labellistx0y0.config(bg=white,relief=tk.RAISED)self.food_x=random.ran
11、dint(0,14)self.food_y=random.randint(0,19)while Blocklistself.food_xself.food_y=1:self.food_x=random.randint(0,14)self.food_y=random.randint(0,19)Blocklistself.food_xself.food_y=1Labellistself.food_xself.food_y.config(bg=red,relief=tk.RIDGE)self.snakelen+=1Snakelist.insert(0,(x0,y0-1)self.x=x0self.y
12、=y0-1elif(x0=0 and x0=0 and y0-1Blocklistself.x_tailself.y_tail=0Labellistself.x_tailself.y_tail.config(bg=black,relief=tk.FLAT)Blocklistx0y0-1=1Labellistx0y0-1.config(bg=green,relief=tk.RAISED)Labellistx0y0.config(bg=white,relief=tk.RAISED)del Snakelistself.snakelen-1Snakelist.insert(0,(x0,y0-1)sel
13、f.x=x0self.y=y0-1self.x_tail=Snakelistself.snakelen-10self.y_tail=Snakelistself.snakelen-11else: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:Labellistx0-1y0.config(bg=green,relief=tk.RAISED)Labellistx0y0.config(bg=w
14、hite,relief=tk.RAISED)self.food_x=random.randint(0,14)self.food_y=random.randint(0,19)while Blocklistself.food_xself.food_y=1:self.food_x=random.randint(0,14)self.food_y=random.randint(0,19)Blocklistself.food_xself.food_y=1Labellistself.food_xself.food_y.config(bg=red,relief=tk.RIDGE)self.snakelen+=
15、1Snakelist.insert(0,(x0-1,y0)self.x=x0-1self.y=y0elif(x0-1=0 and x0-1=0 and y0Blocklistself.x_tailself.y_tail=0Labellistself.x_tailself.y_tail.config(bg=black,relief=tk.FLAT)Blocklistx0-1y0=1Labellistx0-1y0.config(bg=green,relief=tk.RAISED)Labellistx0y0.config(bg=white,relief=tk.RAISED)del Snakelist
16、self.snakelen-1Snakelist.insert(0,(x0-1,y0)self.x=x0-1self.y=y0self.x_tail=Snakelistself.snakelen-10self.y_tail=Snakelistself.snakelen-11else:tk.messagebox.showinfo(title=snake,message=game over)self.overflag=1elif self.direction=down:if x0+1=self.food_x and y0=self.food_y:Labellistx0+1y0.config(bg=
17、green,relief=tk.RAISED)Labellistx0y0.config(bg=white,relief=tk.RAISED)self.food_x=random.randint(0,14)self.food_y=random.randint(0,19)while Blocklistself.food_xself.food_y=1:self.food_x=random.randint(0,14)self.food_y=random.randint(0,19)Blocklistself.food_xself.food_y=1Labellistself.food_xself.food
18、_y.config(bg=red,relief=tk.RIDGE)self.snakelen+=1Snakelist.insert(0,(x0+1,y0)self.x=x0+1self.y=y0elif(x0+1=0 and x0+1=0 and y0Blocklistself.x_tailself.y_tail=0Labellistself.x_tailself.y_tail.config(bg=black,relief=tk.FLAT)Blocklistx0+1y0=1Labellistx0+1y0.config(bg=green,relief=tk.RAISED)Labellistx0y
19、0.config(bg=white,relief=tk.RAISED)del Snakelistself.snakelen-1Snakelist.insert(0,(x0+1,y0)self.x=x0+1self.y=y0self.x_tail=Snakelistself.snakelen-10self.y_tail=Snakelistself.snakelen-11else:tk.messagebox.showinfo(title=snake,message=game over)self.overflag=1elif self.direction=right:if x0=self.food_
20、x and y0+1=self.food_y:Labellistx0y0+1.config(bg=green,relief=tk.RAISED)Labellistx0y0.config(bg=white,relief=tk.RAISED)self.food_x=random.randint(0,14)self.food_y=random.randint(0,19)while Blocklistself.food_xself.food_y=1:self.food_x=random.randint(0,14)self.food_y=random.randint(0,19)Blocklistself
21、.food_xself.food_y=1Labellistself.food_xself.food_y.config(bg=red,relief=tk.RIDGE)self.snakelen+=1Snakelist.insert(0,(x0,y0+1)self.x=x0self.y=y0+1elif(x0=0 and x0=0 and y0+1Blocklistself.x_tailself.y_tail=0Labellistself.x_tailself.y_tail.config(bg=black,relief=tk.FLAT)Blocklistx0y0+1=1Labellistx0y0+
22、1.config(bg=green,relief=tk.RAISED)Labellistx0y0.config(bg=white,relief=tk.RAISED)del Snakelistself.snakelen-1Snakelist.insert(0,(x0,y0+1)self.x=x0self.y=y0+1self.x_tail=Snakelistself.snakelen-10self.y_tail=Snakelistself.snakelen-11else:tk.messagebox.showinfo(title=snake,message=game over)self.overf
23、lag=1self.after(self.time,self.Onetime)def Start_Stop():app.Istart=1-app.Istartdef Restart():app.Restart(0)#主菜单mainmenu=tk.Menu(window)windowmenu=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语实现贪吃蛇游戏