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

Re: [pygame] Tiling a Rotated Bitmap



there were 3 problems with the thing above
---
the major problem was that sin & cos work in a space where +y is up,
pygame in a space where +y is down
a minor problem was that the four corners will never be visible at all
(think about the surface rotate produces - the bounds go through the
four corners of the rotated image)
a problem that actually isn't a problem (once it was tiling okay) was
that it kept drawing over itself (rotsurf.blit(rotsruf))

also as a note, you can tile a rotated image on a rotated grid (as
this code demonstrates) but their are complexities with filtering (as
I mentioned earlier)

but if you want to make a rectangular tile that tiles on a rectangular
grid from your rotated image, then that is more complicated (unless
the rotation is 45 and the original image square - which is what Aaron
was talking about), but I would guess it can be done, but may result
in a disgustingly large tile (you'd have to keep tiling the image on
the rotated grid until the edge lines line up and cut a rect at that
point)

anyways, here's the corrected code
----
import pygame
import math

filename=r"MockUpMemory.png"

SCREENWIDTH = 800
SCREENHEIGHT = 600

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),
#      (-widthx, -widthy),
       (-widthx, widthy),
#       (heightx, heighty),
       (heightx, heighty),
#       (-heightx, -heighty),
       (-heightx, -heighty),
   ]

   rot_copy = rotsurf.copy()
   # apply the blits
   for pos in positions:
       rotsurf.blit(rot_copy, 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()