2014-10-15 3 views
0
import pygame 
from pygame.locals import * 
import sys 

pygame.init() 

# Creating a Window 
pygame.display.set_caption("Hello there") 
screen = pygame.display.set_mode((640, 480)) 

player = pygame.image.load("player.png") 
clock = pygame.time.Clock() 

# X AND Y VALUES FOR PLAYER 
player_x = 32 
player_y = 32 

#MOVING AROUND 
#moving_right = False 
#moving_left = False 
#moving_up = False 
#moving_down = False 

#GAME LOOP 
moving_right = False 
moving_left = False 
moving_up = False 
moving_down = False 
clock.tick(60) 

while True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      sys.exit() 
     elif event.type == KEYDOWN: 
      if event.key == K_LEFT: 
       moving_left = True 
      elif event.key == K_RIGHT: 
       moving_right = True 
      elif event.key == K_UP: 
       moving_up = True 
      elif event.key == K_DOWN: 
       moving_down = True 

    elif event.type == KEYUP: 
     if event.key == K_LEFT: 
      moving_left = False 
     elif event.key == K_RIGHT: 
      moving_right = False 
     elif event.key == K_UP: 
      moving_up = False 
     elif event.key == K_DOWN: 
      moving_down = False 

screen.blit(player, (player_x, player_y)) 

pygame.display.update() 

    #UPDATING PLAYER 
player_speed = 15 
if moving_up: 
    player_y -= player_speed 
elif moving_down: 
    player_y += player_speed 
if moving_left: 
    player_x -= player_speed 
elif moving_right: 
    player_x += player_speed 


#CLEAR THE SCREEN OFF SO THERE'S NO TRAIL WHEN THE PLAYER MOVES 
screen.fill((0, 0, 0)) 

어디서 잘못 본지를 알 수없는 것 같습니다. 게임을 실행할 때 오류가 없습니다. sptite가 화면에 나타나지 않습니다. 키를 누르고있는 동안 스프라이트를 움직일 수 있어야합니다.키보드 입력으로 이동해야하는 스프라이트가 표시되지 않습니다.

+2

키 누름이 올바르게 들여 쓰기되어 있는지 확인하는 것이 모두 아래에 있습니까? 화면을 업데이트하는 비트가 while 루프 외부에 있으므로 호출되지 않은 것 같습니다. – elParaguayo

답변

1

코드가 괜찮습니다. 들여 쓰기에 문제가 있습니다. While True: 루프는 다음과 같아야합니다

while True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      sys.exit() 
     elif event.type == KEYDOWN: 
      if event.key == K_LEFT: 
       moving_left = True 
      elif event.key == K_RIGHT: 
       moving_right = True 
      elif event.key == K_UP: 
       moving_up = True 
      elif event.key == K_DOWN: 
       moving_down = True 

     elif event.type == KEYUP: 
      if event.key == K_LEFT: 
       moving_left = False 
      elif event.key == K_RIGHT: 
       moving_right = False 
      elif event.key == K_UP: 
       moving_up = False 
      elif event.key == K_DOWN: 
       moving_down = False 

    screen.blit(player, (player_x, player_y)) 

    pygame.display.update() 

     #UPDATING PLAYER 
    player_speed = 15 
    if moving_up: 
     player_y -= player_speed 
    elif moving_down: 
     player_y += player_speed 
    if moving_left: 
     player_x -= player_speed 
    elif moving_right: 
     player_x += player_speed 


    #CLEAR THE SCREEN OFF SO THERE'S NO TRAIL WHEN THE PLAYER MOVES 
    screen.fill((0, 0, 0)) 

여기 아이디어는 당신이 블럭 전송 및 발생할 때마다 event에 대한 While 루프에서 화면을 업데이트해야한다는 것입니다.

관련 문제