how rotate the images with keyboards? [duplicate]

一笑奈何 提交于 2021-02-05 09:12:54

问题


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)

i just wanna rotate this images keyboards but a don`t how i can do it somebody could help me and the full code is here https://pastebin.com/Rb7jh2GH


回答1:


Add an angle variable. Use pygame.key.get_pressed() to get the sate of the keys (see How can I make a sprite move when key is held down). Change the angle dependent on the key pressed:

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

Use 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)

If you just want to rotate the current_image, you need a list of angles

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()


来源:https://stackoverflow.com/questions/64862405/how-rotate-the-images-with-keyboards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!