2017-12-01 2 views
1

파이썬에서 파이 게임을 사용하여 키 입력 이미지를로드하는 간단한 이미지 갤러리를 만들려고 노력 임과이 전이미지로드

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 루프 내에서 키를 확인해야하므로

+0

'for event' 루프 - 내부에서'실행 중 '하나만 사용하십시오. 첫 번째 '이벤트'는 키 누르기를 기다리지 않습니다. 그것은 키를 체크하지 않는'while running'으로 바로 갈 것입니다. – furas

답변

1

프로그램의 키를 기다릴 결코로드되지됩니다.

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

고마워요, 그 동안 루프를 고정 시켰습니다. –