[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Tiling a Rotated Bitmap



Pete Shinners wrote:
I would expect you could do this. You'll need to rotate the surface, and then blit it back onto itself 8 times with offsets. The offsets will be the rotated corners of the original image rect.


Yeah, it didn't work. It seems like it should, but run the following code (your code worked up into a program), and wave the mouse back and forth to change the angle.

Odd, huh? It's a tricky bit, apparently. The only hits I found on the internet were for software patents I didn't understand.

--Kamilche


import pygame
import math

filename=r"C:\Documents and Settings\Administrator\Desktop\batik.jpg"

SCREENWIDTH = 640
SCREENHEIGHT = 480

def Rotated(surf, degrees = 0):
    # rotate surface
    print 'Rotated ', degrees, ' degrees.'
    rotsurf = pygame.transform.rotate(surf, degrees)

    # compute the offsets needed
    c = math.cos(math.radians(degrees))
    s = math.sin(math.radians(degrees))
    widthx = (surf.get_width() - 1) * c
    widthy = (surf.get_width() - 1) * s
    heightx = (surf.get_height() - 1) * s
    heighty = (surf.get_height() - 1) * c

    # the 8 positions, corners first
    positions = [
        (widthx + heightx, widthy + heighty),
        (widthx - heightx, widthy - heighty),
        (-widthx + heightx, -widthy + heighty),
        (-widthx - heightx, -widthy - heighty),
        (widthx, widthy),
        (-widthx, -widthy),
        (heightx, heighty),
        (-heightx, -heighty),
    ]

    # apply the blits
    for pos in positions:
        rotsurf.blit(rotsurf, pos)
    return rotsurf

def main():
    pygame.init()
    bg = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT), 0, 32)

    quit = 0
    pic = pygame.image.load(filename).convert_alpha()
    pic2 = pic
    while not quit:
        bg.fill((255, 0, 0, 255))
        bg.blit(pic2, (0, 0))
        pygame.display.flip()
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                quit = 1
                break
            elif e.type == pygame.MOUSEMOTION:
                pic2 = Rotated(pic, e.pos[0])

    pygame.quit()

main()