Python 3.x 在图像和文件之间向前和向后移动
尝试制作一个简单的图像库,在这里我可以浏览一组图像 我通过一次按键就可以加载每个图像。 如何让python像在图像库中一样,用箭头翻转一组图像Python 3.x 在图像和文件之间向前和向后移动,python-3.x,image,python-2.7,pygame,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.NO
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()
将图像放入一个列表中,然后只增加一个索引变量,使用它获取列表中的下一个图像,并将其分配给一个变量(
image
),然后在每个帧中对该变量进行blit
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()
非常感谢你,我会贪婪地问我是否可以制作两个列表,每个列表由一个数字键调用?你到底想做什么?您可以创建任意多个列表(仅受计算机内存的限制)。我计划创建5个列表,每个列表都应该通过1到5号按键来调用您想要同时显示五个不同的图像(每个列表一个)还是只显示一个图像并在不同的列表之间切换?也许最好问一个新问题,并添加一些代码来向我们展示您的尝试,这样我们就可以更好地理解您想要实现的目标。一次只需一个图像,并在不同的列表之间切换。多谢各位