Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
209 lines (173 loc) · 8.77 KB
/
Copy pathmain.py
File metadata and controls
209 lines (173 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""종스크롤 슈팅 게임. 실행: python main.py"""
import sys
import pygame
from settings import (
WIDTH, HEIGHT, FPS, TITLE, HIGHSCORE_FILE,
ENEMY_SPAWN_MS, START_LIVES, SCORE_PER_ENEMY,
WHITE, YELLOW, RED, HUD_SHADE, GAME_OVER_SHADE,
)
from entities import Background, Player, Enemy
SPAWN_ENEMY = pygame.USEREVENT + 1 # "적을 하나 만들 때가 됐다"는 타이머 이벤트
def load_high_score():
"""HIGHSCORE_FILE 에서 최고 점수를 읽어 돌려준다."""
try:
with open(HIGHSCORE_FILE, "r") as file:
return int(file.read())
except (FileNotFoundError, ValueError):
return 0
def save_high_score(score):
"""최고 점수를 HIGHSCORE_FILE 에 저장한다."""
# TODO(F5): score 를 파일에 저장할 것. (다음 실행 때 load_high_score() 로 읽을 수 있게)
with open(HIGHSCORE_FILE, "w") as file:
file.write(str(score))
class Game:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
self.clock = pygame.time.Clock()
self.font = pygame.font.Font(None, 32)
self.big_font = pygame.font.Font(None, 72)
pygame.time.set_timer(SPAWN_ENEMY, ENEMY_SPAWN_MS)
self.background = Background()
# 밝은 배경 위에서도 글자가 보이도록 깔아 줄 반투명 어두운 막
self.hud_shade = pygame.Surface((WIDTH, 44), pygame.SRCALPHA)
self.hud_shade.fill(HUD_SHADE)
self.dark_shade = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA) # 시작·게임 오버 화면용
self.dark_shade.fill(GAME_OVER_SHADE)
self.high_score = load_high_score()
self.running = True
self.on_title = True # True 면 시작 화면, SPACE 를 누르면 False 가 되며 게임 시작
self.reset()
def reset(self):
"""새 판을 시작한다. 스프라이트·점수·생명을 처음 상태로 되돌린다."""
self.all_sprites = pygame.sprite.Group() # 화면에 그릴 모든 스프라이트
self.bullets = pygame.sprite.Group()
self.enemies = pygame.sprite.Group()
self.player = Player()
self.all_sprites.add(self.player)
self.score = 0
self.lives = START_LIVES
self.game_over = False
# ---------------- 입력 ----------------
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif self.on_title:
self.handle_title_event(event) # 시작 화면 입력은 여기서만 처리
elif event.type == SPAWN_ENEMY and not self.game_over:
enemy = Enemy()
self.enemies.add(enemy)
self.all_sprites.add(enemy)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
elif event.key == pygame.K_SPACE and not self.game_over:
bullet = self.player.shoot()
self.bullets.add(bullet)
self.all_sprites.add(bullet)
elif event.key == pygame.K_r and self.game_over:
self.reset()
# TODO(F5): 게임 오버 상태에서 R 키를 누르면 재시작
# TODO(F2): 스페이스 키를 누르면 self.player.shoot() 으로 총알을 만들어
# self.bullets 와 self.all_sprites 두 그룹에 넣을 것. (게임 오버 중에는 쏘지 않게)
# TODO(F5): 게임 오버 상태에서 R 키를 누르면 self.reset() 으로 새 판을 시작할 것.
def handle_title_event(self, event):
"""시작 화면의 입력: SPACE 면 새 판 시작, ESC 면 종료. (적 생성·발사 없음)"""
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.on_title = False
self.reset()
elif event.key == pygame.K_ESCAPE:
self.running = False
# ---------------- 갱신 ----------------
def update(self):
if self.game_over:
return # 게임 오버면 화면(배경 포함)을 멈춤.
self.background.update()
if self.on_title:
return # 시작 화면에서는 배경만 흐름.
self.player.update(pygame.key.get_pressed())
self.bullets.update()
self.enemies.update()
# TODO(F3): 총알과 적이 부딪히면 둘 다 없애고, 맞힌 적 하나당 SCORE_PER_ENEMY 점을 더할 것.
# 힌트: pygame.sprite.groupcollide
hits = pygame.sprite.groupcollide(self.bullets, self.enemies, True, True)
for enemy in hits:
self.score += SCORE_PER_ENEMY
# TODO(F4): 적이 플레이어에 닿으면 그 적을 없애고 생명(self.lives)을 1 줄일 것.
# 힌트: pygame.sprite.spritecollide
hits = pygame.sprite.spritecollide(self.player, self.enemies, True)
for enemy in hits:
self.lives -= 1
# 화면 아래로 지나간 적은 없앨 것.
for enemy in list(self.enemies):
if enemy.rect.top > HEIGHT:
enemy.kill()
self.lives -= 1
# TODO(F4): 적이 아래로 지나갔으니 생명을 1 줄일 것.
# TODO(F4): 생명이 0 이하가 되면 self.end_game() 을 실행할 것.
if self.lives <= 0:
self.end_game()
def end_game(self):
"""게임 오버 처리."""
self.game_over = True
# TODO(F5): 이번 점수가 최고 점수보다 높으면 self.high_score 를 바꾸고 save_high_score() 로 저장할 것.
if self.score > self.high_score:
self.high_score = self.score
save_high_score(self.high_score)
# ---------------- 그리기 ----------------
def draw_text(self, text, font, color, **position):
"""글자를 화면에 그린다. 예: self.draw_text("Hi", self.font, WHITE, center=(240, 320))"""
surface = font.render(text, True, color)
self.screen.blit(surface, surface.get_rect(**position))
def draw(self):
self.background.draw(self.screen)
if self.on_title:
self.draw_title()
pygame.display.flip()
return
self.all_sprites.draw(self.screen)
if self.game_over:
self.screen.blit(self.dark_shade, (0, 0)) # 게임 오버 글자가 잘 보이게
self.screen.blit(self.hud_shade, (0, 0)) # 점수·생명 글자 뒤 어두운 띠
self.draw_text(f"LIVES: {self.lives}", self.font, WHITE, topright=(WIDTH - 10, 10))
# TODO(F3): 화면 왼쪽 위에 현재 점수를 "SCORE: 120" 형태로 표시할 것.
self.draw_text(f"SCORE: {self.score}", self.font, WHITE, topleft=(10, 10))
if self.game_over:
self.draw_game_over()
pygame.display.flip()
def draw_title(self):
"""시작 화면: 게임 이름, 플레이어 그림, 조작 안내, 깜빡이는 시작 안내."""
cx = WIDTH // 2
self.screen.blit(self.dark_shade, (0, 0))
self.draw_text(TITLE, self.big_font, YELLOW, center=(cx, 150))
self.screen.blit(self.player.image, self.player.image.get_rect(center=(cx, 270)))
controls = ("LEFT / RIGHT : Move", "SPACE : Shoot", "R : Restart", "ESC : Quit")
for i, line in enumerate(controls):
self.draw_text(line, self.font, WHITE, center=(cx, 370 + i * 34))
if pygame.time.get_ticks() // 500 % 2 == 0: # 0.5초마다 깜빡임
self.draw_text("Press SPACE to start", self.font, YELLOW, center=(cx, 540))
def draw_game_over(self):
"""게임 오버 화면. self.game_over 가 True 일 때 draw() 가 부른다."""
# TODO(F4): 화면 가운데에 "GAME OVER" 와 이번 판 점수를 표시할 것.
# TODO(F5): 최고 점수("HIGH SCORE: 300")와 재시작 안내("Press R to restart")도 표시할 것.
self.draw_text("GAME OVER", self.big_font, WHITE, center=(WIDTH // 2, HEIGHT // 2 - 40))
self.draw_text(f"SCORE: {self.score}", self.font, WHITE, center=(WIDTH // 2, HEIGHT // 2 + 20))
self.draw_text(f"HIGH SCORE: {self.high_score}", self.font, WHITE, center=(WIDTH // 2, HEIGHT // 2 + 60))
self.draw_text("Press R to restart", self.font, WHITE, center=(WIDTH // 2, HEIGHT // 2 + 100))
# ---------------- 게임 루프 ----------------
def step(self):
"""한 프레임: 입력 → 갱신 → 그리기."""
self.handle_events()
self.update()
self.draw()
def run(self):
while self.running:
self.clock.tick(FPS)
self.step()
pygame.quit()
if __name__ == "__main__":
Game().run()
sys.exit()