简体   繁体   中英

Pygame and rotation of a sprite

I've been playing around for a while now with the function pygame.transform.rotate() , _get_rect() , _get_rect().center and _get_size() .

I have an image of an arrow that I want to rotate and I have trouble to understand what's going on.

My question is from which point (x,y) is the rotation done?

In my example I have an image of size 28x182 and I put at 200,100 with screen.blit() .

You can use the examples here to rotate while keeping the image's centre:

def rot_center(image, rect, angle):
    """rotate an image while keeping its center"""
    rot_image = pygame.transform.rotate(image, angle)
    rot_rect = rot_image.get_rect(center=rect.center)
    return rot_image,rot_rect

I can't seem to find out what the centre of rotation pygame uses actually is, bar a reference here :

it doesn't rotate about the origin, or any particular point for that matter: the fixed point of rotation depends on the surface dimensions and the rotation angle

The original documentation doesn't seem to specify the answer to your question.

pygame.transform.rotate()

Unfiltered counterclockwise rotation. The angle argument represents degrees and can be any floating point value. Negative angle amounts will rotate clockwise.

Unless rotating by 90 degree increments, the image will be padded larger to hold the new size. If the image has pixel alphas, the padded area will be transparent. Otherwise pygame will pick a color that matches the Surface colorkey or the topleft pixel value.

#Imports
import random
import pygame
from pygame.locals import (K_LEFT, K_RIGHT)

# Pygame initialization
pygame.init()


# Class for creating test object
class Object(pygame.sprite.Sprite):

    # Init
    def __init__(self):
        super(Object, self).__init__()
        self.og_surf = pygame.transform.smoothscale(pygame.image.load("new_bullet.png").convert(), (100, 100))
        self.surf = self.og_surf
        self.rect = self.surf.get_rect(center=(400, 400))
        self.angle = 0
        self.change_angle = 0

    # THE MAIN ROTATE FUNCTION
    def rot(self):
        self.surf = pygame.transform.rotate(self.og_surf, self.angle)
        self.angle += self.change_angle
        self.angle = self.angle % 360
        self.rect = self.surf.get_rect(center=self.rect.center)

    # Move for keypresses
    def move(self, li):
        self.change_angle = 0
        if li[K_LEFT]:
            self.change_angle = 10
        elif li[K_RIGHT]:
            self.change_angle = -10
        obj.rot()


# Assigning required vars
run = True
screen = pygame.display.set_mode((800, 800))
obj = Object()
obj.angle = random.randint(50, 100)
obj.rect.x = 300
obj.rect.y = 299
all_sprites = pygame.sprite.Group()
all_sprites.add(obj)


# Main run loop
while run:
    # Screen fill
    screen.fill((50, 50, 50))

    # Getting presses for move
    presses = pygame.key.get_pressed()

    # Closing the game
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False

    # Rendering and rotating
    for x in all_sprites:
        x.move(presses)
        screen.blit(x.surf, x.rect)

    # Flipping and setting framerate
    pygame.display.flip()
    pygame.time.Clock().tick(10)

# Quit
pygame.quit()

That's the entire code of what I found from my testing, but the only part required to properly grasp it is this one -

# Class for creating test object
class Object(pygame.sprite.Sprite):

    # Init
    def __init__(self):
        super(Object, self).__init__()
        self.og_surf = pygame.transform.smoothscale(pygame.image.load("new_bullet.png").convert(), (100, 100))
        self.surf = self.og_surf
        self.rect = self.surf.get_rect(center=(400, 400))
        self.angle = 0
        self.change_angle = 0

    # THE MAIN ROTATE FUNCTION
    def rot(self):
        self.surf = pygame.transform.rotate(self.og_surf, self.angle)
        self.angle += self.change_angle
        self.angle = self.angle % 360
        self.rect = self.surf.get_rect(center=self.rect.center)

    # Move for keypresses
    def move(self, li):
        self.change_angle = 0
        if li[K_LEFT]:
            self.change_angle = 10
        elif li[K_RIGHT]:
            self.change_angle = -10
        obj.rot()

With the rot() and move() functions you can rotate the image with your keys, without distorting it, moving away from the center, or running out of memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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