Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按住可玩pygame_Python_Pygame_Pyautogui - Fatal编程技术网

Python 按住可玩pygame

Python 按住可玩pygame,python,pygame,pyautogui,Python,Pygame,Pyautogui,在Python2.7中,我使用pygame和pyautogui在屏幕上移动鼠标。我的代码如下所示: import pyautogui import pygame pygame.init() pygame.display.set_mode() loop = True while loop: for event in pygame.event.get(): if event.type == pygam

在Python2.7中,我使用pygame和pyautogui在屏幕上移动鼠标。我的代码如下所示:

    import pyautogui
    import pygame
    pygame.init()
    pygame.display.set_mode()
    loop = True
    while loop:

         for event in pygame.event.get():
                if event.type == pygame.quit:
                    pygame.quit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_a:
                        pyautogui.moveRel(-50,0)

当我按下“a”时,我的代码将鼠标向左移动,但当我想在屏幕上移动鼠标时,我必须反复按下按钮。有没有一种方法可以按住鼠标并在屏幕上移动鼠标?我看过关于这个主题的其他教程,但它们似乎非常特定于项目

基本上,您要做的是设置一个变量,指示keydown上的键是否已关闭,并在键打开后更新它

在这里,我更新了您的代码来实现这一点,因为它可能更容易理解

import pyautogui
import pygame
loop = True

a_key_down = False                                    # Added variable
while loop:

     for event in pygame.event.get():
            if event.type == pygame.quit:
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    a_key_down = True                 # Replaced keydown code with this
            if event.type == pygame.KEYUP:            # Added keyup
                if event.key == pygame.K_a:
                    a_key_down = False                
    if a_key_down:                                    # Added to check if key is down
         pyautogui.moveRel(-50,0)

我想如果在while循环中按下一个键,你会想把这个
,而不是在pygame.event.get()中的
for事件中。否则,它将根据事件的数量导致移动。当然,这一改变仍然会使移动依赖于游戏循环率,我相信这是OP需要处理的事情。。。