简体   繁体   English

如何在pygame中旋转图片?

[英]how can I rotate my image in pygame?

I am making a simple space game and want my ship (xwingImg) to move freely. 我正在做一个简单的太空游戏,希望我的船(xwingImg)自​​由移动。 I need the ship to rotate when the a,d, and s keys are pressed. 按下a,d和s键时,我需要船旋转。 I thought that this would work: pg.transform.rotate(xwingImg, 90), but nothing seems to change. 我认为这会起作用:pg.transform.rotate(xwingImg,90),但似乎没有任何变化。

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()

pygame.transform.rotate() returns a new Surface with the rotated object on it. pygame.transform.rotate()返回带有旋转对象的新Surface。 You are not using the returned Surface. 您没有使用返回的Surface。 You dont want to keep rotating an image repeatedly as rotate needs to pad the returned Surface to make it fit in a rect. 您不希望继续重复旋转图像,因为旋转需要填充返回的Surface以使其适合于rect。

rotate(Surface, angle) -> Surface 旋转(表面,角度)->表面

Unfiltered counterclockwise rotation. 未过滤的逆时针旋转。 The angle argument represents degrees and can be any floating point value. angle参数表示度,可以是任何浮点值。 Negative angle amounts will rotate clockwise. 负角度量将顺时针旋转。

Unless rotating by 90 degree increments, the image will be padded larger to hold the new size. 除非以90度为增量旋转,否则图像将被较大地填充以保留新的尺寸。 If the image has pixel alphas, the padded area will be transparent. 如果图像具有像素Alpha,则填充区域将是透明的。 Otherwise pygame will pick a color that matches the Surface colorkey or the topleft pixel value. 否则pygame将选择与Surface colorkey或左上像素值匹配的颜色。

I would just keep track of the angle your image should be rotated then when a rotate event is detected change that angle. 我只是跟踪图像应该旋转的角度,然后在检测到旋转事件时更改该角度。 Then in your draw step blit the rotate image by that angle. 然后在你的牌步骤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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM