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

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

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

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

Q&A

解決済

1回答

201閲覧

【Python】NameError: name 'collided_right' is not define

born

総合スコア9

Python

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

0グッド

0クリップ

投稿2020/05/17 08:51

前提・実現したいこと

楽しいプログラミング Pythonで始めようを用いて勉強をしているものです。こちらの本の最後の方にあるミスタースティックマンゲームを作成していたところ上記タイトルのようなエラーが出ました。ほとんど完成しており、ゲームのキャラクターは動かせるはずなので、動かしたいです。現状画面にキャラクターが表示されるが操作できないといった感じになっています。

発生している問題・エラーメッセージ

Traceback (most recent call last): File "C:\Users\Name\Desktop\stickman\stickmangame.py", line 258, in <module> g.mainloop() File "C:\Users\Name\Desktop\stickman\stickmangame.py", line 32, in mainloop sprite.move() File "C:\Users\Name\Desktop\stickman\stickmangame.py", line 223, in move if right and self.x > 0 and collided_right(co, sprite_co): NameError: name 'collided_right' is not defined

該当のソースコード

Python

1from tkinter import * 2import random 3import time 4 5class Game: 6 def __init__(self): 7 self.tk = Tk() 8 self.tk.title("ミスタースティックマンの脱出") 9 self.tk.resizable(0, 0) 10 self.tk.wm_attributes("-topmost", 1) 11 self.canvas = Canvas(self.tk, width=500, height=500, highlightthickness=0) 12 self.canvas.pack() 13 self.tk.update() 14 self.canvas_height = 500 15 self.canvas_width = 500 16 17 18 self.bg = PhotoImage(file="background.gif") 19 w = self.bg.width() 20 h = self.bg.height() 21 for x in range(0, 5): 22 for y in range(0, 5): 23 self.canvas.create_image(x * w, y * h, image=self.bg, anchor='nw') 24 self.sprites = [] 25 self.running = True 26 27 28 def mainloop(self): 29 while 1: 30 if self.running == True: 31 for sprite in self.sprites: 32 sprite.move() 33 self.tk.update_idletasks() 34 self.tk.update() 35 time.sleep(0.01) 36 37 38class Coords: 39 def __init__(self, x1=0, y1=0, x2=0, y2=0): 40 self.x1 = x1 41 self.y1 = y1 42 self.x2 = x2 43 self.y2 = y2 44 45def within_x(co1, co2): 46 if (co1.x1 > co2.x1 and co1.x1 < co2.x2)\ 47 or (co1.x2 > co2.x1 and co1.x2 < co2.x2)\ 48 or (co2.x1 > co1.x1 and co2.x1 <co1.x2)\ 49 or (co2.x2 > co1.x1 and co2.x2 < co1.x1): 50 return True 51 else: 52 return False 53 54def within_y(co1, co2): 55 if (co1.y1 > co2.y1 and co1.y1 < co2.y2)\ 56 or (co1.y2 > co2.y1 and co1.y1 < co2.y2)\ 57 or (co2.y1 > co1.y1 and co2.y1 < co1.y2)\ 58 or (co2.y2 > co1.y1 and co2.y2 < co1.y1): 59 return True 60 else: 61 return False 62 63def collided_left(co1, co2): 64 if within_y(co1, co2): 65 if co1.x1 <= co2.x2 and co1.x1 >= co2.x1: 66 return True 67 return False 68 69def collided_top(co1, co2): 70 if within_x(co1, co2): 71 if co1.y1 <= co2.y2 and co1.y1 >= co2.y1: 72 return True 73 return False 74 75def collided_bottom(y, co1, co2): 76 if within_x(co1, co2): 77 y_calc = co1.y2 + y 78 if y_calc >= co2.y1 and y_calc <= co2.y2: 79 return True 80 return False 81 82 83 84class Sprite: 85 def __init__(self, game): 86 self.game = game 87 self.endgame = False 88 self.coordinates = None 89 def move(self): 90 pass 91 def coords(self): 92 return self.coordinates 93 94class PlatformSprite(Sprite): 95 def __init__(self, game, photo_image, x, y, width, height): 96 Sprite.__init__(self,game) 97 self.photo_image = photo_image 98 self.image = game.canvas.create_image(x,y, image=self.photo_image, anchor='nw') 99 self.coordinates = Coords(x, y, x + width, y + height) 100 101 102class StickFigureSprite(Sprite): 103 def __init__(self, game): 104 Sprite.__init__(self, game) 105 self.images_left = [ 106 PhotoImage(file="figure-L1.gif"), 107 PhotoImage(file="figure-L2.gif"), 108 PhotoImage(file="figure-L3.gif") 109 ] 110 self.images_right = [ 111 PhotoImage(file="figure-R1.gif"), 112 PhotoImage(file="figure-R2.gif"), 113 PhotoImage(file="figure-R3.gif") 114 ] 115 self.image = game.canvas.create_image(200, 470, image=self.images_left[0] 116, anchor='nw') 117 self.x = -2 118 self.y = 0 119 self.current_image = 0 120 self.current_image_add = 1 121 self.jump_count = 0 122 self.last_time = time.time() 123 self.coordinates = Coords() 124 game.canvas.bind_all('<KeyPress-Left>', self.turn_left) 125 game.canvas.bind_all('<KeyPress-Right>', self.turn_right) 126 game.canvas.bind_all('<space>', self.jump) 127 128 def turn_left(self, evt): 129 if self.y == 0: 130 self.x = 2 131 132 def turn_right(self, evt): 133 if self.y == 0: 134 self.x = 2 135 136 def jump(self, evt): 137 if self.y == 0: 138 self.y = -4 139 self.jump_count = 0 140 141 def animate(self): 142 if self.x !=0 and self.y == 0: 143 if time.time() - self.last_time > 0.1: 144 self.last_time = time.time() 145 self.current_image += self.current_image_add 146 if self.current_image >= 2: 147 self.current_image_add = -1 148 if self.current_image <= 0: 149 self.current_image_add = 1 150 151 if self.x < 0: 152 if self.y !=0: 153 self.game.canvas.itemconfig(self.image, image=self.images_left[2]) 154 else: 155 self.game.canvas.itemconfig(self.image, image=self.images_left[self.current_image]) 156 elif self.x > 0: 157 if self.y !=0: 158 self.game.canvas.itemconfig(self.image, image=self.images_right[2]) 159 else: 160 self.game.canvas.itemconfig(self.image, image=self.images_right[self.current_image]) 161 162 def coords(self): 163 xy = list(self.game.canvas.coords(self.image)) 164 self.coordinates.x1 = xy[0] 165 self.coordinates.y1 = xy[1] 166 self.coordinates.x2 = xy[0] + 27 167 self.coordinates.y2 = xy[1] + 30 168 return self.coordinates 169 170 171 def move(self): 172 self.animate() 173 if self.y < 0: 174 self.jump_count += 1 175 if self.jump_count > 20: 176 self.y = 4 177 if self.y > 0: 178 self.jump_count -= 1 179 180 co = self.coords() 181 left = True 182 right = True 183 top = True 184 bottom = True 185 falling = True 186 187 if self.y > 0 and co.y2 >= self.game.canvas_height: 188 self.y = 0 189 bottom = False 190 elif self.y < 0 and co.y1 <= 0: 191 self.y = 0 192 top = False 193 194 if self.x > 0 and co.x2 >= self.game.canvas_width: 195 self.x = 0 196 right = False 197 elif self.x < 0 and co.x1 <= 0: 198 self.x = 0 199 left = False 200 201 for sprite in self.game.sprites: 202 if sprite == self: 203 continue 204 sprite_co = sprite.coords() 205 if top and self.y < 0 and collided_top(co, sprite_co): 206 self.y = -self.y 207 top = False 208 209 if bottom and self.y > 0 and collided_bottom(self.y, co, sprite_co): 210 self.y = sprite_co.y1 - co.y2 211 if self.y < 0: 212 self.y = 0 213 bottom = False 214 top = False 215 216 if bottom and falling and self.y == 0 and co.y2 < self.game.canvas_height and collided_bottom(1, co, sprite_co): 217 falling = False 218 219 if left and self.x < 0 and collided_left(co, sprite_co): 220 self.x = 0 221 left = False 222 223 if right and self.x > 0 and collided_right(co, sprite_co): 224 self.x = 0 225 right = False 226 227 if falling and bottom and self.y == 0 and co.y2 < self.game.canvas_height: 228 self.y = 4 229 230 self.game.canvas.move(self.image, self.x, self.y) 231 232 233 234 235g = Game() 236platform1 = PlatformSprite(g, PhotoImage(file="platform1.gif"), 0, 480, 100, 10) 237platform2 = PlatformSprite(g, PhotoImage(file="platform1.gif"), 150, 440, 100, 10) 238platform3 = PlatformSprite(g, PhotoImage(file="platform1.gif"), 300, 400, 100, 10) 239platform4 = PlatformSprite(g, PhotoImage(file="platform1.gif"), 300, 160, 100, 10) 240platform5 = PlatformSprite(g, PhotoImage(file="platform2.gif"), 175, 350, 66, 10) 241platform6 = PlatformSprite(g, PhotoImage(file="platform2.gif"), 50, 300, 66, 10) 242platform7 = PlatformSprite(g, PhotoImage(file="platform2.gif"), 170, 120, 66, 10) 243platform8 = PlatformSprite(g, PhotoImage(file="platform2.gif"), 45, 60, 66, 10) 244platform9 = PlatformSprite(g, PhotoImage(file="platform3.gif"), 170, 250, 32, 10) 245platform10 = PlatformSprite(g, PhotoImage(file="platform3.gif"), 230, 200, 32, 10) 246g.sprites.append(platform1) 247g.sprites.append(platform2) 248g.sprites.append(platform3) 249g.sprites.append(platform4) 250g.sprites.append(platform5) 251g.sprites.append(platform6) 252g.sprites.append(platform7) 253g.sprites.append(platform8) 254g.sprites.append(platform9) 255g.sprites.append(platform10) 256sf = StickFigureSprite(g) 257g.sprites.append(sf) 258g.mainloop() 259 260

試したこと

エラーメッセージを読み、定義されていないとのことだったため、定義とはそもそも何かを調べた。(関数云々のことは出てきましたが、これが今回関係あるのかわからなかった)
著者がネット上にあげているソースコードと見比べて、誤字がないか目視で確認した(こちらも比較ツールがあるとのことでしたので、利用の仕方を調べましたが、よくわかりませんでした。)
テラテイル上で同様の質問がないか調べた。

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

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

メッセージの通り、collided_rightという関数が定義されていないからです。
def collided_leftという行はありますが、def collided_rightという行はありませんよね。
これが書籍と全く同じということであれば、そちらが間違いということになります。

投稿2020/05/17 08:57

x98000

総合スコア1096

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

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

born

2020/05/17 09:00

三度確認したところrightの関数を私が定義し忘れていただけでした...。すみませんでした、もっと確認してから質問をするように気を付けます。迅速な回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問