python语言实现贪吃蛇游戏
本文实例为大家分享了python实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下
新手自学python(简易贪吃蛇代码)
环境python3.7
刚刚大学毕业进入工作岗位,发现同事基本都会写py脚本,于是自学了一下,并写了一个简单的贪吃蛇代码,我觉得写的还是比较容易看懂,适合新手接触python。
#-*-coding:utf-8-*-
importtkinterastk
#使用Tkinter前需要先导入
importtkinter.messagebox
importpickle
importrandom
importtime
#第1步,实例化object,建立窗口window
window=tk.Tk()
#第2步,给窗口的可视化起名字
window.title('Greedysnake')
#第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=[]#存放所有方块的label
Blocklist=[]#存放背景方块的值1:被占用0:空闲
Snakelist=[]#存放snake的坐标
height=15
width=20
#snack前进方向
left=0
right=1
up=2
down=3
pause=0
start=1
classApp(tk.Frame):
def__init__(self,master):
self.window=master
tk.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.bind('',self.Restart)
self.Init_snake()#初始化界面方法
self.time=1000
self.Onetime()
defUp(self,event):
ifself.Istart:
self.direction=up
defDown(self,event):
ifself.Istart:
self.direction=down
defLeft(self,event):
ifself.Istart:
self.direction=left
defRight(self,event):
ifself.Istart:
self.direction=right
defInit_snake(self):
delLabellist[:]
delBlocklist[:]
delSnakelist[:]
#初始化背景方块
LabelRowList=[]
BlockRowlist=[]
c=r=0
forkinrange(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+1
ifc>=20:
r=r+1
c=0
Labellist.append(LabelRowList)
Blocklist.append(BlockRowlist)
LabelRowList=[]
BlockRowlist=[]
#初始化snake
self.Istart=0
self.direction=left
self.direction_last=left
self.overflag=0
#snakehead的初始位置
self.x=7
self.y=8
#snaketail的初始位置
self.x_tail=7
self.y_tail=10
Snakelist.append((7,8))
Snakelist.append((7,9))
Snakelist.append((7,10))
self.snakelen=len(Snakelist)
Blocklist[self.x][self.y]=1
Blocklist[self.x][self.y+1]=1
Blocklist[self.x][self.y+2]=1
Labellist[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)
#初始化food
self.food_x=random.randint(0,14)
self.food_y=random.randint(0,19)
whileBlocklist[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)
defPause(self,event):
self.Istart=pause
defStart(self,event):
self.Istart=start
defRestart(self,event):
self.Init_snake()
defOnetime(self):#每1000ms做一次界面刷新
ifself.Istartandself.overflag==0:
if(self.direction_last==downandself.direction==up)or(self.direction_last==upandself.direction==down)or(self.direction_last==leftandself.direction==right)or(self.direction_last==rightandself.direction==left):
self.direction=self.direction_last
self.direction_last=self.direction
x0=self.x
y0=self.y
ifself.direction==left:
ifx0==self.food_xandy0-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)
whileBlocklist[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>=0andx0=0andy0-1=0andx0-1=0andy0=0andx0+1=0andy0=0andx0=0andy0+1
更多有趣的经典小游戏实现专题,分享给大家:
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
JavaScript经典游戏玩不停
java经典小游戏汇总
javascript经典小游戏汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。