python实现贪吃蛇小游戏

初始化及函数定义

#导入库
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()

上述代码原创无抄袭,按照顺序复制粘贴即可使用。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

华科菜鸡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值