Arcade Library를 사용한 2D 게임 시작하기 (Part 11): 화면 전환
Source: Dev.to
위의 소스 링크에 포함된 전체 텍스트를 제공해 주시면, 해당 내용을 한국어로 번역해 드리겠습니다. 코드 블록, URL 및 마크다운 형식은 그대로 유지하면서 번역해 드릴 수 있습니다. 부탁드립니다.
파일 정리
개발이 진행됨에 따라 소스 파일 수가 증가하고 관리가 어려워집니다.
src 폴더를 소스 코드 전용으로 만들고 화면을 각각 하나씩 세 개의 모듈로 분리하세요.
| 파일 이름 | 역할 | 위치 |
|---|---|---|
title.py | 타이틀 화면 | src/title.py |
game.py | 게임 화면 | src/game.py |
result.py | 결과 화면 | src/result.py |
폴더 구조는 다음과 같습니다:
# Folder structure
working_directory/
├─ main.py
├─ images/
└─ src/ # Folder for source code
├─ title.py
├─ game.py
└─ result.py
화면 전환
화면 전환을 위한 코드는 이전 예제와 동일합니다.
화면 전환이 필요할 때마다 간단히:
arcade.View를 생성합니다.window.show_view()에 전달합니다.
# main.py (example)
view = title.TitleView(window) # TitleView
window.show_view(view)
전체 코드
아래는 완전한 작동 예제입니다. SPACE 키를 눌러 다음 순서대로 화면을 전환하세요:
제목 화면 → 게임 화면 → 결과 화면
코드를 그대로 복사해서 실행할 수 있습니다.
main.py
import arcade
import src.title as title
def main():
"""Main process"""
window = arcade.Window(480, 320, "Hello, Arcade!!")
view = title.TitleView(window) # TitleView
window.show_view(view)
arcade.run()
if __name__ == "__main__":
main()
src/title.py
import arcade
import src.game as game
# Title screen
class TitleView(arcade.View):
def __init__(self, window):
super().__init__()
self.window = window
self.background_color = arcade.color.PRUNE
# Info text
self.msg_info = arcade.Text(
"TITLE: SPACE TO NEXT",
window.width / 2, window.height - 20,
arcade.color.WHITE, 12,
anchor_x="center", anchor_y="top"
)
def on_key_press(self, key, key_modifiers):
# SPACE → Game screen
if key == arcade.key.SPACE:
view = game.GameView(self.window) # GameView
self.window.show_view(view)
def on_update(self, delta_time):
pass
def on_draw(self):
self.clear()
self.msg_info.draw()
src/game.py
import arcade
import src.result as result
# Game screen
class GameView(arcade.View):
def __init__(self, window):
super().__init__()
self.window = window
self.background_color = arcade.color.DARK_SPRING_GREEN
# Info text
self.msg_info = arcade.Text(
"GAME: SPACE TO NEXT",
window.width / 2, window.height - 20,
arcade.color.WHITE, 12,
anchor_x="center", anchor_y="top"
)
def on_key_press(self, key, key_modifiers):
# SPACE → Result screen
if key == arcade.key.SPACE:
view = result.ResultView(self.window) # ResultView
self.window.show_view(view)
def on_update(self, delta_time):
pass
def on_draw(self):
self.clear()
self.msg_info.draw()
src/result.py
import arcade
import src.title as title
# Result screen
class ResultView(arcade.View):
def __init__(self, window):
super().__init__()
self.window = window
self.background_color = arcade.color.CERULEAN
# Info text
self.msg_info = arcade.Text(
"RESULT: SPACE TO NEXT",
window.width / 2, window.height - 20,
arcade.color.WHITE, 12,
anchor_x="center", anchor_y="top"
)
def on_key_press(self, key, key_modifiers):
# SPACE → Title screen
if key == arcade.key.SPACE:
view = title.TitleView(self.window) # TitleView
self.window.show_view(view)
def on_update(self, delta_time):
pass
def on_draw(self):
self.clear()
self.msg_info.draw()
결과는 다음과 같이 보일 것입니다:
최종 생각
읽어 주셔서 대단히 감사합니다.
이 시리즈가 여러분의 게임 개발 여정의 출발점이 되길 바랍니다. ޱ(ఠ皿ఠ)ว👍
