0

How can I make an Image / Sprite rotate to left / right when left-/right arrow key is pressed ? ... Like a wheel.

So here's the code which runs the game loop:

def gameplay_loop():
    exitgame = False
    cellpos_x = 0
    cellpos_y = cancer_cell.get_rect().height*2
    while not exitgame:
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                exitgame = True
                quitgame()
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_LEFT]:
            cellpos_x -= 10
        if key_pressed[pygame.K_RIGHT]:
            cellpos_x += 10

        gameplay_bg = pygame.image.load("/Users/nedimkanat/Desktop/python/img/gameplay_bg.png").convert()
        main_screen.fill(white)
        main_screen.blit(gameplay_bg, [0,0])
        main_screen.blit(cancer_cell, [cellpos_x, cellpos_y])
        pygame.display.flip()
        clock.tick(20)

I have tried to add it under key_pressed conditions, but nothing happened and I did as this guy says : https://stackoverflow.com/a/19316827/6256879

The sprite is called cancer_cell and as I mentioned above, I want it to rotate to Left/Right while Left/Right key is held down.

Community
  • 1
  • 1
  • WHat do you already have? The intent of this site is not to write entire progrmas for you. – jsbueno Aug 29 '16 at 16:59
  • In Pygame you have to use the `pygame.transform.rotozoom` function passing it your image as a pygame surface, but you will need a whole program around that call for it to work. – jsbueno Aug 29 '16 at 17:01
  • **1.** Learn how to handle events such as [key presses](http://stackoverflow.com/documentation/pygame/5110/event-handling#t=201608291705566686826). **2.** Learn about the functions that [rotate Surfaces](http://www.pygame.org/docs/ref/transform.html#pygame.transform.rotate). **3.** Try, make mistakes, try again, gain experience, and figure it out or ask more specific questions about what you don't understand here on Stack Overflow, where you show us what you've tried, what you expected and what went wrong. – Ted Klein Bergman Aug 29 '16 at 17:11
  • Check out my comment to your answer in this post : http://stackoverflow.com/questions/39201171/pygame-smoother-movement/39201830#comment65744938_39201830 . I think it will be important for you here, depending if you want the sprite to keep rotating whilst the key is pressed or to rotate by a given amount just once every time you press a key. As Ted Klein suggested check the Pygame documentation for rotating, and ask a more specific question with a code sample when you have done a few attempts. – Sorade Aug 29 '16 at 17:18
  • Updated question .. –  Aug 29 '16 at 19:24
  • @sorade , Im not sure what mouse_getpos() has to do with a spinning image. be specific now. –  Aug 29 '16 at 19:25

1 Answers1

0

Something like this might work, if you want to rotate by one degree everytime you click on the left or right arrow. If you want the image to rotate continuously you need to use the get_pressed() for the key of interest, so that the angle is applied at every frame.

def gameplay_loop():
    exitgame = False
    cellpos_x = 0
    cellpos_y = cancer_cell.get_rect().height*2
    while not exitgame:
        angle = 0 #reset the angle at each iteration
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                exitgame = True
                quitgame()
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_LEFT]:
            cellpos_x -= 10
            angle += 1.0
        if key_pressed[pygame.K_RIGHT]:
            cellpos_x += 10
            angle -= 1.0

        gameplay_bg = pygame.image.load("/Users/nedimkanat/Desktop/python/img/gameplay_bg.png").convert()
        main_screen.fill(white)
        main_screen.blit(gameplay_bg, [0,0])
        cancer_cell = pygame.transform.rotate(cancer_cell,angle)
        main_screen.blit(cancer_cell, [cellpos_x, cellpos_y])
        pygame.display.flip()
        clock.tick(20)

for a continuous rotation you can do:

def gameplay_loop():
    exitgame = False
    cellpos_x = 0
    cellpos_y = cancer_cell.get_rect().height*2
    while not exitgame:
        angle = 0 #reset the angle at each iteration
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                exitgame = True
                quitgame()
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_LEFT]:
            cellpos_x -= 10

        if key_pressed[pygame.K_RIGHT]:
            cellpos_x += 10


        gameplay_bg = pygame.image.load("/Users/nedimkanat/Desktop/python/img/gameplay_bg.png").convert()
        main_screen.fill(white)
        main_screen.blit(gameplay_bg, [0,0])
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_LEFT]: #do the same for the other key
               angle += 1              #and put -= instead of +=

        cancer_cell = pygame.transform.rotate(cancer_cell,angle)
        main_screen.blit(cancer_cell, [cellpos_x, cellpos_y])
        pygame.display.flip()
        clock.tick(20)
Sorade
  • 915
  • 1
  • 14
  • 29
  • `UnboundLocalError: local variable 'cancer_cell' referenced before assignment` –  Aug 30 '16 at 12:33
  • line 81 : `cellpos_y = cancer_cell.get_rect().height*2` line 99: `cancer_cell = pygame.transform.rotate(cancer_cell,angle)` –  Aug 30 '16 at 12:35
  • that is normal, look at the code from the question you provided, there as well `cancer_cell` is being used before you assign it. I assumed that you had defined your `cancer_cell` surface somewhere else in your code. For this code to work the `cancer_cell` variable needs to be a Pygame surface. – Sorade Aug 30 '16 at 13:30
  • i have, lol. line 28: `cancer_cell = pygame.image.load("/Users/wolf/Desktop/python/img/thornyball.png").convert_alpha()` –  Aug 30 '16 at 13:34
  • I'm sorry I can't see this in your code. Just define it further on and that should solve your issue. – Sorade Aug 30 '16 at 14:26
  • I have no idea how this is happening. Are you using two images to make up your cell ? If you are using a single surface it should work. One correction to my answer is that the angle should be in degrees not, in radians. – Sorade Aug 30 '16 at 15:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122211/discussion-between-sorade-and-idontreallywolf). – Sorade Aug 30 '16 at 15:06
  • Maybe worth checking [this page](http://www.pygame.org/docs/ref/transform.html#comment_pygame_transform_rotate) out as it has plenty of examples and things to look out for when thinking of rotating an image in Pygame. – Sorade Aug 31 '16 at 08:11