1

I want the image to face the way it is going. The game runs, but it does not show the player rotating. I need help because I've tried many different ways. There are no errors in the code either it just doesn't work. I also tried making a new variable that's the rotated version of the first one.

here's the code

import pygame
import os
pygame.init

pygame.font.init()

WIDTH, HEIGHT = 800, 700
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("zombie game")

FPS = 60
VEL = 5

PERSON_WIDTH, PERSON_HEIGHT = 55,40

GRASS = pygame.transform.scale(pygame.image.load(os.path.join(r'C:\Users\gabri\Downloads\zombie game\grass.png')), (WIDTH, HEIGHT))
PERSON_IMAGE = pygame.image.load(os.path.join(r'C:\Users\gabri\Downloads\zombie game\person.png'))
PERSON = pygame.transform.scale(PERSON_IMAGE, (PERSON_WIDTH, PERSON_HEIGHT))

WHITE = (0,0,0)


def draw_window(player):
    WIN.blit(GRASS, (0,0))
    WIN.blit(PERSON, (player.x, player.y))
    pygame.display.update()

def handle_movement(keys_pressed, player):
    if keys_pressed[pygame.K_a] and player.x - VEL > 0:
        player.x -= VEL
        pygame.transform.rotate(PERSON, 270)
    if keys_pressed[pygame.K_d] and player.x + VEL < WIDTH - 55:
        player.x += VEL
        pygame.transform.rotate(PERSON, 90)
    if keys_pressed[pygame.K_w] and player.y - VEL > 0:
        player.y -= VEL
        pygame.transform.rotate(PERSON, 0)
    if keys_pressed[pygame.K_s] and player.y +  VEL + player.height < HEIGHT:
        player.y += VEL
        pygame.transform.rotate(PERSON, 180)

def main():
    player  = pygame.Rect(372, 330, PERSON_WIDTH, PERSON_HEIGHT)

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        keys_pressed = pygame.key.get_pressed()
        handle_movement(keys_pressed, player)

        draw_window(player)


if __name__ == "__main__":
    main()
  • you already do `PERSON = pygame.transform.scale(PERSON_IMAGE, ...)` and I wonder why you didn't do the same with `pygame.transform.rotate`. But it would be better tokeep scaled image as original image and later always rotate original version `PERSON_ORIGINAL = pygame.transform.scale(...)` and later `PERSON = pygame.transform.rotate(PERSON_ORIGINAL,...)` – furas Jul 27 '21 at 20:40

1 Answers1

2

pygame.transform.rotate doesn't rotate the pygame.Surfece object itself. The function returns a new and rotated image:

pygame.transform.rotate(PERSON, 270)

rotated_image = pygame.transform.rotate(PERSON, 270)

e.g.:

PERSON = pygame.transform.scale(PERSON_IMAGE, (PERSON_WIDTH, PERSON_HEIGHT))
rotated_person = PERSON

# [...]

def draw_window(player):
    WIN.blit(GRASS, (0,0))
    WIN.blit(rotated_person, (player.x, player.y))
    pygame.display.update()

def handle_movement(keys_pressed, player):
    global rotated_person
    if keys_pressed[pygame.K_a] and player.x - VEL > 0:
        player.x -= VEL
        rotated_person = pygame.transform.rotate(PERSON, 270)
    if keys_pressed[pygame.K_d] and player.x + VEL < WIDTH - 55:
        player.x += VEL
        rotated_person = pygame.transform.rotate(PERSON, 90)
    if keys_pressed[pygame.K_w] and player.y - VEL > 0:
        player.y -= VEL
        rotated_person = pygame.transform.rotate(PERSON, 0)
    if keys_pressed[pygame.K_s] and player.y +  VEL + player.height < HEIGHT:
        player.y += VEL
        rotated_person = pygame.transform.rotate(PERSON, 180)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Now what do i do with the rotated image variable – gabriel riera Jul 27 '21 at 20:01
  • 2
    @gabrielriera `blit` it. Please read [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/54714144#54714144) – Rabbid76 Jul 27 '21 at 20:03