강의노트 마우스 이벤트
강의노트
• 조회수 33
• 댓글 0
• 수정 4일 전
- 이벤트
마우스 이벤트
마우스의 버튼에 따른 명령이 동작하게하기 위한 루틴은 다음과 같다.
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == pygame.키상수:
명령 모음
마우스 위치를 얻는 명령어
x,y = pygame.mouse.get_pos()
마우스 이벤트 키 상수
상수 | 설명 | 상수 | 설명 | |
---|---|---|---|---|
1 | left click | 2 | middle click | |
3 | right click | 4 | scroll up | |
5 | scroll down |
pygame.MOUSEMOTION
마우스가 움직이면 발생하는 참을 반환
import pygame
pygame.init( )
#색설정
WHITE= (255, 255, 255)
BLUE = ( 0, 0, 255)
RED = (255, 0, 0)
#화면설정
size = [1200, 300]
screen = pygame.display.set_mode(size)
font= pygame.font.SysFont('Verdana', 20)
pygame.display.set_caption('MOUSEMOTION 이벤트 처리하기')
#메인게임루프
running = True
motionFlag = False
clock = pygame.time.Clock( )
while running:
clock.tick(10)
#이벤트루프
for event in pygame.event.get( ):
if event.type == pygame.MOUSEMOTION: # 마우스를 움직이먼
motionFlag =True
eventMessage = str(event)
elif event.type == pygame.QUIT: # 〈닫기〉 버튼을 클릭하면
running = False
screen.fill(WHITE)
#마우스가움직였을때
if motionFlag == True:
textSurface = font.render('Moving!!' + eventMessage, True, RED, None)
textRect = textSurface.get_rect( )
textRect.topleft = (50, 50)
screen.blit(textSurface, textRect)
motionFlag = False
# 마우스가 움직이지 않을 때
elif motionFlag == False:
textSurface = font.render('Mouse is stopped!', True, BLUE, None)
textRect = textSurface.get_rect( )
textRect.topleft = (50, 50)
screen.blit(textSurface, textRect)
pygame.display.flip( )
pygame.quit( )
import pygame
pygame.init( )
#색설정
WHITE= (255, 255, 255)
BLUE = ( 0, 0, 255)
RED = (255, 0, 0)
#화면설정
size = [1200, 300]
screen = pygame.display.set_mode(size)
font= pygame.font.SysFont('Verdana', 20)
pygame.display.set_caption('MOUSEMOTION 이벤트 처리하기')
#메인게임루프
running = True
motionFlag = False
clock = pygame.time.Clock( )
# 텍스트 출력 함수
def dispText(dispMessage, txtColor='BLACK’, start_pos=(50, 50)):
textSurface =font.render(dispMessage. True, pygame.Color(txtColor), None)
textRect = textSurface.get_rect( )
textRect.topleft = start_pos
screen.blit(textSurface, textRect)
while running:
clock.tick(1 O)
#이벤트루프
for event in pygame.event.get( ):
if event.type == pygame.MOUSEMOTION: # 마우스를 움직이먼
motionFlag =True
eventMessage = str(event)
elif event.type == pygame.QUIT: # 〈닫기〉 버튼을 클릭하면
running = False
screen.fill(WHITE)
#마우스가움직였을때
if motionFlag == True:
dispText (’Moving!! ’ + eventMessage, ’RED')
motionFlag = False
# 마우스가 움직이지 않을 때
elif motionFlag == False:
dispText (’Moving is Stopped!! ', ’BLUE')
pygame.display.flip( )
pygame.quit( )
pygame.MOUSEBUTTONDOWN & pygame.MOUSEBUTTONUP
import pygame
pygame.init( )
#색설정
BLACK= ( 0, 0, 0)
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
RED = (255, 0, 0)
#화면설정
size = [1000, 300]
screen = pygame.display.set_mode(size)
font= pygame.font.SysFont(”Verdana”, 20)
pygame.display.set_caption(”MOUSEBUTTONDOWN/MOUSEBUTTONUP 이벤트 처리하기”)
#머|인게임루프
running = True
buttonFlag = None
clock = pygame.time.Clock( )
#텍스츠출력함수
def dispText(dispMessage, txtColor =’BLACK’, start_pos=(50, 50)):
textSurface = font.render(dispMessage, True, pygame.Color(txtColor), None)
textRect = textSurface.get_rect( )
textRect.topleft = start_pos
screen.blit(textSurface, textRect)
running = True
while running:
clock.tick(10)
#이벤트루프
for event in pygame.event.get( ):
if event.type == pygame.MOUSEBUTTONDOWN: # 버튼을 놀렀을 때
buttonFlag = True
eventMessage = str(event)
elif event.type == pygame.MOUSEBUTTONUP:
buttonFlag = False
eventMessage = str(event)
elif event.type == pygame.QUIT:
running = False
screen.fill(WHITE)
#버튼을누른경우
if buttonFlag ==True:
dispText('Mouse Button Down!!', 'RED')
dispText(' => ' + eventMessage, 'RED', (50, 70))
#버튼을해제한경우
elif buttonFlag == False:
dispText('Mouse Button UP !!', 'BLUE')
dispText(' => ' + eventMessage, 'BLUE', (50, 70))
# 버튼을 누르지 않은 경우
else:
dispText('Please press any button ∼.')
pygame.display.flip( )
pygame.quit( )
예제
# pg_init05.py
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500,400))
clock = pygame.time.Clock()
white = (255,255,255)
black = (0,0,0)
x = 0
y = 0
vel = 5
running = True
left_key, right_key, up_key, down_key = False, False, False, False
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print('left key clicked')
if event.key == pygame.K_RIGHT:
print('right key clicked')
if event.key == pygame.K_UP:
print('up key clicked')
if event.key == pygame.K_DOWN:
print('down key clicked')
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
print('left key released')
if event.key == pygame.K_RIGHT:
print('right key released')
if event.key == pygame.K_UP:
print('up key released')
if event.key == pygame.K_DOWN:
print('down key released')
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if event.button == 1:
s = 'left button'
elif event.button == 2:
s = 'middle button'
elif event.button == 3:
s = 'right button'
elif event.button == 4:
s = 'scroll up'
elif event.button == 5:
s = 'scroll down'
print(f'mouse {s} and position : x={pos[0]}, y={pos[1]}')
screen.fill(white)
#screen.blit(p_img, (x,y))
pygame.display.update()
pygame.quit()
이전 글
마지막 글입니다.
로그인 하면 댓글을 쓸 수 있습니다.