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 :: abc python 
Python :: Example of lambda function in python with list 
Python :: linked lists python 
Python :: python pygame how to start a game 
Python :: how to make a resizable python tkinter window have a limit 
Python :: what does int do in python 
Python :: how to create enter pressed for qlineedit in pyqt5 
Python :: como comentar en Python? 
Python :: python get list of file and time created 
Python :: binary, decimal, hex conversion python 
Python :: Transform networkx graph to dataframe 
Python :: wintp python manage.py createsuperuser 
Python :: twitter api v2 python tweepy 
Python :: print 2 decimal places python 
Python :: extract int from string python 
Python :: swagger library for django 
Python :: how to create dictionary between two columns in python 
Python :: try python import 
Python :: python spammer 
Python :: seaborn and matplotlib Setting the xlim and ylim python 
Python :: select realted for foreign key table in django 
Python :: python find string 
Python :: pandas average every n rows 
Python :: Python JSON API example 
Python :: python mettre en minuscule 
Python :: python is folder or file 
Python :: add place in certain index python string 
Python :: python extract zip file 
Python :: get multiple inputs in python 
Python :: install a lower version of python using conda 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =