Pythonゲーム作りの過程 その5

今回の目標

選択肢の正誤判定
選択肢をランダムにする
選択肢A~Dを割り振る
モンスターの名前の設定

今回の結果

選択肢の正誤判定
→手を付けていない
選択肢をランダムにする
→未解決
選択肢A~Dを割り振る
→解決済み
モンスターの名前の設定
→無事解決

今回は、目標の一部を達成できました。

A~Dの割り振りについて

 chr() 関数を以下のように使いました。
※chr()関数はビルトイン関数といい、何もモジュールをインポートしなくても使えます。

for i in range(0,len(shuffle_kouho)):
        print(chr(65+i)+': '+shuffle_kouho[i])#ASCIIコードで設定しました

 ASCIIコードではこのように、65から91で大文字のA~Z,97から123で小文字のa~zに対応しています。

問題のカテゴリからモンスターの名前を変える

 思ったより簡単だったので説明は軽くにします。カテゴリを参照にしてprint関数で表示しただけです。

import random

def get_answer(q):
    shuffle_kouho=q['kouho']
    random.shuffle(shuffle_kouho)
    answer_str=1
    print(q['category']+"出題モンスターが現れた")
    for i in range(0,len(shuffle_kouho)):
        print(chr(65+i)+': '+shuffle_kouho[i])
        print(i)
    return [shuffle_kouho,answer_str]

questions =[
    {'question':"「洋琴」が表す楽器はどれ?",'kouho':['ピアノ','ギター','チェロ','ハープ'],'answer':"ピアノ",'category':'漢字'},
    {'question':"ラッコを漢字で書くとどれ?",'kouho':['海鼠','海獺','海狸','海犬'],'answer':"海獺",'category':'漢字'},
    {'question':"ガーナの首都は?",'kouho':['アクラ','アピア','アテネ','アンカラ'],'answer':"アクラ",'category':'地理'},
]
this_question = questions[random.randint(0,2)] #questions[0] とかquestions[1]とか

print(get_answer(this_question))

 以上になりますが、結果として候補をシャッフルできていませんでした。
 失敗したまま今日は寝ます。以下が現状のコードです。

import subprocess
import time
import csv
from playsound import playsound #メモ windows11では古めのバージョンをインストールすること。pip install playsound==1.2.2
import pygame
import random
import sys
import string

def get_answer(q):#関数定義
    shuffle_kouho=q['kouho']
    random.shuffle(shuffle_kouho)
    answer_str=1
    for i in range(0,len(shuffle_kouho)):
        print(chr(65+i)+': '+shuffle_kouho[i])#ASCIIコードで設定しました
    return [shuffle_kouho,answer_str]

i=0
mhp=40 #主人公のHP
ehp=50 #敵モンスターのHP

pygame.mixer.init(frequency = 44100)
bgm_sound = pygame.mixer.Sound("./sound/battle.wav")
bgm_sound2 = pygame.mixer.Sound("./sound/quiz1.wav")
bgm_sound3 = pygame.mixer.Sound("./sound/quiz2.wav")
bgm_sound4 = pygame.mixer.Sound("./sound/madamada.mp3")
bgm_sound5 = pygame.mixer.Sound("./sound/kakugo.mp3")
bgm_sound.play()

#問題のデータベース。ここから問題がピックアップされて出題される。
questions =[
    {'question':"「洋琴」が表す楽器はどれ?",'kouho':['ピアノ','ギター','チェロ','ハープ'],'answer':"ピアノ",'category':'漢字'},
    {'question':"ラッコを漢字で書くとどれ?",'kouho':['海鼠','海獺','海狸','海犬'],'answer':"海獺",'category':'漢字'},
    {'question':"ガーナの首都は?",'kouho':['アクラ','アピア','アテネ','アンカラ'],'answer':"アクラ",'category':'地理'},
]

this_question = questions[random.randint(0,2)] #questions[0] とかquestions[1]とか
#print(this_question)
#sys.exit( )

while i<3:
    ran = random.randint(1,3)
    if ran ==2 :
        bgm_sound5.play()

    if mhp<=0:
        bgm_sound4.play() 
        print("主人公は敗北した…")
        time.sleep(2)
        break
    elif ehp<=0:
        print("モンスターは倒れた!")
        break
    else:
        """
        s1="漢字出題モンスターが現れた!"
        s2=s1.replace(s1,"")
        print(s1)
        time.sleep(2)
        print(s2)
        """
        #没になった。メモリ内容が変わるだけで、画面に出力したのは変わらない。
        
        
        #print(this_question['category'],end='')
        #print("出題モンスターが現れた!")
        #print("漢字出題モンスターが現れた!")
        print(this_question['category']+"出題モンスターが現れた")#モンスターの名前の表示
        time.sleep(2)
        subprocess.run('CLS',shell=True)#2秒後に消す
        print(this_question['question'])
        #print(this_question['kouho'])
        print(get_answer(this_question))
        """
        for question_kouho in this_question['kouho']:
            print('候補: '+question_kouho)
        """
        
        
    # ans=input()
    # if ans=="A":
    #     bgm_sound2.play()
    #     print("モンスターは30のダメージを受けた!")
    #     #bgm_sound.stop()
    #     ehp=ehp-30
    #     time.sleep(1)
    #     break
    ans=input()
    if ans=="A":
        bgm_sound2.play()
        print("モンスターは30のダメージを受けた!")
        #bgm_sound.stop()
        ehp=ehp-30
        time.sleep(1)
        break
    else:
        bgm_sound3.play()
        print("主人公は30のダメージを受けた!")
        mhp=mhp-30
        time.sleep(1)
    i=i+1
    time.sleep(2)
    subprocess.run('CLS',shell=True)

参考になったサイト:

https://random-tech-note.jp/alphabet/