python - How to rotate an image around its center using Pygame?

Python - How to rotate an image around its center using Pygame?

To rotate an image around its center using Pygame, you can follow these steps:

  1. Load the image you want to rotate.
  2. Get the rectangle that represents the image and calculate its center.
  3. Set the angle of rotation.
  4. Rotate the image around its center using the pygame.transform.rotate() function.
  5. Blit (draw) the rotated image onto the screen surface.

Here's an example implementation:

import pygame import sys # Initialize Pygame pygame.init() # Set the width and height of the screen (window) width, height = 800, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption('Image Rotation Example') # Load the image image = pygame.image.load('example.png') image_rect = image.get_rect() # Get the center of the image center = image_rect.center # Set the angle of rotation angle = 0 # Main game loop running = True while running: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Clear the screen screen.fill((255, 255, 255)) # Rotate the image rotated_image = pygame.transform.rotate(image, angle) rotated_rect = rotated_image.get_rect(center=center) # Blit the rotated image onto the screen screen.blit(rotated_image, rotated_rect.topleft) # Update the display pygame.display.flip() # Increment the angle of rotation angle += 1 # Limit the frame rate pygame.time.Clock().tick(60) # Quit Pygame pygame.quit() sys.exit() 

In this example:

  • We load an image named 'example.png' using pygame.image.load().
  • We get the rectangle representing the image and calculate its center using image.get_rect().center.
  • We set the initial angle of rotation to 0.
  • Inside the main game loop, we rotate the image using pygame.transform.rotate() with the specified angle.
  • We get the rectangle representing the rotated image with the center set to the original center using rotated_image.get_rect(center=center).
  • We blit the rotated image onto the screen surface at the appropriate position.
  • We update the display with pygame.display.flip() and limit the frame rate with pygame.time.Clock().tick(60).
  • The rotation angle is incremented by 1 degree in each iteration of the loop.

You can adjust the angle increment and other parameters according to your specific requirements.

Examples

  1. Python Pygame rotate image around center

    • Description: Demonstrates how to rotate an image around its center using the pygame.transform.rotate function.
    • Code:
      import pygame import sys pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() image = pygame.image.load('image.png') image_rect = image.get_rect(center=(400, 300)) angle = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() angle += 1 rotated_image = pygame.transform.rotate(image, angle) rotated_rect = rotated_image.get_rect(center=image_rect.center) screen.fill((255, 255, 255)) screen.blit(rotated_image, rotated_rect.topleft) pygame.display.flip() clock.tick(60) 
  2. Pygame rotate image around its own center

    • Description: Shows how to keep the image centered while rotating it around its own center.
    • Code:
      import pygame import sys pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() image = pygame.image.load('image.png') image_rect = image.get_rect(center=(400, 300)) angle = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() angle += 1 rotated_image = pygame.transform.rotozoom(image, angle, 1) rotated_rect = rotated_image.get_rect(center=image_rect.center) screen.fill((0, 0, 0)) screen.blit(rotated_image, rotated_rect.topleft) pygame.display.flip() clock.tick(60) 
  3. Pygame rotate image smoothly around center

    • Description: Utilizes the pygame.transform.smoothscale to ensure smooth rotation of the image.
    • Code:
      import pygame import sys pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() image = pygame.image.load('image.png') image_rect = image.get_rect(center=(400, 300)) angle = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() angle += 1 rotated_image = pygame.transform.rotate(image, angle) rotated_image = pygame.transform.smoothscale(rotated_image, rotated_image.get_rect().size) rotated_rect = rotated_image.get_rect(center=image_rect.center) screen.fill((255, 255, 255)) screen.blit(rotated_image, rotated_rect.topleft) pygame.display.flip() clock.tick(60) 
  4. Pygame rotate sprite around center of image

    • Description: Demonstrates how to rotate a sprite image around its center.
    • Code:
      import pygame import sys pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() image = pygame.image.load('sprite.png') image_rect = image.get_rect(center=(400, 300)) angle = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() angle += 1 rotated_image = pygame.transform.rotate(image, angle) rotated_rect = rotated_image.get_rect(center=image_rect.center) screen.fill((50, 50, 50)) screen.blit(rotated_image, rotated_rect.topleft) pygame.display.flip() clock.tick(60) 
  5. Rotate image by angle in Pygame

    • Description: Rotates an image by a specific angle and keeps it centered.
    • Code:
      import pygame import sys pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() image = pygame.image.load('image.png') image_rect = image.get_rect(center=(400, 300)) angle = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() angle = (angle + 1) % 360 # Rotate angle by 1 degree rotated_image = pygame.transform.rotate(image, angle) rotated_rect = rotated_image.get_rect(center=image_rect.center) screen.fill((0, 0, 0)) screen.blit(rotated_image, rotated_rect.topleft) pygame.display.flip() clock.tick(60) 
  6. Center image while rotating in Pygame

    • Description: Ensures the image stays centered in the window while rotating.
    • Code:
      import pygame import sys pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() image = pygame.image.load('image.png') image_rect = image.get_rect(center=(400, 300)) angle = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() angle += 1 rotated_image = pygame.transform.rotate(image, angle) rotated_rect = rotated_image.get_rect(center=image_rect.center) screen.fill((255, 255, 255)) screen.blit(rotated_image, rotated_rect.topleft) pygame.display.flip() clock.tick(60) 
  7. Pygame continuously rotate image around center

    • Description: Continuously rotates the image around its center in a loop.
    • Code:
      import pygame import sys pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() image = pygame.image.load('image.png') image_rect = image.get_rect(center=(400, 300)) angle = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() angle += 1 rotated_image = pygame.transform.rotate(image, angle) rotated_rect = rotated_image.get_rect(center=image_rect.center) screen.fill((0, 0, 0)) screen.blit(rotated_image, rotated_rect.topleft) pygame.display.flip() clock.tick(60) 
  8. Pygame rotate image around its center using rotate function

    • Description: Uses Pygame's pygame.transform.rotate function to rotate an image around its center.
    • Code:
      import pygame import sys pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() image = pygame.image.load('image.png') image_rect = image.get_rect(center=(400, 300)) angle = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() angle += 1 rotated_image = pygame.transform.rotate(image, angle) rotated_rect = rotated_image.get_rect(center=image_rect.center) screen.fill((255, 255, 255)) screen.blit(rotated_image, rotated_rect.topleft) pygame.display.flip() clock.tick(60) 
  9. Pygame rotate image around center point dynamically

    • Description: Dynamically rotates the image around a specified center point.
    • Code:
      import pygame import sys pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() image = pygame.image.load('image.png') image_rect = image.get_rect(center=(400, 300)) angle = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() angle += 1 center = (image_rect.centerx, image_rect.centery) rotated_image = pygame.transform.rotate(image, angle) rotated_rect = rotated_image.get_rect(center=center) screen.fill((200, 200, 200)) screen.blit(rotated_image, rotated_rect.topleft) pygame.display.flip() clock.tick(60) 

More Tags

angular-http shapely dateinterval widget google-polyline inspector drop-down-menu properties busybox firemonkey

More Programming Questions

More Housing Building Calculators

More Physical chemistry Calculators

More Fitness-Health Calculators

More Fitness Calculators