Lv.1
签名:我在编python程序
在 【Python作品分享】贪吃蛇【作品秀】 中回复
上面代码不能运行(有一些代码没显示出来),最好使用以下代码:
import random
from tkinter import Tk, Label, Menu, ttk, messagebox, filedialog
root = Tk()
root.title('贪吃蛇')
square = {}
scorelabel = ttk.Label(root, text='得分:0')
scorelabel.pack(fill='x', expand=1)
score = 0
def _reset():
global games, scores, scorest
games = 0
scores = 0
scorest = []
for i in range(20):
frame = ttk.Frame(root)
for j in range(20):
square[j, i] = Label(frame, width=6, height=2, relief='groove')
square[j, i].pack(side='left', fill='both', expand=1)
frame.pack(fill='both', expand=1)
stlst = [0, 90, 180, 270]
seth = random.choice(stlst)
_reset()
menubar = Menu(root, tearoff=0)
gamemenu = Menu(menubar, tearoff=0)
body = []
leaf = []
def turn(event):
global seth
if event.keysym == 'Up': seth = 0
elif event.keysym == 'Down': seth = 180
elif event.keysym == 'Left': seth = 270
else: seth = 90
def move():
if gamemenu.entrycget(0, 'state') == 'disabled':
x = body[0][0]
y = body[0][1]
if seth == 90: x += 1
elif seth == 180: y += 1
elif seth == 270: x -= 1
else: y -= 1
body.insert(0, (x, y))
if len(body) > score + 1: del body[score+1:]
create()
root.after(200, move)
def over():
global scores
gamemenu.entryconfig(0, state='normal')
if score > scores:
scores = score
scorest.append(str(score))
messagebox.showinfo('贪吃蛇', '游戏结束,得分:{}。'.format(score))
def place_leaf():
global leaf
leaf = (random.randint(0, 17), random.randint(0, 17))
def create():
global score
try:
for i in square: square[i]['bg'] = 'SystemButtonFace'
for i in body: square[i]['bg'] = 'red'
square[leaf]['bg'] = 'green'
if body[0] in body[1:]: raise
if body[0] == leaf:
score += 1
place_leaf()
except: over()
def newgame():
global games, leaf, body, score
score = 0
games += 1
gamemenu.entryconfig(0, state='disabled')
leaf = (random.randint(0, 17), random.randint(0, 17))
body = [(random.randint(2, 15), random.randint(2, 15))]
root.after(200, move)
root.bind('\u003cLeft\u003e', turn)
root.bind('\u003cRight\u003e', turn)
root.bind('\u003cUp\u003e', turn)
root.bind('\u003cDown\u003e', turn)
def _quit():
if messagebox.askyesno('退出', '你确定要退出游戏吗?'):
root.quit()
root.protocol('WM_DELETE_WINDOW', _quit)
gamemenu.a喵_command(label='新游戏', command=newgame)
gamemenu.a喵_command(label='帮助', command=lambda: messagebox.showinfo('帮助', '你将用←、→键控制贪吃蛇,每吃到一片叶子,就加1分,贪吃蛇边长,如果贪吃蛇出边缘或碰到身体某个部位,游戏就结束了。'))
gamemenu.a喵_separator()
gamemenu.a喵_command(label='退出', command=_quit)
menubar.a喵_cascade(label='游戏', menu=gamemenu)
staticmenu = Menu(menubar, tearoff=0)
staticmenu.a喵_command(label='统计', command=lambda: messagebox.showinfo('统计', '你玩了 {} 局游戏,最高分为 {} 分,每一局分别得分如下(按局数排列):\n{}'.format(games, scores, '\n'.join(scorest))))
def reset():
if messagebox.askokcancel('重置', '你确认要重置记录?', detail='确认后将无法撤销。'): _reset()
staticmenu.a喵_command(label='重置', command=reset)
menubar.a喵_cascade(label='统计', menu=staticmenu)
root.config(menu=menubar)
def ___():
scorelabel['text'] = '得分:{}'.format(score)
root.after(1, ___)
root.after(1, ___)
root.mainloop()
2021-12-25T22:18:15 点赞:0