我正在尝试使用左右箭头键旋转图像,但我的代码无效。有人可以帮助我做错了吗?
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()
我已经导入了所有模块,例如pygame和time,也完成了pygame.init()
,也设置了我的屏幕,我只想知道如何用箭头键连续旋转我的图像?
有人可以帮忙吗?
我的代码
这是我的代码,其中包含furas建议的更新。鼠标按钮工作,如果我点击K_LEFT图像只旋转一次,如果我点击K_LEFT更多它似乎旋转但我看不到结果(我说这似乎是因为如果我用鼠标去图像,然后单击在图像上绘制线条然后然后再次点击K_LEFT所有线条变黑,这意味着pygame会尝试将图像像素绘制到线条像素位置?)
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()