今回は、Python言語で独自のRock Paper Scissorsゲームを作成するための手順を説明します。
ロック・ペーパー・シザーズについて
ジャンケンは、2人で同時に行うゲームで、ジャンケンではさみをつぶし、はさみで紙を切り、紙ではさみをするものです。
ルールに基づき、勝者が決定されます。
あなたが人気のテレビシリーズ、ビッグバン-セオリーに精通している場合は、ジャンケントカゲ-Spockと呼ばれるゲームの別のバージョンを知っているかもしれません。
以下のインフォグラフィックが参考になるかもしれません。
# The mapping between moves and numbersgame_map = {0:"rock", 1:"paper", 2:"scissors", 3:"lizard", 4:"Spock"}
# Win-lose matrix for traditional gamerps_table = [[-1, 1, 0], [1, -1, 2], [0, 2, -1]]
# Win-lose matrix for new version of the gamerpsls_table = [[-1, 1, 0, 0, 4],[1, -1, 2, 3, 1], [0, 2, -1, 2, 4], [0, 3, 2, -1, 3], [4, 1, 4, 3, -1]]
|
矢印の向きで勝敗が決まります。
矢印は、その特定の対戦で、そのエンティティの優位性を意味し、出て行く。
この記事もチェック:Pythonでジャンケンゲーム(ロック・ペーパー・サイザーズ)を自作する方法
ゲームプレイデモ
PythonでRock Paper Scissorsを作成する
今日作成するゲームは、両方のバージョンのジャンケンをサポートします。
誰もが新しいバージョンのゲームをやりたがるとは限らないので、ファン層を分けるためのゲームメニューが必要です。
メニューは以下のようなものです。
# The GAME LOOPwhile True:
# The Game Menu
print()
print("Let's Play!!!")
print("Which version of Rock-Paper-Scissors?")
print("Enter 1 to play Rock-Paper-Scissors")
print("Enter 2 to play Rock-Paper-Scissors-Lizard-Spock")
print("Enter 3 to quit")
print()
# Try block to handle the player choice
try:
choice = int(input("Enter your choice = "))
except ValueError:
clear()
print("Wrong Choice")
continue
# Play the traditional version of the game
if choice == 1:
rps()
# Play the new version of the game
elif choice == 2:
rpsls()
# Quit the GAME LOOP
elif choice == 3:
break
# Other wrong input
else:
clear()
print("Wrong choice. Read instructions carefully.")
|
データ構造の作成
ゲームを完全に機能させるために必要な特定のデータ構造が2つあります。
# Set of instructions for Rock-Paper-Scissorsdef rps_instructions():
print()
print("Instructions for Rock-Paper-Scissors : ")
print()
print("Rock crushes Scissors")
print("Scissors cuts Paper")
print("Paper covers Rock")
print()
# Set of instructions for Rock-Paper-Scissors-Lizard-Spockdef rpsls_instructions():
print()
print("Instructions for Rock-Paper-Scissors-Lizard-Spock : ")
print()
print("Scissors cuts Paper")
print("Paper covers Rock")
print("Rock crushes Lizard")
print("Lizard poisons Spock")
print("Spock smashes Scissors")
print("Scissors decapitates Lizard")
print("Lizard eats Paper")
print("Paper disproves Spock")
print("Spock vaporizes Rock")
print("Rock crushes Scissors")
print()
|
ゲームの動きをマッピングする
勝者と敗者を決定するために、可能な手と数字の間にある種のマッピングが必要です。
この目的のためにpythonの辞書を使用します。
勝敗マトリックス
Python でじゃんけんの勝敗を確認する最も簡単な方法は、各プレイヤーの入力に対応するセルを参照することです。
以下は、伝統的なゲームの行列です。
# Player Inputinp = input("Enter your move : ")
if inp.lower() == "help":
clear()
rps_instructions()
continue
elif inp.lower() == "exit":
clear()
break elif inp.lower() == "rock":
player_move = 0
elif inp.lower() == "paper":
player_move = 1 elif inp.lower() == "scissors":
player_move = 2
else:
clear()
print("Wrong Input!!")
rps_instructions()
continue
|
行列の非負の値は、対応する入力の勝者を表します。
この行列を理解するために、例を挙げてみましょう。
あるゲームにおいて、プレイヤー1がはさみ(インデックス=2)を選び、プレイヤー2が紙(インデックス=1)を選んだとします。
行列のセル(2, 1)と(1, 2)は、ScissorsとPaperが一致したことを示す。
はさみは紙を切るので、これらのセルにははさみのインデックスが含まれる。
‘-1’` を含むセルは引き分けを表します。
以下は、新バージョンの行列です。
# Get the computer move randomlycomp_move = random.randint(0, 2)
# Print the computer moveprint("Computer chooses ", game_map[comp_move].upper())
|
以上のデータ構造によって、私たちのゲーム「じゃんけんぽん」を作るのに必要なゲームロジックが完成した。
残るは、プレイヤーの入力とコンピュータの動きの管理です。
ゲームループの構築
Pythonでロック・ペーパー・シザーズを行う上で最も重要な部分の一つが、ゲーム・ループです。
このゲームの文脈では、ゲームループはプレイヤー間のマッチの文字列を維持する役割を担っています。
# Find the winner of the matchwinner = rps_table[player_move][comp_move]
# Declare the winner if winner == player_move:
print(name, "WINS!!!")
elif winner == comp_move:
print("COMPUTER WINS!!!")
else:
print("TIE GAME")
print()
time.sleep(2)
clear() |
rps()’と‘rpsls()’` 関数は、それぞれのバージョンの一連のゲームを処理するために、内部にゲームループを持っています。
‘clear()' 関数は、出力が混雑するのを防ぐために端末をクリアする役割を担っている。
PythonによるRock Paper Scissorsのゲーム命令
プレイヤーが手を出す前に、プログラマはゲーマーに一連の指示を与えることが義務です。
ゲームに指示を与えるための関数を2つ作成する必要があります。
import random
import os
import time
def clear():
os.system("clear")
# Set of instructions for Rock-Paper-Scissorsdef rps_instructions():
print()
print("Instructions for Rock-Paper-Scissors : ")
print()
print("Rock crushes Scissors")
print("Scissors cuts Paper")
print("Paper covers Rock")
print()
# Set of instructions for Rock-Paper-Scissors-Lizard-Spockdef rpsls_instructions():
print()
print("Instructions for Rock-Paper-Scissors-Lizard-Spock : ")
print()
print("Scissors cuts Paper")
print("Paper covers Rock")
print("Rock crushes Lizard")
print("Lizard poisons Spock")
print("Spock smashes Scissors")
print("Scissors decapitates Lizard")
print("Lizard eats Paper")
print("Paper disproves Spock")
print("Spock vaporizes Rock")
print("Rock crushes Scissors")
print()
def rps():
global rps_table
global game_map
global name
# Game Loop for each game of Rock-Paper-Scissors
while True:
print("--------------------------------------")
print(" Menu")
print("--------------------------------------")
print("Enter "help" for instructions")
print("Enter "Rock","Paper","Scissors" to play")
print("Enter "exit" to quit")
print("--------------------------------------")
print()
# Player Input
inp = input("Enter your move : ")
if inp.lower() == "help":
clear()
rps_instructions()
continue
elif inp.lower() == "exit":
clear()
break elif inp.lower() == "rock":
player_move = 0
elif inp.lower() == "paper":
player_move = 1 elif inp.lower() == "scissors":
player_move = 2
else:
clear()
print("Wrong Input!!")
rps_instructions()
continue
print("Computer making a move....")
print()
time.sleep(2)
# Get the computer move randomly
comp_move = random.randint(0, 2)
# Print the computer move
print("Computer chooses ", game_map[comp_move].upper())
# Find the winner of the match
winner = rps_table[player_move][comp_move]
# Declare the winner
if winner == player_move:
print(name, "WINS!!!")
elif winner == comp_move:
print("COMPUTER WINS!!!")
else:
print("TIE GAME")
print()
time.sleep(2)
clear()
def rpsls():
global rpsls_table
global game_map
global name
# Game Loop for each game of Rock-Paper-Scissors-Lizard-Spock
while True:
print("--------------------------------------")
print(" Menu")
print("--------------------------------------")
print("Enter "help" for instructions")
print("Enter "Rock","Paper","Scissors","Lizard","Spock" to play")
print("Enter "exit" to quit")
print("--------------------------------------")
print()
# Player Input
inp = input("Enter your move : ")
if inp.lower() == "help":
clear()
rpsls_instructions()
continue
elif inp.lower() == "exit":
clear()
break elif inp.lower() == "rock":
player_move = 0
elif inp.lower() == "paper":
player_move = 1 elif inp.lower() == "scissors":
player_move = 2
elif inp.lower() == "lizard":
player_move = 3
elif inp.lower() == "spock":
player_move = 4
else:
clear()
print("Wrong Input!!")
rps_instructions()
continue
print("Computer making a move....")
comp_move = random.randint(0, 4)
print()
time.sleep(2)
print("Computer chooses ", game_map[comp_move].upper())
winner = rpsls_table[player_move][comp_move]
print()
if winner == player_move:
print(name, "WINS!!!")
elif winner == comp_move:
print("COMPUTER WINS!!!")
else:
print("TIE GAME")
print()
time.sleep(2)
clear()
# The main functionif __name__ == '__main__':
# The mapping between moves and numbers
game_map = {0:"rock", 1:"paper", 2:"scissors", 3:"lizard", 4:"Spock"}
# Win-lose matrix for traditional game
rps_table = [[-1, 1, 0], [1, -1, 2], [0, 2, -1]]
# Win-lose matrix for new version of the game
rpsls_table = [[-1, 1, 0, 0, 4],[1, -1, 2, 3, 1], [0, 2, -1, 2, 4], [0, 3, 2, -1, 3], [4, 1, 4, 3, -1]]
name = input("Enter your name: ")
# The GAME LOOP
while True:
# The Game Menu
print()
print("Let's Play!!!")
print("Which version of Rock-Paper-Scissors?")
print("Enter 1 to play Rock-Paper-Scissors")
print("Enter 2 to play Rock-Paper-Scissors-Lizard-Spock")
print("Enter 3 to quit")
print()
# Try block to handle the player choice
try:
choice = int(input("Enter your choice = "))
except ValueError:
clear()
print("Wrong Choice")
continue
# Play the traditional version of the game
if choice == 1:
rps()
# Play the new version of the game
elif choice == 2:
rpsls()
# Quit the GAME LOOP
elif choice == 3:
break
# Other wrong input
else:
clear()
print("Wrong choice. Read instructions carefully.")
|
指示を表示した後、プレイヤーに入力を求める。
プレイヤー入力の処理
私たちのゲームはプレイヤー対コンピュータのシナリオをサポートしているので、Pythonでジャンケンの0ゲームごとに1つの手を処理する必要があります。
入力メニュー
プレイヤーにゲームを操作している感覚を与える必要があります。
そのためには、「助けを求める」、「可能な手」、「ゲームを終了する」というオプションを提供する必要があります。
以下の入力メニューがこれにあたる。

