关灯游戏及扩展


7.8    图形界面应用案例——关灯游戏

题目:

[案例]游戏初步——关灯游戏。    

关灯游戏是很有意思的益智游戏,玩家通过单击关掉(或打开)一盏灯。如果关(掉(或打开)一个电灯,其周围(上下左右)的电灯也会触及开关,成功地关掉所有电灯即可过关。


图7-43 关灯游戏运行效果
分析:游戏中采用二维列表存储灯的状态,'you'表示电灯亮(黄色的圆),'wu'表示电灯关掉(背景色的圆)。在Canvas画布单击事件中,获取鼠标单击位置从而换算成棋盘位(x1,y1),并处理四周灯的状态转换。
案例代码:

from tkinter import *
from tkinter import messagebox
root = Tk()
l= [['wu', 'wu', 'you', 'you', 'you'] ,['wu', 'you', 'wu', 'wu', 'wu'],['wu', 'wu', 'wu', 'wu', 'wu'],['wu', 'wu', 'wu', 'you', 'wu'],['you', 'you', 'you', 'wu', 'wu']]
#绘制灯的状态情况图
def huaqi():for i in range (0,5):for u in range (0,5):if l[i][u]=='you':cv.create_oval(i * 40 + 10,u * 40 + 10,(i+1)* 40+10,(u+1)*40 + 10,outline='white', fill='yellow', width=2)#亮灯else:cv.create_oval(i*40 + 10,u*40 +10,(i+1) *40+10,(u+1)*40 + 10,outline='white', fill='white', width=2)	#灭灯
#反转(x1,yl)处灯的状态
def reserve(x1,y1):if l[x1][y1] =='wu':l[x1][y1]='you'else:l[x1][y1] = 'wu'
#单击事件函数
def luozi(event):x1 = (event.x - 10) // 40y1 = (event.y - 10) // 40print(x1, y1)reserve(x1, y1)  # 翻转(x1,y1)处灯的状态# 以下翻转(x1,yI)周围的灯的状态#左侧灯的状态反转if x1 !=0:reserve(x1 - 1, y1)# 右侧灯的状态反转if x1!=4:reserve(x1 + 1, y1)# 上侧灯的状态反转if y1!=0:reserve(x1, y1 - 1)# 下侧灯的状态反转if y1!=4:reserve(x1, y1 + 1)huaqi()
# 主程序
cv = Canvas(root, bg='white', width=210, height=210)
for i in range(0, 6):  # 绘制网格线cv.create_line(10, 10 + 1 * 40,210, 10 + i * 40, arrow = 'none')cv.create_line(10 + i * 40,10,10 + 1 * 40, 210, arrow = 'none' )
huaqi()  # 绘制灯的状态情况图
p = 0
for i in range(0, 5):for u in l[i]:if u == 'wu':p= p + 1
if p == 25:messagebox.showinfo('win', '你过关了')  # 显示赢信息的消息窗口
cv.bind('<Button-1>', luozi)
cv.pack()
root.mainloop ()

这段代码是一个基于Tkinter库的灯泡游戏。游戏界面是一个5x5的网格,每个网格代表一个灯泡。初始状态下,所有的灯泡都是灭的(白色)。玩家的目标是通过点击灯泡,将所有的灯泡都点亮(黄色)。

代码中的主要函数和操作包括:

1. `huaqi()`函数:根据灯泡的状态情况,绘制灯泡的图形。如果灯泡是亮的,则绘制一个黄色的圆形;如果灯泡是灭的,则绘制一个白色的圆形。

2. `reserve(x1, y1)`函数:根据给定的坐标`(x1, y1)`,反转该位置的灯泡的状态。如果灯泡是亮的,则变为灭的;如果灯泡是灭的,则变为亮的。

3. `luozi(event)`函数:处理鼠标点击事件。根据点击的位置,确定对应的灯泡,并进行状态反转。同时,还会反转该灯泡周围的灯泡状态。

4. 主程序部分:创建一个画布(Canvas)对象,并设置背景色为白色。然后,使用循环绘制网格线。接着,调用`huaqi()`函数绘制灯泡的初始状态。最后,绑定鼠标左键点击事件到`luozi()`函数,并将画布显示在窗口中。

在游戏过程中,玩家通过点击灯泡来改变它的状态,并且反转周围灯泡的状态。当所有的灯泡都点亮时,会弹出一个消息窗口显示玩家胜利的信息。

注(Tkinter库):

Tkinter是Python的标准图形用户界面(GUI)工具包,它提供了创建和管理GUI应用程序所需的组件和功能。Tkinter是基于Tcl/Tk工具包的Python接口,Tcl是一种脚本语言,而Tk是一个用于创建图形用户界面的工具包。

Tkinter库包含了许多常用的GUI组件,比如按钮、标签、文本框、复选框、单选按钮、菜单等,同时也支持布局管理器来帮助开发者设计和布局界面。开发者可以使用Tkinter来创建各种类型的应用程序,从简单的工具到复杂的桌面应用程序都可以使用Tkinter来实现。

Tkinter的优点包括:

1. **易于学习和使用**:Tkinter是Python的标准库,因此无需额外安装即可使用。它的接口简单直观,适合初学者入门。

2. **跨平台性**:Tkinter可以在多个平台上运行,包括Windows、Linux和Mac OS等。

3. **丰富的组件**:Tkinter提供了丰富的GUI组件,可以满足大部分应用程序的需求。

4. **灵活性**:Tkinter支持自定义组件和样式,开发者可以根据自己的需求进行定制。

总之,Tkinter是一个功能强大且易于使用的GUI工具包,适合用于开发Python图形界面应用程序。

扩展题目:

题目要求:

请完成《7.8关灯游戏》(课本p.170),并完成以下扩展内容:

扩展内容:

1. 请设计并实现界面功能,允许玩家选择初始地图的大小,分别为:小(5x5),中(8x8),大(12x10),玩家选择之后立即刷新窗口界面并重设地图

2. 请设计并实现界面功能,允许玩家选择游戏难度,分别为:

容易:一开始有20%的格子状态为翻转状态

中等:一开始有40%的格子状态为翻转状态

困难:一开始有60%的格子状态为翻转状态

3. 游戏开始之前,提示玩家输入唯一的用户名,用于保留该玩家的闯关记录

4. 按照玩家选择的地图大小和难度开始游戏,游戏过程中记录玩家闯关的总时间以及翻转次数(即开关灯的次数)

5. 玩家通关成功后,保存相关信息到数据库,表结构可以自定,但应当至少包含以下信息:

 - 玩家选择的地图大小

 - 玩家选择的难度

 - 玩家通关的日期时间

 - 玩家通关所用的总时间

 - 玩家通关所用的总点击次数

6. 设计窗口界面,显示通关排行榜,具体说明如下:

 - 根据不同地图大小以及不同难度显示各自排名,例如:大地图中等难度与中地图困难难度的排名是分开的

 - 可以选择显示总排名(所有玩家的记录)与个人排名(玩家自己的所有记录)

 - 可以选择根据通关时间排名,以及根据点击次数排名,两个排名都是按从小到大排列

扩展完第一个功能的代码

(1. 请设计并实现界面功能,允许玩家选择初始地图的大小,分别为:小(5x5),中(8x8),大(12x10),玩家选择之后立即刷新窗口界面并重设地图):

from tkinter import *
from tkinter import messagebox
import randomclass LightsOutGame:def __init__(self, master=None, size=5):self.master = masterself.size = sizeself.lights = [['wu' for _ in range(size)] for _ in range(size)]self.create_widgets()self.random_open_lights()  # 随机打开一些灯def create_widgets(self):self.cv = Canvas(self.master, bg='white', width=self.size*40+10, height=self.size*40+10)self.cv.pack()self.draw_grid()  # 绘制网格self.huaqi()  # 绘制灯的状态情况图self.cv.bind('<Button-1>', self.luozi)def draw_grid(self):self.cv.delete("grid")  # 清除之前的网格for i in range(self.size+1):  # 绘制网格线self.cv.create_line(10, 10 + i * 40, 10 + self.size * 40, 10 + i * 40, arrow='none', tags="grid")self.cv.create_line(10 + i * 40, 10, 10 + i * 40, 10 + self.size * 40, arrow='none', tags="grid")def huaqi(self):self.cv.delete("lights")  # 清除之前的灯for i in range(self.size):for u in range(self.size):if self.lights[i][u] == 'you':self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='yellow', width=2, tags="lights")  # 亮灯else:self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='white', width=2, tags="lights")  # 灭灯def reserve(self, x1, y1):if self.lights[x1][y1] == 'wu':self.lights[x1][y1] = 'you'else:self.lights[x1][y1] = 'wu'def luozi(self, event):x1 = (event.x - 10) // 40y1 = (event.y - 10) // 40print(x1, y1)self.reserve(x1, y1)  # 翻转(x1,y1)处灯的状态# 以下翻转(x1,yI)周围的灯的状态#左侧灯的状态反转if x1 != 0:self.reserve(x1 - 1, y1)# 右侧灯的状态反转if x1 != self.size - 1:self.reserve(x1 + 1, y1)# 上侧灯的状态反转if y1 != 0:self.reserve(x1, y1 - 1)# 下侧灯的状态反转if y1 != self.size - 1:self.reserve(x1, y1 + 1)self.huaqi()def random_open_lights(self):num_of_lights = random.randint(1, self.size*self.size)  # 随机选择要打开的灯的数量positions = random.sample(range(self.size*self.size), num_of_lights)  # 随机选择要打开的灯的位置for pos in positions:x = pos // self.sizey = pos % self.sizeself.reserve(x, y)def reset_game(size):game.size = sizegame.lights = [['wu' for _ in range(size)] for _ in range(size)]game.cv.config(width=size*40+10, height=size*40+10)game.draw_grid()game.random_open_lights()game.huaqi()def on_small():reset_game(5)def on_medium():reset_game(8)def on_large():reset_game(12)root = Tk()
game = LightsOutGame(root)menu_frame = Frame(root)
menu_frame.pack()small_button = Button(menu_frame, text="小", command=on_small)
small_button.pack(side=LEFT)medium_button = Button(menu_frame, text="中", command=on_medium)
medium_button.pack(side=LEFT)large_button = Button(menu_frame, text="大", command=on_large)
large_button.pack(side=LEFT)root.mainloop()

扩展完功能二的代码:

请设计并实现界面功能,允许玩家选择游戏难度,分别为:

容易:一开始有20%的格子状态为翻转状态

中等:一开始有40%的格子状态为翻转状态

困难:一开始有60%的格子状态为翻转状态

from tkinter import *
from tkinter import messagebox
import randomclass LightsOutGame:def __init__(self, master=None, size=5, difficulty='medium'):self.master = masterself.size = sizeself.lights = [['wu' for _ in range(size)] for _ in range(size)]self.create_widgets()self.random_open_lights(difficulty)  # 随机打开一些灯def create_widgets(self):self.cv = Canvas(self.master, bg='white', width=self.size*40+10, height=self.size*40+10)self.cv.pack()self.draw_grid()  # 绘制网格self.huaqi()  # 绘制灯的状态情况图self.cv.bind('<Button-1>', self.luozi)def draw_grid(self):self.cv.delete("grid")  # 清除之前的网格for i in range(self.size+1):  # 绘制网格线self.cv.create_line(10, 10 + i * 40, 10 + self.size * 40, 10 + i * 40, arrow='none', tags="grid")self.cv.create_line(10 + i * 40, 10, 10 + i * 40, 10 + self.size * 40, arrow='none', tags="grid")def huaqi(self):self.cv.delete("lights")  # 清除之前的灯for i in range(self.size):for u in range(self.size):if self.lights[i][u] == 'you':self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='yellow', width=2, tags="lights")  # 亮灯else:self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='white', width=2, tags="lights")  # 灭灯def reserve(self, x1, y1):if self.lights[x1][y1] == 'wu':self.lights[x1][y1] = 'you'else:self.lights[x1][y1] = 'wu'def luozi(self, event):x1 = (event.x - 10) // 40y1 = (event.y - 10) // 40print(x1, y1)self.reserve(x1, y1)  # 翻转(x1,y1)处灯的状态# 以下翻转(x1,yI)周围的灯的状态#左侧灯的状态反转if x1 != 0:self.reserve(x1 - 1, y1)# 右侧灯的状态反转if x1 != self.size - 1:self.reserve(x1 + 1, y1)# 上侧灯的状态反转if y1 != 0:self.reserve(x1, y1 - 1)# 下侧灯的状态反转if y1 != self.size - 1:self.reserve(x1, y1 + 1)self.huaqi()def random_open_lights(self, difficulty):if difficulty == 'easy':num_of_lights = int(self.size * self.size * 0.2)  # 20%的格子状态为翻转状态elif difficulty == 'medium':num_of_lights = int(self.size * self.size * 0.4)  # 40%的格子状态为翻转状态elif difficulty == 'hard':num_of_lights = int(self.size * self.size * 0.6)  # 60%的格子状态为翻转状态positions = random.sample(range(self.size*self.size), num_of_lights)  # 随机选择要打开的灯的位置for pos in positions:x = pos // self.sizey = pos % self.sizeself.reserve(x, y)def reset_game(size, difficulty):game.size = sizegame.lights = [['wu' for _ in range(size)] for _ in range(size)]game.cv.config(width=size*40+10, height=size*40+10)game.draw_grid()game.random_open_lights(difficulty)game.huaqi()def on_small():reset_game(5, difficulty_var.get())def on_medium():reset_game(8, difficulty_var.get())def on_large():reset_game(12, difficulty_var.get())root = Tk()
game = LightsOutGame(root)menu_frame = Frame(root)
menu_frame.pack()difficulty_var = StringVar()
difficulty_var.set('medium')small_button = Button(menu_frame, text="小", command=on_small)
small_button.pack(side=LEFT)medium_button = Button(menu_frame, text="中", command=on_medium)
medium_button.pack(side=LEFT)large_button = Button(menu_frame, text="大", command=on_large)
large_button.pack(side=LEFT)difficulty_frame = Frame(root)
difficulty_frame.pack()easy_radio = Radiobutton(difficulty_frame, text="容易", variable=difficulty_var, value='easy')
easy_radio.pack(side=LEFT)medium_radio = Radiobutton(difficulty_frame, text="中等", variable=difficulty_var, value='medium')
medium_radio.pack(side=LEFT)hard_radio = Radiobutton(difficulty_frame, text="困难", variable=difficulty_var, value='hard')
hard_radio.pack(side=LEFT)root.mainloop()

扩展完功能三的代码:

3. 游戏开始之前,提示玩家输入唯一的用户名,用于保留该玩家的闯关记录

from tkinter import *
from tkinter import messagebox
import randomclass LightsOutGame:def __init__(self, master=None, size=5, difficulty='medium'):self.master = masterself.size = sizeself.lights = [['wu' for _ in range(size)] for _ in range(size)]self.username = None  # 存储玩家的用户名self.create_widgets()self.random_open_lights(difficulty)  # 随机打开一些灯def create_widgets(self):self.cv = Canvas(self.master, bg='white', width=self.size*40+10, height=self.size*40+10)self.cv.pack()self.draw_grid()  # 绘制网格self.huaqi()  # 绘制灯的状态情况图self.cv.bind('<Button-1>', self.luozi)self.username_label = Label(self.master, text="用户名:")self.username_label.pack()self.username_entry = Entry(self.master)self.username_entry.pack()self.username_button = Button(self.master, text="确认", command=self.set_username)self.username_button.pack()def set_username(self):self.username = self.username_entry.get()self.username_label.config(text="用户名:" + self.username)# 移除用户名输入框和确认按钮self.username_entry.pack_forget()self.username_button.pack_forget()def draw_grid(self):self.cv.delete("grid")  # 清除之前的网格for i in range(self.size+1):  # 绘制网格线self.cv.create_line(10, 10 + i * 40, 10 + self.size * 40, 10 + i * 40, arrow='none', tags="grid")self.cv.create_line(10 + i * 40, 10, 10 + i * 40, 10 + self.size * 40, arrow='none', tags="grid")def huaqi(self):self.cv.delete("lights")  # 清除之前的灯for i in range(self.size):for u in range(self.size):if self.lights[i][u] == 'you':self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='yellow', width=2, tags="lights")  # 亮灯else:self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='white', width=2, tags="lights")  # 灭灯def reserve(self, x1, y1):if self.lights[x1][y1] == 'wu':self.lights[x1][y1] = 'you'else:self.lights[x1][y1] = 'wu'def luozi(self, event):x1 = (event.x - 10) // 40y1 = (event.y - 10) // 40print(x1, y1)self.reserve(x1, y1)  # 翻转(x1,y1)处灯的状态# 以下翻转(x1,yI)周围的灯的状态#左侧灯的状态反转if x1 != 0:self.reserve(x1 - 1, y1)# 右侧灯的状态反转if x1 != self.size - 1:self.reserve(x1 + 1, y1)# 上侧灯的状态反转if y1 != 0:self.reserve(x1, y1 - 1)# 下侧灯的状态反转if y1 != self.size - 1:self.reserve(x1, y1 + 1)self.huaqi()def random_open_lights(self, difficulty):if difficulty == 'easy':num_of_lights = int(self.size * self.size * 0.2)  # 20%的格子状态为翻转状态elif difficulty == 'medium':num_of_lights = int(self.size * self.size * 0.4)  # 40%的格子状态为翻转状态elif difficulty == 'hard':num_of_lights = int(self.size * self.size * 0.6)  # 60%的格子状态为翻转状态positions = random.sample(range(self.size*self.size), num_of_lights)  # 随机选择要打开的灯的位置for pos in positions:x = pos // self.sizey = pos % self.sizeself.reserve(x, y)def reset_game(size, difficulty, username):game.username = usernamegame.size = sizegame.lights = [['wu' for _ in range(size)] for _ in range(size)]game.cv.config(width=size*40+10, height=size*40+10)game.draw_grid()game.random_open_lights(difficulty)game.huaqi()def on_small():reset_game(5, difficulty_var.get(), game.username)def on_medium():reset_game(8, difficulty_var.get(), game.username)def on_large():reset_game(12, difficulty_var.get(), game.username)root = Tk()
game = LightsOutGame(root)menu_frame = Frame(root)
menu_frame.pack()difficulty_var = StringVar()
difficulty_var.set('medium')small_button = Button(menu_frame, text="小", command=on_small)
small_button.pack(side=LEFT)medium_button = Button(menu_frame, text="中", command=on_medium)
medium_button.pack(side=LEFT)large_button = Button(menu_frame, text="大", command=on_large)
large_button.pack(side=LEFT)difficulty_frame = Frame(root)
difficulty_frame.pack()easy_radio = Radiobutton(difficulty_frame, text="容易", variable=difficulty_var, value='easy')
easy_radio.pack(side=LEFT)medium_radio = Radiobutton(difficulty_frame, text="中等", variable=difficulty_var, value='medium')
medium_radio.pack(side=LEFT)hard_radio = Radiobutton(difficulty_frame, text="困难", variable=difficulty_var, value='hard')
hard_radio.pack(side=LEFT)root.mainloop()

扩展完功能四的代码:

按照玩家选择的地图大小和难度开始游戏,游戏过程中记录玩家闯关的总时间以及翻转次数(即开关灯的次数)

from tkinter import *
from tkinter import messagebox
import random
import timeclass LightsOutGame:def __init__(self, master=None, size=5, difficulty='medium'):self.master = masterself.size = sizeself.lights = [['wu' for _ in range(size)] for _ in range(size)]self.username = None  # 存储玩家的用户名self.create_widgets()self.random_open_lights(difficulty)  # 随机打开一些灯self.total_time = 0self.flip_count = 0self.start_time = 0def create_widgets(self):self.cv = Canvas(self.master, bg='white', width=self.size*40+10, height=self.size*40+10)self.cv.pack()self.draw_grid()  # 绘制网格self.huaqi()  # 绘制灯的状态情况图self.cv.bind('<Button-1>', self.luozi)self.username_label = Label(self.master, text="用户名:")self.username_label.pack()self.username_entry = Entry(self.master)self.username_entry.pack()self.username_button = Button(self.master, text="确认", command=self.set_username)self.username_button.pack()def set_username(self):self.username = self.username_entry.get()self.username_label.config(text="用户名:" + self.username)# 移除用户名输入框和确认按钮self.username_entry.pack_forget()self.username_button.pack_forget()def draw_grid(self):self.cv.delete("grid")  # 清除之前的网格for i in range(self.size+1):  # 绘制网格线self.cv.create_line(10, 10 + i * 40, 10 + self.size * 40, 10 + i * 40, arrow='none', tags="grid")self.cv.create_line(10 + i * 40, 10, 10 + i * 40, 10 + self.size * 40, arrow='none', tags="grid")def huaqi(self):self.cv.delete("lights")  # 清除之前的灯for i in range(self.size):for u in range(self.size):if self.lights[i][u] == 'you':self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='yellow', width=2, tags="lights")  # 亮灯else:self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='white', width=2, tags="lights")  # 灭灯def reserve(self, x1, y1):if self.lights[x1][y1] == 'wu':self.lights[x1][y1] = 'you'else:self.lights[x1][y1] = 'wu'def luozi(self, event):x1 = (event.x - 10) // 40y1 = (event.y - 10) // 40print(x1, y1)self.reserve(x1, y1)  # 翻转(x1,y1)处灯的状态# 以下翻转(x1,yI)周围的灯的状态#左侧灯的状态反转if x1 != 0:self.reserve(x1 - 1, y1)# 右侧灯的状态反转if x1 != self.size - 1:self.reserve(x1 + 1, y1)# 上侧灯的状态反转if y1 != 0:self.reserve(x1, y1 - 1)# 下侧灯的状态反转if y1 != self.size - 1:self.reserve(x1, y1 + 1)self.huaqi()self.flip_count += 1def random_open_lights(self, difficulty):if difficulty == 'easy':num_of_lights = int(self.size * self.size * 0.2)  # 20%的格子状态为翻转状态elif difficulty == 'medium':num_of_lights = int(self.size * self.size * 0.4)  # 40%的格子状态为翻转状态elif difficulty == 'hard':num_of_lights = int(self.size * self.size * 0.6)  # 60%的格子状态为翻转状态positions = random.sample(range(self.size*self.size), num_of_lights)  # 随机选择要打开的灯的位置for pos in positions:x = pos // self.sizey = pos % self.sizeself.reserve(x, y)def reset_game(size, difficulty, username):game.username = usernamegame.size = sizegame.lights = [['wu' for _ in range(size)] for _ in range(size)]game.cv.config(width=size*40+10, height=size*40+10)game.draw_grid()game.random_open_lights(difficulty)game.huaqi()game.total_time = 0game.flip_count = 0game.start_time = 0def on_small():reset_game(5, difficulty_var.get(), game.username)def on_medium():reset_game(8, difficulty_var.get(), game.username)def on_large():reset_game(12, difficulty_var.get(), game.username)root = Tk()
game = LightsOutGame(root)menu_frame = Frame(root)
menu_frame.pack()difficulty_var = StringVar()
difficulty_var.set('medium')small_button = Button(menu_frame, text="小", command=on_small)
small_button.pack(side=LEFT)medium_button = Button(menu_frame, text="中", command=on_medium)
medium_button.pack(side=LEFT)large_button = Button(menu_frame, text="大", command=on_large)
large_button.pack(side=LEFT)difficulty_frame = Frame(root)
difficulty_frame.pack()easy_radio = Radiobutton(difficulty_frame, text="容易", variable=difficulty_var, value='easy')
easy_radio.pack(side=LEFT)medium_radio = Radiobutton(difficulty_frame, text="中等", variable=difficulty_var, value='medium')
medium_radio.pack(side=LEFT)hard_radio = Radiobutton(difficulty_frame, text="困难", variable=difficulty_var, value='hard')
hard_radio.pack(side=LEFT)root.mainloop()

扩展完功能五的代码:

 玩家通关成功后,保存相关信息到数据库,表结构可以自定,但应当至少包含以下信息:

 - 玩家选择的地图大小

 - 玩家选择的难度

 - 玩家通关的日期时间

 - 玩家通关所用的总时间

 - 玩家通关所用的总点击次数

import sqlite3
from tkinter import *
from tkinter import messagebox
import random
from datetime import datetimeclass LightsOutGame:def __init__(self, master=None, size=5, difficulty='medium'):self.master = masterself.size = sizeself.lights = [['wu' for _ in range(size)] for _ in range(size)]self.username = None  # 存储玩家的用户名self.create_widgets()self.random_open_lights(difficulty)  # 随机打开一些灯self.total_time = 0self.flip_count = 0self.start_time = 0# 连接到数据库self.conn = sqlite3.connect('game.db')self.cursor = self.conn.cursor()# 创建表self.cursor.execute('''CREATE TABLE IF NOT EXISTS game_records (id INTEGER PRIMARY KEY AUTOINCREMENT,map_size INTEGER,difficulty TEXT,completion_time DATETIME,total_clicks INTEGER)''')def create_widgets(self):self.cv = Canvas(self.master, bg='white', width=self.size*40+10, height=self.size*40+10)self.cv.pack()self.draw_grid()  # 绘制网格self.huaqi()  # 绘制灯的状态情况图self.cv.bind('<Button-1>', self.luozi)self.username_label = Label(self.master, text="用户名:")self.username_label.pack()self.username_entry = Entry(self.master)self.username_entry.pack()self.username_button = Button(self.master, text="确认", command=self.set_username)self.username_button.pack()def set_username(self):self.username = self.username_entry.get()self.username_label.config(text="用户名:" + self.username)# 移除用户名输入框和确认按钮self.username_entry.pack_forget()self.username_button.pack_forget()def draw_grid(self):self.cv.delete("grid")  # 清除之前的网格for i in range(self.size+1):  # 绘制网格线self.cv.create_line(10, 10 + i * 40, 10 + self.size * 40, 10 + i * 40, arrow='none', tags="grid")self.cv.create_line(10 + i * 40, 10, 10 + i * 40, 10 + self.size * 40, arrow='none', tags="grid")def huaqi(self):self.cv.delete("lights")  # 清除之前的灯for i in range(self.size):for u in range(self.size):if self.lights[i][u] == 'you':self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='yellow', width=2, tags="lights")  # 亮灯else:self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='white', width=2, tags="lights")  # 灭灯def reserve(self, x1, y1):if self.lights[x1][y1] == 'wu':self.lights[x1][y1] = 'you'else:self.lights[x1][y1] = 'wu'def luozi(self, event):x1 = (event.x - 10) // 40y1 = (event.y - 10) // 40print(x1, y1)self.reserve(x1, y1)  # 翻转(x1,y1)处灯的状态# 以下翻转(x1,yI)周围的灯的状态#左侧灯的状态反转if x1 != 0:self.reserve(x1 - 1, y1)# 右侧灯的状态反转if x1 != self.size - 1:self.reserve(x1 + 1, y1)# 上侧灯的状态反转if y1 != 0:self.reserve(x1, y1 - 1)# 下侧灯的状态反转if y1 != self.size - 1:self.reserve(x1, y1 + 1)self.huaqi()self.flip_count += 1def random_open_lights(self, difficulty):if difficulty == 'easy':num_of_lights = int(self.size * self.size * 0.2)  # 20%的格子状态为翻转状态elif difficulty == 'medium':num_of_lights = int(self.size * self.size * 0.4)  # 40%的格子状态为翻转状态elif difficulty == 'hard':num_of_lights = int(self.size * self.size * 0.6)  # 60%的格子状态为翻转状态positions = random.sample(range(self.size*self.size), num_of_lights)  # 随机选择要打开的灯的位置for pos in positions:x = pos // self.sizey = pos % self.sizeself.reserve(x, y)def save_game_record(self):# 保存通关信息到数据库map_size = self.sizedifficulty = difficulty_var.get()completion_time = datetime.now()total_clicks = self.flip_countself.cursor.execute('''INSERT INTO game_records (map_size, difficulty, completion_time, total_clicks)VALUES (?, ?, ?, ?)''', (map_size, difficulty, completion_time, total_clicks))self.conn.commit()def reset_game(size, difficulty, username):game.username = usernamegame.size = sizegame.lights = [['wu' for _ in range(size)] for _ in range(size)]game.cv.config(width=size*40+10, height=size*40+10)game.draw_grid()game.random_open_lights(difficulty)game.huaqi()game.total_time = 0game.flip_count = 0game.start_time = 0def on_small():reset_game(5, difficulty_var.get(), game.username)def on_medium():reset_game(8, difficulty_var.get(), game.username)def on_large():reset_game(12, difficulty_var.get(), game.username)root = Tk()
game = LightsOutGame(root)menu_frame = Frame(root)
menu_frame.pack()difficulty_var = StringVar()
difficulty_var.set('medium')small_button = Button(menu_frame, text="小", command=on_small)
small_button.pack(side=LEFT)medium_button = Button(menu_frame, text="中", command=on_medium)
medium_button.pack(side=LEFT)large_button = Button(menu_frame, text="大", command=on_large)
large_button.pack(side=LEFT)difficulty_frame = Frame(root)
difficulty_frame.pack()easy_radio = Radiobutton(difficulty_frame, text="容易", variable=difficulty_var, value='easy')
easy_radio.pack(side=LEFT)medium_radio = Radiobutton(difficulty_frame, text="中等", variable=difficulty_var, value='medium')
medium_radio.pack(side=LEFT)hard_radio = Radiobutton(difficulty_frame, text="困难", variable=difficulty_var, value='hard')
hard_radio.pack(side=LEFT)save_button = Button(root, text="保存通关信息", command=game.save_game_record)
save_button.pack()root.mainloop()# 关闭数据库连接
game.conn.close()

扩展完功能六的代码:

设计窗口界面,显示通关排行榜,具体说明如下:

 - 根据不同地图大小以及不同难度显示各自排名,例如:大地图中等难度与中地图困难难度的排名是分开的

 - 可以选择显示总排名(所有玩家的记录)与个人排名(玩家自己的所有记录)

 - 可以选择根据通关时间排名,以及根据点击次数排名,两个排名都是按从小到大排列

import sqlite3
from tkinter import *
from tkinter import messagebox
import random
from datetime import datetimeclass LightsOutGame:def __init__(self, master=None, size=5, difficulty='medium'):self.master = masterself.size = sizeself.lights = [['wu' for _ in range(size)] for _ in range(size)]self.username = None  # 存储玩家的用户名self.create_widgets()self.random_open_lights(difficulty)  # 随机打开一些灯self.total_time = 0self.flip_count = 0self.start_time = 0# 连接到数据库self.conn = sqlite3.connect('game.db')self.cursor = self.conn.cursor()# 创建表self.cursor.execute('''CREATE TABLE IF NOT EXISTS game_records (id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT,map_size INTEGER,difficulty TEXT,completion_time DATETIME,total_clicks INTEGER)''')def create_widgets(self):self.cv = Canvas(self.master, bg='white', width=self.size * 40 + 10, height=self.size * 40 + 10)self.cv.pack()self.draw_grid()  # 绘制网格self.huaqi()  # 绘制灯的状态情况图self.cv.bind('<Button-1>', self.luozi)self.username_label = Label(self.master, text="用户名:")self.username_label.pack()self.username_entry = Entry(self.master)self.username_entry.pack()self.username_button = Button(self.master, text="确认", command=self.set_username)self.username_button.pack()self.show_leaderboard_button = Button(self.master, text="显示排行榜", command=self.show_leaderboard)self.show_leaderboard_button.pack()def set_username(self):self.username = self.username_entry.get()self.username_label.config(text="用户名:" + self.username)# 移除用户名输入框和确认按钮self.username_entry.pack_forget()self.username_button.pack_forget()def draw_grid(self):self.cv.delete("grid")  # 清除之前的网格for i in range(self.size + 1):  # 绘制网格线self.cv.create_line(10, 10 + i * 40, 10 + self.size * 40, 10 + i * 40, arrow='none', tags="grid")self.cv.create_line(10 + i * 40, 10, 10 + i * 40, 10 + self.size * 40, arrow='none', tags="grid")def huaqi(self):self.cv.delete("lights")  # 清除之前的灯for i in range(self.size):for u in range(self.size):if self.lights[i][u] == 'you':self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='yellow', width=2, tags="lights")  # 亮灯else:self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='white', width=2, tags="lights")  # 灭灯def reserve(self, x1, y1):if self.lights[x1][y1] == 'wu':self.lights[x1][y1] = 'you'else:self.lights[x1][y1] = 'wu'def luozi(self, event):x1 = (event.x - 10) // 40y1 = (event.y - 10) // 40print(x1, y1)self.reserve(x1, y1)  # 翻转(x1,y1)处灯的状态# 以下翻转(x1,yI)周围的灯的状态#左侧灯的状态反转if x1 != 0:self.reserve(x1 - 1, y1)# 右侧灯的状态反转if x1 != self.size - 1:self.reserve(x1 + 1, y1)# 上侧灯的状态反转if y1 != 0:self.reserve(x1, y1 - 1)# 下侧灯的状态反转if y1 != self.size - 1:self.reserve(x1, y1 + 1)self.huaqi()self.flip_count += 1def random_open_lights(self, difficulty):if difficulty == 'easy':num_of_lights = int(self.size * self.size * 0.2)  # 20%的格子状态为翻转状态elif difficulty == 'medium':num_of_lights = int(self.size * self.size * 0.4)  # 40%的格子状态为翻转状态elif difficulty == 'hard':num_of_lights = int(self.size * self.size * 0.6)  # 60%的格子状态为翻转状态positions = random.sample(range(self.size * self.size), num_of_lights)  # 随机选择要打开的灯的位置for pos in positions:x = pos // self.sizey = pos % self.sizeself.reserve(x, y)def save_game_record(self):# 保存通关信息到数据库map_size = self.sizedifficulty = difficulty_var.get()completion_time = datetime.now()total_clicks = self.flip_countself.cursor.execute('''INSERT INTO game_records (username, map_size, difficulty, completion_time, total_clicks)VALUES (?, ?, ?, ?, ?)''', (self.username, map_size, difficulty, completion_time, total_clicks))self.conn.commit()  # 确保数据被写入数据库def show_leaderboard(self):leaderboard_window = Toplevel(self.master)leaderboard_window.title("通关排行榜")leaderboard_frame = Frame(leaderboard_window)leaderboard_frame.pack()map_size = self.sizedifficulty = difficulty_var.get()self.cursor.execute('''SELECT username, completion_time, total_clicks FROM game_recordsWHERE map_size=? AND difficulty=?ORDER BY completion_time, total_clicks''', (map_size, difficulty))records = self.cursor.fetchall()if len(records) == 0:messagebox.showinfo("提示", "暂无记录")else:for i, record in enumerate(records):username_label = Label(leaderboard_frame, text=record[0])username_label.grid(row=i, column=0)completion_time_label = Label(leaderboard_frame, text=record[1])completion_time_label.grid(row=i, column=1)total_clicks_label = Label(leaderboard_frame, text=record[2])total_clicks_label.grid(row=i, column=2)def reset_game(size, difficulty, username):game.username = usernamegame.size = sizegame.lights = [['wu' for _ in range(size)] for _ in range(size)]game.cv.config(width=size * 40 + 10, height=size * 40 + 10)game.draw_grid()game.random_open_lights(difficulty)game.huaqi()game.total_time = 0game.flip_count = 0game.start_time = 0def on_small():reset_game(5, difficulty_var.get(), game.username)def on_medium():reset_game(8, difficulty_var.get(), game.username)def on_large():reset_game(12, difficulty_var.get(), game.username)root = Tk()
game = LightsOutGame(root)menu_frame = Frame(root)
menu_frame.pack()difficulty_var = StringVar()
difficulty_var.set('medium')small_button = Button(menu_frame, text="小", command=on_small)
small_button.pack(side=LEFT)medium_button = Button(menu_frame, text="中", command=on_medium)
medium_button.pack(side=LEFT)large_button = Button(menu_frame, text="大", command=on_large)
large_button.pack(side=LEFT)difficulty_frame = Frame(root)
difficulty_frame.pack()easy_radio = Radiobutton(difficulty_frame, text="容易", variable=difficulty_var, value='easy')
easy_radio.pack(side=LEFT)medium_radio = Radiobutton(difficulty_frame, text="中等", variable=difficulty_var, value='medium')
medium_radio.pack(side=LEFT)hard_radio = Radiobutton(difficulty_frame, text="困难", variable=difficulty_var, value='hard')
hard_radio.pack(side=LEFT)save_button = Button(root, text="保存通关信息", command=game.save_game_record)
save_button.pack()root.mainloop()# 关闭数据库连接
game.conn.close()

扩展添加了一个查询次数和时间的代码(最终):

import sqlite3
from tkinter import *
from tkinter import messagebox
import random
from datetime import datetimeclass LightsOutGame:def __init__(self, master=None, size=5, difficulty='medium'):self.master = masterself.size = sizeself.lights = [['wu' for _ in range(size)] for _ in range(size)]self.username = None  # 存储玩家的用户名self.create_widgets()self.random_open_lights(difficulty)  # 随机打开一些灯self.total_time = 0self.flip_count = 0self.start_time = 0# 连接到数据库self.conn = sqlite3.connect('game.db')self.cursor = self.conn.cursor()# 创建表self.cursor.execute('''CREATE TABLE IF NOT EXISTS game_records (id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT,map_size INTEGER,difficulty TEXT,completion_time DATETIME,total_clicks INTEGER)''')def create_widgets(self):self.cv = Canvas(self.master, bg='white', width=self.size * 40 + 10, height=self.size * 40 + 10)self.cv.pack()self.draw_grid()  # 绘制网格self.huaqi()  # 绘制灯的状态情况图self.cv.bind('<Button-1>', self.luozi)self.username_label = Label(self.master, text="用户名:")self.username_label.pack()self.username_entry = Entry(self.master)self.username_entry.pack()self.username_button = Button(self.master, text="确认", command=self.set_username)self.username_button.pack()self.show_leaderboard_button = Button(self.master, text="显示排行榜", command=self.show_leaderboard)self.show_leaderboard_button.pack()self.stats_button = Button(self.master, text="查询统计", command=self.show_game_stats)self.stats_button.pack()self.stats_label = Label(self.master, text="")self.stats_label.pack()def set_username(self):self.username = self.username_entry.get()self.username_label.config(text="用户名:" + self.username)# 移除用户名输入框和确认按钮self.username_entry.pack_forget()self.username_button.pack_forget()def draw_grid(self):self.cv.delete("grid")  # 清除之前的网格for i in range(self.size + 1):  # 绘制网格线self.cv.create_line(10, 10 + i * 40, 10 + self.size * 40, 10 + i * 40, arrow='none', tags="grid")self.cv.create_line(10 + i * 40, 10, 10 + i * 40, 10 + self.size * 40, arrow='none', tags="grid")def huaqi(self):self.cv.delete("lights")  # 清除之前的灯for i in range(self.size):for u in range(self.size):if self.lights[i][u] == 'you':self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='yellow', width=2, tags="lights")  # 亮灯else:self.cv.create_oval(i * 40 + 10, u * 40 + 10, (i + 1) * 40 + 10, (u + 1) * 40 + 10,outline='white', fill='white', width=2, tags="lights")  # 灭灯def reserve(self, x1, y1):if self.lights[x1][y1] == 'wu':self.lights[x1][y1] = 'you'else:self.lights[x1][y1] = 'wu'def luozi(self, event):x1 = (event.x - 10) // 40y1 = (event.y - 10) // 40print(x1, y1)self.reserve(x1, y1)  # 翻转(x1,y1)处灯的状态# 以下翻转(x1,yI)周围的灯的状态#左侧灯的状态反转if x1 != 0:self.reserve(x1 - 1, y1)# 右侧灯的状态反转if x1 != self.size - 1:self.reserve(x1 + 1, y1)# 上侧灯的状态反转if y1 != 0:self.reserve(x1, y1 - 1)# 下侧灯的状态反转if y1 != self.size - 1:self.reserve(x1, y1 + 1)self.huaqi()self.flip_count += 1def random_open_lights(self, difficulty):if difficulty == 'easy':num_of_lights = int(self.size * self.size * 0.2)  # 20%的格子状态为翻转状态elif difficulty == 'medium':num_of_lights = int(self.size * self.size * 0.4)  # 40%的格子状态为翻转状态elif difficulty == 'hard':num_of_lights = int(self.size * self.size * 0.6)  # 60%的格子状态为翻转状态positions = random.sample(range(self.size * self.size), num_of_lights)  # 随机选择要打开的灯的位置for pos in positions:x = pos // self.sizey = pos % self.sizeself.reserve(x, y)def save_game_record(self):# 保存通关信息到数据库map_size = self.sizedifficulty = difficulty_var.get()completion_time = datetime.now()total_clicks = self.flip_countself.cursor.execute('''INSERT INTO game_records (username, map_size, difficulty, completion_time, total_clicks)VALUES (?, ?, ?, ?, ?)''', (self.username, map_size, difficulty, completion_time, total_clicks))self.conn.commit()  # 确保数据被写入数据库def show_leaderboard(self):leaderboard_window = Toplevel(self.master)leaderboard_window.title("通关排行榜")leaderboard_frame = Frame(leaderboard_window)leaderboard_frame.pack()map_size = self.sizedifficulty = difficulty_var.get()self.cursor.execute('''SELECT username, completion_time, total_clicks FROM game_recordsWHERE map_size=? AND difficulty=?ORDER BY completion_time, total_clicks''', (map_size, difficulty))records = self.cursor.fetchall()if len(records) == 0:messagebox.showinfo("提示", "暂无记录")else:for i, record in enumerate(records):username_label = Label(leaderboard_frame, text=record[0])username_label.grid(row=i, column=0)completion_time_label = Label(leaderboard_frame, text=record[1])completion_time_label.grid(row=i, column=1)total_clicks_label = Label(leaderboard_frame, text=record[2])total_clicks_label.grid(row=i, column=2)def get_game_stats(self):self.cursor.execute('''SELECT SUM(completion_time), SUM(total_clicks) FROM game_recordsWHERE username=?''', (self.username,))result = self.cursor.fetchone()return resultdef show_game_stats(self):stats = self.get_game_stats()total_time = stats[0]total_clicks = stats[1]self.stats_label.config(text=f"总时间:{total_time} 秒,翻转次数:{total_clicks} 次")def reset_game(size, difficulty, username):game.username = usernamegame.size = sizegame.lights = [['wu' for _ in range(size)] for _ in range(size)]game.cv.config(width=size * 40 + 10, height=size * 40 + 10)game.draw_grid()game.random_open_lights(difficulty)game.huaqi()game.total_time = 0game.flip_count = 0game.start_time = 0game.stats_label.config(text="")def on_small():reset_game(5, difficulty_var.get(), game.username)def on_medium():reset_game(8, difficulty_var.get(), game.username)def on_large():reset_game(12, difficulty_var.get(), game.username)root = Tk()
game = LightsOutGame(root)menu_frame = Frame(root)
menu_frame.pack()difficulty_var = StringVar()
difficulty_var.set('medium')small_button = Button(menu_frame, text="小", command=on_small)
small_button.pack(side=LEFT)medium_button = Button(menu_frame, text="中", command=on_medium)
medium_button.pack(side=LEFT)large_button = Button(menu_frame, text="大", command=on_large)
large_button.pack(side=LEFT)difficulty_frame = Frame(root)
difficulty_frame.pack()easy_radio = Radiobutton(difficulty_frame, text="容易", variable=difficulty_var, value='easy')
easy_radio.pack(side=LEFT)medium_radio = Radiobutton(difficulty_frame, text="中等", variable=difficulty_var, value='medium')
medium_radio.pack(side=LEFT)hard_radio = Radiobutton(difficulty_frame, text="困难", variable=difficulty_var, value='hard')
hard_radio.pack(side=LEFT)save_button = Button(root, text="保存通关信息", command=game.save_game_record)
save_button.pack()root.mainloop()# 关闭数据库连接
game.conn.close()

相关问题:

  1. 请描述你的系统详细设计(提示:按照需求分析列出概要功能点)

答:系统详细设计:

创建一个名为LightsOutGame的类,用于实现Lights Out游戏的逻辑和界面。

在初始化方法中,设置游戏的大小、难度,并创建一个二维数组来表示灯的状态。

创建游戏界面,包括一个Canvas用于绘制网格和灯的状态,一个Label用于显示用户名,一个Entry和一个Button用于输入和确认用户名,以及一些按钮用于选择游戏难度和操作。

实现灯的状态翻转和绘制网格、灯的方法。

实现随机打开一些灯的方法,根据难度设置打开的灯的数量。

实现保存通关记录到数据库的方法,包括用户名、地图大小、难度、完成时间和翻转次数。

实现显示排行榜和查询统计的方法,从数据库中获取相应的记录并展示在界面上。

实现重置游戏的方法,根据选择的地图大小和难度重新设置游戏参数,并清空灯的状态和统计数据。

创建一个主窗口,并将游戏对象放入其中,启动主循环。

  1. 请通过原型图+说明的方式展示你的界面详细设计

答:

界面详细设计:

主界面包括一个Canvas用于绘制网格和灯的状态,一个Label用于显示用户名,一个Entry和一个Button用于输入和确认用户名,以及一些按钮用于选择游戏难度和操作。

用户名输入框和确认按钮位于顶部,用于输入和确认用户名。

网格和灯的状态绘制在Canvas上,根据灯的状态绘制不同的颜色和形状。

游戏难度选择按钮位于中间,用于选择不同的难度。

排行榜和查询统计按钮位于底部,用于显示排行榜和查询统计信息。

  1. 请通过ER图+表结构说明的方式展示你的数据库详细设计

答:

数据库详细设计:

创建一个名为game_records的表,用于存储通关记录。

表中包含以下字段:id(主键,自增),username(用户名),map_size(地图大小),difficulty(难度),completion_time(完成时间),total_clicks(翻转次数)。

使用SQLite数据库,通过Python的sqlite3模块连接和操作数据库。

在游戏开始时创建表,如果表已存在则不创建。

在保存通关记录时,将相关信息插入到game_records表中。

在显示排行榜和查询统计时,从game_records表中查询相应的记录,并按照完成时间和翻转次数排序。

  1. 请按功能点顺序(1-6)依次展示你的程序运行截图:

答:功能1:

功能2:

功能3:

功能4:

功能5:

功能6:

  1. 请讨论说明图形界面设计(GUI)包括哪些重要组成部分?

答:图形界面设计(GUI)的重要组成部分包括:

窗口:用于承载界面的容器,提供标题栏、边框和关闭按钮等功能。

组件:用于构建界面的可视化元素,如按钮、标签、文本框、下拉框等。

布局管理器:用于控制组件在窗口中的位置和大小,如pack、grid和place等。

事件处理:用于响应用户的操作,如点击按钮、输入文本等,触发相应的事件处理函数。

图形绘制:用于绘制图形元素,如绘制网格、绘制灯的状态等。

2、请讨论说明在本实验中,你应如何使用数据库支撑整个应用程序的正确运行?

答:在本实验中,数据库用于支持整个应用程序的正确运行,具体包括:

创建数据库表game_records用于存储游戏记录。

在游戏通关时,将通关信息插入到game_records表中。

在显示排行榜时,从game_records表中查询对应难度和地图大小的游戏记录,并在界面上显示。

3、其他关于完成本次实验作业中的心得与讨论

答:本次实验是一个简单的游戏的实现,通过使用Tkinter库构建了一个图形界面,并使用SQLite数据库存储游戏记录。通过这个实验,我学习到了如何使用Tkinter库创建图形界面,以及如何使用SQLite数据库进行数据存储和查询。同时,我也了解到了图形界面设计的重要组成部分和数据库的应用场景。这个实验对于我进一步掌握Python编程和图形界面开发有很大的帮助,也提升了我的数据库操作能力。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/174312.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python实现扫雷游戏,代码示例,边玩边学+回忆童年!

文章目录 前言实现总结关于Python技术储备一、Python所有方向的学习路线二、Python基础学习视频三、精品Python学习书籍四、Python工具包项目源码合集①Python工具包②Python实战案例③Python小游戏源码五、面试资料六、Python兼职渠道 前言 扫雷是一款益智类小游戏&#xff0…

【LeetCode刷题-双指针】--80.删除有序数组中的重复项II

80.删除有序数组中的重复项II 方法&#xff1a;双指针 因为给定数组是有序的&#xff0c;所以相同元素必然连续&#xff0c;使用双指针解决&#xff0c;遍历数组检查每一个元素是否应该被保留&#xff0c;如果应该保留&#xff0c;就将其移动到指定位置。我们定义两个指针slow…

【Java0基础学Java第八颗】 -- 继承与多态 -- 多态

8.继承与多态 8.2 多态8.2.1 多态的概念8.2.2 多态实现条件8.2.3 重写8.2.4 向上转型和向下转型8.2.5 向下转型8.2.6 多态的优缺点8.2.7 避免在构造方法中调用重写的方法 8.2 多态 8.2.1 多态的概念 通俗来说就是多种形态&#xff0c;具体点就是去完成某个行为&#xff0c;当…

ZYNQ_project:ram_dual_port

伪双端口ram&#xff1a;写端口&#xff1a;clk_w,en_A,we_A,addr_A,din_A;读端口:clk_r,en_B,addr_B;dout_B. 设计读写模块&#xff0c;写入256个数据&#xff0c;再读出256个数据。 输入时钟100Mhz&#xff0c;输出时钟50Mhz。 多bit数据&#xff0c;高速时钟域到低速时钟…

做一个springboot登陆注册功能

目录 一、环境搭建 1、数据库 2、引入依赖 3、配置信息 4、创建包结构和数据库实体类 二、接口开发-注册接口 前提准备 响应数据 需求分析 全局异常处理 代码编写 测试 三、接口开发-登录接口 前提准备 响应数据 需求分析 代码编写 测试 拦截器 测试 一、环…

字典管理怎么使用,vue3项目使用若依的的字典管理模块

若依框架数据字典的使用_若依数据字典_哈哈水水水水的博客-CSDN博客 【精选】关于数据字典的理解与设计_数据字典怎么设计-CSDN博客 若依的字典值如何使用&#xff08;超详细图文教程&#xff09;_若依字典管理_丿BAIKAL巛的博客-CSDN博客 Vue3组合式API&#xff1a;getCurr…

行业追踪,2023-11-13

自动复盘 2023-11-13 凡所有相&#xff0c;皆是虚妄。若见诸相非相&#xff0c;即见如来。 k 线图是最好的老师&#xff0c;每天持续发布板块的rps排名&#xff0c;追踪板块&#xff0c;板块来开仓&#xff0c;板块去清仓&#xff0c;丢弃自以为是的想法&#xff0c;板块去留让…

CentOs7 NAT模式连接网络

1.配置动态网络 1.1 检查主机网卡配置 检查主机的网络设置 进入控制面板&#xff0c;找到网络共享中心 查看适配器是否都已经开启 1.2 设置虚拟机的网络配置 打开虚拟机网络配置设置&#xff0c;对网卡VMnet8 进行设置 记住网关 全部选择应用&#xff0c;确定 1.3 设置…

您的计算机已被Mallox勒索病毒感染?恢复您的数据的方法在这里!

尊敬的读者&#xff1a; 随着科技的迅速发展&#xff0c;网络安全问题日益凸显&#xff0c;其中勒索病毒是一种极具威胁性的恶意软件。在这些勒索病毒中&#xff0c;.mallox 勒索病毒尤为突出&#xff0c;它能够加密用户的数据文件&#xff0c;要求支付赎金才能解密。本文将介…

【Python基础】一个简单的TCP通信程序

&#x1f308;欢迎来到Python专栏 &#x1f64b;&#x1f3fe;‍♀️作者介绍&#xff1a;前PLA队员 目前是一名普通本科大三的软件工程专业学生 &#x1f30f;IP坐标&#xff1a;湖北武汉 &#x1f349; 目前技术栈&#xff1a;C/C、Linux系统编程、计算机网络、数据结构、Mys…

从0开始python学习-34.pytest常用插件

目录 1. pytest-html&#xff1a;生成HTML测试报告 2.pytest-xdist&#xff1a;并发执行用例 3. pytest-order&#xff1a;自定义用例的执行顺序 4. pytest-rerunfailures&#xff1a;用例失败时自动重试 5. pytest-result-log:用例执行结果记录到日志文件 1. pytest-html…

STM32笔记—USART

课外知识插入&#xff1a;STM32单片机extern全局变量_stm32全局变量-CSDN博客 如果你把temple定义在A中&#xff0c;然后让A.h和B.h包含在includes.h中&#xff0c;然后把includes.h放在A.c和B.c中单个编译是没有问题的&#xff0c;但是链接的时候会出现问题&#xff0c; “S…