简体   繁体   English

pygame选择菜单导航,使用箭头键并返回

[英]pygame selection menu navigation using arrow keys and return

I am trying to make it so I can generate a group of options to select from and using the up and down arrows the user can navigate between them the current option being highlighted and if you press enter it calls the function that option contains. 我正在尝试使其生成一个选项组,并使用上下箭头,用户可以在它们之间导航当前突出显示的选项,如果按Enter,它将调用该选项包含的函数。

Excuse the mess, I had it cleaner, but was trying several things to try to get the intended result. 打扰一下,我把它弄干净了,但是我在尝试几件事以达到预期的效果。

The selection function works fine by itself 选择功能本身可以正常工作

def selection(text, selected_status, x, y, function):
if selected_status:
    fill_rect(black, x, y, display_width - x, font_size + 2)
    fill_rect( settings[3], x, y, display_width - x, font_size +2)
    message_to_screen("["+ text + "]", settings[3],  black, x, y)
    if event_handler() == pygame.K_RETURN:
        function()
        return True
elif not selected_status:
    fill_rect(black, x, y, display_width - x, font_size + 2)
    message_to_screen("["+ text + "]", black, settings[3], x, y)
    return False

The selection handler is the issue. 选择处理程序就是问题。 it seems I have to hit the arrow keys at certain times in order for them to work. 看来我必须在某些时候按箭头键才能使它们工作。 the event_handler just grabs the currently pressed key and returns it. event_handler只是获取当前按下的键并返回它。 overall it's just a broken mess. 总体而言,这只是一团糟。

def selection_handler(selection_param_array, x, y):
    text_array = selection_param_array[0]
    selected_status_array  = selection_param_array[1]
    function_array = selection_param_array[2]
    index = 0
    chosen = False
    original_y = y
    while not chosen:
        y = original_y

        for i in range(len(selected_status_array)):
            if selected_status_array[i] == selected_status_array[index]:
                selected_status_array[i] = True
            else:
                selected_status_array[i] = False
            selection(text_array[i], selected_status_array[i], x, y, function_array[i])
            y += font_size

        if event_handler() == pygame.K_UP and index > 0:
            index -= 1
        elif event_handler() == pygame.K_DOWN and index < len(selection_param_array):
            index += 1
        elif event_handler() == pygame.K_RETURN:
            chosen = True
            function_array[index]()
        pygame.display.update()

Instead of just checking whether a key is pressed down right now , you need to use pygame.event.get() to obtain all events that have occurred since the last pygame.event.get() call. 而不是仅仅检查按键是否被按下,现在 ,你需要使用pygame.event.get()获取自从上次发生的所有事件pygame.event.get()调用。 The usual syntax is this: 通常的语法是这样的:

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_UP:
            index += 1
        elif event.key == pygame.K_DOWN:
            index -= 1
        elif event.key == pygame.K_RETURN:
            chosen = True
            function_array[index]()
            break
index %= len(selection_param_array)

Also, if you call from pygame.locals import * at the start of your program, you can omit pygame. 另外,如果在程序开始时from pygame.locals import *调用,则可以省略pygame. from all of the keys and event types, so you get more readable code: 从所有键和事件类型中获得,因此您获得了更具可读性的代码:

for event in pygame.event.get():
    if event.type == KEYDOWN:
        if event.key == K_UP:
            index += 1
        elif event.key == K_DOWN:
            index -= 1
        elif event.key == K_RETURN:
            chosen = True
            function_array[index]()
            break
index %= len(selection_param_array)

This replaces your if event_handler() == pygame.K_UP and index > 0: ... function_array[index]() code. 这将替换您的if event_handler() == pygame.K_UP and index > 0: ... function_array[index]()代码。 Note that the code above will loop from the top to the bottom of the list when you are selecting things, I don't know if you want this. 请注意,当您选择内容时,上面的代码将从列表的顶部到底部循环,我不知道您是否要这样做。 You can stop it from doing this by reintroducing your index comparisons on the event.key == lines and removing index %= len(selection_param_array) . 您可以通过在event.key ==行上重新引入索引比较并删除index %= len(selection_param_array)来阻止此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM