본문 바로가기
[ CODING STUDY ]/》파이썬 코드실습

파이썬 - [pygame, 마우스입력, 마우스좌표, event]

by MRG 2021. 5. 15.
728x90
반응형

▣ 키보드를 입력받는 것과 동일하게 for event in pygame.event.get()으로 입력받는 값을 for으로 event에 하나하나 넣어줍니다. 

 

▣ for으로 뿐만 아니라 바로바로 단일화해서 이벤트 값을 반환해주는 함수도 있습니다. poll() 함수입니다. 

 

https://www.pygame.org/docs/ref/event.html#pygame.event.poll

 

pygame.event — pygame v2.0.1.dev1 documentation

Pygame handles all its event messaging through an event queue. The routines in this module help you manage that event queue. The input queue is heavily dependent on the pygame.displaypygame module to control the display window and screen module. If the dis

www.pygame.org

 

▣ 메뉴얼을 통해 기능에 차이를 살펴보세요.

 

▣ 그리고 if event.type으로 어떤 이벤트가 발생했는지에 대한 조건문을 만들어주세요.

pygame.MOUSEBUTTONDOWN 마우스 버튼을 눌렀을 경우 

어떤 버튼을 눌렀는지 print(event.button)을 확인해주세요. 

그리고 pygame.mouse.get_pos() 함수를 이용하면 현재 마우스 위치도 튜플 형식으로 확인할 수 있습니다. 

결과를 확인해보면 1 왼쪽 마우스 클릭, 2 마우스 휠 클릭, 3 오른쪽 마우스 클릭이라는걸 확인할 수 있습니다.

 

▣ pygame.MOUSEBUTTONUP 을 확인해보면 마우스 버튼을 누르고 떼면 출력되는 걸 확인할 수 있습니다. 

 

▣ 그럼 이렇게 and 논리연산자를 사용하여 event.button == 1 왼쪽 마우스를 클릭하면 

event.button == 2 마우스 휠을 클릭하면 출력해라라는 코드를 완성할 수 있습니다. 

 

https://www.pygame.org/docs/ref/event.html

 

pygame.event — pygame v2.0.1.dev1 documentation

Pygame handles all its event messaging through an event queue. The routines in this module help you manage that event queue. The input queue is heavily dependent on the pygame.displaypygame module to control the display window and screen module. If the dis

www.pygame.org

▣ 이외에 마우스에 관한 기능들이 많이 있습니다. pygame 매뉴얼을 확인해주세요.

 

▣ 이제 마우스 이벤트 효과를 이용해서 이미지를 클릭하면 출력되게 만들어보세요.

728x90
반응형

댓글