如何让某个精灵在 pygame 中移动?

How do I make a certain sprite move in pygame?

我需要让播放器在我按下某个按钮时连续移动 key.The 我遇到的问题是播放器的图像移动一次(当我按下一个定义的键时)然后它停止。 在此处输入代码

import pygame
import sys
import os
import random
import time
from pygame.locals import *

pygame.init()

white = ( 255, 255, 255 )
black = ( 0, 0, 0 ) 

screenw = 800
screenh = 600 

screen = pygame.display.set_mode( ( screenw, screenh ) ) 

pygame.display.set_caption( "Game" ) # Here I create a display.

clock = pygame.time.Clock()

class Car(pygame.sprite.Sprite): # Here I create a class.

    def __init__( self, color = black, width = 100, height = 100 ):

        pygame.sprite.Sprite.__init__( self )

        self.image = pygame.Surface( ( width, height ) )

        self.image.fill( color )

        self.rect = self.image.get_rect()

    def set_pos(self, x, y):
        self.rect.x = x
        self.rect.y = y 

    def set_img( self, filename = None):
        if filename != None:

            self.image = pygame.image.load( filename )

            self.rect = self.image.get_rect()

def main():*I create a game loop
    x_change = 0
    y_change = 0 

    x = 0
    y = 0

    car_group = pygame.sprite.Group() # Make a group

    player = Car()

    player.set_img( "images.jpg" )

    car_group.add( player )

    exit = False

    FPS = 60

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

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_LEFT:
                    x_change = -10

                elif event.key == pygame.K_RIGHT:
                    x_change = 10

                elif event.key == pygame.K_UP:
                    y_change = -10

                elif event.key == pygame.K_DOWN:
                    y_change = 10

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.type == pygame.K_DOWN:
                    x_change = 0
                    y_change = 0

            x += x_change
            y += y_change


        screen.fill( white )
        player.set_pos( x, y ) # Blit the player to the screen
        car_group.draw( screen )
        clock.tick( FPS )
        pygame.display.update()

main()
pygame.quit()
quit()  

好吧,问题是您只允许精灵移动一次。例如,以您的这段代码为例:

if event.key == pygame.K_LEFT:
    x_change = -10

这将使x位置变小10。然后停止。你可能想做一个更新功能。还将创建一个变量以允许使用或不使用该功能。另一个将用于控制它前进的方向。这是 update 函数。请随意更改它以满足您的需求:

def update():
    global direction
    if direction == 'LEFT':
        x -= 10 
    elif direction == 'RIGHT':
        x += 10
    elif direction == 'UP'
        y -= 10
    elif direction == 'DOWN':
        y += 10

现在我们需要变量来控制函数是否运行。为我们的新两个变量添加这两行:

run = 0
direction = 'NONE'

它们应该在 class 的代码之前。您绝对更改 event.key 行中的行,它应该是(正确缩进):

if event.key == pygame.K_LEFT:
    direction = 'LEFT'
    run = 1
elif event.key == pygame.K_RIGHT:
    direction = 'RIGHT'
    run = 1
elif event.key == pygame.K_UP:
    direction = 'UP'
    run = 1
elif event.key == pygame.K_DOWN:
    direction = 'DOWN'
    run = 1

当然要有什么东西可以阻止小车反复无限的运动对不对?使用上面的行添加这些行:

elif event.key == pygame.K_SPACE:
    direction = 'NONE'
    run = 0

现在将这两行放在 while 循环中,但在 for 循环之前:

if run == 1:
    Car.update()
else:
    pass   

这应该可行,因为只要 run 等于 1,汽车就会继续沿给定方向移动,直到按空格键停止。希望对您有所帮助!