Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rotating a rectangle
#17
This works. I finally installed pygame and found an error with pygame rotating surface the opposite direction that the surface was rotated about the origin.
import pygame, math
from pygame.locals import *
    
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
degrees = 0
FPS = 30
 
def move_surface(surface, center=(0, 0)):
    '''Move surface to be centered at center(x, y)'''
    bounds = surface.get_rect(center = center)
    DISPLAYSURF.blit(surface, bounds)
 
def rotate_surface(surface, degrees, origin=(0, 0), offset=0):
    '''Rotate surface about an origin.
    surface - The thing to rotate
    degrees - How much to rotate surface in degrees
    origin - Center of rotation
    offset - Distance from center of rotation to center of surface
    '''
    x, y = origin  # Where are we rotating around
 
    # Calculate center of rotated surface
    if offset != 0:
        radians = degrees * math.pi / 180  # Need angle in radians for sin() and cos()
        x += int(offset * math.cos(radians))
        y -= int(offset * math.sin(radians))   # This was +=
 
    # Rotate the surface
    if degrees != 0:
        surface = pygame.transform.rotate(surface, degrees)  # Pygame uses degrees
 
    move_surface(surface, (x, y))
 
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Sailing!')
fpsClock = pygame.time.Clock()
Sail = pygame.Surface([100,10])
Sail.set_colorkey(BLACK)
Sail.fill(WHITE)
 
running = True
while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
            break
      
    DISPLAYSURF.fill(BLUE)
    rotate_surface(Sail, degrees, (200, 150), 50)
    pygame.display.flip()
    fpsClock.tick(FPS)
    degrees += 1
 
pygame.quit()
Another way to fix this is:
# Change surface rotation so + is clockwise
x += int(offset * math.cos(radians))
y += int(offset * math.sin(radians))
surface = pygame.transform.rotate(surface, -degrees)
Now the rectangle rotates clockwise for positive degrees.
Reply


Messages In This Thread
Rotating a rectangle - by CompleteNewb - Aug-23-2021, 09:20 PM
RE: Rotating a rectangle - by deanhystad - Aug-24-2021, 09:54 AM
RE: Rotating a rectangle - by CompleteNewb - Aug-24-2021, 04:15 PM
RE: Rotating a rectangle - by deanhystad - Aug-24-2021, 05:49 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-24-2021, 06:19 PM
RE: Rotating a rectangle - by deanhystad - Aug-24-2021, 08:22 PM
RE: Rotating a rectangle - by BashBedlam - Aug-24-2021, 09:46 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-24-2021, 11:30 PM
RE: Rotating a rectangle - by Windspar - Aug-25-2021, 04:15 AM
RE: Rotating a rectangle - by deanhystad - Aug-25-2021, 03:47 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-25-2021, 03:55 PM
RE: Rotating a rectangle - by deanhystad - Aug-25-2021, 04:34 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-25-2021, 05:27 PM
RE: Rotating a rectangle - by deanhystad - Aug-25-2021, 06:44 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-25-2021, 07:13 PM
RE: Rotating a rectangle - by deanhystad - Aug-25-2021, 07:32 PM
RE: Rotating a rectangle - by deanhystad - Aug-27-2021, 09:39 AM
RE: Rotating a rectangle - by deanhystad - Aug-27-2021, 11:42 AM
RE: Rotating a rectangle - by Windspar - Aug-27-2021, 05:18 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-28-2021, 03:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Surface and rectangle in pygame Fabrizio_fg 6 2,783 May-27-2023, 09:15 AM
Last Post: Fabrizio_fg
  Make rectangle snap to other rectangle corners Pedroski55 2 4,552 Aug-13-2021, 09:59 PM
Last Post: Pedroski55
  [PyGame] Rotating image issue Evoluxman 8 6,487 Oct-12-2019, 12:54 PM
Last Post: Evoluxman
  [PyGame] Rectangle keeps teleporting? keyfive 1 3,332 Jun-27-2018, 11:49 PM
Last Post: sonnekrieger7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020