问题
I'm trying to create a "player" - black square - which moves when you hold down WASD. I tried looking around here, on google and on youtube on how to make this work, but every solution I've tried has the same problem: instead of moving it while I hold down the key, I have to tap the key constantly to make it move in small bits. I've no clue what I'm doing wrong. Here's the code (using python 3.3 - pygame 1.9):
import pygame
from pygame.locals import *
from pygame.time import *
import sys
pygame.init()
velX = 0
velY = 0
running = True
clock = pygame.time.Clock()
def draw():
global velX
global velY
playerx = 20
playery = 20
screen = pygame.display.set_mode((700,300))
pygame.display.set_caption('something')
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255,255,255))
screen.blit(background, (0,0))
playerx = playerx + velX
playery = playery + velY
player_filename = 'player.png'
player = pygame.image.load(player_filename)
screen.blit(player, (playerx,playery))
pygame.display.flip()
def main():
global velX
global velY
global running
while running:
keys_down = pygame.key.get_pressed()
pygame.key.set_repeat(1, 50)
time = 50/1000
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running=False
if keys_down[K_d]:
velX += 50*time
if keys_down[K_w]:
velY -= 50*time
if keys_down[K_s]:
velY += 50*time
if keys_down[K_a]:
velX -= 50*time
clock.tick(50)
draw()
if __name__ == '__main__':
main()
I already tried the set repeat command, but it didn't seem to do much anything. I also tried directly copying from a few solutions I found here on stackoverflow, but none of them worked. I suppose there is something else wrong in the code.
回答1:
There are two problems with your code. First to your question. The reason the player never moves more than one step is that you reset the player position in every call to draw() when you do
playerx = 20
playery = 20
Instead, you should put that code above the draw()
function and add
global playerx
global playery
at the top of draw()
. Now, the player position is not reset every frame.
The second problem is that you create a new screen in every call to draw()
when you do
screen = pygame.display.set_mode((700,300))
pygame.display.set_caption('something')
instead you should move those to lines above draw()
and just use the same screen for every draw.
Also, like elyase points out, what you probably want is to set the velocities to fixed values, not increase them. Like so
velX = 0
velY = 0
if keys_down[K_d]:
velX = 10
if keys_down[K_w]:
velY = -10
if keys_down[K_s]:
velY = 10
if keys_down[K_a]:
velX = -10
This way the player will move around with a constant speed in the direction you steer it.
Hope that clear some stuff up. :)
回答2:
You are resetting the position inside draw(). Also you should put the code that changes the direction, inside the event.type == pygame.KEYDOWN
condition. Something like this:
if event.type == KEYDOWN:
if event.key == K_d:
velX += 50*time
elif event.key == K_w:
velY -= 50*time
...
if event.type == KEYUP:
# set velocity to 0
来源:https://stackoverflow.com/questions/16626143/how-to-make-a-character-move-while-key-is-held-down