I just want to preface this by saying I am very new to pygame.
At the moment I have calculated the angle that I need to apply to my character so he can rotate but I am unsure how to actually make him rotate (I understand the principle of it but not how to code it). I have tried to watch videos and look at other questions on here but I have only seen lots of different solutions that I can't implement into my own code.
I want to make it so that my character faces wherever my mouse is moving.
import pygame import math from random import randint pygame.init() screen = pygame.display.set_mode((600, 600)) screen_width = 600 screen_height = 600 pygame.display.set_caption("Mousey") char = pygame.image.load('76011.png') background = pygame.image.load('pygameback.png') clock = pygame.time.Clock() class Player: def __init__(self, x, y, width, height): self.x = x self.y = y self.width = width self.height = height self.velocity = 12.5 self.original = pygame.image.load('76011.png') self.rotated = self.original self.rect = self.rotated.get_rect() self.angle = 0 def draw(self, win): screen.blit(char, (self.x, self.y)) class Enemy(Player): def __init__(self, x, y, width, height): Player.__init__(self, x, y, width, height) def draw(self, win): pygame.draw.rect(win, (0, 255, 0), (jeff.x, jeff.y, jeff.width, jeff.height)) def drawWindow(): screen.blit(background, (0, 0)) mag.draw(screen) jeff.draw(screen) pygame.display.update() def XOR(a, b): if bool(a) != bool(b): return True return False mag = Player(300, 300, 60, 60) jeff = Enemy(180, 180, 60, 60) run = True while run: clock.tick(40) keys = pygame.key.get_pressed() for e in pygame.event.get(): if e.type == pygame.QUIT: run = False if e.type == pygame.MOUSEBUTTONDOWN: mouse_x, mouse_y = pygame.mouse.get_pos() if e.type == pygame.MOUSEMOTION: mouse_x, mouse_y = pygame.mouse.get_pos() rel_x, rel_y = mouse_x - mag.x, mouse_y - mag.y angle = math.atan2(rel_y, rel_x) if XOR(keys[pygame.K_a], keys[pygame.K_LEFT]) and mag.x > mag.velocity: mag.x -= mag.velocity right = False left = True elif XOR(keys[pygame.K_d], keys[pygame.K_RIGHT]) and mag.x < screen_height - mag.width - mag.velocity: mag.x += mag.velocity right = True left = False elif XOR(keys[pygame.K_w], keys[pygame.K_UP]) and mag.y > mag.velocity: mag.y -= mag.velocity right = False left = False elif XOR(keys[pygame.K_s], keys[pygame.K_DOWN]) and mag.y < screen_height - mag.height - mag.velocity: mag.y += mag.velocity right = False left = False drawWindow()
I was also confused about where I need to make the character rotate. By this I mean, do I make a method inside the class that calculates the angle and makes the character rotate OR do I do all the calculations down where they are now?