対局者の動き
プレイヤー入力の処理では、文字列入力を数値対応に変換することに主眼が置かれています。
Pythonの伝統的なゲームであるじゃんけんでは、以下のような入力の手を受け付けます。

if-elif-else文を使って、条件付きでチェックを行うことができる。
この記事もチェック:Python if else elif ステートメント
コンピューター移動の管理
不偏のコンピュータの動きを管理するために、Pythonの 'random' ライブラリの助けを借りる必要があります。
可能な選択肢の中からランダムな手を選択します。

randomライブラリの 'randint()' 関数を使用して、コンピュータのランダムな動きを選択します。
勝敗の決定と宣言
勝敗表を使って、簡単に勝者を決めることができる。

Pythonスクリプトの実行を一時停止するには、'sleep()'関数を使用します。
上の例では、2秒間待機しています。
以上、じゃんけんゲームを実行するPythonスクリプトの説明でした。
Pythonで作るロック・ペーパー・シザーズの完全なコード
以下は、Pythonで作成したジャンケンゲームの完全なコードです。

まとめ
プログラマーがゲームロジックとデザインの基本を知っていれば、ゲームを作ることは決して難しいことではありません。
この記事で、簡単な端末ベースのゲームを開発するための上記の原則を、読者に知ってもらえたなら幸いです。
動作するコードは、私のGithubアカウントにもあります。
お読みいただきありがとうございました。
何か疑問があれば、コメント欄でお気軽にお尋ねください。