전체 코드

코드 분석

  1. 라이브러리 가져오기 및 초기화

    python
    Copy code
    import pygame, sys
    from pygame.locals import *
    pygame.init()
    
    
  2. 기본 설정

    WIDTH = 700
    HEIGHT = 600
    DISPLAYSF = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Title")
    clock = pygame.time.Clock()
    
    
  3. 변수 초기화

    col_arr = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
    col = 0
    run = True
    ball_pos = [0, 500]
    jump = False
    t = 0
    
  4. 메인 루프

    python
    Copy code
    while run:
        clock.tick(900)
        DISPLAYSF.fill((0, 0, 0))
    
    
  5. 점프 상태와 중력 적용

    if ball_pos[1] >= 500-100:
        jump = False
    pygame.draw.line(DISPLAYSF, (255, 255, 255), (0, 500), (700, 500))
    
    
  6. 이벤트 처리

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN        col += 1
            if jump == False:
    		        t = 0
                jump = True
    
    
  7. 점프와 중력 구현

    if jump == True:
        t += 0.017
        ball_pos[1] = 500 - 100 + 0.5 * 9.8 * t**2 + (-100) * t
    
    
  8. 사각형 공 그리기 및 화면 업데이트

    rect = pygame.Rect(ball_pos[0], ball_pos[1], 100, 100)
    pygame.draw.rect(DISPLAYSF, col_arr[col % 3], rect)
    pygame.display.update()
    
    
  9. 종료 처리

    pygame.quit()
    sys.exit()
    
    

요약

이 코드는 마우스 클릭 시 공이 점프하는 간단한 중력 애니메이션을 보여줍니다. ball_pos의 y 위치는 중력 가속도와 초기 속도로 계산되며, col 값을 순환해 공의 색상이 매번 바뀝니다.