How to Rotate and Scale images using PyGame?


We can Rotate and scale an image in Pygame using pygame.transform.rotate and pygame.transform.scale function respectively of the Pygame module. The Function is used to rotate an image by a given angle in Degrees. The function takes two parameters - the image to rotate and the angle of rotation. In this article, we will use the pygame.transform.rotate and pygame.transform.scale function to rotate and scale the image respectively.

Rotate an image in Pygame

Algorithm

Rotating an Image in Pygame −

  • Import the pygame library.

  • Initialize Pygame by calling pygame.init().

  • Set the screen size using pygame.display.set_mode().

  • Load the image to rotate using pygame.image.load().

  • Rotate the image using pygame.transform.rotate() and specify the angle of rotation in degrees.

  • Use screen.blit() to draw the original and rotated images on the screen.

  • Call pygame.display.flip() to update the display.

  • Wait for the user to close the window using a while loop that listens for the pygame.QUIT event.

  • Quit Pygame using pygame.quit().

Syntax

pygame.transform.rotate(Surface, angle)

Here, Surface is the Surface to rotate and angle is the angle to rotate the Surface, in degrees. The function returns a new Surface with the rotated image.

Example

The code below loads an image called python-logo.png, and rotates it by 45 degrees using the pygame.transform.rotate function, and then displays both the original and rotated images on the screen. The blit function is used to draw the images on the screen.

import sys
import pygame

pygame.init()

screen = pygame.display.set_mode((400, 400))

# Load the image to rotate
image = pygame.image.load('python-logo.png')

# Rotate the image by 45 degrees
rotated_image = pygame.transform.rotate(image, 45)

# Display the original and rotated image on the screen
screen.blit(image, (0, 0))
screen.blit(rotated_image, (200, 0))

pygame.display.flip()

# Wait for the user to close the window
while True:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         pygame.quit()
         sys.exit()

Output

Scaling an image in Pygame

Pygame provides a function called pygame.transform.scale that scales an image to a given size. The function takes two parameters: the image to scale and the new size of the image. The code below uses the pygame.transform.scale function to scale the image to twice the original size.

Algorithm

Scaling an Image in Pygame −

  • Import the pygame library.

  • Initialize Pygame by calling pygame.init().

  • Set the screen size using pygame.display.set_mode().

  • Load the image to scale using pygame.image.load().

  • Scale the image using pygame.transform.scale() and specify the new size of the image as a tuple of (width, height).

  • Use screen.blit() to draw the original and scaled images on the screen.

  • Call pygame.display.flip() to update the display.

  • Wait for the user to close the window using a while loop that listens for the pygame.QUIT event.

  • Quit Pygame using pygame.quit().

Syntax

pygame.transform.scale(Surface, size)

Here, Surface is the Surface to scale and size is a tuple representing the new size of the Surface. The function returns a new Surface with the scaled image.

Example

import pygame
import sys

pygame.init()

screen = pygame.display.set_mode((700, 500))

# Load the image to scale
image = pygame.image.load('python-logo.png')

# Scale the image to 2x its original size
scaled_image = pygame.transform.scale(image, (image.get_width() * 2, image.get_height() * 2))

# Display the original and scaled image on the screen
screen.blit(image, (0, 0))
screen.blit(scaled_image, (200, 0))

pygame.display.flip()

# Wait for the user to close the window
while True:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         pygame.quit()
         sys.exit()

Output

Conclusion

In this article, we have rotated and scaled an image using Pygame pygame.transform.rotate and pygame.transform.scale methods. These are easy-to-use methods and take certain parameters to rotate and scale the images. With these functions, you can create dynamic and visually appealing games and applications.

Updated on: 11-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements