gpt4 book ai didi

python - 使用 pygame 在 Python 中通过按键加载图像

转载 作者:行者123 更新时间:2023-12-01 02:23:59 26 4
gpt4 key购买 nike

我正在尝试制作一个简单的图像库,它使用 python 中的 pygame 通过按键加载图像,这就是我所取得的进展

import pygame

pygame.init()
width=1366;
height=768
screen = pygame.display.set_mode((width, height ), pygame.NOFRAME)
pygame.display.set_caption('Katso')
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

*keys = pygame.event.get()
for event in keys:
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
screen.blit(mickey,(x,y)); pygame.display.update()
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
screen.blit(penguin,(x,y)); pygame.display.update()*

running = True
while (running): # loop listening for end of game
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#loop over, quit pygame

pygame.quit()

我希望按箭头键加载某些图像

屏幕打开但未加载图像

最佳答案

程序从不等待按键,因此您必须在 while 循环内检查按键。

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()

关于python - 使用 pygame 在 Python 中通过按键加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47601460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com