0

I am trying to rotate an image using the right and left arrow keys, but my code is not working. Can someone help what am i doing wrong?

        for ev in pygame.event.get():
            degree=5
            while pygame.key.get_pressed()[pygame.K_LEFT] == True:
                screen.blit(pygame.transform.rotate(image, degree), (100,100))
                degree+=5
                sleep(0.01)
                pygame.display.update()

I have already imported all the modules like pygame and time and also done the pygame.init(), also have my screen set up, i only want to know how can i rotate my image continusly with the arrow keys? Can someone help?

My code

Here is my code with the update suggested by furas in it. The mouse button works, if i hit the K_LEFT the image rotates only once, if i hit K_LEFT more it seems to rotate but i can't see the result (i say it seems because if i go on the image with the mouse and click to draw lines on the image and then hit K_LEFT again all the lines get black, which means pygame tries to draw image pixels to the line pixel positions?)

import pygame
import sys
from time import sleep
import random
from pygame.locals import *

# init video system:
pygame.init()

# Definitionen
pygame.mixer.music.load(
    "/home/amir/Music/free-sound/67884__benboncan__lake-waves-2.wav")
bildSchirm = pygame.display.set_mode((800, 800))
bild = pygame.image.load(
    "/home/amir/Documents/python/pygame/plane.png").convert()
pygame.display.set_caption("Shooting")
bild_pos = (400 - 74, 400 - 58)
bildSchirm.blit(bild, bild_pos)
pygame.display.update()
pygame.mixer.music.play(0, 0)


def bewegen(a, b=0):  # der Weg und die Richtung der Kugel
    lst = [pygame.mouse.get_pos()]
    richtungen = [(0, 10), (-10, 0), (10, 0), (0, -10)]
    rnd_richtung = random.choice(richtungen)
    while b < a - 1:
        b += 1
        lst.append((lst[-1][0] + rnd_richtung[0],
                    lst[-1][1] + rnd_richtung[1]))
    return lst


# main_loop
running = True
while running:
    for e in pygame.event.get():
        if e.type == KEYDOWN and e.key == K_q:
            running = False
        elif e.type == MOUSEBUTTONDOWN:
            for x in bewegen(random.randint(1, 50)):
                pygame.draw.rect(bildSchirm, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), Rect(x, (10, 10)), 0)
                sleep(0.002)  # die Geschwindigkeit der Schiesserei
                pygame.display.update()
        elif pygame.key.get_pressed()[pygame.K_LEFT] == True:
            bildSchirm.blit(pygame.transform.rotate(bild, 50), bild_pos)
    pygame.display.update()
pygame.quit()
  • The error is probably that you use `pygame.mouse.get_pressed()` instead of `pygame.key.get_pressed()` as your mouse doesn't have a left arrow key ;) – B8vrede Feb 01 '16 at 11:09
  • sorry, i corrected my question. No thats not the problem –  Feb 01 '16 at 11:11
  • In that case could you specify what is not working, is it displaying the image, is it not rotating or not responding to the key? Assuming it is displaying did it rotate when you did not make it depend on the keystroke? And does it trigger print statements inside the `while` section? – B8vrede Feb 01 '16 at 11:21
  • don't use `while` loop and `sleep` - it stops `mainloop` and all other functions. – furas Feb 01 '16 at 11:36
  • use `if` instead of `while` and `mainloop` will repeat it many times. And you will not need `update()` in this `if` – furas Feb 01 '16 at 11:48

0 Answers0