首页 > 解决方案 > Python 3:当我按住一个键并按下然后释放另一个键时,pygame 不会接受新事件

问题描述

我有一个脚本可以让“角色”在地图上移动,但是当我按住一个方向,然后按下(按下然后释放)另一个键时,“pygame.event_get() 中的 for 事件”循环不再起作用我确实有一个 'pygame.key.set_repeat()' 并且我尝试在每次按键释放时更改 'event' 的值,但我不知道该怎么做。这是脚本:

import math
import numpy as np
import random as rdm
import pygame
from pygame.locals import *

#functions :

def printScreen():
    global delta
    global Map
    global player
    
    x = (w-player.w)//2
    xMap = x - player.x


    
    y = (h-player.w)//2
    yMap = player.w + y - Map.h + player.y


    fenetre.fill((0, 0, 0))
    fenetre.blit(Map.layer1, (xMap, yMap))
    fenetre.blit(player.image, (x, y))
    pygame.display.flip()

#variables :

h, w = 600, 700
delta = 1 #size of a pixel
run = True
moving = False

#constants :

zeros = tuple([0 for i in range(323)])
directions = ['up', 'down', 'right', 'left']

#pygame initialisation :


pygame.init()
clock = pygame.time.Clock()
fenetre = pygame.display.set_mode((w*delta, h*delta))
pygame.key.set_repeat(40, 40)

#classes :

class player :
    def __init__(self, save) :
        File = open(save + '\savedata.txt')
        Text = File.read()
        File.close
        noLine = 1
        nbCharacters = 0
        lineStart = 0
        for i in range(len(Text)) :
            if Text[i] == '\n' :
                line = Text[lineStart:i]
                lineStart = i+1
                if noLine == 1 :
                    nickName = line
                if noLine == 2 :
                    x = int(line)
                if noLine == 3 :
                    y = int(line)
                noLine += 1
        self.image = pygame.image.load(save + '\player.png')
        self.w = self.image.get_width()
        self.h = self.image.get_height()
        self.nickName = nickName
        self.x = x
        self.y = y
        self.direction = 'down'

player = player("data\player\save01")

class backGround :
    def __init__(self, directory) :
        self.layer1 = pygame.image.load(directory)
        self.w = self.layer1.get_width()
        self.h = self.layer1.get_height()

Map = backGround("data"+"\\"+"town.png")

#images :

#main pygame loop :
        
while run :
    clock.tick(120)
    for event in pygame.event.get() :
        if event.type == QUIT :
            run = False

        if event.type == KEYUP :
            event = 0
            
        keys = pygame.key.get_pressed()
        arrows = keys[273 : 277]
        for i in range(4) :
            if player.direction == directions[i] and arrows[i] == 0 :
                moving = False
            

        if keys == zeros :
            moving = False
            
        if keys[pygame.K_UP] :
            player.y += player.w
            if not moving :
                player.direction = 'up'
            moving = True

        if keys[pygame.K_DOWN] :
            player.y -= player.w
            if not moving :
                player.direction = 'down'
            moving = True

        if keys[pygame.K_RIGHT] :
            player.x += player.w
            if not moving :
                player.direction = 'right'
            moving = True

        if keys[pygame.K_LEFT] :
            player.x -= player.w
            if not moving :
                player.direction = 'left'
            moving = True

        print(player.direction, keys[273 : 277])
    printScreen()
    
pygame.quit()

arrows = keys[273 : 277] 指的是 pygame.key.get_pressed() 中包含箭头键的部分。

标签: python-3.xpygamekeypress

解决方案


问题是您仅在发生新事件时才检查键数组。您想在每个循环中检查键数组。你只需要调整你的缩进。

这是更新的代码:

while run :
    clock.tick(10)
    for event in pygame.event.get() :  # check for new event
        if event.type == QUIT :
            run = False

        if event.type == KEYUP :
            event = 0
            
    keys = pygame.key.get_pressed()  # always check this
    arrows = keys[273 : 277]
    for i in range(4) :
        if player.direction == directions[i] and arrows[i] == 0 :
            moving = False
        
    if keys[pygame.K_SPACE] : print('--------------------- space')

    if keys == zeros :
        moving = False
        
    .............
    
pygame.quit()

推荐阅读