2
有什麼方法可以讓用戶輸入並移動播放器圖像?我可以每次按W,A,S或D時移動播放器圖像,但它只移動1個像素。我可以做些什麼來讓它在移動的同時按下一個鍵? 。python pygame輸入控件
import pygame
from pygame.locals import *
pygame.init()
size = width, height = 640, 480
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Gaben's Future Adventure")
player = pygame.image.load("images/player.png")
player_rect = player.get_rect()
white = (255, 255, 255)
black = (0, 0, 0)
class World(object):
def __init__(self):
pass
class Controls(object):
def __init__(self):
self.speed = 1
self.keys = [False, False, False, False]
def moveUp(self, player_rect):
player_rect.y -= self.speed
def moveLeft(self, player_rect):
player_rect.x -= self.speed
def moveRight(self, player_rect):
player_rect.x += self.speed
def moveDown(self, player_rect):
player_rect.y += self.speed
def update(self):
pass
def main():
running = True
while running:
screen.fill(white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
print "Exiting game."
if event.type == pygame.KEYDOWN:
if event.key == K_w:
print "W down"
elif event.key == K_a:
print "A down"
elif event.key == K_s:
Controls().moveDown(player_rect)
print "S down"
elif event.key == K_d:
Controls().moveRight(player_rect)
print "D down"
if event.type == pygame.KEYUP:
if event.key == K_w:
print "W up"
elif event.key == K_a:
print "A up"
elif event.key == K_s:
print "S up"
elif event.key == K_d:
print "D up"
screen.blit(player, player_rect)
pygame.display.flip()
print player_rect.x
print player_rect.y
main()
不錯,我喜歡那種方法 – CrackSmoker9000