python - 设置并选择要浏览其图像的画廊

标签 python image pygame imaging

我试图使用键盘输入 1 和 2 来选择图库 1 或 2,这是要在屏幕上加载的图像列表 使用此代码在一个画廊中运行良好

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('assets/download.png').convert(),
    pygame.image.load('assets/mickey.jpg').convert(),
    pygame.image.load('assets/cat.jpg').convert(),
    pygame.image.load('assets/flavours.jpg').convert(),
    pygame.image.load('assets/hallway.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()

但是尝试添加更多列表并被选择是行不通的。我尝试在不同的循环和同一循环下选择图库,但没有任何效果。

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.
gallery1 = [
    pygame.image.load('assets/download.png').convert(),
    pygame.image.load('assets/mickey.jpg').convert(),
    pygame.image.load('assets/cat.jpg').convert(),
    pygame.image.load('assets/flavours.jpg').convert(),
    pygame.image.load('assets/hallway.jpg').convert(),
    ]

gallery2 = [
    pygame.image.load('assets/adv.jpg').convert(),
    pygame.image.load('assets/star.jpg').convert(),
    pygame.image.load('assets/images.jpeg').convert(),
    pygame.image.load('assets/tile.png').convert(),
    ]

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

running = True
gallery = gallery1 #set the default gallery

image_index = 0
image = gallery[image_index]  # The current image.

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 = gallery1
            elif event.key == pygame.K_2:
                gallery = gallery2            
            elif 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(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()

最佳答案

在我发布问题后,我终于让它工作了 我很高兴看到任何改进该程序工作方式的提示或想法。

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.
gallery1 = [
    pygame.image.load('assets/download.png').convert(),
    pygame.image.load('assets/mickey.jpg').convert(),
    pygame.image.load('assets/cat.jpg').convert(),
    pygame.image.load('assets/flavours.jpg').convert(),
    pygame.image.load('assets/hallway.jpg').convert(),
    ]

gallery2 = [
    pygame.image.load('assets/adv.jpg').convert(),
    pygame.image.load('assets/star.jpg').convert(),
    pygame.image.load('assets/images.jpeg').convert(),
    pygame.image.load('assets/tile.png').convert(),
    ]

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

running = True
gallery = gallery1 #set the default gallery

image_index = 0
image = gallery[image_index]  # The current image.

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 = gallery1
            elif event.key == pygame.K_2:
                gallery = gallery2                      
            elif 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(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()

关于python - 设置并选择要浏览其图像的画廊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49590866/

相关文章:

android - 我可以让 Android WebView 支持其他图像格式(例如 TIFF)吗?

python - 我的 py2app 应用程序无法打开。有什么问题?

python - 全局变量未绑定(bind)局部错误

Python livewires 调整屏幕大小

Python:获取属性值

python - pycuda 失败; Theano 与 Anaconda

java - KeyListener 和 runnable 不起作用

python - 有效地计算转换矩阵 (m*m) * (n*n) 的逐元素乘积以给出 (mn*mn) 矩阵

python - 如何在 mac 上安装 numpy

python - 使用 openpyxl 将 python 字典列表写入 xlsx