import pygame
#pygmme초기화
pygame.init()
#창 크기 설정
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
#색깔 설정
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
#창 설정
display_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("텍스트 표시")
clock = pygame.time.Clock()
#이미지 붙이고 설정
elephant_image = pygame.image.load("elephant.png")
elephant_rect = elephant_image.get_rect()
elephant_rect.centerx = (WINDOW_WIDTH//2)
elephant_rect.bottom = (WINDOW_HEIGHT//2)
#텍스트 객체 만들기
system_font = pygame.font.SysFont('verdanai', 30)
game_font = system_font.render("boanproject", True, GREEN, BLACK)
game_font_rect = game_font.get_rect()
game_font_rect.center = (WINDOW_WIDTH//2, 100)
#게임이 동작하는 동안 이벤트
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#마우스 버튼이 클릭 했을 이벤트 적용
#pos[0]은 마우스의 x좌표, pos[1]은 마우스의 y좌표를 설정
#가져온 마우스 위치 정보를 이미지의 x, y좌표에 적용
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x = event.pos[0]
mouse_y = event.pos[1]
elephant_rect.centerx = mouse_x
elephant_rect.centery = mouse_y
#마우스가 움직을 때 이벤트 적용
#마우스를 클릭한 상태에서 움직이는 것을 동시에 확인
#가져온 마우스 위치 정보를 이미지의 x, y좌표에 적용
if event.type == pygame.MOUSEMOTION and event.buttons[0] == 1:
mouse_x = event.pos[0]
mouse_y = event.pos[1]
elephant_rect.centerx = mouse_x
elephant_rect.centery = mouse_y
display_surface.fill((0,0,0))
display_surface.blit(elephant_image, elephant_rect)
#텍스트 표시
display_surface.blit(game_font, game_font_rect)
#디스플레이 업데이트
pygame.display.update()
#분당 프레임 설정
clock.tick(60)
pygame.quit()
https://youtu.be/aTH7kel_vZA