전체 코드

코드 분석

  1. 라이브러리 가져오기

    import pygame, sys
    from pygame.locals import *
    
  2. Pygame 초기화

    pygame.init()
    
  3. 화면 설정

    WIDTH = 700
    HEIGHT = 600
    DISPLAYSF = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Title")
    
  4. 메인 루프 정의

    def main():
        col_arr = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
        col = 0
        run = True
    
  5. 메인 루프

    while run:
        DISPLAYSF.fill((255, 255, 255))
    
    
  6. 이벤트 처리

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEMOTION:
            col += 1
    
  7. 사각형 그리기 및 색상 변경

    rect = pygame.Rect(300, 250, 100, 100)
    pygame.draw.rect(DISPLAYSF, col_arr[col % 3], rect)
    
  8. 화면 업데이트

    pygame.display.update()
    
  9. 프로그램 종료

    if __name__ == "__main__":
        main()
        pygame.quit()
        sys.exit()
    

주요 동작 요약