= 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()
초기화:
python
Copy code
import pygame, sys
from pygame.locals import *
pygame.init()
화면 설정:
python
Copy code
WIDTH = 700
HEIGHT = 600
DISPLAYSF = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Title")
clock = pygame.time.Clock()
WIDTH
, HEIGHT
)를 설정합니다.DISPLAYSF
)를 만들고, 윈도우 제목을 지정합니다.clock
객체를 생성합니다.변수 설정:
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
col_arr
는 세 가지 색상 배열로, 사각형의 색상을 바꾸는 데 사용됩니다.col
은 색상을 바꾸기 위한 인덱스입니다.run
은 게임 루프를 실행할지 여부를 결정하는 변수입니다.ball_pos
는 공의 초기 위치를 설정합니다.ball_v
는 공의 속도를 나타냅니다.jump
는 점프할 수 있는 횟수입니다.t
와 tJump
는 시간과 점프 타이밍을 관리합니다.게임 루프:
python
Copy code
while run:
clock.tick(900)
DISPLAYSF.fill((0, 0, 0))
공이 바닥에 닿을 때의 처리:
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
jump
횟수를 2로 초기화하고, 속도를 0으로 만듭니다.이벤트 처리:
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
QUIT
)가 발생하면 루프가 종료됩니다.공의 위치 업데이트:
python
Copy code
ball_pos[1] += ball_v * 0.017
사각형 그리기 및 화면 업데이트:
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()
종료 처리:
python
Copy code
pygame.quit()
sys.exit()