|
| 1 | +import pygame |
| 2 | +import math |
| 3 | +import random |
| 4 | + |
| 5 | +# 게임 초기화 |
| 6 | +pygame.init() |
| 7 | + |
| 8 | +# 화면 크기 설정 |
| 9 | +SCREEN_WIDTH = 900 |
| 10 | +SCREEN_HEIGHT = 700 |
| 11 | +window = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) |
| 12 | +pygame.display.set_caption("파이썬응용_예시게임") |
| 13 | + |
| 14 | +# 색상 설정 |
| 15 | +COLOR_WHITE = (255, 255, 255) |
| 16 | +COLOR_BLACK = (0, 0, 0) |
| 17 | +COLOR_RED = (255, 0, 0) |
| 18 | +COLOR_BLUE = (0, 0, 255) |
| 19 | +COLOR_GREEN = (0, 255, 0) |
| 20 | + |
| 21 | +# 몬스터 |
| 22 | +class Enemy: |
| 23 | + def __init__(self): |
| 24 | + self.pos_x = 0 |
| 25 | + self.pos_y = random.randint(50, SCREEN_HEIGHT - 50) |
| 26 | + self.velocity = random.randint(1, 3) |
| 27 | + self.hp = 100 |
| 28 | + |
| 29 | + def update_position(self): |
| 30 | + self.pos_x += self.velocity |
| 31 | + |
| 32 | + def render(self): |
| 33 | + pygame.draw.rect(window, COLOR_RED, (self.pos_x, self.pos_y, 20, 20)) |
| 34 | + health_bar_width = 20 * (self.hp / 100) #체력바 |
| 35 | + pygame.draw.rect(window, COLOR_GREEN, (self.pos_x, self.pos_y - 10, health_bar_width, 5)) |
| 36 | + |
| 37 | +# 디펜스타워 |
| 38 | +class DefenseTower: |
| 39 | + def __init__(self, x, y): |
| 40 | + self.center_x = x |
| 41 | + self.center_y = y |
| 42 | + self.attack_range = 150 |
| 43 | + self.attack_power = 10 |
| 44 | + self.reload_time = 30 |
| 45 | + self.current_reload = 0 |
| 46 | + |
| 47 | + def render(self): |
| 48 | + pygame.draw.rect(window, COLOR_BLUE, (self.center_x - 15, self.center_y - 15, 30, 30)) |
| 49 | + pygame.draw.circle(window, (0, 255, 255, 64), (self.center_x, self.center_y), self.attack_range, 1) |
| 50 | + |
| 51 | + def shoot(self, targets): |
| 52 | + if self.current_reload <= 0: |
| 53 | + for target in targets: |
| 54 | + distance = math.hypot(self.center_x - target.pos_x, self.center_y - target.pos_y) |
| 55 | + |
| 56 | + if distance <= self.attack_range: |
| 57 | + target.hp -= self.attack_power |
| 58 | + self.current_reload = self.reload_time |
| 59 | + |
| 60 | + |
| 61 | + pygame.draw.line(window, COLOR_GREEN, (self.center_x, self.center_y), |
| 62 | + (target.pos_x + 10, target.pos_y + 10), 2) |
| 63 | + return |
| 64 | + else: |
| 65 | + self.current_reload -= 1 |
| 66 | + |
| 67 | +# 게임 상태 변수 |
| 68 | +enemy_list = [] |
| 69 | +tower_list = [] |
| 70 | +player_score = 0 |
| 71 | +player_lives = 10 |
| 72 | + |
| 73 | +# 게임 루프 |
| 74 | +frame_clock = pygame.time.Clock() |
| 75 | +game_active = True |
| 76 | +while game_active: |
| 77 | + for event in pygame.event.get(): |
| 78 | + if event.type == pygame.QUIT: |
| 79 | + game_active = False |
| 80 | + if event.type == pygame.MOUSEBUTTONDOWN: |
| 81 | + mouse_x, mouse_y = pygame.mouse.get_pos() |
| 82 | + tower_list.append(DefenseTower(mouse_x, mouse_y)) |
| 83 | + |
| 84 | + # 적 생성 |
| 85 | + if random.randint(1, 60) == 1: |
| 86 | + enemy_list.append(Enemy()) |
| 87 | + |
| 88 | + # 화면 업데이트 |
| 89 | + window.fill(COLOR_WHITE) |
| 90 | + |
| 91 | + # 적 이동 및 그리기 |
| 92 | + for enemy in enemy_list[:]: |
| 93 | + enemy.update_position() |
| 94 | + enemy.render() |
| 95 | + if enemy.pos_x > SCREEN_WIDTH: |
| 96 | + enemy_list.remove(enemy) |
| 97 | + player_lives -= 1 |
| 98 | + elif enemy.hp <= 0: |
| 99 | + enemy_list.remove(enemy) |
| 100 | + player_score += 10 |
| 101 | + |
| 102 | + # 타워 업데이트 및 공격 |
| 103 | + for tower in tower_list: |
| 104 | + tower.render() |
| 105 | + tower.shoot(enemy_list) |
| 106 | + |
| 107 | + # 상단 점수표 |
| 108 | + font = pygame.font.Font(None, 36) |
| 109 | + score_display = font.render(f"Score: {player_score}", True, COLOR_BLACK) |
| 110 | + lives_display = font.render(f"Lives: {player_lives}", True, COLOR_BLACK) |
| 111 | + window.blit(score_display, (10, 10)) |
| 112 | + window.blit(lives_display, (10, 50)) |
| 113 | + |
| 114 | + # 화면 갱신 |
| 115 | + pygame.display.flip() |
| 116 | + frame_clock.tick(60) |
| 117 | + |
| 118 | + # 게임 오버 |
| 119 | + if player_lives <= 0: |
| 120 | + game_active = False |
| 121 | + |
| 122 | +# 게임 종료 |
| 123 | +pygame.quit() |
0 commit comments