单击按钮后更改Pyglet窗口的内容

2024-06-28 11:06:57 发布

您现在位置:Python中文网/ 问答频道 /正文

最近,我在用Pyglet制作一个UI应用程序,它基本上是一个实干家。它由主菜单组成,主菜单将显示文件夹中的所有文本文件。这些文本文件包含我必须执行的任务的列表,文本文件的名称作为这些任务的通用名称。例如,如果我在编写游戏代码时必须制作一个待办事项列表,则文件名可以是“Pong.txt”,并且可能包含一个列表,如["Set up background", "Player Inputs", "Character movement", "UI", "Exporting"]。单击项目旁边的项目符号后,应该会打开一个菜单,显示我必须执行的所有任务。为新项目创建新任务还有一个加号,您键入的第一件事就是文件名

接下来的问题是从主菜单过渡到应该显示的待办菜单

  • 设置背景
  • 玩家输入
  • 人物运动
  • 用户界面
  • 出口

点击一个按钮。因为代码是300行,我不确定哪些部分都很重要,所以我在这里附上整个代码,因为在堆栈中,他们很欣赏,让代码带有问题,而不是GitHub链接

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


Tags: andinforlenifline菜单range
1条回答
网友
1楼 · 发布于 2024-06-28 11:06:57

您需要的是一个场景管理器。在制作游戏时,场景管理器基本上可以处理所有场景变化和类似的事情。在pyglet中创建一个类的好方法是为所有项目创建一个类。然后在类中创建一个函数,该函数将隐藏和显示所有项

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

现在创建一个名为sceneManager的类

class sceneManager:
    def __init__(self): 
        pass 
    def scene1(self):
        item1.show()
        item2.hide()

    def scene2(self):
        item1.hide()
        item2.show()

现在,只要单击按钮,就可以调用sceneManager.scene2()

相关问题 更多 >