Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
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
Tags
more
Archives
Today
Total
관리 메뉴

개발자입니다

[유튜브 소놀코딩] 2강.마우스로 조종하기 본문

파이게임/파이썬 게임 만들기 강의(유튜브 소놀코딩)

[유튜브 소놀코딩] 2강.마우스로 조종하기

끈기JK 2023. 8. 30. 18:49

pygame05.py

마우스 움직임 및 클릭 감지하기
import pygame

pygame.init()

background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")

play = True
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        if event.type == pygame.MOUSEMOTION:
            print('MOUSEMOTION')
        if event.type == pygame.MOUSEBUTTONDOWN:
            print('MOUSEBUTTONDOWN')
        if event.type == pygame.MOUSEBUTTONUP:
            print('MOUSEBUTTONUP')

pygame.quit()

 

 

 

pygame06.py

마우스 클릭 어떤 행위인지 감지하기
  • pygame.mouse.get_pos() 로 마우스 포인터 위치 값을 튜플로 반환 받는다.
  • event.button 에서 마우스 행위별로 숫자가 나타난다.
import pygame

pygame.init()

background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")

play = True
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())
            pass
        if event.type == pygame.MOUSEBUTTONDOWN:
            # print(pygame.mouse.get_pos())
            # print(event.button)
            if event.button == 1:
                print('왼쪽 클릭')
            elif event.button == 2:
                print('휠 클릭')
            elif event.button == 3:
                print('오른쪽 클릭')
            elif event.button == 4:
                print('휠 올리기')
            elif event.button == 5:
                print('휠 내리기')
        if event.type == pygame.MOUSEBUTTONUP:
            # print(pygame.mouse.get_pos())
            pass

pygame.quit()

 

 

 

pygame07.py

마우스를 따라 원 이어서 그리기 및 클릭시 지우기
  • 마우스 이동시 위치 값을 저장하고 해당 위치값에 원을 연속해서 그린다.
  • 왼쪽 클릭시 배경을 검게 만든다.
import pygame

pygame.init()

background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")

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()
            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()

 

 

 

pygame08.py

마우스를 따라다니는 원
  • 마우스 값을 저장하고 배경을 칠하고 원을 그린다.
import pygame

pygame.init()

background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")

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()

    background.fill((0,0,0))
    pygame.draw.circle(background, (255,0,255), (x_pos,y_pos), 10)
    pygame.display.update()

pygame.quit()