2011-10-10 6 views
2

방금 ​​"w, a, s, d"를 사용하여 창 주위를 돌아 다니는 스프라이트가 생겼습니다. 스페이스 바를 눌러이 스프라이트를 "촬영"하고 싶습니다.파이 게임에서 키 이벤트가있는 스프라이트 이동하기

문제는 내가 스페이스 바를 놓을 때까지 스프라이트가 나타나지만 움직이지 않는다는 것입니다. 스프라이트 샷은 스페이스 바를 누르기 만하면 창 끝까지 전진합니다. 그것을 공개하지 말라.

이 내 메인 루프입니다 :

while pygame.event.poll().type != QUIT: 


    screen.blit(background, (0, 0)) 

    #"player" is the sprite moving around the window 
    player.move(width,height) 
    screen.blit(player.image, player.rect) 
    key = pygame.key.get_pressed() 

    if key[K_SPACE]: 
     xpos = player.rect.right 
     ypos = player.rect.top 
     shot_on_screen = True 


    if shot_on_screen: 
     xpos += 1 
     #"kame" is the sprite i want to move forward 
     screen.blit(shot.kame, (xpos, ypos)) 
    else: 
     shot.x = player.rect.right 
     shot.y = player.rect.top 
     shot_on_screen = False 


    pygame.display.update() 

나는이 파이썬 - 파이 게임의 세계에 새로운 해요, 난 그래도 요청하기 전에 매뉴얼 및 문서를 많이 확인했지만, 당신이 도움이 될 수 있습니다 감사하겠습니다.

답변

0

K_SPACE 키를 누를 때마다 모든 루프에서 플레이어의 가장 오른쪽 위치에 xpos를 설정합니다. 너는 그렇게해서는 안된다. 해당 행을 제거하십시오. 촬영이 코드에 다른 곳에서 화면에 있는지 확인하려는 경우

if key[K_SPACE]: 
    ypos = player.rect.top 
    shot_on_screen = True 

또한, 나도 몰라,하지만 당신이 게시 코드는 false로 설정하지 않습니다.

if shot_on_screen: 
    xpos += 1 
    #"kame" is the sprite i want to move forward 
    screen.blit(shot.kame, (xpos, ypos)) 
else: 
    shot.x = player.rect.right 
    shot.y = player.rect.top 
    shot_on_screen = False # won't work. It is already False. 
+0

감사합니다. "ypos = player.rect.top"을 제거해 주셔서 감사합니다. 문제가 해결 되었으니 이제는 분명히 xD였습니다. – Castell

관련 문제