是否可以通过一行代码模拟pygame中的按键操作?

时间:2018-11-14 05:30:23

标签: python python-3.x pygame

对于我的游戏,当我输入一个字母数字符号并尝试与该游戏进行交互时,我需要花几秒钟的时间才能单击该面板并进行注册。

由于某种原因,如果我在按任意键后又按了控制按钮,则可以立即与面板交互。

所以我的问题是为什么会发生这种情况以及如何防止这种情况发生。我看不到代码有什么问题。

while True:
    for event in pygame.event.get():
        if (event.type == pygame.QUIT):
            pygame.quit()
            sys.exit()

        elif (event.type == pygame.VIDEORESIZE):
            new_res = event.dict['size']
            screen = pygame.display.set_mode(new_res, pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE)

        elif (event.type == pygame.KEYDOWN):
            print(0)
            keys = pygame.key.get_pressed()
            if (event.key == pygame.K_0 and keys[pygame.K_LCTRL]):
                COLOR_1 = ((255,255,255))
                COLOR_2 = ((0,0,0))
                COLOR_3 = ((255,255,0))
                COLOR_4 = ((0,0,255))

                full_screen.fill(COLOR_1)                               # Background
                title.set_color(COLOR_1,                                # Background
                                COLOR_2)                                # Text
                full_screen.blit(title.display(),title_offset)
                board.set_color(COLOR_1,                                # Background
                                COLOR_2,                                # Thin
                                COLOR_2,                                # Thick
                                COLOR_3,                                # Highlight
                                COLOR_2,                                # Game Digits
                                COLOR_4)                                # User Digits
                full_screen.blit(board.display(),board_offset)
                helpbar.set_color(COLOR_1,                              # Background
                                  COLOR_2,                              # Thin
                                  COLOR_2,                              # Thick,
                                  COLOR_2)                              # Text
                full_screen.blit(helpbar.set_up(),helpbar_offset)

            elif (event.key == pygame.K_1 and keys[pygame.K_LCTRL]):
                COLOR_1 = pygame.Color("#5d001e")
                COLOR_2 = pygame.Color("#e3e2df")
                COLOR_3 = pygame.Color("#e3afbc")
                COLOR_4 = pygame.Color("#9a1750")
                COLOR_5 = pygame.Color("#ee4c7c")

                full_screen.fill(COLOR_2)                               # Background
                title.set_color(COLOR_2,                                # Background
                                COLOR_5)                                # Text
                full_screen.blit(title.display(),title_offset)
                board.set_color(COLOR_2,                                # Background
                                COLOR_4,                                # Thin
                                COLOR_1,                                # Thick
                                COLOR_3,                                # Highlight
                                COLOR_1,                                # Game Digits
                                COLOR_4)                                # User Digits
                full_screen.blit(board.display(),board_offset)
                helpbar.set_color(COLOR_2,                              # Background
                                  COLOR_4,                              # Thin
                                  COLOR_1,                              # Thick,
                                  COLOR_5)                              # Text
                full_screen.blit(helpbar.set_up(),helpbar_offset)

            elif (event.key in [pygame.K_1,pygame.K_2,pygame.K_3,pygame.K_4,pygame.K_5,pygame.K_6,pygame.K_7,pygame.K_8,pygame.K_9]):
                surf = board.user_move(event.key-48)
                full_screen.blit(surf,board_offset)

            elif (event.key == pygame.K_q):
                pygame.quit()
                sys.exit()

        elif (event.type == pygame.MOUSEBUTTONDOWN):
            print(1)
            (x,y) = pygame.mouse.get_pos()
            (x,y) = (x*SCREEN_SIZE[0]/new_res[0],y*SCREEN_SIZE[1]/new_res[1]) # Prealtered x,y

            # If on the board
            if (x >= board_offset[0] and
                y >= board_offset[1] and
                x <= board_offset[0] + board_size[0] and
                y <= board_offset[1] + board_size[1]):
                surf = board.remove_highlight_selection()
                full_screen.blit(surf,board_offset)
                surf = board.highlight_selection(x-board_offset[0],y-board_offset[1])
                full_screen.blit(surf,board_offset)
            else:
                surf = board.remove_highlight_selection()
                full_screen.blit(surf,board_offset)
                board.current_selection = []

    screen.blit(pygame.transform.scale(full_screen,new_res),(0,0))        
    pygame.display.flip()

任何帮助将不胜感激。如果您需要查看其余的代码,请告诉我,但它的长度为800行。

0 个答案:

没有答案