0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

プログラミング初心者がPythonでコードを書いてみたよ~

Posted at

puts 'Qiita'

Pythonを使ってコードを組んでみました。

random 関数を使用した「格闘ゲーム」ユーザーが技を出して、勝ち負けを判定するゲームです。

ゲームのルール

プログラムがランダムで「蹴る」「殴る」「飛ぶ」「避ける」「フェイント」 から1つを選びます。
ユーザーが「蹴る」「殴る」「飛ぶ」「避ける」「フェイント」のいずれかを入力します。
プログラムが勝ち負けを判定し、結果を表示します。
ユーザーが「終了」と入力するまで何度もプレイできます。

格闘ゲーム

import random
acction = ['蹴る','殴る','飛ぶ','避ける','フェイント']

#格闘ゲーム
print("格闘ゲームを始めます!「蹴る」「殴る」「飛ぶ」「避ける」「フェイント」を入力してください。終了するには「終了」と入力してください。")

while True:
    # コンピュータの手をランダムに選ぶ
    computer_choice = random.choice(action)
    user_input = input("あなたの技: ")

    if user_input == '終了':                # Tabをするのが重要 tabがされていないと構文をかいても使えない
        print('ゲームを終了します')
        break
    
    if user_input not in action:
            print("エラー: 「蹴る」「殴る」「飛ぶ」「避ける」「フェイント」のいずれかを入力してください。")
            continue
    
    print(f'コンピュータの技: {computer_choice}')
    
    if user_input == computer_choice:
        print("引き分けです!")
    elif  (user_input == "蹴る" and computer_choice == "殴る") or \
    (user_input == "飛ぶ" and computer_choice == "蹴る") or \
    (user_input == "避ける" and computer_choice == "殴る") or \
                (user_input == "フェイント" and computer_choice == "蹴る"):
        print("あなたの勝ちです!")   
    else:
         print("コンピュータの勝ちです!")
    
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?