Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to rotate image in pygame

# first argument is the original_image
# second one is the angle by how much angle you want you image to be rotated
# third is the zoom amount here it is set to 1 because we don't need to zomm in or out

img_rotated = pygame.transform.rotozoom(original_image,angle,1)

# NOTE: you can use the pygame.transform.rotate but some times rotozoom works better
#** -*= Happy Coding /=*- **#

Ninja Hatori Cat Codr (https://github.com/PrathameshBhatkar)
                      (https://www.youtube.com/channel/UCHzOI93T5ZZ4TqYJqz6WmYQ)
Comment

rotate image in pygame

# Import pygame
import pygame
  
# Initialise pygame
pygame.init()
  
# Set window size
size = width,height = 600, 600
screen = pygame.display.set_mode(size)
  
# Clock
clock = pygame.time.Clock()
  
# Load image
image = pygame.image.load('gfg.png')
  
# Set the size for the image
DEFAULT_IMAGE_SIZE = (200, 200)
  
# Rotate the image by any degree
image = pygame.transform.rotate(image, 180)
  
# Set a default position
DEFAULT_IMAGE_POSITION = (200,200)
  
# Prepare loop condition
running = False
  
# Event loop
while not running:
  
    # Close window event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = True
  
    # Background Color
    screen.fill((0, 0, 0))
  
    # Show the image
    screen.blit(image, DEFAULT_IMAGE_POSITION)
  
    # Part of event loop
    pygame.display.flip()
    clock.tick(30)
Comment

pygame rotate image

import pygame
import pygame.font

pygame.init()
size = (400,400)
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()

def blitRotate(surf, image, pos, originPos, angle):

    # calcaulate the axis aligned bounding box of the rotated image
    w, h       = image.get_size()
    box        = [pygame.math.Vector2(p) for p in [(0, 0), (w, 0), (w, -h), (0, -h)]]
    box_rotate = [p.rotate(angle) for p in box]
    min_box    = (min(box_rotate, key=lambda p: p[0])[0], min(box_rotate, key=lambda p: p[1])[1])
    max_box    = (max(box_rotate, key=lambda p: p[0])[0], max(box_rotate, key=lambda p: p[1])[1])

    # calculate the translation of the pivot 
    pivot        = pygame.math.Vector2(originPos[0], -originPos[1])
    pivot_rotate = pivot.rotate(angle)
    pivot_move   = pivot_rotate - pivot

    # calculate the upper left origin of the rotated image
    origin = (pos[0] - originPos[0] + min_box[0] - pivot_move[0], pos[1] - originPos[1] - max_box[1] + pivot_move[1])

    # get a rotated image
    rotated_image = pygame.transform.rotate(image, angle)

    # rotate and blit the image
    surf.blit(rotated_image, origin)

    # draw rectangle around the image
    pygame.draw.rect (surf, (255, 0, 0), (*origin, *rotated_image.get_size()),2)

font = pygame.font.SysFont('Times New Roman', 50)
text = font.render('image', False, (255, 255, 0))
image = pygame.Surface((text.get_width()+1, text.get_height()+1))
pygame.draw.rect(image, (0, 0, 255), (1, 1, *text.get_size()))
image.blit(text, (1, 1))
w, h = image.get_size()

angle = 0
done = False
while not done:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key==pygame.K_ESCAPE:
                done = True

    pos = (screen.get_width()//2, screen.get_height()//2)
    pos = (200, 200)

    screen.fill(0)
    blitRotate(screen, image, pos, (w//2, h//2), angle)
    angle += 1

    pygame.draw.line(screen, (0, 255, 0), (pos[0]-20, pos[1]), (pos[0]+20, pos[1]), 3)
    pygame.draw.line(screen, (0, 255, 0), (pos[0], pos[1]-20), (pos[0], pos[1]+20), 3)
    pygame.draw.circle(screen, (0, 255, 0), pos, 7, 0)

    pygame.display.flip()

pygame.quit()
Comment

PREVIOUS NEXT
Code Example
Python :: exit in python 
Python :: merge two series by index 
Python :: only get top 10 python dataframe 
Python :: python merge dictionaries 
Python :: how to clean environment python 
Python :: RuntimeError: Broken toolchain: cannot link a simple C program 
Python :: colorbar min max matplotlib 
Python :: python logging basicconfig stdout 
Python :: numpy euclidean distance 
Python :: Write a Python program to get the Python version you are using. 
Python :: how to connect wifi using python 
Python :: color name to hex python 
Python :: how to encode hexadecimal python 
Python :: found features with object datatype 
Python :: add system path python jupytre 
Python :: split a variable into multiple variables in python 
Python :: python anagram finder 
Python :: ta-lib python install 
Python :: from array to tuple python 
Python :: count number of spaces in string python 
Python :: how to ask a question in python 
Python :: drupal 8 request_time 
Python :: Import "whitenoise.django" could not be resolved 
Python :: validate ip address python 
Python :: tensor to int python 
Python :: anaconda 3 geopandas 
Python :: max date by group pandas 
Python :: pandas drop row from a list of value 
Python :: python iterate with index 
Python :: html.unescape python 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =