0

I am trying to rotate an image around a point, ive got the code working, but it is turning from the center of the image but i want it to rotate on the bottom of my image: enter image description here

Here is my code:

import pygame
import math
screen = pygame.display.set_mode((500,500))
run = True
player_x = 250
player_y = 250
correction_angle = 90
while run:
    screen.fill((255,255,255))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            run = False
    #make the player move in every direction
    key = pygame.key.get_pressed()
    if key[pygame.K_a]:
        player_x -= 1 / 5
    if key[pygame.K_d]:
        player_x += 1 / 5
    if key[pygame.K_w]:
        player_y -= 1 / 5
    if key[pygame.K_s]:
        player_y += 1 / 5

    mx,my = pygame.mouse.get_pos()
    player_rect = pygame.Rect(player_x ,player_y,50,50)
    gun = pygame.image.load('gun.png').convert_alpha()
    dx,dy = mx - player_x  , my - player_y 
    angle = math.degrees(math.atan2(-dy,dx)) - correction_angle
    rotate_image = pygame.transform.rotate(gun, angle)
    rect = rotate_image.get_rect()

    pos = (((500 - rect.width)/2),((500 - rect.height)/2))
    pygame.draw.circle(screen, (0,255,0), ((500 - rect.width)/2,(500 - rect.height)/2),10)
    screen.blit(rotate_image,pos)
    pygame.display.update()
pass
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
Morris
  • 21
  • 3
  • See [How to set the pivot point (center of rotation) for pygame.transform.rotate()?](https://stackoverflow.com/questions/15098900/how-to-set-the-pivot-point-center-of-rotation-for-pygame-transform-rotate/69312319#69312319) and [Rotating and scaling an image around a pivot, while scaling width and height separately in Pygame](https://stackoverflow.com/questions/70819750/rotating-and-scaling-an-image-around-a-pivot-while-scaling-width-and-height-sep/70820034#70820034) – Rabbid76 Apr 18 '22 at 07:05
  • rabbid than you for being a very well cntributor to pygame, i see u everywhere and apprecate all your help. <3, now i already saw all the posts u mentioned above, but, it is really confusing for me to understand what is going on, like what is offset, why aree we multiplying it or..., I just need an easier solution, not 170 lines of code to do it, i mean ive already got it rotating but not from where i want it. – Morris Apr 18 '22 at 07:49
  • It is not possible to do this with 1 line of code. However, all you need is the 1st function from here: [How to set the pivot point (center of rotation) for pygame.transform.rotate()?](https://stackoverflow.com/questions/15098900/how-to-set-the-pivot-point-center-of-rotation-for-pygame-transform-rotate/69312319#69312319). There is no point in copying that answer to this question. – Rabbid76 Apr 18 '22 at 07:52
  • Your question is "duplicate". I used to answer questions and only then closed them as "duplicate". Unfortunately the moderators blamed me for it and I can't do that anymore. – Rabbid76 Apr 18 '22 at 07:57

0 Answers0