单击按钮后更改 Pyglet Window 的内容
Changing the contents of a Pyglet Window, after clicking a buttons
最近,我正在从 Pyglet 制作一个 UI 应用程序,它基本上是一个 To-Doer。它由一个主菜单组成,它将显示文件夹中的所有文本文件。这些文本文件包含我必须执行的任务列表,文本文件的名称作为这些任务的通用名称。例如,如果我在编写游戏代码时必须制作一个待办事项列表,则文件名可以是“Pong.txt”并且可能包含一个列表 ["Set up background", "Player Inputs", "Character movement", "UI", "Exporting"]
。单击项目旁边的项目符号后,它应该会打开一个菜单,显示我必须执行的所有任务。还有一个加号可以为新项目创建新任务,您输入的第一件事就是文件名。
即将出现的问题是从主菜单到待办事项菜单的过渡,它应该显示在哪里。
- 设置背景
- 玩家输入
- 角色移动
- UI
- 正在导出
点击一个按钮。由于代码有 300 行,我不确定哪些部分很重要,所以我在这里附上了 整个 代码,因为在 Stack,他们很欣赏,有问题的代码而不是一个 GitHub link.
def draw(line:list):
dataCopy.clear()
textList.clear()
coords.clear()
dataCopyGenerator(len(line), dataCopy)
endpoint = 585 - (len(dataCopy) * 45)
for i in range(585, endpoint, -45):
l = len(dataCopy) - (585 - i)//45
j = len(dataCopy) - l
if i % 45 == 0 and j <= len(dataCopy):
if len(textList) >= len(boxList):
dataCopy[j] = pt.shapes.Rectangle(10, i-15, 30, 30, color=pink, batch=otherMenu)
boxList.append(dataCopy[j])
dataCopy[j].draw()
if {'x': 10, 'y': i, 'size': 30} in coords:
continue
else:
coords.append({'x': 10, 'y': i, 'size': 30})
dataCopy[j] = pt.text.Label(t, font_name="Times New Roman", font_size=24, x=60,
y=i, anchor_x='left', anchor_y='center', batch=otherMenu, color=TextColor2)
textList.append(dataCopy[j])
for i in range(len(textList)):
print(i)
textList[i].text = line[i]
def mainMenu():
filesCopy.clear()
dataCopyGenerator(len(files), filesCopy)
endpoint = 585 - (len(filesCopy) * 45)
for i in range(585, endpoint, -45):
l = len(filesCopy) - (585 - i)//45
j = len(filesCopy) - l
if i % 45 == 0 and j <= len(filesCopy):
if len(filesList) >= len(circleList):
filesCopy[j] = pt.shapes.Circle(30, i-5, radius=10,color=pink, batch=menu)
circleList.append(filesCopy[j])
filesCopy[j].draw()
if {'x': 10, 'y': i, 'size': 30} in menuCoords:
continue
else:
menuCoords.append({'x': 10, 'y': i, 'size': 30})
filesCopy[j] = pt.text.Label(t, font_name="Times New Roman", font_size=24, x=60,
y=i, anchor_x='left', anchor_y='center', batch=menu, color=TextColor2)
filesList.append(filesCopy[j])
for i in range(len(filesList)):
filesList[i].text = files[i]
@window.event
def on_mouse_press(x, y, button, modifiers):
global data, menuIsVisible, line
#Main Menu
for i in range(len(menuCoords)):
if (button == pt.window.mouse.LEFT) and x > circleList[i].x - circleList[i].radius and x < ( circleList[i].x+ circleList[i].radius) and y > circleList[i].y - circleList[i].radius and y < ( circleList[i].y + circleList[i].radius):
menuIsVisible == False
back_line1.visible = True
back_line2.visible = True
with open(files[i] + ".txt", "r") as f:
line = f.read()
line1 = list(map(str.strip, line.strip('][').replace('"', '').split(',')))
draw(line1)
print (line1)
return menuIsVisible, line1
@window.event
def on_draw():
global menuIsVisible, line1
window.clear()
batch.draw()
title.draw()
textFieldB.draw()
typed_text.draw()
if menuIsVisible == True:
mainMenu()
menu.draw()
elif menuIsVisible == False:
draw(line1)
print
otherMenu.draw()
@window.event
def on_mouse_scroll(x, y, scroll_x, scroll_y):
if scroll_y == 1:
if title.y < background.height - 40:
return
else:
for i in range(len(textList)):
textList[i].y -= 10
for i in range(len(boxList)):
boxList[i].y -= 10
title.y -= 10
elif scroll_y == -1:
for i in range(len(textList)):
textList[i].y += 10
for i in range(len(boxList)):
boxList[i].y += 10
title.y += 10
pt.app.run()
点赞!
编辑:
我只发布了绘图功能、主菜单和其他菜单以及一些重要的 window.event.
您需要的是场景管理器。场景管理器在制作游戏时基本上处理所有的场景变化和类似的事情。在 pyglet 中创建一个的一个好方法是为所有项目创建一个 class。然后在 class 中创建一个函数,它将隐藏和显示所有项目
class item1:
def __init__(self):
self.obj1 = pyglet.shapes.Rectangle(x, y, width, height, color, batch)
self.obj2 = pyglet.shapes.Rectangle(x, y, width, height, color, batch)
def show(self):
obj1.visible = True
obj2.visible = True
def hide(self):
obj1.visible = False
obj2.visible = False
class item2:
def __init__(self):
self.obj1 = pyglet.shapes.Rectangle(x, y, width, height, color, batch)
self.obj2 = pyglet.shapes.Rectangle(x, y, width, height, color, batch)
def show(self):
obj1.visible = True
obj2.visible = True
def hide(self):
obj1.visible = False
obj2.visible = False
现在创建一个 class 名为 sceneManager
class sceneManager:
def __init__(self):
pass
def scene1(self):
item1.show()
item2.hide()
def scene2(self):
item1.hide()
item2.show()
现在您可以在单击按钮后调用 sceneManager.scene2()
。
最近,我正在从 Pyglet 制作一个 UI 应用程序,它基本上是一个 To-Doer。它由一个主菜单组成,它将显示文件夹中的所有文本文件。这些文本文件包含我必须执行的任务列表,文本文件的名称作为这些任务的通用名称。例如,如果我在编写游戏代码时必须制作一个待办事项列表,则文件名可以是“Pong.txt”并且可能包含一个列表 ["Set up background", "Player Inputs", "Character movement", "UI", "Exporting"]
。单击项目旁边的项目符号后,它应该会打开一个菜单,显示我必须执行的所有任务。还有一个加号可以为新项目创建新任务,您输入的第一件事就是文件名。
即将出现的问题是从主菜单到待办事项菜单的过渡,它应该显示在哪里。
- 设置背景
- 玩家输入
- 角色移动
- UI
- 正在导出
点击一个按钮。由于代码有 300 行,我不确定哪些部分很重要,所以我在这里附上了 整个 代码,因为在 Stack,他们很欣赏,有问题的代码而不是一个 GitHub link.
def draw(line:list):
dataCopy.clear()
textList.clear()
coords.clear()
dataCopyGenerator(len(line), dataCopy)
endpoint = 585 - (len(dataCopy) * 45)
for i in range(585, endpoint, -45):
l = len(dataCopy) - (585 - i)//45
j = len(dataCopy) - l
if i % 45 == 0 and j <= len(dataCopy):
if len(textList) >= len(boxList):
dataCopy[j] = pt.shapes.Rectangle(10, i-15, 30, 30, color=pink, batch=otherMenu)
boxList.append(dataCopy[j])
dataCopy[j].draw()
if {'x': 10, 'y': i, 'size': 30} in coords:
continue
else:
coords.append({'x': 10, 'y': i, 'size': 30})
dataCopy[j] = pt.text.Label(t, font_name="Times New Roman", font_size=24, x=60,
y=i, anchor_x='left', anchor_y='center', batch=otherMenu, color=TextColor2)
textList.append(dataCopy[j])
for i in range(len(textList)):
print(i)
textList[i].text = line[i]
def mainMenu():
filesCopy.clear()
dataCopyGenerator(len(files), filesCopy)
endpoint = 585 - (len(filesCopy) * 45)
for i in range(585, endpoint, -45):
l = len(filesCopy) - (585 - i)//45
j = len(filesCopy) - l
if i % 45 == 0 and j <= len(filesCopy):
if len(filesList) >= len(circleList):
filesCopy[j] = pt.shapes.Circle(30, i-5, radius=10,color=pink, batch=menu)
circleList.append(filesCopy[j])
filesCopy[j].draw()
if {'x': 10, 'y': i, 'size': 30} in menuCoords:
continue
else:
menuCoords.append({'x': 10, 'y': i, 'size': 30})
filesCopy[j] = pt.text.Label(t, font_name="Times New Roman", font_size=24, x=60,
y=i, anchor_x='left', anchor_y='center', batch=menu, color=TextColor2)
filesList.append(filesCopy[j])
for i in range(len(filesList)):
filesList[i].text = files[i]
@window.event
def on_mouse_press(x, y, button, modifiers):
global data, menuIsVisible, line
#Main Menu
for i in range(len(menuCoords)):
if (button == pt.window.mouse.LEFT) and x > circleList[i].x - circleList[i].radius and x < ( circleList[i].x+ circleList[i].radius) and y > circleList[i].y - circleList[i].radius and y < ( circleList[i].y + circleList[i].radius):
menuIsVisible == False
back_line1.visible = True
back_line2.visible = True
with open(files[i] + ".txt", "r") as f:
line = f.read()
line1 = list(map(str.strip, line.strip('][').replace('"', '').split(',')))
draw(line1)
print (line1)
return menuIsVisible, line1
@window.event
def on_draw():
global menuIsVisible, line1
window.clear()
batch.draw()
title.draw()
textFieldB.draw()
typed_text.draw()
if menuIsVisible == True:
mainMenu()
menu.draw()
elif menuIsVisible == False:
draw(line1)
print
otherMenu.draw()
@window.event
def on_mouse_scroll(x, y, scroll_x, scroll_y):
if scroll_y == 1:
if title.y < background.height - 40:
return
else:
for i in range(len(textList)):
textList[i].y -= 10
for i in range(len(boxList)):
boxList[i].y -= 10
title.y -= 10
elif scroll_y == -1:
for i in range(len(textList)):
textList[i].y += 10
for i in range(len(boxList)):
boxList[i].y += 10
title.y += 10
pt.app.run()
点赞!
编辑: 我只发布了绘图功能、主菜单和其他菜单以及一些重要的 window.event.
您需要的是场景管理器。场景管理器在制作游戏时基本上处理所有的场景变化和类似的事情。在 pyglet 中创建一个的一个好方法是为所有项目创建一个 class。然后在 class 中创建一个函数,它将隐藏和显示所有项目
class item1:
def __init__(self):
self.obj1 = pyglet.shapes.Rectangle(x, y, width, height, color, batch)
self.obj2 = pyglet.shapes.Rectangle(x, y, width, height, color, batch)
def show(self):
obj1.visible = True
obj2.visible = True
def hide(self):
obj1.visible = False
obj2.visible = False
class item2:
def __init__(self):
self.obj1 = pyglet.shapes.Rectangle(x, y, width, height, color, batch)
self.obj2 = pyglet.shapes.Rectangle(x, y, width, height, color, batch)
def show(self):
obj1.visible = True
obj2.visible = True
def hide(self):
obj1.visible = False
obj2.visible = False
现在创建一个 class 名为 sceneManager
class sceneManager:
def __init__(self):
pass
def scene1(self):
item1.show()
item2.hide()
def scene2(self):
item1.hide()
item2.show()
现在您可以在单击按钮后调用 sceneManager.scene2()
。