我正在开发一个使用Thorpy作为图形用户界面元素的PyGame项目.我已经创建了连接到PyGame表面的Thorpy按钮.当我使用blit函数移动曲面时,按钮的视觉表示会移动,但它们的点击框保持不变,从而导致点击问题.

在本例中,Thorpy按钮附加到蓝色曲面,它们的视觉表示是其点击框右侧的100个像素.

import pygame
import thorpy as tp

pygame.init()
def GUI_elements():
    tp.init(surface, tp.theme_round)
    
    button1 = tp.Button('Main Menu')
    button2 = tp.Button('Exit Game')
    group = tp.Group([button1, button2], 'v')
    game_GUI = group.get_updater()

    return game_GUI

screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Pygame Window")
surface = pygame.Surface((400, 800))

def main_loop():
    game_GUI = GUI_elements()
    menu_button = game_GUI.element.children[0]
    menu_button.at_unclick = exit

    exit_button = game_GUI.element.children[1]
    exit_button.at_unclick = exit

    while True:
        game_GUI.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        screen.fill((255, 0, 0))
        screen.blit(surface, (100, 0))  # (100, 0) is the offset
        surface.fill((0, 0, 200))

        pygame.display.flip()

if __name__ == '__main__':
    main_loop()
    pygame.quit()

我试过移动Thorpy组或按钮,这很好用,但我看不出这如何解决我的问题,因为我仍然必须移动PyGame表面到它的理想位置,这将抵消一切.

推荐答案

The first argument of tp.init is supposed to be the screen, but not a surface that is moved on the screen. If you move the surface, you would also have to move the mouse position, respectively the UI would have to evaluate the mouse position relative to the surface. But I don't see any possibility for this.
I suggest drawing the menu on the screen and marking the center of the menu with:

tp.init(screen, tp.theme_round)
...
group.set_center(300, 400)

要使其起作用,需要在绘制场景之后更新UI,但就在更新显示之前.您甚至可以使用曲面的中心来对齐UI.例如:

screen.fill((255, 0, 0))
surface_rect = screen.blit(surface, (100, 0))
game_GUI.element.set_center(*surface_rect.center)
game_GUI.update()
pygame.display.flip()

完整的示例:

import pygame
import thorpy as tp

pygame.init()
def GUI_elements():
    tp.init(screen, tp.theme_round)
    button1 = tp.Button('Main Menu')
    button2 = tp.Button('Exit Game')
    group = tp.Group([button1, button2], 'v')
    game_GUI = group.get_updater()
    return game_GUI

screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Pygame Window")
surface = pygame.Surface((400, 800))
surface.fill((0, 0, 200))

def main_loop():
    game_GUI = GUI_elements()
    menu_button = game_GUI.element.children[0]
    menu_button.at_unclick = exit
    exit_button = game_GUI.element.children[1]
    exit_button.at_unclick = exit

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        screen.fill((255, 0, 0))
        surface_rect = screen.blit(surface, (100, 0))
        game_GUI.element.set_center(*surface_rect.center)
        game_GUI.update()
        pygame.display.flip()

if __name__ == '__main__':
    main_loop()
    pygame.quit()

Python相关问答推荐

在Python中管理多个OpenGVBO和VAO实例

阅读Polars Python中管道的函数定义

如何根据情况丢弃大Pandas 的前n行,使大Pandas 的其余部分完好无损

Python plt.text中重叠,包adjust_text不起作用,如何修复?

通过仅导入pandas来在for循环中进行多情节

如何计算两极打印机中 * 所有列 * 的出现次数?

为什么我的Python代码在if-else声明中的行之前执行if-else声明中的行?

使可滚动框架在tkinter环境中看起来自然

用NumPy优化a[i] = a[i-1]*b[i] + c[i]的迭代计算

如何使Matplotlib标题以图形为中心,而图例框则以图形为中心

pandas在第1列的id,第2列的标题,第3列的值,第3列的值?

如何在FastAPI中为我上传的json文件提供索引ID?

Tkinter菜单自发添加额外项目

在Python中使用yaml渲染(多行字符串)

巨 Python :逆向猜谜游戏

如何在Gekko中使用分层条件约束

Autocad使用pyautocad/comtypes将对象从一个图形复制到另一个图形

极点替换值大于组内另一个极点数据帧的最大值

浏览超过10k页获取数据,解析:欧洲搜索服务:从欧盟站点收集机会的微小刮刀&

如何写一个polars birame到DuckDB