提前透露:
大家好,我是涵子。看到了上面的图片了吗?
一看就知道:这不就是一个数独吗?今天我要为大家带来tkinter-gui版的数独!
python小游戏:建议你去看一看哦~(都是热搜啊!都是好玩的经典游戏!)
python-pygame小游戏之球球大作战 <热搜
python-pyglet游戏之Minecraft <经典
在此,我衷心祝福大家:大家战胜疫情,大家一起快乐!希望你点一个赞,希望关注一下,我会尽量多发一些文章。
目录
一、主要步骤
1.1.导入模块
1.2.程序提前准备
1.3.程序编写
二、效果图
三、总结
首先导入模块,这里用到tkinter和numpy
小贴士:
没有下载(pip)过numpy的人们,你们要在命令提示符(cmd)里面pip一下啊。
代码如下:
pip install numpy
import tkinter as tk
import numpy as np
from tkinter.messagebox import showinfo
别忘了在电脑桌面上创建一个文本文档(记事本),命名为soduku,里面内容是:
文本提示:空格别漏
8
36
7 9 2
5 7
457
1 3
1 68
85 1
9 4
8
36
7 9 2
5 7
457
1 3
1 68
85 1
9 4
程序还是有一点复杂,我只发布程序不细讲。
这里运用了open函数,用来打开之前的文本。
这里还有核查的方法,你可以核查一下自己的答案正不正确哦~
N = 9
layout = [[0 for j in range(N)] for k in range(N)]
root = tk.Tk()
root.title("数独游戏")
frametop = tk.Frame(root)
gridvar = [[tk.StringVar() for column in range(N)] for row in range(N)]
frame = [tk.Frame(frametop) for row in range(N)]
grid = [[tk.Button(frame[row], width = 3, textvariable = gridvar[row][column],\
relief = tk.GROOVE, command = lambda row = row,\
column = column:gridclick(row, column),\
font = ('Helvetica', '12')) for column in range(N)] for row in range(N)]
for row in range(N):
for column in range(N):
grid[row][column].pack(side = tk.LEFT)
frame[row].pack(side = tk.TOP)
frametop.pack(side=tk.TOP, pady = 10)
framemiddle = tk.Frame(root)
selections = [tk.Button(framemiddle, width = 3, text = '%d' % number, relief = tk.RAISED,\
command = lambda number=number:numberclick(selections[number - 1]),\
font = ('Helvetica', '12')) for number in range(1, 10)]
for each in selections:
each.pack(side = tk.LEFT)
framemiddle.pack(side=tk.TOP, pady = 15)
framebottom = tk.Frame(root)
erase = tk.Button(framebottom, text = '删除', relief = tk.RAISED,\
font = ('HeiTi', '14', 'bold'), width = 7, height = 1, bg = 'darkgreen', fg = 'white')
erase.pack(side = tk.LEFT, padx = 15)
check = tk.Button(framebottom, text = '核查', relief = tk.RAISED,\
font = ('HeiTi', '14', 'bold'), width = 7, height = 1, bg = 'darkblue', fg = 'white')
check.pack(side = tk.LEFT, padx = 15)
ok = tk.Button(framebottom, text = '退出', relief = tk.RAISED, command = exit,\
font = ('HeiTi', '14', 'bold'), width = 7, height = 1, bg = 'darkred', fg = 'white')
ok.pack(side = tk.LEFT, padx = 15)
framebottom.pack(side = tk.TOP, pady = 5)
def gridclick(row, column):
number = ''
for i in range(N):
if selections[i]["relief"] == tk.SOLID:
number = '%d' % (i + 1)
break
gridvar[row][column].set(number)
if number == '':
layout[row][column] = 0
else :
layout[row][column] = int(number)
def numberclick(selectionbutton):
for i in range(N):
selections[i]['relief'] = tk.RAISED
erase["relief"] = tk.RAISED
selectionbutton["relief"] = tk.SOLID
def eraseclick(event):
for i in range(N):
selections[i]['relief'] = tk.RAISED
def checkclick(event):
correct = verify()
if correct:
showinfo("核查结果", "答案正确")
else:
showinfo("核查结果", "答案不正确")
check.bind("", checkclick)
def readlayout(filename):
layoutfile = open(filename, 'r')
lines = layoutfile.readlines()
for row in range(N):
line = lines[row].strip('\n')
if line != '':
for i in range(len(line)):
if line[i] != '' and line[i] != ' ':
layout[row][i] = int(line[i])
return layout
def showlayout(layout, gridvar):
for row in range(N):
for column in range(N):
gridvar[row][column].set(str(layout[row][column]) if (layout[row][column] != 0) else '')
if layout[row][column] != 0:
grid[row][column]["state"] = tk.DISABLED
def verifyrow():
correct = True
for row in range(N):
line = layout[row].copy()
line.sort()
if line != [1, 2, 3, 4, 5, 6, 7, 8, 9]:
correct = False
return correct
def verifycolumn():
correct = True
for column in range(N):
line = list((np.array(layout))[:, column])
line.sort()
if line != [1, 2, 3, 4, 5, 6, 7, 8, 9]:
correct = False
return correct
def verifyblock():
correct = True
for blockindex in range(N):
block = getblock(blockindex)
line = list((np.array(layout))[block[0] : block[1] + 1, block[2] : block[3] + 1].reshape(N))
line.sort()
if line != [1, 2, 3, 4, 5, 6, 7, 8, 9]:
correct = False
return correct
def getblock(index):
rowstart = index // 3 * 3
rowend = rowstart + 2
columnstart = index % 3 * 3
columnend = columnstart + 2
return rowstart, rowend, columnstart, columnend
def verify():
return verifyrow() & verifycolumn() & verifyblock()
erase.bind("", eraseclick)
数独就是这么难,不过做出来才发现:这代码行数也不是很多吗~
python作为一个编程语言,啥都能写出来。大家可以自己试着写一写属于你自己的游戏哦~