2017-12-05 2 views
1

이미지 (투명 이미지)를 보려고합니다. 제가 누르는 키를 회전 시키십시오. I.E. 플레이어의 머리를 위아래로 회전시키고 싶습니다 (머리에서 총을 쏘고 있습니다).하지만 코드 작성법에 대한 지침은 없습니다. 누구든지 나를 도울 수 있다면 크게 감사하겠습니다. 또한 나는 열쇠를 누르고있을 때 머리를 부드럽게 회전 (조준)하기를 원한다. 아래 코드 :파이 게임에서 키를 사용하여 이미지를 회전하는 방법은 무엇입니까?

import pygame 

pygame.init() 

white = (255,255,255) 
BLACK = (0,0,0) 
red = (255,0,0) 


gameDisplay = pygame.display.set_mode((640,360)) 

background = pygame.image.load('background.jpg').convert() 

player = pygame.image.load('BigShagHoofdz.png') #this must be rotateable 

pygame.display.set_caption('Leslie is Lauw') 
clock = pygame.time.Clock() 


gameDisplay.blit(background, [0,0]) 
gameDisplay.blit(player, [-1,223]) 
pygame.display.update() 

FPS = 60 

direction = "Down" 

def head(): 
    if direction == "Down": 
     playerhead = player 
    if direction == "Up": 
     playerhead = pygame.transform.rotate(player, 60) 



def gameLoop(): 
    global direction 
    gameExit = False 


    while gameExit==False: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
      gameExit = True 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
      gameExit = True 

      if event.key == pygame.KEYDOWN: 
       if event.key == pygame.K_UP: 
       direction = "Up" 
       elif event.key == pygame.K_DOWN: 
       direction = "Down" 

clock.tick(60) 
pygame.quit() 
quit() 

위로 또는 아래로 키를 눌렀을 때만 위아래로 회전해야하며 아래 또는 위로 밀면 부드럽게 움직여야합니다.

+0

당신은 블릿'에 gameloop'에서()' – furas

+0

내부'헤드()'당신은 지금 당신이 지역 만들기 때문에'글로벌 playerhead' 또는'반환 playerhead'을 사용해야합니다 while''()'내부가 변수. – furas

+0

정상 이미지와 회전 이미지의 두 이미지를 전환하고 싶습니까? – skrx

답변

2

일반 이미지와 회전 된 이미지간에 전환하려면 회전 된 버전을 while 루프 전에 만든 다음 키를 누르면 이미지를 전환하면됩니다.

import pygame 

pygame.init() 

gameDisplay = pygame.display.set_mode((640, 360)) 
clock = pygame.time.Clock() 
# The normal image/pygame.Surface. 
player_image = pygame.Surface((30, 50), pygame.SRCALPHA) 
player_image.fill((0, 100, 200)) 
pygame.draw.circle(player_image, (0, 50, 100), (15, 20), 12) 
# The rotated image. 
player_rotated = pygame.transform.rotate(player_image, 60) 

FPS = 60 

def gameLoop(): 
    player = player_image 
    player_pos = [100, 223] 
    gameExit = False 

    while gameExit == False: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       gameExit = True 
      elif event.type == pygame.KEYDOWN: 
       # Assign the current image to the `player` variable. 
       if event.key == pygame.K_UP: 
        player = player_image 
       elif event.key == pygame.K_DOWN: 
        player = player_rotated 

     gameDisplay.fill((30, 30, 30)) 
     gameDisplay.blit(player, player_pos) 

     pygame.display.flip() 
     clock.tick(60) 

gameLoop() 
pygame.quit() 
+0

최근 코드와 이미지를 질문에 추가 할 수 있습니까? 그리고 애니메이션이 어떻게 보일지 좀 더 자세하게 설명하십시오. – skrx

관련 문제