猫史档案馆


L0_0

L0_0

Lv.1

获赞:360收藏:173浏览:24006作品收藏:621

签名:我在编python程序

回复帖子评论
上一页1 页 / 共 1下一页

【作品秀】必读攻略,如何才能让作品上首页≥ω≤ 中回复

https://ide.codemao.cn/we/3261789

2018-10-21T15:56:19 点赞:0

【作品秀】必读攻略,如何才能让作品上首页≥ω≤ 中回复

www.codemao.cn

2018-10-21T21:21:15 点赞:0

【作品秀】必读攻略,如何才能让作品上首页≥ω≤ 中回复

https://www.codemao.cn/

2018-10-27T14:08:27 点赞:0

【萌新の第1课】如何开始创作? 中回复

www.cod喵

2018-12-18T18:27:37 点赞:0

【Python作品分享】滚球游戏【作品秀】 中回复

钻石不会扣血,但三角形是障碍物

2021-02-27T14:06:25 点赞:0

【Python作品分享】滚球游戏【作品秀】 中回复

 

center_image

2021-02-27T18:01:51 点赞:0

【作品秀】扫雷 中回复

好厉害,我可以第一次点到雷

emotion_编程猫_点赞

2021-03-13T21:28:57 点赞:0

全新MIDI音乐画板,乱画都好听! 中回复

我希望 MIDI音乐能有黑键音

2021-05-01T19:39:20 点赞:5

【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