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

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

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

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

489閲覧

53行目のIndexエラーが解決できません。

TakeshiSaito

総合スコア7

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/12/14 01:44

いつもお世話になっております。
教本のコピーをしています。
53行目のインデックスエラーが表示されて色々試しましたが、どうにもなりません。
ご教授いただければ幸いです。
よろしくお願いいたします。

Python

1コード 2import tkinter 3import random 4 5index=0 6timer=0 7score=0 8tsugi=0 9 10cursor_x=0 11cursor_y=0 12mouse_x=0 13mouse_y=0 14mouse_c=0 15 16def mouse_move(e): 17 global mouse_x,mouse_y 18 mouse_x=e.x 19 mouse_y=e.y 20 21def mouse_press(e): 22 global mouse_c 23 mouse_c=1 24 25neko=[] 26check=[] 27for i in range(10): 28 neko.append([0,0,0,0,0,0,0,0]) 29 check.append([0,0,0,0,0,0,0,0]) 30 31def draw_neko(): 32 cvs.delete("NEKO") 33 for y in range(10): 34 for x in range(8): 35 if neko[y][x]>0: 36 cvs.create_image(x*72+60,y*72+60,image=img_neko[neko[y][x]],tag="NEKO") 37 38def check_neko(): 39 for y in range(10): 40 for x in range(8): 41 check[y][x]=neko[y][x] 42 43 for y in range(1,9): 44 for x in range(8): 45 if check[y-1][x]>0: 46 if check[y-1][x]==check[y][x] and check[y+1][x]==check[y][x]: 47 neko[y-1][x]=7 48 neko[y][x]=7 49 neko[y+1][x] 50 51 for y in range(10): 52 for x in range(1,7): 53 if check[y][x]>0: 54 if check[y][x-1]==check[y][x] and [y][x+1]==check[y][x]: 55 neko[y][x-1]=7 56 neko[y][x]=7 57 neko[y][x+1]=7 58 59 for y in range(1,9): 60 for x in range(1,7): 61 if check[y-1][x]>0: 62 if check[y-1][x-1]==check[y][x] and check[y+1][x+1]==check[y][x]: 63 neko[y-1][x-1]=7 64 neko[y][x]=7 65 neko[y+1][x]=7 66 if check[y+1][x-1]==check[y][x] and check[y-1][x+1]==check[y][x]: 67 neko[y+1][x-1]=7 68 neko[y][x]=7 69 neko[y-1][x+1]=7 70 71def sweep_neko(): 72 num=0 73 for y in range(10): 74 for x in range(8): 75 if neko[y][x]==7: 76 neko[y][x]=0 77 num=num+1 78 return num 79 80def drop_neko(): 81 flg=False 82 for y in range(8,-1,-1): 83 for x in range(8): 84 if neko[y][x] !=0 and neko[y+1][x]==0: 85 neko[y+1][x]=neko[y][x] 86 neko[y][x]=0 87 flg=True 88 return flg 89 90def over_neko(): 91 for x in range(8): 92 if neko[0][x]>0: 93 return True 94 return False 95 96def set_neko(): 97 for x in range(8): 98 neko[0][x]=random.randint(0,6) 99 100def draw_txt(txt,x,y,siz,col,tg): 101 fnt=("Times New Roman",siz,"bold") 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 if tsugi>0: 170 cvs.create_image(752,128,image=img_neko[tsugi],tag="INFO") 171 root.after(100,game_main) 172 173root=tkinter.Tk() 174root.title("落ち物パズル「ネコネコ」") 175root.resizable(False,False) 176root.bind("<Motion>",mouse_move) 177root.bind("<ButtonPress>",mouse_press) 178cvs=tkinter.Canvas(root,width=912,height=768) 179cvs.pack() 180 181bg=tkinter.PhotoImage(file="neko_bg.png") 182cursor=tkinter.PhotoImage(file="neko_cursor.png") 183img_neko=[ 184 None, 185 tkinter.PhotoImage(file="neko1.png"), 186 tkinter.PhotoImage(file="neko2.png"), 187 tkinter.PhotoImage(file="neko3.png"), 188 tkinter.PhotoImage(file="neko4.png"), 189 tkinter.PhotoImage(file="neko5.png"), 190 tkinter.PhotoImage(file="neko6.png"), 191 tkinter.PhotoImage(file="neko_niku.png") 192] 193 194cvs.create_image(456,384,image=bg) 195game_main() 196root.mainloop() 197 198 199 200 201 202 203 204

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

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

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

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

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

1T2R3M4

2021/12/14 02:02

調べたこと、試したことを質問に追記していただけませんか。
退会済みユーザー

退会済みユーザー

2021/12/14 02:09

> 53行目のIndexエラーが解決できません 行番号が振ってないのでどの行だか閲覧者には分からないということが分かりませんか? まさか上から数えろとか言ってないですよね。
1T2R3M4

2021/12/14 02:11

File "plt_neko.py", line 53, in check_neko if check[y][x-1]==check[y][x] and [y][x+1]==check[y][x]: IndexError: list index out of range エラーメッセージちゃんと読んでますか。
guest

回答1

0

自己解決

問題の行を再度新たに入力し直したら、エラーが出なくなりました。
ありがとうございました。

投稿2021/12/14 03:31

TakeshiSaito

総合スコア7

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問