2014-05-21 2 views
0

파이 게임 새내기 여기 Flappy Bird 클론을 만들려고합니다. 점프 키를 누르고 있으면 새가 점프하지 않도록 컨트롤을 설정하고 싶습니다. 플레이어는 원래 게임처럼 새가 떠 다니기 위해 도청 작업을 계속해야합니다. 키 반복을 끄려면 pygame.key.set_repeat()을 사용했지만, 작동하지 않는 것 같습니다. 이 동일한 주제에 대한 다른 게시물을 검토하면서 내 이벤트 루프에서 문제가 될 수 있다고 생각하기 시작했습니다.pygame 이벤트 루프에 문제가 있습니다.

도움 주셔서 감사합니다.

내 코드 :

import pygame 

class Bird(pygame.sprite.Sprite): 
    def __init__(self): 
     #load pic of bird 
     self.image = pygame.image.load('ball.png') 
     #sets bird pic as a rectangle object and moves position to centre 
     self.rect = pygame.rect.Rect((320, 240), self.image.get_size()) 

     #default value for gravity 
     self.dy = 0 #how much to add to current player position 

    def update(self, dt, game): 
     pygame.key.set_repeat() 
     key = pygame.key.get_pressed() 
     if key[pygame.K_UP]: 
      print "jump!!!" 
      self.dy = -400 

     #apply gravity 
     self.dy = min(400, self.dy + 40) 
     self.rect.y += self.dy * dt 

     #collision detection 
     if(self.rect.top <= 0): #top 
      self.rect.y = 0 
      self.dy = -4 
     elif(self.rect.bottom >= 480): #ground 
      self.rect.y = (480-self.rect.width) 

     #blit image to screen 
     screen.blit(self.image, (320, self.rect.y)) 
     pygame.display.flip() 

     print self.rect.center 
     print self.dy 

class Ground(pygame.sprite.Sprite): 
    def __init__(self): 
     self.image = pygame.image.load('ground.png') 
     self.rect = pygame.rect.Rect((0, 480-self.image.get_width()), self.image.get_size()) 



class Game(object): 
    def main(self, screen): 
     clock = pygame.time.Clock() 

     #create background and player object 
     background = pygame.image.load('background.png') 
     #instantiate bird object 
     self.bird = Bird() 
     self.ground = Ground() 

     while 1: 

      dt = clock.tick(30) 

      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        return 
       if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: 
        return 

      screen.blit(background, (0, 0)) 
      pygame.display.flip() 
      self.bird.update(dt/1000., self) #for some reason, update must go last 


if __name__ == '__main__': 
    pygame.init() 
    screen = pygame.display.set_mode((640, 480)) 
    Game().main(screen) 
    pygame.quit() 

답변

2

pygame.key.set_repeat() 키 반복은 기본적으로 비활성화되어 있기 때문에, 여기에 아무것도 변경되지 않습니다.

오류가 다소 간단합니다. K_UP이 인 경우 업데이트() 메소드가 현재으로 눌려져 있는지 확인합니다.하지만 푸시 버튼이 이벤트로 가로 채기 때문에 이벤트 만 확인해야합니다.

간단히 말해서 : 이벤트는 키를 눌렀는지 알려주고 "get_pressed()"는 키가 눌려 졌는지 알려줍니다. 바로 지금입니다.

"jump()"와 같은 메소드를 작성하고 K_UP 키를 누른 상태에서 이벤트를 수신하면 update() 메소드에서 키 상태를 확인하지 않고 실행해야합니다. 그리고 "update()"메소드에서 점프 코드를 삭제하는 것을 잊지 마십시오!

class Bird(pygame.sprite.Sprite): 

    def jump(self): 
     print "jump!!!" 
     self.dy = -400 

(...)

for event in pygame.event.get(): 
    if event.type == pygame.QUIT: 
     return 
    if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: 
     return 
    if event.type == pygame.KEYDOWN and event.key == pygame.K_UP: 
     self.bird.jump() 
+0

이 다른 "이벤트"를 확인하는 무슨에 아직 완벽하게 약간 혼란스러워했다. 도와 주셔서 감사합니다! – whodareswins

관련 문제