-1

I'm importing an image and trying to get it to "face" the mouse as a player character would. I've gotten the movement to work, including scaling to length with a vector, so diagonal movement isn't faster than horizontal/vertical movement but moving the mouse just changes the size of the image for some reason, it doesn't rotate it.

I'm not sure what I could be doing wrong, to cause pygame.transform.rotate to instead scale the image, so I'm just putting the whole code here because I have no idea what I'm doing wrong. I've tried about 4 different ways of getting it to rotate from various places on this website, and they all seems to make the image scale in various ways, rather than rotate.

import pygame
import math
from pygame.locals import (
    RLEACCEL,
    K_w,
    K_a,
    K_s,
    K_d,
    K_ESCAPE,
    KEYDOWN,
    KEYUP,
    QUIT
)

pygame.mixer.init()

pygame.init()

clock = pygame.time.Clock()

player_class = "default"

screen_width = 1920
screen_height = 1080
screen = pygame.display.set_mode((screen_width,screen_height))

#player character class speed. Move into player class info when ready
player_speed = 5
player_spawn = [20.0, 20.0]


class Player(pygame.sprite.Sprite):

    def __init__(self, player_spawn):
        super(Player, self).__init__()
        self.image = pygame.image.load(f"{player_class}.png").convert()
        self.orig_image = self.image
        self.rect = self.image.get_rect(center=player_spawn)


    def update(self, pressed_keys):
        global player_speed

        move_up = 0
        move_down = 0
        move_left = 0
        move_right = 0
        if pressed_keys[K_w]:
            move_up = 1
        if pressed_keys[K_a]:
            move_left = 1
        if pressed_keys[K_s]:
            move_down = 1
        if pressed_keys[K_d]:
            move_right = 1

        player_vector = pygame.math.Vector2(
            move_right - move_left,
            move_down - move_up
        )

        if player_vector.x != 0 or player_vector.y != 0:
            player_vector.scale_to_length(player_speed)
            self.rect.move_ip(player_vector.x, player_vector.y)

        direction = pygame.math.Vector2(pygame.mouse.get_pos()) - self.rect.center
        radius, angle = direction.as_polar()
        self.image = pygame.transform.rotate(self.orig_image, angle)
        self.rect = self.image.get_rect(center=self.rect.center)


        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > screen_width:
            self.rect.right = screen_width
        if self.rect.top < 0:
            self.rect.top = 0
        if self.rect.bottom > screen_height:
            self.rect.bottom = screen_height


player = Player(player_spawn)

all_sprites = pygame.sprite.Group()
all_sprites.add(player)

running = True

while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False

        elif event.type == QUIT:
            running = False

    pressed_keys = pygame.key.get_pressed()
    player.update(pressed_keys)

    screen.fill((135, 206, 250))

    for entity in all_sprites:
        screen.blit(entity.image, entity.rect)

    pygame.display.flip()

    clock.tick(60)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Dornam
  • 1
  • 1
  • [How do I rotate an image around its center using Pygame?](https://stackoverflow.com/questions/4183208/how-do-i-rotate-an-image-around-its-center-using-pygame) is the answer to your question. A rotated image is larger than the original image because a surface is organized in axially aligned rows and columns. Once you rotate an image, the bounding rectangle of the image is larger than the rectangle of the original image. – Rabbid76 Aug 12 '23 at 19:05
  • See also [How to rotate an image(player) to the mouse direction?](https://stackoverflow.com/questions/58603835/how-to-rotate-an-imageplayer-to-the-mouse-direction/58604116#58604116) – Rabbid76 Aug 12 '23 at 19:08
  • Of course the image must have an alpha channel. Use `convert_alpha()` instead of `convert`. See [How can I make an Image with a transparent Backround in Pygame?](https://stackoverflow.com/questions/62623341/how-can-i-make-an-image-with-a-transparent-backround-in-pygame/62623422#62623422) – Rabbid76 Aug 12 '23 at 19:15
  • Your code works fine if you replace `convert()` with `convert_alpha()` and `angle` with `-angle`. This is all well explained in the answers to the duplicate questions. – Rabbid76 Aug 12 '23 at 19:19
  • Thank you! Your comment #3 was what I needed! I had actually looked at all those other questions you linked, and didn't see convert_alpha() being the solution to their problems, so none of those other solutions worked when I tried them out. Could you explain why that was the solution? I'm not really sure convert_alpha() works, but not convert() – Dornam Aug 12 '23 at 21:22
  • The examples of all these other solutions are working. You just added `convert()` for no reason. Your problem has nothing at all to do with rotation. Your problem is only that you are using a surface without trasparency. – Rabbid76 Aug 13 '23 at 04:44

0 Answers0