Python 我如何在pygame中做出特定的精灵移动?
当我按下某个键时,我需要使播放器不断移动。我遇到的问题是,播放器的图像移动一次(当我按下其中一个已定义的键时),然后停止。 在这里输入代码Python 我如何在pygame中做出特定的精灵移动?,python,pygame,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
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。然后它停止了。您可能需要创建一个更新函数。还将生成一个变量,以允许使用或不使用该函数。另一个将用于控制它的方向。以下是更新
功能。请随意更改,以满足您的需要:
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'
它们应该在类的代码之前。您完全可以更改事件中的行。键行,它应该是(在正确的缩进中):
当然,一定有什么东西可以阻止汽车反复无限地行驶,对吗?将这些行与上面的行一起添加:
elif event.key == pygame.K_SPACE:
direction = 'NONE'
run = 0
现在将这两行放在while
循环中,但在for
循环之前:
if run == 1:
Car.update()
else:
pass
这应该是可行的,因为只要run
等于1,汽车将继续沿给定方向移动,直到按下空格键停止。我希望这对你有帮助 请学习使用搜索功能。另外,如果您取消输入x+=,请阅读Pygame的文档。。。y+=…
一个缩进它可以做你想做的。乍一看。事情看起来很正常-尝试在主while循环中添加一个“pygame.event.pump”调用。对event.*函数和系统的特定调用的需求各不相同。此外,请注意“big if”中的键入错误:event.type==pygame.K\u DOWN:
它应该是event.key。实际上,这个if可以重写为if event.key in(K_DOWN,K_UP,K_LEFT,K_RIGHT):
(您的名称空间中有pygame.locals import*
语句中的键常量)
if run == 1:
Car.update()
else:
pass