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

时间:2015-01-21 17:57:59

标签: python pygame

当我按下某个键时,我需要让播放器连续移动。我遇到的问题是播放器的图像移动一次(当我按下其中一个定义的键时)然后停止。        在这里输入代码

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()  

1 个答案:

答案 0 :(得分:0)

问题是你只允许精灵移动一次。例如,拿这段代码:

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'

它们应该在该类的代码之前。你绝对改变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时,汽车将继续沿给定方向移动,直到按空格键停止。我希望这可以帮助你!