Reputation: 77
I am trying to make a list of image galleries that i can flip through using the right/left arrow keys. once i choose a gallery i would use the up and down arrows to flip through the images of the selected gallery.
This is where i stopped before using number keys to choose which gallery i want to flip through its images. 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()
Upvotes: 0
Views: 43
Reputation: 101052
In your main loop, you never do anything with gallery_index
after changing it.
I guess you want to do something like
...
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]
...
Upvotes: 2