import pygame, sys
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Level Devil - 완전체")
WHITE, BLACK, RED, BLUE, GREEN, GRAY = (255,255,255), (0,0,0), (200,0,0), (0,0,200), (0,200,0), (180,180,180)
font = pygame.font.Font("C:/Windows/Fonts/malgun.ttf", 40)
clock = pygame.time.Clock()
# ---------------- 플레이어 이미지 로드 ----------------
player_stand = pygame.image.load("standing.png")
player_jump = pygame.image.load("jump.png")
player_run1 = pygame.image.load("walk1.png")
player_run2 = pygame.image.load("walk 2.png")
player_stand = pygame.transform.scale(player_stand, (50, 50))
player_jump = pygame.transform.scale(player_jump, (50, 50))
player_run1 = pygame.transform.scale(player_run1, (50, 50))
player_run2 = pygame.transform.scale(player_run2, (50, 50))
run_animation = [player_run1, player_run2]
# ---------------- 난이도 설정 ----------------
def set_difficulty(difficulty):
if difficulty == "easy":
player_speed = 6
jump_power = -18
traps = [pygame.Rect(500, 450, 50, 20)]
platforms = [pygame.Rect(0, 500, 800, 50), pygame.Rect(250, 500, 150, 20)]
start_y = 450
score_add = 10
elif difficulty == "normal":
player_speed = 5
jump_power = -15
traps = [pygame.Rect(400, 530, 50, 20), pygame.Rect(250, 430, 50, 20)]
platforms = [pygame.Rect(0, 550, 800, 50), pygame.Rect(200, 450, 100, 20), pygame.Rect(370, 370, 100, 20)]
start_y = 400
score_add = 20
else: # hard
player_speed = 4
jump_power = -15
traps = [pygame.Rect(350, 530, 50, 20), pygame.Rect(250, 430, 50, 20), pygame.Rect(500, 330, 50, 20)]
platforms = [pygame.Rect(0, 550, 800, 50), pygame.Rect(200, 480, 80, 20), pygame.Rect(400, 360, 80, 20)]
start_y = 420
score_add = 30
return player_speed, jump_power, traps, platforms, start_y, score_add
# ---------------- 메뉴 ----------------
def menu_screen():
while True:
screen.fill(WHITE)
title = font.render("난이도 선택", True, BLACK)
screen.blit(title, (WIDTH//2 - 100, 100))
buttons = {"easy": pygame.Rect(300, 200, 200, 60),
"normal": pygame.Rect(300, 300, 200, 60),
"hard": pygame.Rect(300, 400, 200, 60)}
for text, rect in buttons.items():
pygame.draw.rect(screen, GRAY, rect)
label = font.render(text, True, BLACK)
screen.blit(label, (rect.x+50, rect.y+10))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
for diff, rect in buttons.items():
if rect.collidepoint(event.pos):
return diff
pygame.display.flip()
clock.tick(60)
# ---------------- 라이프 표시 ----------------
def draw_lives(lives):
for i in range(lives):
pygame.draw.circle(screen, RED, (30 + i*30, 30), 10)
# ---------------- 점수 표시 ----------------
def draw_score(score):
text = font.render(f"점수: {score}", True, BLUE)
screen.blit(text, (WIDTH - 200, 30))
# ---------------- 게임 루프 ----------------
def game_loop(difficulty, score):
player_speed, jump_power, traps, platforms, start_y, score_add = set_difficulty(difficulty)
player = pygame.Rect(100, start_y, 50, 50)
gravity, y_velocity, on_ground = 1, 0, False
goal = pygame.Rect(700, 300, 50, 80)
lives = 3
run_index = 0
run_timer = 0
while True:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit()
keys = pygame.key.get_pressed()
moving_left = keys[pygame.K_a] and not keys[pygame.K_d]
moving_right = keys[pygame.K_d] and not keys[pygame.K_a]
if moving_left:
player.x -= player_speed
elif moving_right:
player.x += player_speed
if keys[pygame.K_SPACE] and on_ground:
y_velocity = jump_power
on_ground = False
y_velocity += gravity
player.y += y_velocity
on_ground = False
if player.bottom > HEIGHT:
player.bottom = HEIGHT
y_velocity = 0
on_ground = True
for plat in platforms:
if player.colliderect(plat):
if y_velocity >= 0 and player.bottom > plat.top and player.top < plat.top:
player.bottom = plat.top
y_velocity = 0
on_ground = True
elif y_velocity < 0 and player.top < plat.bottom and player.bottom > plat.bottom:
player.top = plat.bottom
y_velocity = 0
for trap in traps:
if player.colliderect(trap):
lives -= 1
if lives > 0:
text = font.render(f"죽음! 목숨 {lives}개 남음", True, RED)
screen.blit(text, (220, 250))
pygame.display.flip()
pygame.time.delay(1000)
player.x, player.y = 100, start_y
y_velocity = 0
else:
text = font.render("게임 오버!", True, RED)
screen.blit(text, (300, 250))
pygame.display.flip()
pygame.time.delay(2000)
return None, score
if player.colliderect(goal):
text = font.render("클리어! 🎉", True, BLUE)
screen.blit(text, (300, 250))
pygame.display.flip()
pygame.time.delay(1500)
score += score_add
next_difficulty = {"easy":"normal","normal":"hard","hard":"easy"}[difficulty]
return next_difficulty, score
# ---------------- 플레이어 애니메이션 ----------------
player_img = player_stand
if not on_ground:
player_img = player_jump
elif moving_left or moving_right:
run_timer += 1
if run_timer % 10 == 0:
run_index = (run_index + 1) % len(run_animation)
player_img = run_animation[run_index]
if moving_left:
player_img = pygame.transform.flip(player_img, True, False)
screen.blit(player_img, player.topleft)
for plat in platforms: pygame.draw.rect(screen, GREEN, plat)
for trap in traps: pygame.draw.rect(screen, RED, trap)
pygame.draw.rect(screen, BLUE, goal)
draw_lives(lives)
draw_score(score)
pygame.display.flip()
clock.tick(60)
# ---------------- 실행 ----------------
current_diff = menu_screen()
score = 0
while True:
next_diff, score = game_loop(current_diff, score)
if next_diff:
current_diff = next_diff
else:
current_diff = menu_screen()



