본문 바로가기
파이썬/파이게임(Pygame)

[파이게임.004] 마우스 입력처리

by 긱펀 2022. 6. 13.
반응형

 

 

🚩 마우스 움직임과 클릭 감지하기

[1] 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import pygame
 
pygame.init()
 
background = pygame.display.set_mode((480,360))
pygame.display.set_caption('mouse')
 
play = True
count = 0 #MOUSEMOTION count
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        if event.type == pygame.MOUSEMOTION: #마우스 모션(움직임) 확인
            count += 1
            print('MOUSEMOTION', count)
        if event.type == pygame.MOUSEBUTTONDOWN: #마우스 클릭다운 확인
            print('MOUSEBUTTONDOWN')
        if event.type == pygame.MOUSEBUTTONUP: #마우스 클릭업 확인
            print('MOUSEBUTTONUP')
 
pygame.quit()
cs

 

[2] 실행결과

마우스 움직임과 클릭 감지하기

 

 

 

🚩 마우스 포인터의 좌표값 알아내기

[1] 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import pygame
 
pygame.init()
 
background = pygame.display.set_mode((480,360))
pygame.display.set_caption('mouse')
 
play = True
count = 0 #MOUSEMOTION count
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        if event.type == pygame.MOUSEMOTION: # 마우스 움직임이 감지되면
            print(pygame.mouse.get_pos()) # 마우스 x,y좌표 출력하기
        if event.type == pygame.MOUSEBUTTONDOWN: # 마우스 클릭 누를때
            print('MOUSEBUTTONDOWN', pygame.mouse.get_pos())
        if event.type == pygame.MOUSEBUTTONUP: #마우스 클릭을 뗄때
            print('MOUSEBUTTONUP', pygame.mouse.get_pos())
 
pygame.quit()
cs

 

[2] 실행결과

마우스 포인터의 좌표값 알아내기

 

 

 

🚩 마우스 (왼쪽/오른쪽/가운데)클릭, 휠 업/휠 다운 감지하기

[1] 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import pygame
 
pygame.init()
 
background = pygame.display.set_mode((480,360))
pygame.display.set_caption('mouse')
 
play = True
count = 0 #MOUSEMOTION count
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        if event.type == pygame.MOUSEMOTION:
            pass
        if event.type == pygame.MOUSEBUTTONDOWN: # 마우스의 어떤 버튼을 눌렀을때
            #(1)마우스의 무슨 버튼인지 확인하기
            #print(event.button)
            #(2)마우스 버튼 이름 출력
            if event.button == 1:  # 마우스 왼쪽 클릭시
                print('left click')
            elif event.button == 2# 마우스 가운데 클릭시
                print('middle click')
            elif event.button == 3# 마우스 오른쪽 클릭시
                print('right click')
            elif event.button == 4# 마우스 스크롤 업
                print('scroll up')
            elif event.button == 5# 마우스 스크롤 다운
                print('scroll down')
 
        if event.type == pygame.MOUSEBUTTONUP:
            pass
 
pygame.quit()
 
cs

 

[2] 실행결과

마우스 (왼쪽/오른쪽/가운데)클릭, 휠 업/휠 다운 감지하기

 

 

 

🚩 마우스로 굵은 선 그리고 지우기

[1] 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#마우스로 선 그리기
import pygame
 
pygame.init()
 
background = pygame.display.set_mode((480,360))
pygame.display.set_caption('mouse')
 
x_pos = 0
y_pos = 0
 
play = True
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        if event.type == pygame.MOUSEMOTION: # 마우스의 움직임이 감지되면
            x_pos, y_pos = pygame.mouse.get_pos() # 마우스 x,y좌표값 저장
            pygame.draw.circle(background, (255,0,255), (x_pos,y_pos), 10#원그리기(보라색)
        if event.type == pygame.MOUSEBUTTONDOWN: # 마우스 클릭을 하면
            if event.button == 1# 왼쪽 클릭이라면
                background.fill((0,0,0)) #화면 검은색으로
 
    pygame.display.update()       
 
pygame.quit()
cs

 

[2] 실행결과

마우스로 굵은 선 그리고 지우기

 

 

 

🚩 마우스를 따라다니는 원

[1] 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#원이 마우스 따라 다니게 하기
import pygame
 
pygame.init()
 
background = pygame.display.set_mode((480,360))
pygame.display.set_caption('mouse')
 
x_pos = background.get_size()[0// 2 #240
y_pos = background.get_size()[1// 2 #180
 
play = True
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        if event.type == pygame.MOUSEMOTION: # 마우스 움직임이 감지되면
            x_pos, y_pos = pygame.mouse.get_pos() # 마우스 x,y좌표값 저장
      
    # 화면을 먼저 검은색으로 칠한 후, 마우스 좌표에 원을 그리면
    # 마치 원이 마우스를 따라오는 것처럼 보이게 된다.
    background.fill((0,0,0)) # 화면 검은색으로
    pygame.draw.circle(background, (255,0,255), (x_pos,y_pos), 10#원그리기
    pygame.display.update()       
 
pygame.quit()
cs

 

 

[2] 실행결과

마우스를 따라다니는 원

 

 

 

 

 

728x90
반응형

댓글