-1

I am at the very beginning of learning pygame, and I have an image [https://imgur.com/a/t24fNiA], and it moves from side to side. With it being a circle, I want it to rotate/roll a little bit each time it moves.

I have checked online about this, but couldn't really apply it to my own work. so far all I have is def rotcenter(image, angle):. I don't really know where to go from here.

 if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    xchange = -5
                    # image rotates

                elif event.key == pygame.K_RIGHT:
                    xchange = 5
                    # image rotates

In place of the # image rotates, I would like to call a subroutine which rotates the image by a certain angle.

Ideally it shouldn't go to far because each change in x is 5, but hopefully someone can help me to come up with a solution. Please try to simplify/explain stuff which I might not understand, I have only been using pygame for the best part of about 40 minutes, mainly reading tutorials.

Any help would be appreciated! Thanks :)

xupaii
  • 465
  • 4
  • 15
  • 31

1 Answers1

1

You can use pygame.transform.rotate to rotate an image.

def rotcenter(image, angle):
    return pygame.transform.rotate(image, angle)

would be your function. You pass it the already loaded pygame image and it returns a new surface that is rotated. So when you want to rotate the image you would do something like

image = rotcenter(image, 5)

and then blit the new image.

Code I used to test this:

import pygame

pygame.init()

display = pygame.display.set_mode((400, 400))

image = pygame.image.load('test.png') #Use the name of your image
rotated = pygame.transform.rotate(image, 90)

done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    display.blit(image, (0, 0))
    display.blit(rotated, (100, 0))

    pygame.display.flip()

pygame.quit()
nj1234
  • 66
  • 4
  • `image = rotcenter(playerImg, 5)`, `gameDisplay.blit(image, (x,y))` I have this, but nothing happens? Is the blit wrong, or is there something else? – xupaii Mar 24 '19 at 14:00
  • does the image not rotate, or not appear at all? – nj1234 Mar 24 '19 at 14:11
  • My image is there, however doesn't rotate. – xupaii Mar 24 '19 at 14:12
  • Does the x position change by 5 when you press an arrow? We need to make sure that the event code actually gets called. – nj1234 Mar 24 '19 at 14:18
  • The image does move from side to side, but doesn't rotate/roll – xupaii Mar 24 '19 at 14:19
  • I can't see your full code so I don't know. I edited my answer with the full code I used to test that the rotate works so check to see what is different between mine and yours. – nj1234 Mar 24 '19 at 14:24
  • I don't see what I am doing wrong, in honesty. Here is my full code: https://pastebin.com/wXmgfD0Y It might be obvious, but I don't see what is wrong. Sorry for the hassle! – xupaii Mar 24 '19 at 14:33
  • update: https://imgur.com/a/YvNDtFL the image gets crumpled and goes off-screen – xupaii Mar 24 '19 at 14:51