列出画廊名单

2024-05-29 04:21:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个图像库的列表,我可以使用右/左箭头键来浏览。一旦我选择了一个画廊,我会使用向上和向下箭头来翻转所选画廊的图像

这是我停止使用数字键来选择哪个画廊,我想通过它的图像翻转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()

Tags: theimageeventconverthomeindexpngload
1条回答
网友
1楼 · 发布于 2024-05-29 04:21:15

在主循环中,在更改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]
...

相关问题 更多 >

    热门问题