質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

2回答

959閲覧

パズルゲームがスタートしません。

maffiy

総合スコア4

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/12/15 04:38

編集2019/12/15 06:04

前提・実現したいこと

プログラムの正常な運行

(落ちものパズル)

python3.7でネコのイメージを用いた落ちものパズルを試作しています。(ぷよぷよに似たつくりとなっています)
落ちものパズルとして完成させた後にいざ実行したところ、タイトル画面をクリックすると始まるはずが、クリックしても何もおこりません。かつ「エラーコードも発生しない」という異常事態が発生しました。

該当のソースコード

import tkinter import random index=0 timer=0 score=0 tsugi=0 cursor_x=0 cursor_y=0 mouse_x=0 mouse_y=0 mouse_c=0 def mouse_move(e): global mouse_x,mouse_y mouse_x=e.x mouse_y=e.y def mouse_press(e): global mouse_c mouse_c=1 neko=[] check=[] for i in range(10): neko.append([0,0,0,0,0,0,0,0]) check.append([0,0,0,0,0,0,0,0]) def draw_neko(): cvs.delete("NEKO") for y in range(10): for x in range(8): if neko[y][x]>0: cvs.create_image(x*72+60,y*72+60,image=img_neko[neko[y][x]],tag="NEKO") def check_neko(): for y in range(10): for x in range(8): check[y][x]=neko[y][x] for y in range(1,9): for x in range(8): if check[y][x]>0: if check[y-1][x]==check[y][x] and check[y+1][x]==check[y][x]: neko[y-1][x]=7 neko[y][x]=7 neko[y+1][x]=7 for y in range(10): for x in range(1,7): if check[y][x]>0: if check[y][x-1]==check[y][x] and check[y][x+1] ==check[y][x]: neko[y][x-1]=7 neko[y][x]=7 neko[y][x+1]=7 for y in range(1,9): for x in range(1,7): if check[y][x]>0: if check[y-1][x-1]==check[y][x] and check[y+1][x+1]==check[y][x]: neko[y-1][x-1]=7 neko[y][x]=7 neko[y+1][x+1]=7 if check[y+1][x-1]==check[y][x] and check[y-1][x+1]==check[y][x]: neko[y+1][x-1]=7 neko[y][x]=7 neko[y-1][x+1]=7 def sweep_neko(): num=0 for y in range(10): for x in range(8): if neko[y][x]==7: neko[y][x]=0 num=num+1 return num def drop_neko(): flg=False for y in range(8,-1,-1): for x in range(8): if neko[y][x]!=0 and neko[y+1][x]==0: neko[y+1][x]=neko[y][x] neko[y][x]=0 flg=True return flg def over_neko(): for x in range(8): if neko[0][x]>0: return True return False def set_neko(): for x in range(8): neko[0][x]=random.randint(0,6) def draw_txt(txt,x,y,siz,col,tg): fnt=("Time New Roman",siz,"bold") cvs.create_text(x+2,y+2,text=txt,fill="black",font=fnt,tag=tg) cvs.create_text(x,y,text=txt,fill=col,font=fnt,tag=tg) def game_main(): global index,timer,score,tsugi global cursor_x,cursor_y,mouse_c if index==0:#タイトルロゴ draw_txt("ねこねこ",312,240,100,"violet","TITLE") draw_txt("Click to start.",312,560,50,"orange","TITLE") index=1 mouse_c=1 elif index==1:# タイトル画面スタート待ち #if mouse_c==1: for y in range(10): for x in range(8): neko[y][x]=0 mouse_c=0 score=0 tsugi=0 cursor_x=0 cursor_y=0 set_neko() draw_neko() cvs.delete("TITLE") index=2 elif index==2:# 落下 if drop_neko()==False: index=3 draw_neko() elif index==3:# 揃ったか check_neko() draw_neko() index=4 elif index==4:# 揃ったネコがあれば消す sc=sweep_neko() score=score+sc*10 if sc>0: index=2 else: if over_neko()==False: tsugi=random.randint(1,6) index=5 else: index=6 timer=0 draw_neko() elif index==5:# マウス入力を待つ if 24 <= mouse_x and mouse_x < 24+72*8 and 24<= mouse_y and mouse_y<24+72*10: cursor_x=int((mouse_x-24)/72) cursor_y=int((mouse_y-24)/72) if mouse_c==1: mouse_c=0 set_neko() neko[cursor_y][cursor_x]=tsugi tsugi=0 index=2 cvs.delete("CURSOR") cvs.create_image(cursor_x*72+60,cursor_y*72+60,image=cursor,tag="CURSOR") draw_neko() elif index==6:# ゲームオーバー timer=timer+1 if timer==1: draw_txt("GAME OVER",312,348,60,"red","over") if timer==50: cvs.delete("OVER") index=0 cvs.delete("INFO") draw_txt("SCORE"+str(score),160,60,32,"blue","INFO") if tsugi>0: cvs.create_image(752,128,image=img_neko[tsugi],tag="INFO") root.after(100,game_main) root=tkinter.Tk() root.title("落ち物パズル「ねこねこ」") root.resizable(False,False) root.bind("<Motion>",mouse_move) root.bind("<ButtonPress>",mouse_press) cvs=tkinter.Canvas(root,width=912,height=768) cvs.pack() bg=tkinter.PhotoImage(file="neko_bg.png") cursor=tkinter.PhotoImage(file="neko_cursor.png") img_neko=[ None, tkinter.PhotoImage(file="neko1.png"), tkinter.PhotoImage(file="neko2.png"), tkinter.PhotoImage(file="neko3.png"), tkinter.PhotoImage(file="neko4.png"), tkinter.PhotoImage(file="neko5.png"), tkinter.PhotoImage(file="neko6.png"), tkinter.PhotoImage(file="neko_niku.png") ] cvs.create_image(456,384,image=bg) game_main() root.mainloop()

