= 500-100: jump = 2 ball_v = 0 ball_pos[1] = 500 - 100 else: ball_v += 9.8*0.017 pygame.draw.line(DISPLAYSF,(255,255,255),(0,500),(700,500)) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: col += 1 if jump > 0 and (pygame.time.get_ticks() - tJump)>10: tJump = pygame.time.get_ticks() t = 0 jump -= 1 ball_v = -50 #ball_pos[1] = 500 - 1"> = 500-100: jump = 2 ball_v = 0 ball_pos[1] = 500 - 100 else: ball_v += 9.8*0.017 pygame.draw.line(DISPLAYSF,(255,255,255),(0,500),(700,500)) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: col += 1 if jump > 0 and (pygame.time.get_ticks() - tJump)>10: tJump = pygame.time.get_ticks() t = 0 jump -= 1 ball_v = -50 #ball_pos[1] = 500 - 1"> = 500-100: jump = 2 ball_v = 0 ball_pos[1] = 500 - 100 else: ball_v += 9.8*0.017 pygame.draw.line(DISPLAYSF,(255,255,255),(0,500),(700,500)) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: col += 1 if jump > 0 and (pygame.time.get_ticks() - tJump)>10: tJump = pygame.time.get_ticks() t = 0 jump -= 1 ball_v = -50 #ball_pos[1] = 500 - 1">

import pygame, sys
from pygame.locals import *

pygame.init()

WIDTH = 700
HEIGHT = 600

DISPLAYSF = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Title")
clock = pygame.time.Clock()

col_arr = [(255, 0, 0),(0, 255, 0),(0, 0, 255)]
col = 0
run = True
    
cam_pos = [0,0]
ball_pos = [0,500-100]
ball_v = 0
jump = 2
t = 0
tJump = 0

while run:
    clock.tick(900)
    DISPLAYSF.fill((0,0,0))
    if ball_pos[1] >= 500-100:
        jump = 2
        ball_v = 0
        ball_pos[1] = 500 - 100
    else:
        ball_v +=  9.8*0.017
    pygame.draw.line(DISPLAYSF,(255,255,255),(0,500),(700,500))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            col += 1
            if jump > 0 and (pygame.time.get_ticks() - tJump)>10:
                tJump = pygame.time.get_ticks()
                t = 0
                jump -= 1
                ball_v = -50
        #ball_pos[1] = 500 - 100 + 0.5 * 9.8 * t**2 + (-50) * t
        
    ball_pos[1] += ball_v*0.017

    rect = pygame.Rect(ball_pos[0],ball_pos[1],100,100)
    pygame.draw.rect(DISPLAYSF,col_arr[col%3],rect)

    pygame.display.update()

pygame.quit()
sys.exit()

코드 순서대로 설명:

  1. 초기화:

    python
    Copy code
    import pygame, sys
    from pygame.locals import *
    
    pygame.init()
    
    
  2. 화면 설정:

    python
    Copy code
    WIDTH = 700
    HEIGHT = 600
    
    DISPLAYSF = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Title")
    clock = pygame.time.Clock()
    
    
  3. 변수 설정:

    python
    Copy code
    col_arr = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
    col = 0
    run = True
    
    cam_pos = [0, 0]
    ball_pos = [0, 500 - 100]
    ball_v = 0
    jump = 2
    t = 0
    tJump = 0
    
    
  4. 게임 루프:

    python
    Copy code
    while run:
        clock.tick(900)
        DISPLAYSF.fill((0, 0, 0))
    
    
  5. 공이 바닥에 닿을 때의 처리:

    python
    Copy code
    if ball_pos[1] >= 500 - 100:
        jump = 2
        ball_v = 0
        ball_pos[1] = 500 - 100
    else:
        ball_v += 9.8 * 0.017
    
    
  6. 이벤트 처리:

    python
    Copy code
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            col += 1
            if jump > 0 and (pygame.time.get_ticks() - tJump) > 10:
                tJump = pygame.time.get_ticks()
                t = 0
                jump -= 1
                ball_v = -50
    
    
  7. 공의 위치 업데이트:

    python
    Copy code
    ball_pos[1] += ball_v * 0.017
    
    
  8. 사각형 그리기 및 화면 업데이트:

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

    python
    Copy code
    pygame.quit()
    sys.exit()