Craftwr
Craftwr

Reputation: 1

Pygame 2d games sprite rotation jagged edges

I was just wondering, how to make the edges of a rotated 2d sprite smooth? Take the online game lordz.io for example. Even when you spin around, the edges of the player are completely fine. Is something like this achievable with .png assets in pygame (or in general, with any other tools)?

I searched everywhere and I didn't find any good solution. I know that there is antialiasing in 3d games, but I don't know if something like this is possible with png assets.

Upvotes: -1

Views: 60

Answers (2)

F_H
F_H

Reputation: 1

Try something like this :) I think this should work. I tried it out myself ;)


import pygame
from pygame.locals import *                  

image = pygame.image.load("/home/pi/Desktop/image.png") #Image location

#PYGAME_INITIALISATION
pygame.init()

#SCREEN_AND_TEXT_PARAMETERS
screen = pygame.display.set_mode( ( 350, 425 ) )

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

    #offset from pivot to center
    image_rect = image.get_rect(topleft = (pos[0] - originPos[0], pos[1]-originPos[1]))
    offset_center_to_pivot = pygame.math.Vector2(pos) - image_rect.center

    #roatated offset from pivot to center
    rotated_offset = offset_center_to_pivot.rotate(-angle)

    #roatetd image center
    rotated_image_center = (pos[0] - rotated_offset.x, pos[1] - rotated_offset.y)

    #get a rotated image
    rotated_image = pygame.transform.rotate(image, angle)
    rotated_image_rect = rotated_image.get_rect(center = rotated_image_center)

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

def rotate():
    w, h = image.get_size()
    pos = (175, 200) #X and Y-Position
    blitRotate(screen, image, pos, (w/2, h/2), angle) # blit rotated image
    pygame.display.flip() # update entire display

angle = 0 # starting angle

while True:
    for event in pygame.event.get() :
        #Check for pygame event
        if event.type == KEYDOWN:
            if event.key == K_LEFT: # arrow-key left
                angle -=10 #decrease angle
                rotate() #call funktion rotate()
                
            elif event.key == K_RIGHT: # arrow-key right
                angle +=10 # increase angle
                rotate() #call funktion rotate()
                
        elif event.type == pygame.QUIT :
            sys.exit()

All you need to do now is copy the code and save the image on your desktop and call it "image.png" or change the Location in the code. then start the code and you should be able to rotate the image using the left and right arrow keys.

You can use the bliRotate() function in any code, whenever the image needs to be roated. Just remeber, you also need to change the angel before calling the function.

Upvotes: 0

F_H
F_H

Reputation: 1

Though I´ve never tried it. Maybe try using the smoothscale function : pygame.transform.smoothscale(). (You can find mor deatils in the pygame dokumentation). If you scale the sprite 1:1. It may achieve the results your looking for.

Upvotes: -1

Related Questions