如何用键盘旋转图像?

how rotate the images with keyboards?

img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsXOR.png")
img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsNOT.png")
img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsAND.png")
background = pygame.image.load(r'C:\Users\ga-sa\Downloads\honeycomb.png')
images = [img1, img2, img3, img4]

current_image = -1
img_rects = [images[i].get_rect(topleft=(20 + 40 * i, 20)) for i in range(len(images))]

LeftButton = 0
while 1:
    for e in pygame.event.get():
        if e.type == QUIT:
            pygame.quit()
            exit(0)

        if e.type == pygame.MOUSEBUTTONDOWN:
            mouse_rect = pygame.Rect(e.pos, (1, 1))
            current_image = mouse_rect.collidelist(img_rects)

        if e.type == MOUSEMOTION:
            if e.buttons[LeftButton]:
                rel = e.rel
                if 0 <= current_image < len(images):
                    img_rects[current_image].x += rel[0]
                    img_rects[current_image].y += rel[1]
    screen.blit(background,(0,0))
    for i in range(len(images)):
        screen.blit(images[i], img_rects[i])
    pygame.display.flip()
    pygame.time.delay(30)

pygame.display.fli()
pygame.display.update()
mainClock.tick(60)

我只想旋转这个图像键盘,但不知道我该怎么做,有人可以帮助我,完整代码在这里 https://pastebin.com/Rb7jh2GH

添加一个 angle 变量。使用 pygame.key.get_pressed() to get the sate of the keys (see How can I make a sprite move when key is held down)。根据按下的键更改 angle

keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
    angle -= 1
if keys[pygame.K_LEFT]:
    angle += 1

使用pygame.transform.rotate to rotate an image. See How do I rotate an image around its center using PyGame?:

for i in range(len(images)):
    rotated_image = pygame.transform.rotate(images[i], angle)
    rotated_rect = rotated_image.get_rect(center = img_rects[i].center)
    screen.blit(rotated_image, rotated_rect)

如果您只想旋转 current_image,您需要一个角度列表

current_image = -1
img_rects = [images[i].get_rect(topleft=(20 + 40 * i, 20)) for i in range(len(images))]
img_angles = [0 for _ in range(len(images))]

LeftButton = 0
while 1:
    for e in pygame.event.get():
        if e.type == QUIT:
            pygame.quit()
            exit(0)

        if e.type == pygame.MOUSEBUTTONDOWN:
            mouse_rect = pygame.Rect(e.pos, (1, 1))
            current_image = mouse_rect.collidelist(img_rects)

        if e.type == MOUSEMOTION:
            if e.buttons[LeftButton]:
                rel = e.rel
                if 0 <= current_image < len(images):
                    img_rects[current_image].x += rel[0]
                    img_rects[current_image].y += rel[1]

    keys = pygame.key.get_pressed()
    if 0 <= current_image < len(img_angles):
        if keys[pygame.K_RIGHT]:
            img_angles[current_image] -= 1
        if keys[pygame.K_LEFT]:
            img_angles[current_image] += 1

    screen.blit(background,(0,0))
    
    for i in range(len(images)):
        rotated_image = pygame.transform.rotate(images[i], img_angles[i])
        rotated_rect = rotated_image.get_rect(center = img_rects[i].center)
        screen.blit(rotated_image, rotated_rect)

    pygame.display.flip()