# -*- coding: utf-8 -*-

import pygame

pygame.init()

orig_image = pygame.Surface((200, 200))
orig_image.fill((200, 200, 200))
# make the image a bit more interesting
orig_image.fill((255, 0, 0), pygame.Rect(10, 10, 10, 10))
orig_image.fill((0, 255, 0), pygame.Rect(150, 10, 10, 10))
orig_image.fill((0, 0, 255), pygame.Rect(10, 150, 10, 10))
orig_image.fill((255, 255, 0), pygame.Rect(150, 150, 10, 10))
# this part should not get transparent!!
orig_image.fill((0, 0, 0), pygame.Rect(100, 100, 20, 20))

image = orig_image

pygame.display.set_caption("keys: left, right, 'c'")
screen = pygame.display.set_mode((800, 600))

rot = 0.0
use_color_key = False

running = True
while running:
    event = pygame.event.wait()
    if event.type == pygame.QUIT:
        running = False
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            running = False
        elif event.key == pygame.K_LEFT:
            rot += 15
        elif event.key == pygame.K_RIGHT:
            rot -= 15
        elif event.key == pygame.K_c:
            use_color_key = not use_color_key

        # why is 360 degrees not the same as 0 degrees? It shows a black border at the right and bottom
        # side, are those rounding errors?
        # rot %= 360.0
        
        image = pygame.transform.rotozoom(orig_image, rot, 1.0)
        
        # huh? why does the size of the image change for multiples of 90??
        
        print("rotation", rot, "size", image.get_size(), "use_color_key", use_color_key)
        
        # can't apply this always because if angle is 0 then the entire surface would be transparent
        # also I would expect that the padded parts are transparent by default, why do I have to care
        # about it? 
        if rot % 90 != 0 and use_color_key:
            # this line could also affect any parts within the image, that would be very BAD!
            image.set_colorkey(image.get_at((0, 0)))

    screen.fill((255, 0, 255))
    # use the rect to center the image on screen
    rect = image.get_rect(center=(400, 300))
    screen.blit(image, rect)
    
    pygame.display.flip()



