python制作数字华容道游戏

编程入门 行业动态 更新时间:2024-10-06 08:35:54

python制作数字<a href=https://www.elefans.com/category/jswz/34/1720295.html style=华容道游戏"/>

python制作数字华容道游戏

本文将使用python制作一个数字华容道小游戏

运用到的都是标准库,无需使用第三方库,复制即可运行

使用wasd或上下左右进行移动,当数字组成有序组合时即可过关

源码如下:
#制作:宠import tkinter as tk
from tkinter.ttk import *
from random import *
from tkinter import messageboxrow_of_space = 0
col_of_space = 0class Digital_Huarong_Road():"""数字华容道类"""def __init__(self):self.main()def Game_window(self,Difficulty,big):"""游戏窗口"""global row_of_space  global col_of_spaceGame=tk.Tk()Game.title("数字华容道-难度%s级"%Difficulty)if Difficulty<=5:Game.geometry("260x260+300+300")else:Game.geometry("%sx%s+300+300"%(Difficulty*52,Difficulty*52))#背景颜色Game["background"]="snow"#Game.resizable(0,0)row_of_space = 0    #存放空白按钮的行号col_of_space = 0    #存放空白按钮的列号buttons = {}      #存放数字按钮的数组numbers = [ num for num in range(1,Difficulty**2) ] #使用列表推导式列出所有数字numbers.append(" ") #添加一个空白字符shuffle(numbers)     #打乱数字列表中的数字顺序"""制造数字华容道方阵"""for row in range(Difficulty): for col in range(Difficulty):button = tk.Button(Game,bg = "#F2F3D6",fg = "black",font = ("楷体",big))buttons[row,col] = button   #将按钮导入数组button["text"] = numbers.pop()    #设置按钮上的文本if Difficulty<=5:button.place(relx = 0 + col * 52 / 260,rely = 0 + row * 52 / 260,relwidth = 50 / 260,relheight = 50 / 260)    #设置数字按钮大小else:a = Difficulty * 52button.place(relx = 0 + col * 52 / a,rely = 0 + row * 52 / a,relwidth = 50 / a,relheight = 50 / a)    #设置数字按钮大小if button["text"] == " ":        #判断是否为空白按钮,如果是,则记录到空白按钮行列号变量row_of_space = rowcol_of_space = col#还原列表numbers = [ num for num in range(1,Difficulty**2) ]numbers.append(" ")def Restart_the_game(Difficulty,big):"""重新启动游戏,当游戏获胜时执行该函数"""if messagebox.askyesno("胜利","你以获胜,是否重新开始游戏"):Game.destroy()self.Game_window(Difficulty,big)else:Game.destroy()def up(event):"""键盘事件-上"""global row_of_space  global col_of_space#先对空白位置进行判断,如果空白位置在最底层就跳出函数if row_of_space==Difficulty-1:return#交换按钮位置buttons[row_of_space,col_of_space]["text"] = buttons[row_of_space+1,col_of_space]["text"]buttons[row_of_space+1,col_of_space]["text"]  = " "  row_of_space += 1n = 0for row in range(Difficulty):for col in range(Difficulty):#对比所有按钮序列是否正确,不正确则跳出函数if buttons[row,col]["text"] != numbers[n]:returnn += 1#所有按钮判断完毕赢得游戏胜利Restart_the_game(Difficulty,big)def Down(event):"""键盘事件-下"""global row_of_space  global col_of_space#先对空白位置进行判断,如果空白位置在最下层就跳出函数if row_of_space==0:return#交换按钮位置buttons[row_of_space,col_of_space]["text"] = buttons[row_of_space-1,col_of_space]["text"]buttons[row_of_space-1,col_of_space]["text"]  = " "  row_of_space -= 1          n = 0for row in range(Difficulty):for col in range(Difficulty):#对比所有按钮序列是否正确,不正确则跳出函数if buttons[row,col]["text"] != numbers[n]:  returnn += 1#所有按钮判断完毕赢得游戏胜利Restart_the_game(Difficulty,big)def Left(event):"""键盘事件-左"""global row_of_space  global col_of_space#先对空白位置进行判断,如果空白位置在最左层就跳出函数if col_of_space==Difficulty-1:return#交换按钮位置buttons[row_of_space,col_of_space]["text"] = buttons[row_of_space,col_of_space+1]["text"]buttons[row_of_space,col_of_space+1]["text"]  = " "  col_of_space += 1          n = 0for row in range(Difficulty):for col in range(Difficulty):#对比所有按钮序列是否正确,不正确则跳出函数if buttons[row,col]["text"] != numbers[n]:  returnn += 1#所有按钮判断完毕赢得游戏胜利Restart_the_game(Difficulty,big)def Right(event):"""键盘事件-右"""global row_of_space  global col_of_space#先对空白位置进行判断,如果空白位置在最右层就跳出函数if col_of_space==0:return#交换按钮位置buttons[row_of_space,col_of_space]["text"] = buttons[row_of_space,col_of_space-1]["text"]buttons[row_of_space,col_of_space-1]["text"]  = " "  col_of_space -= 1          n = 0for row in range(Difficulty):for col in range(Difficulty):#对比所有按钮序列是否正确,不正确则跳出函数if buttons[row,col]["text"] != numbers[n]:  returnn += 1#所有按钮判断完毕赢得游戏胜利Restart_the_game(Difficulty,big)def Control_up(event):"""键盘事件-列上"""global row_of_space  global col_of_space#先对空白位置进行判断,如果空白位置在最底层就跳出函数if row_of_space==Difficulty-1:return#交换按钮位置for i in range(Difficulty):if row_of_space==Difficulty-1:returnelse:buttons[row_of_space,col_of_space]["text"] = buttons[row_of_space+1,col_of_space]["text"]buttons[row_of_space+1,col_of_space]["text"]  = " "  row_of_space += 1n = 0for row in range(Difficulty):for col in range(Difficulty):#对比所有按钮序列是否正确,不正确则跳出函数if buttons[row,col]["text"] != numbers[n]:returnn += 1#所有按钮判断完毕赢得游戏胜利Restart_the_game(Difficulty,big)def Control_Down(event):"""键盘事件-列下"""global row_of_space  global col_of_space#先对空白位置进行判断,如果空白位置在最底层就跳出函数if row_of_space==0:return#交换按钮位置for i in range(Difficulty):if row_of_space==0:returnelse:buttons[row_of_space,col_of_space]["text"] = buttons[row_of_space-1,col_of_space]["text"]buttons[row_of_space-1,col_of_space]["text"]  = " "  row_of_space -= 1n = 0for row in range(Difficulty):for col in range(Difficulty):#对比所有按钮序列是否正确,不正确则跳出函数if buttons[row,col]["text"] != numbers[n]:returnn += 1#所有按钮判断完毕赢得游戏胜利Restart_the_game(Difficulty,big)def Control_Left(event):"""键盘事件-列左"""global row_of_space  global col_of_space#先对空白位置进行判断,如果空白位置在最底层就跳出函数if col_of_space==Difficulty-1:return#交换按钮位置for i in range(Difficulty):if col_of_space==Difficulty-1:returnelse:buttons[row_of_space,col_of_space]["text"] = buttons[row_of_space,col_of_space+1]["text"]buttons[row_of_space,col_of_space+1]["text"]  = " "  col_of_space += 1n = 0for row in range(Difficulty):for col in range(Difficulty):#对比所有按钮序列是否正确,不正确则跳出函数if buttons[row,col]["text"] != numbers[n]:returnn += 1#所有按钮判断完毕赢得游戏胜利Restart_the_game(Difficulty,big)def Control_Right(event):"""键盘事件-列右"""global row_of_space  global col_of_space#先对空白位置进行判断,如果空白位置在最底层就跳出函数if col_of_space==0:return#交换按钮位置for i in range(Difficulty):if col_of_space==0:returnelse:buttons[row_of_space,col_of_space]["text"] = buttons[row_of_space,col_of_space-1]["text"]buttons[row_of_space,col_of_space-1]["text"]  = " "  col_of_space -= 1n = 0for row in range(Difficulty):for col in range(Difficulty):#对比所有按钮序列是否正确,不正确则跳出函数if buttons[row,col]["text"] != numbers[n]:returnn += 1#所有按钮判断完毕赢得游戏胜利Restart_the_game(Difficulty,big)#为窗口绑定键盘事件Game.bind("<KeyPress-Up>",up)Game.bind("<KeyPress-Down>",Down)Game.bind("<KeyPress-Left>",Left)Game.bind("<KeyPress-Right>",Right)Game.bind("<KeyPress-w>",up)Game.bind("<KeyPress-s>",Down)Game.bind("<KeyPress-a>",Left)Game.bind("<KeyPress-d>",Right)#绑定快捷窗口Game.bind("<Control-Up>",Control_up)Game.bind("<Control-Down>",Control_Down)Game.bind("<Control-Left>",Control_Left)Game.bind("<Control-Right>",Control_Right)Game.bind("<Control-w>",Control_up)Game.bind("<Control-s>",Control_Down)Game.bind("<Control-a>",Control_Left)Game.bind("<Control-d>",Control_Right)Game.mainloop()def main(self):"""主窗口函数"""root=tk.Tk()root.title("数字华容道")root.geometry("400x300+300+300")root.resizable(0,0)def Start(defDifficult):"""开始挑战中转函数"""root.destroy()if defDifficult>10:self.Game_window(defDifficult,23)else:self.Game_window(defDifficult,30)self.main()#创建画布cv=tk.Canvas(root,bg="snow")cv.place(x=0,y=0,width=400,height=300)#创建标题cv.create_text(200,30,text="数字华容道",fill="black",font=("华文新魏",30,"bold"))#难度文字cv.create_text(110,200,text="难度:",fill="black",font=("华文新魏",18))cv.create_text(310,200,text="级",fill="black",font=("华文新魏",18))#制作cv.create_text(280,287,text="制作:宠",activefill="red",fill="black",font=("华文新魏",12))def input_validation(content):"""输入验证函数"""if content.isdigit() or content == "":if content=="":start.config(state="disabled")return Trueelif int(content)>20:if int(content)>=3 and int(content)<=20:start.config(state="normal")return Falseelse:if int(content)<3:start.config(state="disabled")else:start.config(state="normal")return Trueelse:try:if int(content)>=3 and int(content)<=20:start.config(state="normal")except:start.config(state="normal")else:if int(content)<3:start.config(state="disabled")return Falsedef gamerules():rule=tk.Toplevel(root)rule.title("游戏规则")rule.transient(root)rule.geometry("810x650+300+300")rule.resizable(0,0)tk.Label(rule,text="游戏介绍:\n华容道是古老的中国民间益智游戏,以其变化多端、百玩不厌的特点与魔方、独立钻石一起被国外智力专家并称为“智力游戏界的三个不可思议”。它与七巧板、九连环等中国传统益智玩具还有个代名词叫作“中国的难题”。",font=("华文新魏",15),justify="left",wraplength=800).place(x=0,y=0)tk.Label(rule,text="华容道游戏取自著名的三国故事,曹操在赤壁之战中被刘备和孙权的“苦肉计”、“铁索连舟”打败,被迫退逃到华容道,又遇上诸葛亮的伏兵,关羽为了报答曹操对他的恩情,明逼实让,终于帮助曹操逃出了华容道。游戏就是依照“曹瞒兵败走华容,正与关公狭路逢。只为当初恩义重,放开金锁走蛟龙”这一故事情节,但是这个游戏的起源,却不是一般人认为的是“中国最古老的游戏之一”。实际上它的历史可能很短。\n华容道的现在样式是1932年John Harold Fleming在英国申请的专利,并且还附上横刀立马\n的解法。",font=("华文新魏",15),justify="left",wraplength=800).place(x=0,y=100)tk.Label(rule,text="游戏规则:\n数字华容道是一种益智游戏,它的规则非常简单,但是需要一定的思维能力和耐心。下面我们来详细了解一下数字华容道的规则。\n数字华容道的游戏目标是将所有的数字按照从小到大的顺序排列在游戏面板上。游戏面板通常是一个4x4的方格,其中有15个数字和一个空格。\n游戏规则非常简单,玩家需要通过移动数字来将它们按照从小到大的顺序排列。移动数字的方式是将数字与空格交换位置,只有相邻的数字和空格可以交换位置。\n数字华容道看似简单,但是要想在游戏中获胜,需要一定的技巧和策略。\n\n数字华容道的难度可以根据游戏面板的大小来调整。通常情况下,游戏面板的大小越大,游戏的难度就越大。例如,一个5x5的游戏面板比一个4x4的游戏面板更难\n\n数字华容道是一款非常有趣的益智游戏,它可以锻炼玩家的思维能力和耐心。如果你没有尝试过数字华容道,不放试一试,相信你会喜欢上它的。",font=("华文新魏",15),justify="left",wraplength=800).place(x=0,y=260)tk.Label(rule,text="移动按键:\n单个移动:使用方向键上下左右/wasd键实现单个移动\n整行/列移动:使用control+移动键(即上下左右/wasd)实现整行/列移动",font=("华文新魏",15),justify="left",wraplength=800).place(x=0,y=570)#启用登录窗口def enable_window():root.attributes("-disabled", 0)rule.destroy()#禁用登录窗口root.attributes("-disabled", 1)rule.protocol("WM_DELETE_WINDOW",enable_window)#难度输入框input_validate=root.register(input_validation) #输入验证Difficult=tk.Entry(root,font=("Arial",18),validate="all",vcmd=(input_validate,"%P"),justify="center",bd=0,bg="snow")Difficult.place(x=130,y=188,width=170,height=21)#绘制输入框的下划线cv.create_line(130,210,300,210,width=2)#开始按钮start=Button(root,text="开始挑战",takefocus=False,state="disabled",command=lambda:Start(int(Difficult.get())))start.place(x=53,y=230,width=290,height=40)#说明tk.Label(root,text="中国民间益智游戏\n锻炼玩家的思维能力和耐心",font=("华文新魏",14),bg="snow").place(x=85,y=80)tk.Label(root,text="你能通关吗?",font=("华文新魏",14),bg="snow").place(x=10,y=275)#游戏规则Game_Rules=tk.Button(root,text="游戏规则",font=("华文新魏",12),bd=0,bg="snow",activebackground="snow",cursor="hand2",takefocus=False,command=gamerules)Game_Rules.place(x=335,y=280,width=60,height=15)root.mainloop()Digital_Huarong_Road()
运行结果如下:

更多推荐

python制作数字华容道游戏

本文发布于:2024-02-28 07:23:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1768850.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:华容道   数字   游戏   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!