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

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

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

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

Q&A

解決済

1回答

1586閲覧

おちものパズルが落ちません

0910pass.py

総合スコア33

Python 3.x

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

0グッド

1クリップ

投稿2019/08/06 19:00

前提・実現したいこと

プログラムの正常な運行

(落ちものパズル)

python3.7でネコのイメージを用いた落ちものパズルを試作しています。(ぷよぷよに似たつくりとなっています))
落ちものパズルとして完成させた後にいざ実行したところ、「次に落っこちてくる筈の一団が落下してこない。」かつ「エラーコードも発生しない」という異常事態が発生しました。

該当のソースコード

python

1import tkinter 2import random 3 4index = 0 5timer = 0 6score = 0 7tsugi = 0 8 9cursor_x = 0 10cursor_y = 0 11mouse_x = 0 12mouse_y = 0 13mouse_c = 0 14 15def mouse_move(e): 16 global mouse_x, mouse_y 17 mouse_e = e.x 18 mouse_y = e.y 19 20def mouse_press(e): 21 global mouse_c 22 mouse_c = 1 23 24neko = [] 25check = [] 26for i in range(10): 27 neko.append([0,0,0,0,0,0,0,0]) 28 check.append([0,0,0,0,0,0,0,0]) 29 30def draw_neko(): 31 cvs.delete("NEKO") 32 for y in range(10): 33 for x in range(8): 34 if neko[y][x] > 0: 35 cvs.create_image(x*72+60, y*72+60, image=img_neko[neko[y][x]], tag="NEKO") 36 37def check_neko(): 38 for y in range(10): 39 for x in range(8): 40 check[y][x] = neko[y][x] 41 42 for y in range(1, 9): 43 for x in range(8): 44 if check[y][x] > 0: 45 if check[y-1][x] == check[y][x] and check[y+1][x] == check[y][x]: 46 neko[y-1][x] = 7 47 neko[y][x] = 7 48 neko[y+1][x] = 7 49 50 for y in range(10): 51 for x in range(1, 7): 52 if check[y][x] > 0: 53 if check[y][x-1] == check[y][x] and check[y][x+1] == check[y][x]: 54 neko[y][x-1] = 7 55 neko[y][x] = 7 56 neko[y][x+1] = 7 57 58 for y in range(1, 9): 59 for x in range(1, 7): 60 if check[y][x] > 0: 61 if check[y-1][x-1] == check[y][x] and check[y+1][x+1] == check[y][x]: 62 neko[y-1][x-1] = 7 63 neko[y][x] = 7 64 neko[y+1][x+1] = 7 65 if check[y+1][x-1] == check[y][x] and check[y-1][x+1] ==check[y][x]: 66 neko[y+1][x-1] = 7 67 neko[y][x] = 7 68 neko[y-1][x+1] = 7 69 70def sweep_neko(): 71 num = 0 72 for y in range(10): 73 for x in range(8): 74 if neko[y][x] == 7: 75 neko[y][x] = 0 76 num = num + 1 77 return num 78 79def drop_neko(): 80 flg = False 81 for y in range(8, -1, -1): 82 for x in range(8): 83 if neko[y][x] != 0 and neko[y+1][x] == 0: 84 neko[y+1][x] = neko[y][x] 85 neko[y][x] = 0 86 flg = True 87 return flg 88 89def over_neko(): 90 for x in range(8): 91 if neko[0][x] > 0: 92 return True 93 return False 94 95def set_neko(): 96 for x in range(8): 97 neko[0][x] = random.randint(0, 6) 98 99def draw_txt(txt, x, y, siz, col, tg): 100 fnt = ("Times New Roman", siz, "bold") 101 cvs.create_text(x+2, y+2, text=txt, fill="black", font=fnt, tag=tg) 102 cvs.create_text(x, y, text=txt, fill=col, font=fnt, tag=tg) 103 104def game_main(): 105 global index, timer, score, tsugi 106 global cursor_x, cursor_y, mouse_c 107 if index == 0: 108 draw_txt("ねこねこ", 312, 240, 100, "violet", "TITLE") 109 draw_txt("Click to start." ,312, 560, 50, "orange", "TITLE") 110 index = 1 111 mouse_c = 0 112 elif index == 1: 113 if mouse_c == 1: 114 for y in range(10): 115 for x in range(8): 116 neko[y][x] = 0 117 mouse_c = 0 118 score = 0 119 tsugi = 0 120 cursor_x = 0 121 cursor_y = 0 122 set_neko() 123 draw_neko() 124 cvs.delete("TITLE") 125 index = 2 126 elif index == 2: 127 if drop_neko() == False: 128 index = 3 129 draw_neko() 130 elif index == 3: 131 check_neko() 132 draw_neko() 133 index = 4 134 elif index == 4: 135 sc = sweep_neko() 136 score = score + sc*10 137 if sc > 0: 138 index = 2 139 else: 140 if over_neko() == False: 141 tsugi = random.randint(1, 6) 142 index = 5 143 else: 144 index = 6 145 timer = 0 146 draw_neko() 147 elif index == 5: 148 if 24 <= mouse_x and mouse_x < 24+72*8 and 24 <= mouse_y and mouse_y < 24+72*10: 149 cursor_x = int((mouse_x-24)/72) 150 cursor_y = int((mouse_y-24)/72) 151 if mouse_c == 1: 152 mouse_c = 0 153 set_neko() 154 neko[cursor_y][cursor_x] = tsugi 155 tsugi = 0 156 index = 2 157 cvs.delete("CURSOR") 158 cvs.create_image(cursor_x*72+60, cursor_y*72+60, image=cursor, tag="CURSOR") 159 draw_neko() 160 elif index == 6: 161 timer = timer + 1 162 if timer == 1: 163 draw_txt("GAME OVER", 312, 348, 60, "red", "OVER") 164 if timer ==50: 165 cvs.delete("OVER") 166 index = 0 167 cvs.delete("INFO") 168 draw_txt("SCORE " +str(score), 160, 60, 32, "blue", "INFO") 169 root.after(100, game_main) 170 171root = tkinter.Tk() 172root.title("落ちものパズル「ねこねこ」") 173root.resizable(False, False) 174root.bind("<Motion>",mouse_move) 175root.bind("<ButtonPress>",mouse_press) 176cvs = tkinter.Canvas(root,width=912,height=768) 177cvs.pack() 178 179bg = tkinter.PhotoImage(file="neko_bg.png") 180cursor = tkinter.PhotoImage(file="neko_cursor.png") 181 182img_neko = [ 183 None, 184 tkinter.PhotoImage(file="neko1.png"), 185 tkinter.PhotoImage(file="neko2.png"), 186 tkinter.PhotoImage(file="neko3.png"), 187 tkinter.PhotoImage(file="neko4.png"), 188 tkinter.PhotoImage(file="neko5.png"), 189 tkinter.PhotoImage(file="neko6.png"), 190 tkinter.PhotoImage(file="neko_niku.png") 191] 192 193cvs.create_image(456, 384, image=bg) 194game_main() 195root.mainloop() 196

試したこと

1時間のプログラム見直し

補足情報

パソコンのスペックは64bit,windows7です。

どなたがご教授ください…!

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

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

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

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

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

stdio

2019/08/07 00:18

print文を使ってしっかりとデバックして下さい。
0910pass.py

2019/08/07 10:35

返信遅れてしまい申し訳ありません! print文を用いてデバッグするという方法があるのですね…。知りませんでした。 コメントありがとうございました!
guest

回答1

0

自己解決

第一に実行したこと

ソースコードをパソコンからスマートフォンに移し確認

変数tsugi の値のprint()による定期的なチェック(stdio様より)

の実行。

###その結果発見されたミスの訂正。

ソースコード
17行目

mouse_e = e.x

mouse_x = e.x

へと訂正。

および169行目へ以下のプログラムの追加。

if tsugi > 0: cvs.create_image(752,128, image=img_neko[tsugi], tag="INFO") root.after(100, game_main)

###結果

プログラムの正常な実行確認される。

###最後に

注意欠陥ミスでした…。

ご指南頂いたstdio様、ありがとうございました!

投稿2019/08/07 10:49

0910pass.py

総合スコア33

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問