2017-11-30 12 views
0

1 초 동안 timer_event 호출을 실행 한 후 최종 pong 위치 결과를 변수에 저장하려고합니다. 나는 그렇게하는 방법을 모르겠다. 내 기능은 다음과 같습니다.타이머 이벤트 후 반환 값을 변수에 저장하는 방법은 무엇입니까?

def get_final_position(self): 

    return (pong.rect.centerx, pong.rect.centery) 

def hard_AI_mode(self, pong, paddle): 

    initial_pong_position = (pong.rect.centerx, pong.rect.centery) 
    pygame.time.set_timer(self.get_final_position(), 1000) 

    final_pong_position = ??? 
+0

위치를 저장하는 '퐁'개체를 만들려고 생각 했습니까? 그것은 다른 기능들간에 공유 될 수 있습니다. 그렇지 않으면 '전역 변수'를 사용해 볼 수도 있지만 일반적으로 권장하지 않습니다. (pong 객체는 액션에 대응하는 getter 및 setter 함수를가집니다) – boethius

+0

질문의 질을 향상 시키십시오. 그들은 비슷한 문제를 가진 응답자와 미래의 독자들에게 이해하기 쉬워야합니다. [최소한의 완전하고 검증 가능한 예] (https://stackoverflow.com/help/mcve)를 제공하고 목표와 이슈를 정확하게 설명하십시오. – skrx

+1

또한 ['pygame.time.set_timer'] (https://stackoverflow.com/a/15056742/6220679)를 어떻게 사용해야하는지 확인하십시오. 'USEREVENT'를 전달해야하며,이 'USEREVENT'가 큐에 추가 되었다면 이벤트 루프를 체크인 한 다음 이벤트 루프 내부에서 함수를 호출하십시오. – skrx

답변

0

이 경우에는 이미 폰용 스프라이트를 만들어야합니다. 웹상에 여러 가지 가이드가 있습니다. "pong"스프라이트 클래스 내에서 pos 변수와 vel 변수를 배치하십시오.

퐁 스프라이트 키가 최대 일 때 우리가 (x, y) # x is left/right, y is up/down 에 VEL을 변경, 기능을 업데이트 self.vel하는 self.pos을 추가하고 키를 누르면 설정 self.rect.center이 를 POS에
Pos = (x, y) # x and y being position pong needs to be 
Vel = (0, 0) # we will change this later 

, 우리는 다시 (0, 0) 내부 get_final_position이 centerx = pong.pos[1]centery = pong.pos[2] 다음 나는 이것이 당신이 찾고있는 응답 희망 centerx 및 centery

을 반환로 설정 VEL을 변경합니다.

좋아 편집, 쉽게 이잖아, 단지 DT라는 varible를 생성하고 게임 루프를 시작하기 전에 그것을 정의 clock.tick(FPS_You_are_using)/1000로 설정합니다. after, get_final_position에서 timer라는 변수를 설정 한 다음 타이머에서 dt를 뺀 루프를 만들고 타이머 = 0이면 루프를 종료 한 다음 나머지를 수행하십시오.

+0

[이전 질문] (https://stackoverflow.com/questions/47568136/how-to-get-the-initial-and-final-position-of-ball-after-one-second)을 참조하십시오. 주요 문제는 1 초 후에 가치를 얻는 것입니다. – furas

+0

@furas 좋아, 나는 또 다른 것을 추가했다. – SnivyDroid

1

skrx에서 set_timer을 잘못된 방법으로 사용한다고 말했습니다.

mainloop에서 잡아야하는 이벤트를 보내고 함수를 실행해야합니다.

import pygame 

# --- constants --- 

AFTER_SECOND = pygame.USEREVENT + 1 

# --- functions --- 

def get_final_position(): 
    print('one second later') 

    # stop event AFTER_SECOND 
    pygame.time.set_timer(AFTER_SECOND, 0) 

    return pong_rect.center 

# --- main --- 

pygame.init() 
screen = pygame.display.set_mode((400, 300)) 

# - objects - 

pong_rect = pygame.Rect(0, 0, 10, 10) 

# - start - 

print('start game') 

initial_pong_position = pong_rect.center 
print('initial:', initial_pong_position) 

# repeat sending event AFTER_SECOND every 1000ms 
pygame.time.set_timer(AFTER_SECOND, 1000) 

# --- mainloop --- 

clock = pygame.time.Clock() 
is_running = True 

while is_running: 

    for event in pygame.event.get(): 

     if event.type == pygame.QUIT: 
      is_running = False 
     elif event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_ESCAPE: 
       is_running = False 

     elif event.type == AFTER_SECOND: 
      final_pong_position = get_final_position() 
      print('initial:', initial_pong_position) 
      print(' final:', final_pong_position) 

    screen.fill((0,0,0)) 
    pygame.display.update() 
    clock.tick(25) 

pygame.quit()