2017-05-04 61 views
0

我正在做一個簡單的太空遊戲,並希望我的船(xwingImg)自​​由移動。當按下a,d和s鍵時,我需要輪船旋轉。我認爲這將工作:pg.transform.rotate(xwingImg,90),但似乎沒有改變。如何在pygame中旋轉我的圖像?

import sys 
import random 
import pygame as pg 


pg.init() 
screen = pg.display.set_mode((1280, 800)) 
display_width = 1280 
display_height= 800 

black= (0,0,0) 
white= (255,255,255) 
red = (255,0,0) 
blue_violet = (138,43,226) 

xwingImg = pg.image.load('X-Wing.bmp').convert() 
tieImg= pg.image.load('tiefighter.png').convert() 
space=pg.image.load('space.jpg').convert() 

BG_image = pg.image.load('space.jpg').convert() 


def main(): 
    clock = pg.time.Clock() 
    # Surfaces/images have a `get_rect` method which 
    # returns a rect with the dimensions of the image. 
    player_rect = xwingImg.get_rect() 
    player_rect.center = (640,400) 
    change_x = 0 
    change_y = 0 
    enemies = [] 
    spawn_counter = 30 


    done = False 

    while not done: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       done = True 
      if event.type == pg.KEYDOWN: 
       if event.key == pg.K_d: 
        pg.transform.rotate(xwingImg, 90) 
        change_x = 5 
       if event.key == pg.K_w: 
        change_y = -5 
       if event.key == pg.K_s: 
        change_y= 5 
       if event.key == pg.K_a: 
        change_x = -5 
      if event.type == pg.KEYUP: 
       if event.key == pg.K_d and change_x > 0: 
        change_x = 0 
       if event.key == pg.K_a and change_x < 0: 
        change_x = 0 
       if event.key == pg.K_w and change_y<0: 
        change_y=0 
       if event.key == pg.K_s and change_y>0: 
        change_y=0 



     # Spawn enemies if counter <= 0 then reset it. 
     spawn_counter -= 1 
     if spawn_counter <= 0: 
      # Append an enemy rect. You can pass the position directly as an argument. 
      enemies.append(tieImg.get_rect(topleft=(random.randrange(1280), -800))) 
      spawn_counter = 30 


     # Update player_rect and enemies. 
     player_rect.x += change_x 
     player_rect.y += change_y 
     for enemy_rect in enemies: 
      enemy_rect.y += 5 
      # Collision detection with pygame.Rect.colliderect. 
      if player_rect.colliderect(enemy_rect): 
       print('Collision!') 

     # Draw everything. 
     screen.blit(BG_image, (0,0)) 
     for enemy_rect in enemies: 
      screen.blit(tieImg, enemy_rect) 
     screen.blit(xwingImg, player_rect) 

     if player_rect.x >display_width: 
      player_rect.x = 0 
     if player_rect.x < 0: 
      player_rect.x= 1280 
     if player_rect.y>display_height: 
      player_rect.y = 0 
     if player_rect.y < 0: 
      player_rect.y= 800 


     pg.display.flip() 
     clock.tick(40) 


if __name__ == '__main__': 
    main() 
    pg.quit() 
    sys.exit() 
+0

你使用的是什麼樣的形狀? – depperm

+1

可能的重複[如何使用Pygame旋轉圖像的中心?](http://stackoverflow.com/questions/4183208/how-do-i-rotate-an-image-around-its-center-using -pygame) –

+0

在矩形上的xwing圖像 – joe

回答

1

pygame.transform.rotate()返回與在其上的旋轉對象的新表面。您沒有使用返回的Surface。您不想隨着旋轉需要重複旋轉圖像來填充返回的曲面以使其適合於矩形。

旋轉(表面,角) - >表面

未過濾的逆時針旋轉。角度參數代表度數並可以是任何浮點值。負角度量將順時針旋轉。

除非90度的增量旋轉,圖像將被填充更大舉行新的大小。如果圖像具有像素alpha,則填充區域將是透明的。否則,pygame會選擇一個與表面顏色或顏色匹配的顏色相匹配的顏色。

我只是跟蹤圖像應該旋轉的角度,然後當檢測到旋轉事件時改變角度。然後在你的繪製步驟blit以該角度旋轉圖像。

player_image = pg.transform.rotate(xwingImg, angle) 
player_rect = player_image.get_rect() 
player_rect.center = player_pos 
screen.blit(player_image, player_rect)