こんにちは、学習者の皆さん。
今日は、テキストベースの楽しいアドベンチャーゲームをゼロから作ってみましょう。
まず、テキストベースのゲームとは何かを理解し、同じものをプログラミング言語pythonで実装してみましょう。
テキストベースのゲームって何?
テキストベースのゲームとは、完全にテキストベースの入出力可能なシンプルなゲームです。
このようなタイプのゲームでは、ユーザーは、入力の形でユーザーが取る選択肢と一緒に到着した様々な状況を処理するためのオプションを持っています。
私たちのゲームのストーリー
下の図は、このチュートリアルで Python を使って作る小さなストーリーを示しています。
あなたの好みに応じて、ストーリーを拡張したり変更したりすることができます。
|
1
2
3
4
5
6
7
8
|
print("""WELCOME TO THE ADVENTURE GAME!
Let's start the action! ☆--☆
Lily wakes up in her bedroom in the middle of the night. She heard a loud BAN outside the house.
Now she has two choices she can either stay in the room or check what the sound might be about.
Type your choice: Stay or Evaluate?
""")
|
Pythonによるテキストベースアドベンチャーゲームの実装
まず、最初のシーンと物語がどのように進んでいくかをプリントしてみましょう。
これは単純にprint関数を使用することで可能です。
さらに楽しくするために、顔文字や絵文字を追加することもできます。
|
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
|
def scene1():
import time
print("""WELCOME TO THE ADVENTURE GAME!
Let's start the action! ☆--☆
Lily wakes up in her bedroom in the middle of the night. She heard a loud BAN outside the house.
Now she has two choices she can either stay in the room or check what the sound might be about.
Type your choice: Stay or Evaluate?
""")
c1 = input()
time.sleep(2)
ans = 'incorrect'
while(ans=='incorrect'):
if(c1.upper()=="STAY"):
print(")
ans = 'correct'
elif(c1.upper()=="EVALUATE"):
print("Lily exits the room silently and reaches the main hall.")
ans='correct'
scene2()
else:
print("ENTER THE CORRECT CHOICE! Stay or Evaluate?")
c1 = input()
|
はい、できました。
さて、シーンが決まり、面白くなってきましたね。
さて、ユーザーからの入力を受けて、それぞれの選択肢に対して条件文を入力してみましょう。
このゲームでは、ユーザーからのすべてのタイプの入力に対して答えを用意し、どの選択肢を選んでもエラーにならないようにする必要があります。
|
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
|
def scene2():
import time
print("""
In the main hall, she finds a strange but cute teddy bear on the floor.
She wanted to pick the teddy up.
But should she? It doesn't belong to her. (•˳̂•̆)
Type your choice: Pick or Ignore?
""")
time.sleep(2)
c1 = input()
ans = 'incorrect'
while(ans=='incorrect'):
if(c1.upper()=="PICK"):
print(""")
time.sleep(2)
print(""")
ans = 'correct'
pick="True"
elif(c1.upper()=='IGNORE'):
print(""")
ans='correct'
pick="False"
else:
print("Wrong Input! Enter pick or ignore?")
c1=input()
time.sleep(2)
scene3(pick)
|
最初の選択肢 input を選び、その答えが正しいか正しくないかを確認するための変数を作成します。
そして、条件ループとif-else文を作成します。
このゲームは、与えられた答えが有効になるまで、何度も選択肢を求め続けます。
これで、最初のシーンが完成しましたので、次のシーンに移り、同じようにゲーム全体を構築していきます。
以下は、2番目のシーンのコードです。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
def scene3(pick_value):
import time
print("""
It had red eyes and evil looks. She got very scared! """)
time.sleep(2)
if(pick_value=="True"):
time.sleep(2)
print("""But then she remembered! She had the magic portion and she threw it on the moster!
Well she had nothing to lose!""")
time.sleep(2)
print(")
elif(pick_value=="False"):
print("The monster attacked Lily and hurt her! She was then thrown to the new world by the monster!")
|
3番目のシーンのコードは次のようになります。
ここで、3番目のシーンの結果は、2番目のシーンで行われた選択(テディベアを選んだか無視したか、主人公がポーションを受け取ったかどうか)によって決まります。
|
1
2
3
|
scene1()print("
)
print("=================================END OF CHAPTER 1=================================")
|
3つのシーンを経て、物語の第1章は終了します。
お好みに応じて、物語全体を拡大したり、変更したりすることができます。
物語を始めるには、物語のシーン1を開始するだけです。

上記のストーリーの結果は、以下の通りです。
なかなかいい感じです。

まとめ
シンプルで簡単なテキストベースのアドベンチャーゲームを一人で作る方法がわかりましたね! あなたも自分だけのストーリーを作ってみませんか?ハッピーコーディング お読みいただきありがとうございました。