2014-04-11 64 views
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() 

回答

3

我會給我感興趣的所有密鑰的dict對於運動,值將僅僅是一個(x, y)元組:

move_map = {pygame.K_w: (0, -1), 
      pygame.K_s: (0, 1), 
      pygame.K_a: (-1, 0), 
      pygame.K_d: (1, 0)} 

然後,在主循環中,我會計算一個運動矢量:

# get all pressed keys 
    pressed = pygame.key.get_pressed() 

    # get all movement modifiers from our move_map where the key is pressed. 
    move = [move_map[key] for key in move_map if pressed[key]] 

    # add all movement modifiers together in cases multiple keys are pressed. 
    # map(sum, zip(*...)) is not very efficient, but easy to remember :-) 
    final = map(sum, zip(*move)) 

    # usually you want to normalize the result first, and multiplicate it 
    # with the desired velocity of that object. 

    # if we have a movement vector, just move the player_rect 
    if final: 
     player_rect.move_ip(*final) 

    # also, you probably want to use a Clock object to limit your framerate 

全碼:

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 

move_map = {pygame.K_w: (0, -1), 
      pygame.K_s: (0, 1), 
      pygame.K_a: (-1, 0), 
      pygame.K_d: (1, 0)} 

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." 

     pressed = pygame.key.get_pressed() 
     move = [move_map[key] for key in move_map if pressed[key]] 
     final = map(sum, zip(*move)) 
     if final: 
      player_rect.move_ip(*final) 


     screen.blit(player, player_rect) 
     pygame.display.flip() 
     print player_rect.x 
     print player_rect.y 
main() 
+0

不錯,我喜歡那種方法 – CrackSmoker9000

1

你必須寫:

keys = pygame.key.get_pressed() 
f keys[pygame.K_w]: 
    print('K_w pressed') 
elif keys[pygame.K_s]: 
    print('K_s pressed') 
elif keys[pygame.K_a]: 
    print('K_a pressed') 
elif keys[pygame.K_d]: 
    print('K_d pressed') 

for event in pygame.event.get():循環外。