Python 将移动控件移动到玩家类后,我无法移动我的精灵

Python 将移动控件移动到玩家类后,我无法移动我的精灵,python,class,pygame,Python,Class,Pygame,我似乎无法让我的雪碧在移动到一个类后向左或向右移动。我正在使用pygame。我有一个速度变量,加上我的x,我的形状仍然不动。任何帮助都将不胜感激。在我决定把我所有与球员有关的事情都放到一个班上之前,这个动作是有效的 import pygame import Constants from pygame.locals import * pygame.init() pygame.display.set_caption("Platformer") window = pygame.display.set

我似乎无法让我的雪碧在移动到一个类后向左或向右移动。我正在使用pygame。我有一个速度变量,加上我的x,我的形状仍然不动。任何帮助都将不胜感激。在我决定把我所有与球员有关的事情都放到一个班上之前,这个动作是有效的

import pygame
import Constants
from pygame.locals import *
pygame.init()

pygame.display.set_caption("Platformer")
window = pygame.display.set_mode((Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT))
clock = pygame.time.Clock()
all_sprite_list = pygame.sprite.Group()

class player:
    def __init__(self,velocity):
        self.velocity = velocity
    def location(self,x,y):
        self.x = x
        self.y = y 
        self.xVel = 0

    def keys(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    self.xVel = - self.velocity
                elif event.key == pygame.K_RIGHT:
                    self.xVel =  self.velocity
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    self.xVel = 0



    def move(self):
        self.x = + self.xVel

    def draw(self):
        display = pygame.display.get_surface()
        pygame.draw.rect(window,(255,255,255),[self.x,self.y,20,40])

    def do(self):
        self.keys()
        self.move()
        self.draw()

P = player(10)
P.location(Constants.SCREEN_WIDTH/2,0)

#Map
def draw_map():
    window.fill(Constants.BLUE)
#sprites
def draw_player():
    player = Player(50,50)

    all_sprite_list.add(player)
#Game loop 
def game_loop():
    GameQuit = False
    while not GameQuit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                GameQuit = True


        draw_map()
        P.do()
        pygame.display.update()
        clock.tick(30)

#game initialisation
game_loop()
pygame.quit()
quit()

`

问题在于
pygame.event.get()
清除事件队列中的所有事件,因此当玩家调用它时,所有事件都已消失。从

这将获取所有消息并将它们从队列中删除。如果给定类型或类型序列,则仅从队列中删除这些消息

您可以通过指定应从事件队列中删除哪些事件来解决此问题,例如

for event in pygame.event.get(QUIT):
    GameQuit = True

其他事件应该仍然在队列中,供您的玩家使用

Leon Z.已经解释过,
pygame.event.get()
清空事件队列。我通常将事件从事件循环传递给需要事件的对象。您还可以将
pygame.event.get()
返回的列表分配给变量,然后将此变量传递给播放器实例:
events=pygame.event.get()

顺便说一句,
move
方法中有一个小错误:
=+
而不是
+=

import pygame
from pygame.locals import *
pygame.init()

pygame.display.set_caption("Platformer")
window = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
all_sprite_list = pygame.sprite.Group()

class player:

    def __init__(self,velocity):
        self.velocity = velocity

    def location(self,x,y):
        self.x = x
        self.y = y 
        self.xVel = 0

    def keys(self, event):
        """Handle the events that get passed from the event loop."""
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.xVel = -self.velocity
            elif event.key == pygame.K_RIGHT:
                self.xVel =  self.velocity
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                self.xVel = 0

    def move(self):
        self.x += self.xVel  # Add the self.xVel to self.x.

    def draw(self):
        display = pygame.display.get_surface()
        pygame.draw.rect(display, (255,255,255), [self.x, self.y, 20, 40])

    def do(self):
        self.move()
        self.draw()

P = player(10)
P.location(window.get_width()/2, 0)

def draw_map():
    window.fill(pygame.Color('blue'))

def draw_player():
    player = Player(50,50)

    all_sprite_list.add(player)

def game_loop():
    GameQuit = False
    while not GameQuit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                GameQuit = True
            # Pass the event to all objects that need it.
            P.keys(event)

        draw_map()
        P.do()
        pygame.display.update()
        clock.tick(30)


game_loop()
pygame.quit()