試したこと

3時間ほどプログラムの見直し

補足情報(FW/ツールのバージョンなど)

ご教授頂けると誠に助かります。
宜しくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

index == 0 の時、mouse_cをまた0に戻してるので、以下のhereの箇所をmouse_c=1に変更する必要があります。
すいません、関係ないかも?

if index==0:#タイトルロゴ draw_txt("ねこねこ",312,240,100,"violet","TITLE") draw_txt("Click to start.",312,560,50,"orange","TITLE") index=1 mouse_c=1 // here mouse_cが0のままのため、index == 1でも2つ目のifを通らない if index == 1: if mouse_c==1:

追記
以下の箇所の、if mouse_c==1:を外して動作するか確認してみて下さい。

elif index==1:# タイトル画面スタート待ち # このifをコメントにしてインデント直す # if mouse_c==1:

投稿2019/12/15 04:47

編集2019/12/15 05:33
madone99

総合スコア1855

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maffiy

2019/12/15 04:59

ご回答ありがとうございます。 つまり、どの部分を改善すれば宜しいのでしょうか? 初歩的な質問ですいません。
madone99

2019/12/15 05:03 編集

提示したコードのhereと書いてある箇所を直してみて下さい。 mouse_c=0 を 1 にしています。
maffiy

2019/12/15 05:13

mouse_c=1にしてみたんですが、まだ作動しません。 他に原因があるのでしょうか?
madone99

2019/12/15 05:23

回答に追記しましたので、該当箇所をコメントにするか 削除してみて確認して下さい。
maffiy

2019/12/15 05:32

index=1 mouse_c=1 elif index==1:# タイトル画面スタート待ち #if mouse_c==1: 今このように改善したんですが、作動しません。
maffiy

2019/12/15 05:37

index=1 mouse_c=1 elif index==1:# タイトル画面スタート待ち #if mouse_c==1: 今このように改善したのですが、まだ作動しません。
madone99

2019/12/15 05:40

if index == 1: 以降の部分のインデントは直していますか? for y in range(10): for x in range(8): neko[y][x]=0 mouse_c=0 score=0 tsugi=0 cursor_x=0 cursor_y=0 set_neko() draw_neko() cvs.delete("TITLE") index=2
maffiy

2019/12/15 05:52 編集

if index==0:#タイトルロゴ draw_txt("ねこねこ",312,240,100,"violet","TITLE") draw_txt("Click to start.",312,560,50,"orange","TITLE") index=1 mouse_c=1 elif index==1:# タイトル画面スタート待ち #if mouse_c==1: for y in range(10): for x in range(8): neko[y][x]=0 mouse_c=0 score=0 tsugi=0 cursor_x=0 cursor_y=0 set_neko() draw_neko() cvs.delete("TITLE") index=2 このような状態です。インデントは直しました。
madone99

2019/12/15 05:53

コメント部だとインデントがあっているか分からないので、 質問を編集して変更後のコードをご提示ください。 この状態でもエラーはないでしょうか?
maffiy

2019/12/15 06:05

質問を編集しました。 この状態でもエラーは起きていません。
madone99

2019/12/15 06:09

今表示されているのは、タイトルロゴですか? タイトル画面スタート待ちの画面ですか?
maffiy

2019/12/15 06:16

恐らくタイトル画面スタート待ちの画面だと思います。 正常であればクリックしたらゲームが始まる予定です。
madone99

2019/12/15 06:33

ずっとタイトルロゴとばかり思ってました。 if index == 0: のブロックは関係ないですね。 そうするとindexが2以外ということですね。 一番最初の状態に戻して def game_main()内に print(index) を追加して、マウスクリック時にindexが遷移しているか を確認していくと良いのではないでしょうか。
maffiy

2019/12/15 06:59

ありがとうございます。引き続き頑張ってみます。
madone99

2019/12/15 07:26

そのままだとtkinterのPhotoImageでエラーでませんか? どういう環境で動作させておりますでしょう? Win10 PyCharmで実行したら、PhotoImageのimportで引っかかりましたが そこを解消したらindex == 2も普通に進むことができました。
guest

0

ここのif文を

python

1 if timer==50: 2 cvs.delete("OVER") 3 index=0 4 cvs.delete("INFO") 5 draw_txt("SCORE"+str(score),160,60,32,"blue","INFO") 6 if tsugi>0: 7 cvs.create_image(752,128,image=img_neko[tsugi],tag="INFO") 8 root.after(100,game_main)

以下の書き方で進むかと

python

1 if timer==50: 2 cvs.delete("OVER") 3 index=0 4 cvs.delete("INFO") 5 draw_txt("SCORE"+str(score),160,60,32,"blue","INFO") 6 if tsugi>0: 7 cvs.create_image(752,128,image=img_neko[tsugi],tag="INFO") 8 root.after(100,game_main)

投稿2020/12/17 16:08

akiakane

総合スコア4

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問