初始化及函数定义
#导入库
import time
import random
from tkinter import *
from tkinter.messagebox import *
#初始化变量
w=10
x1=60
y1=60
num=5
step=10
ind=1
sui=1
iu=1
id=1
#定义四个方向移动函数
def moveup1(e):
global ind
global iu
global cun
global win
global item
global a
global b
global id
ind=2
while ind==2:
up1()
iu+=1
if iu %20==1:
a = random.randint(20, 390)
b = random.randint(40, 420)
item = Frame(win,width=10, height=10, bg="blue")
item.place(width=5, height=5, x=a, y=b)
cun.append(item)
lenget = len(cun)
for index in range(lenget):
if abs(xx(snake[0]) - xx(cun[index])) <= 10 and abs(yy(snake[0]) - yy(cun[index])) <= 10:
cun[index].config(background="yellow")
id+=1
label.config(text="得分"+str(id))
if xx(snake[0])<10 or xx(snake[0])>410 or yy(snake[0])<30 or yy(snake[0])>430:
showwarning("失败","您已失败,即将退出游戏")
ind=1
time.sleep(0.05)
win.update()
def movedown1(e):
global ind
global iu
global cun
global win
global item
global a
global b
global id
ind=3
while ind==3:
down1()
iu += 1
if iu % 20 == 1:
a = random.randint(20, 390)
b = random.randint(40, 420)
item = Frame(win,width=10, height=10, bg="blue")
item.place(width=5, height=5, x=a, y=b)
cun.append(item)
lenget = len(cun)
for index in range(lenget):
if abs(xx(snake[0])-xx(cun[index]))<=10 and abs(yy(snake[0])-yy(cun[index]))<=10:
cun[index].config(background="yellow")
id += 1
label.config(text="得分" + str(id))
if xx(snake[0])<10 or xx(snake[0])>410 or yy(snake[0])<30 or yy(snake[0])>430:
showwarning("失败","您已失败,即将退出游戏")
ind=1
time.sleep(0.05)
win.update()
def moveleft1(e):
global ind
global iu
global cun
global win
global item
global a
global b
global id
ind=4
while ind==4:
left1()
iu += 1
if iu % 20 == 1:
a = random.randint(20, 390)
b = random.randint(40, 420)
item = Frame(win,width=10, height=10, bg="blue")
item.place(width=5, height=5, x=a, y=b)
cun.append(item)
lenget = len(cun)
for index in range(lenget):
if abs(xx(snake[0]) - xx(cun[index])) <= 10 and abs(yy(snake[0]) - yy(cun[index])) <= 10:
cun[index].config(background="yellow")
id += 1
label.config(text="得分" + str(id))
if xx(snake[0])<10 or xx(snake[0])>410 or yy(snake[0])<30 or yy(snake[0])>430:
showwarning("失败","您已失败,即将退出游戏")
ind=1
time.sleep(0.05)
win.update()
def moveright1(e):
global ind
global iu
global cun
global win
global item
global a
global b
global id
ind=5
while ind==5:
right1()
iu += 1
if iu % 20 == 1:
a = random.randint(20, 390)
b = random.randint(40, 420)
item = Frame(win,width=10, height=10, bg="blue")
item.place(width=5, height=5, x=a, y=b)
cun.append(item)
lenget = len(cun)
for index in range(lenget):
if abs(xx(snake[0]) - xx(cun[index])) <= 10 and abs(yy(snake[0]) - yy(cun[index])) <= 10:
cun[index].config(background="yellow")
id += 1
label.config(text="得分" + str(id))
if xx(snake[0])<10 or xx(snake[0])>410 or yy(snake[0])<30 or yy(snake[0])>430:
showwarning("失败","您已失败")
ind=1
win.quit()
time.sleep(0.05)
win.update()
#获取组件在窗口位置函数
def xx(mod):
return int(mod.winfo_geometry().split("+")[1])
def yy(mod):
return int(mod.winfo_geometry().split("+")[2])
#加入四个方向移动函数循环体的函数
def up1():
for index,ch in enumerate(snake):
if index==0:
snake[index].place(x=xx(snake[index]),y=yy(snake[index])-step)
else:
snake[index].place(x=xx(snake[index-1]),y=yy(snake[index-1]))
def down1():
for index,ch in enumerate(snake):
if index==0:
snake[index].place(x=xx(snake[index]),y=yy(snake[index])+step)
else:
snake[index].place(x=xx(snake[index-1]),y=yy(snake[index-1]))
def left1():
for index,ch in enumerate(snake):
if index==0:
snake[index].place(x=xx(snake[index])-step,y=yy(snake[index]))
else:
snake[index].place(x=xx(snake[index-1]),y=yy(snake[index-1]))
def right1():
for index,ch in enumerate(snake):
if index==0:
snake[index].place(x=xx(snake[index])+step,y=yy(snake[index]))
else:
snake[index].place(x=xx(snake[index-1]),y=yy(snake[index-1]))
#定义游戏规则
def tixing():
showinfo("游戏规则","游戏规则:按键盘上的上下左右键中的任意键即可开始,吃掉小蓝方块,分数不限,不可以出黄色界")
#定义重新开始函数
def chongxin():
global ind
global sui
global id
global iu
ind = 1
sui = 1
iu = 1
id = 1
lenget = len(cun)
for index in range(lenget):
cun[index].config(background="yellow")
for i in range(num):
snake[i].place(x=x1,y=y1+i*w)
label.config(text="得分:0")
tkinker窗口设置
#设置窗口及各组件
win=Tk()
win.geometry("420x430")
win.title("贪吃蛇")
canvas=Canvas(win,width=400,height=400,background="yellow")
canvas.place(x=10,y=30)
label=Label(win,text="得分:0",font=14,foreground="red")
label.place(x=170,y=5)
snake=[]
cun=[]
for i in range(num):
item1=Frame(width=10,height=10,bg="pink")
snake.append(item1)
item1.place(x=x1,y=y1+i*w)
snake[0].config(background="red")
a = random.randint(20, 390)
b = random.randint(40, 420)
item = Frame(width=10, height=10, bg="yellow")
item.place(width=5, height=5, x=a, y=b)
cun.append(item)
menu1=Menu(win)
menu1.add_command(label="游戏规则",command=tixing)
menu1.add_command(label="重新开始",command=chongxin)
menu1.add_command(label="退出",command=win.quit)
win.config(menu=menu1)
win.bind("<Up>",moveup1)
win.bind("<Down>",movedown1)
win.bind("<Left>",moveleft1)
win.bind("<Right>",moveright1)
win.mainloop()
上述代码原创无抄袭,按照顺序复制粘贴即可使用。