###実現したいこと
ある参考書を使ってコードを書いています。
ダンジョンマップを作成し敵にぶつかるとバトル画面を表示するようにしたいです。
質問になれていないため至らない点もあるかと存じますが、解決法をご教示頂ければ幸いです。
###前提条件
ソースコードファイル名:chap7.py(ダンジョン画面), chap7fight.py(バトル画面の表示)
###発生している問題・エラーメッセージ
chap7.pyでchap7fight.pyがインポート出来ていないようなのですが、import文は記載しています。
原因がわからず困っております。
$ python chap7.py Traceback (most recent call last): File "chap7.py", line 108, in <module> fightManager = chap17fight.FightManager() NameError: name 'chap17fight' is not defined
###該当のソースコード
import tkinter from PIL import Image, ImageTk import chap7fight def draw_map(): for y in range(0,MAX_HEIGHT): for x in range(0,MAX_WIDTH): p=map_data[y][x] canvas.create_image(x*62+31,y*62+31,image=images[p]) canvas.create_image(brave_x*62+31,brave_y*62+31,image=images[4],tag="brave") def check_move(x, y): global brave_x, brave_y, flag_key if x>=0 and x<MAX_WIDTH and y>=0 and y<MAX_HEIGHT: p = map_data[y][x] if p == 1: return elif p== 3: flag_key = True map_data[y][x] = 0 canvas.delete("all") draw_map() elif p==2: if flag_key==True: ending() else: return brave_x = x brave_y = y canvas.coords("brave",brave_x*62+31,brave_y*62+31) # 上ボタンが押された def click_button_up(): check_move(brave_x, brave_y-1) # 下ボタンが押された def click_button_down(): check_move(brave_x, brave_y+1) # 左ボタンが押された def click_button_left(): check_move(brave_x-1, brave_y) # 右ボタンが押された def click_button_right(): check_move(brave_x+1, brave_y) def ending(): canvas.delete("all") canvas.create_rectangle(0,0,620,424,fill="black") canvas.create_text(300,200, fill="white",font=("MS ゴシック",15), text="""Congratulations!! Your adventures has begun now. ... continued??""") button_up["state"] = "disabled" button_down["state"] = "disabled" button_left["state"] = "disabled" button_right["state"] = "disabled" root=tkinter.Tk() root.title("ダンジョン&パイソン") root.minsize(840,454) root.option_add("*font",["メイリオ,14"]) canvas=tkinter.Canvas(root, width=620,height=434) canvas.place(x=10,y=10) canvas.create_rectangle(0,0,620,434,fill="gray",tag="drawField") # ボタン配置 button_up = tkinter.Button(text="↑") button_up.place(x=720, y=150) button_up["command"] = click_button_up button_down = tkinter.Button(text="↓") button_down.place(x=720, y=210) button_down["command"] = click_button_down button_left = tkinter.Button(text="←") button_left.place(x=660, y=180) button_left["command"] = click_button_left button_right = tkinter.Button(text="→") button_right.place(x=780, y=180) button_right["command"] = click_button_right images = [ImageTk.PhotoImage(file="img6/chap6-mapfield.png"), ImageTk.PhotoImage(file="img6/chap6-mapwall.png"), ImageTk.PhotoImage(file="img6/chap6-mapgoal.png"), ImageTk.PhotoImage(file="img6/chap6-mapkey.png"), ImageTk.PhotoImage(file="img6/chap6-mapman.png"),] # マップデータ MAX_WIDTH = 10 MAX_HEIGHT = 7 map_data = [[1, 0, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 1, 2, 0, 0, 1, 3, 1], [1, 1, 0, 1, 1, 1, 0, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] # 主人公の位置 brave_x = 1 brave_y = 0 flag_key = False fightManager = chap17fight.FightManager() draw_map() root.mainloop()
import tkinter from PIL import Image, ImageTk class FightManager: def __init__(self): self.dialog = tkinter.Frame(width=820,height=434) self.dialog.place(x=10,y=10) canvas = tkinter.Canvas(self.dialog,width=820, height = 434) canvas.place(x=0, y=0) canvas.create_rectangle(0, 0, 620, 434, fill="black") winbutton = tkinter.Button(self.dialog, text="You win!") winbutton.place(x=180, y=340) losebutton = tkinter.Button(self.dialog, text="You lose!") losebutton.place(x=320, y=340) self.dialog.place_forget()
###試したこと
- 画像パスの確認
- パイソンのバージョン確認
###補足情報(言語/FW/ツール等のバージョンなど)
エディタ:Visual Studio Code
OS:macOS Sierra
python version:3.6.2
参考図書:12歳からはじめる0からのPython
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/15 10:05