我正在使用Kivy创建井字游戏。我想首先提供该应用程序执行的基本操作,以使我对我的应用程序的工作原理有所了解。该应用程序当前具有多用户登录支持。许多玩家可以注册,他们的信息将被存储并可以在以后使用(使用外部.txt文件)。游戏还将个人玩家分数保存到另一个.txt文件中。该应用程序具有许多不同的屏幕,例如注册屏幕,登录屏幕,高分屏幕,设置屏幕等。
当某人注册时,他们的信息(例如姓名,电子邮件和密码)存储在login_info.txt文件中。同时,它们的名称和初始得分为0都存储在highscores.txt文件中。如果玩家赢得游戏,则零将增加1,然后再次保存到高分文件中。
我在下面显示设置屏幕:
“管理玩家”屏幕是此问题的主要主题。
当我单击“删除播放器”时,会出现一个弹出窗口:
点击“删除播放器”可以完成以下操作:
因此,这里的主要问题是在关闭弹出窗口并且播放器从两个.txt文件中消失之后,“管理播放器”屏幕仍显示该播放器的名称和将其删除的按钮。当我关闭该应用程序然后再次运行该应用程序时,我看到先前删除的播放器不见了。我想改善这一点。我希望播放器及其按钮立即消失,因为信息已从.txt文件中删除。因此,一旦按下“删除播放器”按钮,有什么方法可以“加载”或“刷新”屏幕,以便重新加载屏幕,并且由于屏幕从.txt文件中读取数据,因此不会显示该播放器,因为我们删除了他。我认为另一种方式是清除屏幕(也许用self.canvas.clear()
?),然后读取.txt文件,然后将播放器名称及其按钮再次添加到屏幕上。
这是我在“管理播放器”屏幕上的代码。这相当大,可能会有令人困惑的代码,这就是为什么我写了有关某人注册时会发生什么或删除玩家的本质的原因。
class ManagePlayers(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Creating an inside Layout allows the player names and buttons to be
# on one layout and the "back" button to be on another layout.
self.inside = GridLayout()
self.file = open("login_info.txt", "r")
self.inside.cols = 2
self.inside.rows = len(self.file.readlines())
self.file.close()
self.highscores = open("highscores.txt", "r")
self.file = self.highscores.readlines()[1:]
# Here we are reading all the scores of each players and also
# converting them to integers and stripping of
# the newline character
self.scores = [int(entry.split(",")[1].strip("\n")) for entry in self.file]
self.highscores.close()
with open("login_info.txt", "r") as file:
file_contents = file.readlines()[1:]
file_contents = [content.strip("\n") for content in file_contents]
buttons_dict = {}
labels_dict = {}
def manage_player_deletion(instance):
pop_up_layout = BoxLayout(orientation='vertical', padding=(10))
deletion_message = Label(
text="Are you sure? You cannot undo this.", color=[1, 0, 0, 1])
pop_up_layout.add_widget(deletion_message)
yes_button = Button(text="Yes delete the player")
no_button = Button(
text="I changed my mind. The player can stay!")
pop_up_layout.add_widget(yes_button)
pop_up_layout.add_widget(no_button)
def delete_player(fake_instance):
# The parameter is named "fake_instance" so that it
# doesn't get mixed with the main
# "instance" in the outer function manage_player_deletion
for label in labels_dict.values():
if label.text in instance.text:
for data in file_contents:
# Stripping the whitespace between first and last name
# This code removes player from login_info.txt
if label.text.replace(" ", "") == "".join(data.split(",")[0] + data.split(",")[1]):
with open("login_info.txt", "r") as f:
lines = f.readlines()
with open("login_info.txt", "w") as f:
for line in lines:
if line.strip("\n") != data:
f.write(line)
# This code removes player from highscores.txt
with open("highscores.txt", "r") as f:
lines = f.readlines()
with open("highscores.txt", "w") as f:
for line in lines:
name, _ = line.split(",")
if name not in instance.text:
f.write(line)
deletion_message.text = "Success! This popup will dismiss any time now."
deletion_message.color = [0, 1, 0, 1]
Clock.schedule_once(popup.dismiss, 0.5)
popup = Popup(title='Player deletion confirmation', title_size=(30),
title_align='center', content=pop_up_layout,
size_hint=(None, None), size=(400, 400),
auto_dismiss=False)
yes_button.bind(on_press=delete_player)
no_button.bind(on_press=popup.dismiss)
popup.open()
for count, content in enumerate(file_contents, start=1):
first, last, *_ = content.split(",")
name = first + " " + last
labels_dict["player_label_" + str(count)] = Label(text=name)
buttons_dict["remove_butt_" + str(count)] = Button(
text=f"Remove {name}", on_press=manage_player_deletion)
for label, button in zip(labels_dict.values(), buttons_dict.values()):
self.inside.add_widget(label)
self.inside.add_widget(button)
self.add_widget(self.inside)
self.cols = 1
self.go_back = Button(text="Go back to settings", size_hint=(
0.8, 0.19), on_press=self.back_to_settings)
self.add_widget(self.go_back)
def back_to_settings(self, instance):
tic_tac_toe.screen_manager.current = "Settings Page"
谢谢,我们将不胜感激。