通过pygame在Python中的击键加载图像



im试图制作一个简单的图像库,该图库使用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()

最新更新