0

So I have remade my program to rotate and put images in a list, the image rotates, but does not do so centered. The image rotation looks like it is bouncing back and forth between the top left and bottom right corners.

I want the image to rotate in the center of the screen, on the center of the rotated images rectangle.

I recently found this video and tried to implement centered rotation with no luck: https://www.youtube.com/watch?v=ia7Syd2Zjn4

def loadImage(self):
    #load image file
    imagePath = filedialog.askopenfilename()
    self.userImage = pygame.image.load(imagePath).convert_alpha()

    # calculate scaled image size
    scaledImage = self.scale * self.userImage.get_height()
    self.userImage = pygame.transform.scale(self.userImage, (scaledImage, scaledImage))

    # create list for rotated images
    self.cells = []

    for i in range(360):
        # starting rect size
        self.rect = pygame.Rect(0, 0, scaledImage, scaledImage)

        #
        self.processedImage = pygame.Surface(self.rect.size).convert()
        self.processedImage.set_colorkey([0, 0, 0])

        # old center position
        previousCenter = self.rect.center

        # rotate image
        self.rotatedImage = pygame.transform.rotate(self.userImage, i)

        self.rect = self.userImage.get_rect()

        self.rect.center = previousCenter

        rect = self.rotatedImage.get_rect()

        # add rotated images to list
        self.cells.append(self.rotatedImage)
        print('Cell', i, self.cells[i])

        i += 1

def drawImage(self):
    clock = pygame.time.Clock()
    i = 0

    while True:
        thisIm = self.cells[i]
        print(thisIm)

        # rotation speed
        clock.tick(20)

        # screen position for image
        drawPos = (self.windowWidth // 2, self.windowWidth // 2)

        # screen blit
        self.mainWindow.blit(self.cells[i], drawPos)
        pygame.display.flip()

        # increment rotation and limit rotation to 360 degrees
        i += 1
        i %= 360

        # clear
        self.mainWindow.fill((0, 0, 0))
braX
  • 10,905
  • 5
  • 18
  • 32
w2019py
  • 3
  • 3
  • use `pygame.Rect` to keep screen size (`screen.get_rect()`) and for object's position and then you can do `positionImage.center = screen_rect.center` – furas Jul 24 '19 at 02:23
  • if you have 18 cells/images then you could create them before loop and keep in memory all time. And later only rotate them. – furas Jul 24 '19 at 02:28
  • The program will end up using different image files to preview, so cell size is variable. – w2019py Jul 24 '19 at 02:30
  • ` self.window.fill( (0,0,0) )` will clear screen using black color. – furas Jul 24 '19 at 02:30
  • it doesn't matter how many cells you have - you keep them on list - but now you create the same processedImage for every angle. So if you rotate it 360 degrees then you create every cell 360 times instead of only once. if you have 18 cells then you create 18*360 images (it gives 6480) but you could create only 18 – furas Jul 24 '19 at 02:33
  • Do you mind clarifying how to keep the image center? – w2019py Jul 24 '19 at 03:26
  • I started a new project that loads all rotated surfaces into a list. The new code has been posted above. I still have the exact same issue with my rotation bouncing around. – w2019py Jul 27 '19 at 03:21

1 Answers1

0

When you set mode then you get surface and you can get its rectangle

screen = pygame.display.set_mode((800, 600))
screen_rect = screen.get_rect()

to center image on screen you can get its rectangle and assign center or screen rectangle to center of image rectangel

test = pygame.transform.rotate(processedImage, angle)
test_rect = test.get_rect()

test_rect.center = screen_rect.center

self.window.blit(test, test_rect)

Similar way you can keep image position when you rotate it. You have to get its center from original image and assign to rotated image

rectangle = pygame.Rect(self.scaledImageHeight * i, 0, self.scaledImageHeight, ...)

test = pygame.transform.rotate(processedImage, angle)
test_rect = test.get_rect()

test_rect.center = rectangle.center

self.window.blit(test, test_rect)

It can works only if image is regular and it has real rotation's point in center of image.


In similar way you can center text on rectangle to create button.

text_rect.center = button_rect.center
furas
  • 119,752
  • 10
  • 94
  • 135