在pygame中製作一個簡單的遊戲。爲我的charactater導入了一張圖片,並可以自由移動。我想知道如何讓人物慢慢停下來;pygame使用KEYUP減慢對象?
event.type == KEYUP:
中,當我鬆開鍵的字符將說,從按鍵已被釋放到角色慢慢停止50個像素。
我已經嘗試了幾個不同的東西,但似乎沒有任何工作。下面是我的代碼。任何幫助都會很棒=]
bg="bg.jpg"
staticCharacter="toon.png"
import pygame, sys, time
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,360),0,32)
background=pygame.image.load(bg).convert()
staticToon = pygame.image.load(characterMid).convert_alpha()
x = 0
y = 0
movex = 0
movey = 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
movex = -0.20
elif event.key == K_RIGHT:
movex = +0.20
elif event.key == K_UP:
movey = -0.20
elif event.key == K_DOWN:
movey = +0.20
if event.type == KEYUP:
if event.key == K_LEFT:
movex = 0
elif event.key == K_RIGHT:
movex = 0
elif event.key == K_UP:
movey = 0
elif event.key == K_DOWN:
movey = 0
x += movex
y += movey
if x > 560:
x = 560
if y > 280:
y = 280
if x < 0:
x = 0
if y < 0:
y = 0
screen.blit(background,(0,0))
screen.blit(staticToon,(x,y))
pygame.display.update()
我想你問的加速度?握住鑰匙移動到最大速度,但釋放時會慢下來,最終停止? – ninMonkey 2013-03-17 18:03:46