在图像文件之间前后移动

时间:2018-03-28 10:21:20

标签: python-3.x image python-2.7 pygame

尝试制作一个简单的图库,我可以翻阅一组图像 我通过一次击键就足以加载每个图像。 如何告诉python在图像库中翻阅一组带箭头的图像

import pygame

# --- constants --- (UPPER_CASE)

WIDTH = 1366
HEIGHT = 768

# --- main ---

# - init -

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT), 
pygame.NOFRAME)
pygame.display.set_caption('Katso')

# - objects -   

penguin = pygame.image.load("download.png").convert()
mickey = pygame.image.load("mickey.jpg").convert()

x = 0 # x coordnate of image
y = 0 # y coordinate of image

# - mainloop - 

running = True

while running: # loop listening for end of game
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                #screen.fill( (0, 0, 0) )
                screen.blit(mickey,(x,y))
                pygame.display.update()
            elif event.key == pygame.K_RIGHT:
                #screen.fill( (0, 0, 0) )
                screen.blit(penguin,(x,y))
                pygame.display.update()

# - end -

pygame.quit()

1 个答案:

答案 0 :(得分:1)

将图像放入列表中,然后只增加一个索引变量,用它来获取列表中的下一个图像,并将其分配给每个帧blit的变量(image)。

import pygame


WIDTH = 1366
HEIGHT = 768

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.NOFRAME)
clock = pygame.time.Clock()  # Needed to limit the frame rate.
pygame.display.set_caption('Katso')
# Put the images into a list.
images = [
    pygame.image.load('download.png').convert(),
    pygame.image.load('mickey.jpg').convert(),
    ]
image_index = 0
image = images[image_index]  # The current image.

x = 0  # x coordnate of image
y = 0  # y coordinate of image

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                image_index -= 1  # Decrement the index.
            elif event.key == pygame.K_RIGHT:
                image_index += 1  # Increment the index.

            # Keep the index in the valid range.
            image_index %= len(images)
            # Switch the image.
            image = images[image_index]

    screen.fill((30, 30, 30))
    # Blit the current image.
    screen.blit(image, (x, y))

    pygame.display.update()
    clock.tick(30)  # Limit the frame rate to 30 fps.

pygame.quit()
相关问题