我正在尝试列出可以使用向右/向左箭头键浏览的图像库的列表。选择画廊后,我将使用向上和向下箭头来翻阅所选画廊的图像。
在这里,我停止使用数字键选择要翻阅其图像的画廊。 setting up and choosing which gallery to flip through its images
import pygame
WIDTH = 1366
HEIGHT = 768
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
clock = pygame.time.Clock() # Needed to limit the frame rate.
pygame.display.set_caption('CIS')
gallery1 = [
pygame.image.load('/home/hosni/Documents/CIS/assets/chartA1.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/chartA2.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/chartA3.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/chartA4.png').convert(),
]
gallery2 = [
pygame.image.load('/home/hosni/Documents/CIS/assets/ChartH1.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/ChartH2.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/ChartH3.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/ChartH4.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/ChartH5.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/ChartH6.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/ChartH7.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/ChartH8.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/ChartH9.png').convert(),
]
gallery3 = [
pygame.image.load('/home/hosni/Documents/CIS/assets/tumbling.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/chartV1.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/chartV2.png').convert(),
pygame.image.load('/home/hosni/Documents/CIS/assets/chartV3.png').convert(),
]
SG = [gallery1, gallery2, gallery3]
x = 0 # x coordnate of image
y = 0 # y coordinate of image
running = True
gallery_index = 0
gallery = SG[gallery_index]
image_index = 0
image = gallery[image_index]
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_1:
gallery = SG[0]
elif event.key == pygame.K_2:
gallery = SG[1]
elif event.key == pygame.K_3:
gallery = SG[2]
elif event.key == pygame.K_UP:
image_index -= 1 # Decrement the index.
elif event.key == pygame.K_DOWN:
image_index += 1 # Increment the index.
elif event.key == pygame.K_RIGHT:
gallery_index -= 1 # Decrement the index.
elif event.key == pygame.K_LEFT:
gallery_index += 1 # Increment the index.
elif event.key == pygame.K_ESCAPE:
running = False #to end the while loop
gallery_index %= len(SG)
# Keep the index in the valid range.
image_index %= len(gallery)
# Switch the image.
image = gallery[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()
答案 0 :(得分:2)
在主循环中,更改gallery_index
之后,您再也不会做任何事情。
我想你想做类似的事情
...
gallery_index %= len(SG)
# switch the gallery
gallery = SG[gallery_index]
# Keep the index in the valid range.
image_index %= len(gallery)
# Switch the image.
image = gallery[image_index]
...