和菓子タイピング

和菓子タイピング(中1女子/作)
寿司打風のタイピングソフトですね。これを130行で作成している点を評価したいです。

ダラダラ書きがちのプログラムを簡潔にする力はとても重要です。文字入力部分がとても工夫されていますね。

空のリストにappend()でkeysymを代入して、for文でつなげる技術を完全にものにできています。この調子で励んでください。



Pythonで記述したコード
import tkinter,random,pygame

#変数定義
wx=50 #和菓子のx座標
lx=500 #レーンのx座標
tx=250 #お題となる文字のx座標
ty=50 #お題となる文字のx座標
key=""#keysymの管理
表示=""#入力した文字の管理
index=0#進行を管理
n=0#リスト番号の管理
出題フラグ=False#ランダムに出題された問題の管理
score=0#スコア管理
speed=0#お皿のスピード管理

#pygameの初期化
pygame.init()

#音声ファイルの読み込み
BGM=pygame.mixer.Sound("sound\BGM.mp3")
打鍵音=pygame.mixer.Sound("sound\打鍵音.mp3")
入力完了音=pygame.mixer.Sound("sound\入力完了音.mp3")
エンディング=pygame.mixer.Sound("sound\エンディング.mp3")
BGM.set_volume(0.1)
BGM.play(-1)

#ウィンドウ作成
root=tkinter.Tk()
root.title("和菓子タイピング")
root.resizable(False,False)

#キャンバス作成
canvas = tkinter.Canvas(width=1000,height=570)
canvas.pack()

#画像アップロード
オープニング=tkinter.PhotoImage(file="image\オープニング.png")
背景=tkinter.PhotoImage(file="image\背景.png")
レーン=tkinter.PhotoImage(file="image\レーン.png")
おだんご=tkinter.PhotoImage(file="image\おだんご.png")
おはぎ=tkinter.PhotoImage(file="image\おはぎ.png")
カステラ=tkinter.PhotoImage(file="image\カステラ.png")
どらやき=tkinter.PhotoImage(file="image\どらやき.png")
ようかん=tkinter.PhotoImage(file="image\ようかん.png")
桜もち=tkinter.PhotoImage(file="image\桜もち.png")
大福=tkinter.PhotoImage(file="image\大福.png")
今川焼=tkinter.PhotoImage(file="image\今川焼.png")

#リスト作成
和菓子リスト=[おだんご,おはぎ,カステラ,どらやき,ようかん,桜もち,大福,今川焼]
お題リスト=["おだんご","おはぎ","カステラ","どらやき","ようかん","桜もち","大福","今川焼"]
解答リスト=["odango","ohagi","kasutera","dorayaki","youkan","sakuramochi","daihuku","imagawayaki"]

#オープニング画面の表示
canvas.create_image(500,285,image=オープニング,tag="オープニング")

def key_down(e):
    global key,tx,answer,表示,index,score,speed
    打鍵音.play()
    answer=[]#入力した文字を管理するリスト
    key=e.keysym
    answer.append(key)
    for i in answer:#入力した文字をつなげる処理
        if i !="BackSpace":
            表示 +=i
            
    if 表示==解答リスト[n]:#お題の文字と入力した文字が一致したときの処理
        入力完了音.play()
        canvas.delete("moji")#入力した文字の削除
        canvas.delete("和菓子")#和菓子の画像削除
        score+=1
        speed+=10
        index=2
        
    if key=="BackSpace":
        canvas.delete("moji")#入力した文字の削除
        表示=""
        
    canvas.delete("moji")
    canvas.create_text(tx,170,text=表示,font=("Times",60),anchor="w",fill="black",tag="moji") #入力文字
    

def 繰り返し():
    global wx,lx,index,表示,n,出題フラグ
    canvas.delete("オープニング")
    canvas.delete("背景")
    canvas.delete("和菓子")
    canvas.delete("スコア")
    canvas.create_image(500,290,image=背景,tag="背景")
    canvas.create_text(850,30,text="枚数:"+str(score)+"皿",font=("Times",20),anchor="w",fill="brown",tag="スコア")
    
    if index==0:
        if 出題フラグ==False:
            n=random.randint(0,7)
            出題フラグ=True
        canvas.delete("お題")
        canvas.create_text(tx,ty,text=お題リスト[n],font=("Times",60),anchor="w",fill="orange",tag="お題")
        canvas.create_text(tx+10,ty+60,text="("+解答リスト[n]+")",font=("Times",30),anchor="w",fill="orange",tag="お題")

        if wx<900:
            wx+=10+speed#正解するごとにspeedが増していく
        else:#和菓子が端まで行ったときの処理
            エンディング.play()
            canvas.delete("お題")
            canvas.delete("スコア")
            canvas.delete("moji")
            canvas.create_text(300,80,text="終 了",font=("Times",70),anchor="w")
            canvas.create_text(50,170,text="あなたのスコア:"+str(score)+"皿",font=("Times",70),anchor="w",fill="brown")
            return
        
        lx+=5#レーンの速度
        if lx<900:
            canvas.create_image(lx,270,image=レーン,tag="和菓子")
            canvas.create_image(wx,250,image=和菓子リスト[n],tag="和菓子")
        else:
            lx=500

    if index==2:#正解したときの処理
        出題フラグ=False
        wx=50
        lx=500
        canvas.delete("お題")
        canvas.create_text(tx,ty,text=お題リスト[n],font=("Times",60),anchor="w",fill="orange",tag="お題")
        canvas.delete("moji")
        表示=""
        index=0

    root.after(300,繰り返し)

root.after(2000,繰り返し)#2秒後に呼び出すことでオープニング画面を2秒表示させる
root.bind("",key_down)

root.mainloop()