2017-03-25 2 views
0

나는 pythonprogramming.net에서 파이 게임을 사용하는 법을 배웠다. 나는 창에서 이미지를 이동에 현재 오전 :파이 게임 : 이미지가 움직이지 않는다

https://pythonprogramming.net/pygame-tutorial-moving-images-key-input/?completed=/displaying-images-pygame/

여기 사이트가 당신이 그것을 체크 아웃 할 때 다운 된 경우에, 나는 실행하고 코드입니다. 웬일인지, 나는 차 이미지가 움직이지 않는 이유를 모른다. 나는 비디오를 볼 때 모든 것을 타이핑했고, 그것이 작동하지 않는 것을 알아 차렸을 때 여전히 복사하고 붙여 넣어 예측 된 성능을 얻을 수 없었다. 디버깅 할 이벤트를 인쇄했습니다 (추가 된 유일한 줄은 사이트에서 복사 한 코드가 아닙니다).

import pygame 

pygame.init() 

display_width = 800 
display_height = 600 

gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption('A bit Racey') 

black = (0,0,0) 
white = (255,255,255) 

clock = pygame.time.Clock() 
crashed = False 
carImg = pygame.image.load('racecar.png') 

def car(x,y): 
    gameDisplay.blit(carImg, (x,y)) 

x = (display_width * 0.45) 
y = (display_height * 0.8) 
x_change = 0 
car_speed = 0 

while not crashed: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      crashed = True 

     ############################ 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_LEFT: 
       x_change = -5 
      elif event.key == pygame.K_RIGHT: 
       x_change = 5 
     if event.type == pygame.KEYUP: 
      if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: 
       x_change = 0 
     ###################### 
    ## 
    x += x_change 
    ##   
    gameDisplay.fill(white) 
    car(x,y) 

    print(event) # added for debugging 

    pygame.display.update() 
    clock.tick(60) 

pygame.quit() 
quit() 

나는 여전히 움직임을 스파이더를 사용하지 그러나 저에 동결 팝업 창을했고 내 Mac.I이 PyCharm 및 터미널로 전환에 스파이더을 종료했고 모두 동결이 없다는 것을 발견하지만, . 터미널에

출력물 넣어이었다 :

libpng warning: iCCP: known incorrect sRGB profile 
<Event(4-MouseMotion {'pos': (364, 569), 'rel': (364, 569), 'buttons': (0, 0, 0)})> 
<Event(5-MouseButtonDown {'pos': (364, 569), 'button': 1})> 
<Event(6-MouseButtonUp {'pos': (364, 569), 'button': 1})> 
<Event(4-MouseMotion {'pos': (491, 27), 'rel': (127, -542), 'buttons': (0, 0, 0)})> 
^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})> 
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})> 
^[[D^[[D^[[D<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})> 
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})> 
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})> 
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})> 
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})> 
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})> 
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})> 
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})> 
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})> 
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})> 
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})> 
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})> 
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})> 
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})> 
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})> 
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})> 
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})> 
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})> 
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})> 
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})> 
^[[D^[[D^[[D^[[C^[[C<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})> 
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})> 
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})> 
<Event(12-Quit {})> 

내가 코드의 첫 번째 줄 How do I disable the libpng warning? (python, pygame)에서 경고이며, 이러한 방식의 코드에 영향을 미치지 않습니다 이해합니다.

+0

코드가 나를 위해 올바르게 작동합니다. 'MouseButtonDown/Up' 이벤트 만 보여주기 때문에 마우스 버튼으로 자동차를 움직이려고할까요? 화살표 키로 이동해야합니다. – skrx

답변

0

죄송합니다. 이제 설명서를 읽으면서 몇 가지 사항이 변경되었습니다. 그것은 완전히 바뀌 었습니다. 나는 완전히 사과합니다. 내가 지적하고 싶은 것은 게임이 시작되기 전에 배경을 채우는 것이 좋습니다. 나는 이것을 더 자세히 살펴보고 직접 시험해 볼 것입니다.

편집 : 방금 테스트 해본 결과 나와 완전히 작동합니다. 그것은 당신의 키보드 레이아웃 일지 모르지만, 내 생각은 당신이 창을 선택 했습니까?

+0

구체적으로 몇 가지 예를 보여줄 수 있습니까? – heretoinfinity

+0

복사하여 붙여 넣으면 코드에 문제가없는 것 같습니다. – user140052