개발자입니다
[유튜브 소놀코딩] 2강.마우스로 조종하기 본문
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()
'파이게임 > 파이썬 게임 만들기 강의(유튜브 소놀코딩)' 카테고리의 다른 글
[유튜브 소놀코딩] 8강.캐릭터 충돌하기 / 9강.점수 만들기 / 10강.게임 종료하기 (0) | 2023.08.31 |
---|---|
[유튜브 소놀코딩] 6강.벽에 닿았을 때 튕기기 / 7강.캐릭터 추가하기 (0) | 2023.08.31 |
[유튜브 소놀코딩] 4강.이미지 사용하기 / 5강.키보드+마우스+이미지 적용하기 (0) | 2023.08.31 |
[유튜브 소놀코딩] 3강.선, 도형 그리기 (0) | 2023.08.30 |
[유튜브 소놀코딩] 1강.키보드로 조종하기 (0) | 2023.08.29 |