1

This might sound confusing so I'll try my best at describing.

I've wrote some code that in simple has an image in the middle of a window and it will face the mouse position (rotate to face it) when the mouse is over the window. This is fine and all but I have major trouble trying to move the image with WASD as well.

I want to add basic top down WASD movement to the image but whatever I try It's always 'overwritten' (I think) by the rotating code making it not able to move and stay in the direct middle of the screen.

the best I have able to do is move it a set amount (like 1 pixel of movement) from the middle in any direction, but if I hold down the key it stays 1 pixel out from the middle, and as soon as I let go it centers again. Here's the code that I've gotten the furthest in, but it still wont work. you can test it yourself you only need to change the 'cursor' image for it to work.

import math, pygame

pygame.init()
window = pygame.display.set_mode((1280,800))
cursor = pygame.image.load("images/cursor.png").convert_alpha()

#   0 - image is looking to the right
#  90 - image is looking up
# 180 - image is looking to the left
# 270 - image is looking down
correction_angle = 90

run = True
while run:
    pygame.time.delay(2)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    #cursor postion and rectangle
    cursor_pos  = window.get_rect().center
    cursor_rect = cursor.get_rect(center = (cursor_pos))

    #calculates mouse position, angle and rotation for image
    mx, my = pygame.mouse.get_pos()
    dx, dy = mx - cursor_rect.centerx, my - cursor_rect.centery
    angle = math.degrees(math.atan2(-dy, dx)) - correction_angle

    #rotated image surface
    rot_image      = pygame.transform.rotate(cursor, angle)
    rot_image_rect = rot_image.get_rect(center = cursor_rect.center)


    #simple movement / key presses
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        rot_image_rect.x -= 1
    if keys[pygame.K_d]:
        rot_image_rect.x += 1
    if keys[pygame.K_w]:
        rot_image_rect.y -= 1
    if keys[pygame.K_s]:
        rot_image_rect.y += 1

    
    #bliting all images and surfaces to screen and update screen.
    window.fill((220,216,192))
    window.blit(rot_image, rot_image_rect.topleft)
    pygame.display.update()




pygame.quit()
exit()
Meltt
  • 33
  • 3

1 Answers1

0

You have to change cursor_pos rather than rot_image_rect. Note rot_image_rect is calculated from cursor_pos. Hence if you want to change the position of rot_image_rect You have to change cursor_pos:

cursor_pos = list(window.get_rect().center)

run = True
while run:
    # [...]

     #simple movement / key presses
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        cursor_pos[0] -= 1
    # [...]

Complet example:

import math, pygame

pygame.init()
window = pygame.display.set_mode((1280,800))
cursor = pygame.image.load("images/cursor.png").convert_alpha()

#   0 - image is looking to the right
#  90 - image is looking up
# 180 - image is looking to the left
# 270 - image is looking down
correction_angle = 90

cursor_pos = list(window.get_rect().center)

run = True
while run:
    pygame.time.delay(2)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    #simple movement / key presses
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        cursor_pos[0] -= 1
    if keys[pygame.K_d]:
        cursor_pos[0] += 1
    if keys[pygame.K_w]:
        cursor_pos[1] -= 1
    if keys[pygame.K_s]:
        cursor_pos[1] += 1

    #cursor postion and rectangle
    cursor_rect = cursor.get_rect(center = (cursor_pos))

    #calculates mouse position, angle and rotation for image
    mx, my = pygame.mouse.get_pos()
    dx, dy = mx - cursor_rect.centerx, my - cursor_rect.centery
    angle = math.degrees(math.atan2(-dy, dx)) - correction_angle

    #rotated image surface
    rot_image      = pygame.transform.rotate(cursor, angle)
    rot_image_rect = rot_image.get_rect(center = cursor_rect.center)
    
    #bliting all images and surfaces to screen and update screen.
    window.fill((220,216,192))
    window.blit(rot_image, rot_image_rect.topleft)
    pygame.display.update()


pygame.quit()
exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174