Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22466

IslandT: Rotate and scale image with Pygame

$
0
0

In this article, I will create a small python program using the pygame framework to scale up and rotate an image on the display screen. The image will keep on rotating counterclockwise at 0.1 degrees on each update!

The method which I used in this program to scale and rotate the image is:

rotate_image = pygame.transform.rotozoom(pawn0, original_rotation, 2)

The first parameter of the method is the surface of the normal image. The second parameter is the degree of rotation in the counterclockwise motion which is in float value. The third parameter is the scale of the resized image which is also in the float value! This method will return the rotating surface which will be used in the screen.blit method below.

Below is the whole program…

# Import and initialize the pygame library
import pygame
import pygame.gfxdraw

pygame.display.init()

# set the caption on the panel
pygame.display.set_caption("Draw Some Drawing")

# windows height and width
windowwidth = 600
windowheight = 600

# hat image load
pawn0 = pygame.image.load("pawn.png")
width, height = pawn0.get_size() # return width and height of the hat
# the initial position of the hat image
original_image_x = 260
original_image_y = 260
increment_rotation = 0.1
original_rotation = 0

# Print this line on output panel if the initialize process is successful
if(pygame.display.get_init() == True):
    print("Success initialize the game!")

# Initialize a window or screen for display
screen = pygame.display.set_mode([windowwidth, windowheight])

# the triangle color
tricolor = pygame.Color(0,0,0)

# Print the size of the game display window

print("The windows size is " + str(pygame.display.get_window_size()))

# Run the game until the user asks to quit
running = True
while running:

    # If the user clicks on the 'x' button on pygame display window then set running to false
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Fill the screen with white color
    screen.fill((255,255,255))
    original_rotation += increment_rotation # increase the angle at 0.1 degree
    rotate_image = pygame.transform.rotozoom(pawn0, original_rotation, 2) # rotates the image and makes it bigger
    screen.blit(rotate_image, (original_image_x, original_image_y))  # drawing the rotated image within screen surface

    pygame.display.flip() # refresh the screen

# Quit the game
pygame.display.quit()

And here is the outcome…

This image will keep on rotating!

Now here is an exercise for you all, make the image larger and then smaller again during each update!


Viewing all articles
Browse latest Browse all 22466

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>