まちがいさがし(小6女子/作)
講師から一言
プログラムをざっと、見渡してみての感想ですが、二次元リストをfor文とappend()で作っている所にレベルの高さを感じます。方眼紙1=[]を挿入する場所を間違えると変更する際に他の部分も値が変わってしまいます。
将来は未踏のスーパークリエイターを目指しましょう!才能ありです!
Pythonで記述したコード
import tkinter,pygame
pygame.init()
BGM=pygame.mixer.Sound(file="sound\BGM.wav")
正解=pygame.mixer.Sound(file="sound\正解.wav")
クリア=pygame.mixer.Sound(file="sound\クリア.mp3")
BGM.play(-1)
index=0
key=""
cursor_x=0
cursor_y=0
button=0
count=0
check=0
display=0
def mouse_move(e):
global cursor_x,cursor_y
if 0<=e.x<1200 and 0<=e.y<600:
cursor_x=int(e.x/30)
cursor_y=int(e.y/30)
def key_press(e):
global key
key=e.keysym
def button_press(e):
global button
button=1
def button_release(e):
global button
button=0
def 答え():
global display
canvas.delete("丸")
canvas.delete("question")
canvas.delete("count")
answer.destroy()
canvas.create_image(600,300,image=解答)
display=1
def オープニング():
global index,key
if index==0:
canvas.delete("question")
canvas.delete("txt")
canvas.create_image(600,300,image=question,tag="question")
canvas.create_text(50,400,text="Spaceキーでスタート",font=("メイリオ",85,"bold"),tag="txt",anchor="w",fill="lightgreen")
canvas.create_text(70,200,text="間違い探しゲーム!",font=("メイリオ",85,"bold"),tag="txt",anchor="w",fill="brown")
if key=="space":
index=1
canvas.delete("txt")
if index==1:
answer.place(x=1080,y=600)
loop()
return
root.after(100,オープニング)
def loop():
global button,count,check,display
if 方眼紙2[cursor_y][cursor_x]==1 and button==1:
canvas.create_oval(300,30,360,90,width=2,outline="red",tag="丸")
canvas.create_oval(900,30,960,90,width=2,outline="red",tag="丸")
count+=1
check=1
正解.play()
if 方眼紙2[cursor_y][cursor_x]==2 and button==1:
canvas.create_oval(420,90,480,150,width=2,outline="red",tag="丸")
canvas.create_oval(1020,90,1080,150,width=2,outline="red",tag="丸")
count+=1
check=2
正解.play()
if 方眼紙2[cursor_y][cursor_x]==3 and button==1:
canvas.create_oval(270,270,330,330,width=2,outline="red",tag="丸")
canvas.create_oval(870,270,930,330,width=2,outline="red",tag="丸")
count+=1
check=3
正解.play()
if 方眼紙2[cursor_y][cursor_x]==4 and button==1:
canvas.create_oval(330,240,390,300,width=2,outline="red",tag="丸")
canvas.create_oval(930,240,990,300,width=2,outline="red",tag="丸")
count+=1
check=4
正解.play()
if 方眼紙2[cursor_y][cursor_x]==5 and button==1:
canvas.create_oval(120,300,180,360,width=2,outline="red",tag="丸")
canvas.create_oval(720,300,780,360,width=2,outline="red",tag="丸")
count+=1
check=5
正解.play()
if 方眼紙2[cursor_y][cursor_x]==6 and button==1:
canvas.create_oval(480,420,540,480,width=2,outline="red",tag="丸")
canvas.create_oval(1080,420,1140,480,width=2,outline="red",tag="丸")
count+=1
check=6
正解.play()
if 方眼紙2[cursor_y][cursor_x]==7 and button==1:
canvas.create_oval(210,510,270,570,width=2,outline="red",tag="丸")
canvas.create_oval(810,510,870,570,width=2,outline="red",tag="丸")
count+=1
check=7
正解.play()
if display==0:
canvas.delete("count")
canvas.create_text(20,625,text="間違えの数:7個 "+"見つけた数:"+str(count)+"個",font=("メイリオ",35),fill="orange",anchor="w",tag="count")
if count==7:
canvas.delete("丸")
canvas.delete("question")
canvas.delete("count")
answer.destroy()
canvas.create_image(600,300,image=解答)
canvas.create_text(610,50,text="全問正解!",font=("メイリオ",55),fill="blue")
BGM.stop()
クリア.play()
return
root.after(100,loop)
def 解答済():
if check==1:
#太陽
for y in range(1,3):
for x in range(10,12):
方眼紙2[y][x]="済"
for y in range(1,3):
for x in range(30,32):
方眼紙2[y][x]="済"
if check==2:
#帽子
for y in range(3,4):
for x in range(14,16):
方眼紙2[y][x]="済"
for y in range(4,5):
for x in range(13,15):
方眼紙2[y][x]="済"
for y in range(3,4):
for x in range(34,37):
方眼紙2[y][x]="済"
for y in range(4,5):
for x in range(34,36):
方眼紙2[y][x]="済"
if check==3:
#ウサギの帽子
for y in range(9,11):
for x in range(9,11):
方眼紙2[y][x]="済"
for y in range(9,11):
for x in range(29,31):
方眼紙2[y][x]="済"
if check==4:
#ウサギの耳
for y in range(8,11):
for x in range(11,13):
方眼紙2[y][x]="済"
for y in range(8,11):
for x in range(31,33):
方眼紙2[y][x]="済"
if check==5:
#キツネの手
for y in range(10,12):
for x in range(4,6):
方眼紙2[y][x]="済"
for y in range(10,12):
for x in range(24,26):
方眼紙2[y][x]="済"
if check==6:
#ちょうちょ
for y in range(14,16):
for x in range(16,18):
方眼紙2[y][x]="済"
for y in range(14,16):
for x in range(36,38):
方眼紙2[y][x]="済"
if check==7:
#ねずみ
for y in range(16,19):
for x in range(7,9):
方眼紙2[y][x]="済"
for y in range(16,19):
for x in range(27,29):
方眼紙2[y][x]="済"
root.after(100,解答済)
root=tkinter.Tk()
root.title("間違い探し")
root.geometry("1200x650")
root.resizable(False,False)
canvas=tkinter.Canvas(width=1200,height=650,bg="lightgreen")
canvas.place(x=0,y=0)
question=tkinter.PhotoImage(file="image\問題.png")
解答=tkinter.PhotoImage(file="image\解答.png")
canvas.create_image(600,300,image=question,tag="question")
answer=tkinter.Button(text="答えをみる",font=("メイリオ",14),bg="pink",fg="red",command=答え)
方眼紙2=[]
#二次元リストの作成
for i in range(20):
方眼紙1=[]
for i in range(40):
方眼紙1.append(0)
方眼紙2.append(方眼紙1)
#太陽
for y in range(1,3):
for x in range(10,12):
方眼紙2[y][x]=1
for y in range(1,3):
for x in range(30,32):
方眼紙2[y][x]=1
#帽子
for y in range(3,4):
for x in range(14,16):
方眼紙2[y][x]=2
for y in range(4,5):
for x in range(13,15):
方眼紙2[y][x]=2
for y in range(3,4):
for x in range(34,37):
方眼紙2[y][x]=2
for y in range(4,5):
for x in range(34,36):
方眼紙2[y][x]=2
#ウサギの帽子
for y in range(9,11):
for x in range(9,11):
方眼紙2[y][x]=3
for y in range(9,11):
for x in range(29,31):
方眼紙2[y][x]=3
#ウサギの耳
for y in range(8,11):
for x in range(11,13):
方眼紙2[y][x]=4
for y in range(8,11):
for x in range(31,33):
方眼紙2[y][x]=4
#キツネの手
for y in range(10,12):
for x in range(4,6):
方眼紙2[y][x]=5
for y in range(10,12):
for x in range(24,26):
方眼紙2[y][x]=5
#ちょうちょ
for y in range(14,16):
for x in range(16,18):
方眼紙2[y][x]=6
for y in range(14,16):
for x in range(36,38):
方眼紙2[y][x]=6
#ねずみ
for y in range(16,19):
for x in range(7,9):
方眼紙2[y][x]=7
for y in range(16,19):
for x in range(27,29):
方眼紙2[y][x]=7
#方眼紙の描画(座標チェック用)
##for y in range(20):
## for x in range(40):
## if 方眼紙2[y][x]==0:
## canvas.create_rectangle(x*30,y*30,x*30+30,y*30+30)
## canvas.create_text(x*30+15,y*30+15,text=方眼紙2[y][x],font=("メイリオ",15))
## if 方眼紙2[y][x]==1 or 方眼紙2[y][x]==2 or 方眼紙2[y][x]==3 or 方眼紙2[y][x]==4 or 方眼紙2[y][x]==5 or 方眼紙2[y][x]==6 or 方眼紙2[y][x]==7:
## canvas.create_rectangle(x*30,y*30,x*30+30,y*30+30)
## canvas.create_text(x*30+15,y*30+15,text=方眼紙2[y][x],font=("メイリオ",15),fill="blue")
root.bind("",key_press)
root.bind("",mouse_move)
root.bind("",button_press)
root.bind("",button_release)
オープニング()
解答済()
root.mainloop()