I wanted to implement that the player rotates acording too the mouse cursor ( too look around ).
First i calcultate the degree the player needs to rotate in the : get_rotation() method , the result is stored in self.degree. After that i called the pygame.transform.rotate(self.imageF, self.degree) function , but it doesnt rotate the sprite.
import pygame
import settings
import math
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('sprites/Sprite-0001-player.png').convert_alpha()
self.imageF = pygame.transform.scale_by(self.image, 3)
self.rect = self.imageF.get_rect()
self.rect.center = (600, 400)
self.degree = 0
def get_rotation(self):
# Using Geometry to calculate the degree needed to rotate the player
posM = pygame.mouse.get_pos()
c = int(posM[0]) - self.rect.x
a = int(posM[1] - self.rect.y)
try:
tan = a/c
self.degree = round(math.degrees(math.atan(tan)), 1)
print(self.degree)
except:
print("0")
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
self.rect.x -= 5
if keys[pygame.K_d]:
self.rect.x += 5
if keys[pygame.K_w]:
self.rect.y -= 5
if keys[pygame.K_s]:
self.rect.y += 5
# Should rotate the player
Player.get_rotation(self)
pygame.transform.rotate(self.imageF, self.degree)
First of all i thought i maybe messed up the calculation but if i print the self.degree in the terminal it works just fine. So i guess there has to be a problem with my implemntation of pygame.
Thats how i came up with the Calculations
Tim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.