Introduction
Pygame library is one of the python programming libraries with very efficient functionalities such as building custom events, taking input from the user through the keyboard, creating custom input boxes for taking inputs, etc. This article will discuss how to use the mouse to scale and rotate (to transform) an image in Pygame. I hope you will enjoy the article!
Transformation Of an Image in Pygame
To transform an image, we have two cases: to scale and rotate an image. To scale an image: we need to know at what ratio the image needs to be increased or decreased. And to rotate an image, we need to know at what angle the image needs to be rotated. For these transformations, we need to know the current coordinates of an image. The following functions and properties will help us to transform an image.
- > event.pos: to get the current position.
- > math.degrees: to calculate and get the angle in degrees.
- >pygame.transform.rotozoom(): We can calculate the updated position of the image using the rotozoom function, which is a combined scale and rotation transform.
The process of transforming an image is simple. We need to create a rectangle box around our image, and at first, we need to fetch the current position, and then we need to set what happens when the game is in a running state. We will take a look at the code below.
Pygame application code to demonstrate the transformation of an image:
import pygame
import math
from pygame.locals import *
# Set the colors
RED = (255, 0, 0)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
#intialize the pygamem constructor
pygame.init()
#Set dimensions of screen
w, h = 600, 440
screen = pygame.display.set_mode((w, h))
# Set the intial running, angle and scale values
running = True
angle = 0
scale = 1
# Set a sample image
img_logo = pygame.image.load('Login_page.png')
img_logo.convert()
# Draw a rectangle around the image using get_rect()
rect_logo = img_logo.get_rect()
pygame.draw.rect(img_logo, RED, rect_logo, 1)
# Set the current center and mouse position
center = w//2, h//2
mouse = pygame.mouse.get_pos()
img = img_logo
rect = img.get_rect()
rect.center = center
# we need to set what happens when game is in running state
while running:
for event in pygame.event.get():
# Close if the user quits the game
if event.type == QUIT:
running = False
# Dealing with the angle part
if event.type == KEYDOWN:
if event.key == K_ra:
if event.mod & KMOD_SHIFT:
angle -= 5
else:
angle += 5
# Dealing with the scaling part
elif event.key == K_sa:
if event.mod & KMOD_SHIFT:
scale /= 1.5
else:
scale *= 1.5
# Move the image with the modified coordinates,
# angle and scale
elif event.type == MOUSEMOTION:
mouse = event.pos
xi = mouse[0] - center[0]
yi = mouse[1] - center[1]
d = math.sqrt(xi ** 2 + yi ** 2)
angle = math.degrees(-math.atan2(yi, xi))
scale = abs(5 * d / w)
img = pygame.transform.rotozoom(img_logo, angle, scale)
rect = img.get_rect()
rect.center = center
# Set screen color and image on screen
screen.fill(YELLOW)
screen.blit(img, rect)
# Draw the rectangle, line and circle through which image can be transformed
pygame.draw.rect(screen, BLACK, rect, 3)
pygame.draw.line(screen, RED, center, mouse, 2)
pygame.draw.circle(screen, RED, center, 6, 1)
pygame.draw.circle(screen, BLACK, mouse, 6, 2)
# Updating the pygame application
pygame.display.update()
# Quit the GUI game
pygame.quit()
You can have a try on running the above pygame program to taste how to use the mouse pointers to scale and rotate an image in pygame. You can refer gfg blog for more information.
Also See, Image Sampling