RPG (中3男子/作)
講師から一言
主人公の画像を二枚用意してリスト化し、トコトコ歩けるようにすれば、もっと雰囲気がでると思います。棒倒し法のアルゴリズムを習得すればダンジョンも作れるようになります。
Pythonで記述したコード
import tkinter
#マップの描画
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*48+26,y*48+26,image=images[p])
canvas.create_image(man_x*48+26,man_y*48+26,image=images[8],tag="man")
MAX_HEIGHT = 10
MAX_WIDTH = 20
key_flag = False
endd = False
#button_used
def button_used():
canvas.delete("man_e")
canvas.create_image(man_x*48+26,man_y*48+26,image=images[8],tag="man")
map_data[8][7] = 0
canvas.delete("all")
draw_map()
#goal
def goal():
canvas.delete("all")
end = tkinter.Canvas(root,width=960,height=482,bg="black")
end.pack()
end.create_text(400,200,fill="white",font=("SYSTEM",20),
text="""ゴールおめでとう。
だが、君の戦いはまだ始まったばかりだ。
・・・・・・つづく?""")
#メインシステム
def key_down(e):
global key
key = e.keysym
def key_up(e):
global key
key = ""
def main_proc():
global man_x,man_y,key_flag,endd
if endd == False:
#Up
if key =="Up" and map_data[man_y-1][man_x] == 7 or key =="Up" and map_data[man_y-1][man_x] == 6 or key =="Up" and map_data[man_y-1][man_x] == 4 or key =="Up" and map_data[man_y-1][man_x] == 2 or key =="Up" and map_data[man_y-1][man_x] == 0:
man_y = man_y - 1
#Down
if key =="Down" and map_data[man_y+1][man_x] == 4 or key =="Down" and map_data[man_y+1][man_x] == 2 or key =="Down" and map_data[man_y+1][man_x] == 0:
man_y = man_y + 1
#Right
if key =="Right" and map_data[man_y][man_x+1] == 4 or key =="Right" and map_data[man_y][man_x+1] == 2 or key =="Right" and map_data[man_y][man_x+1] == 0 or key =="Right" and map_data[man_y][man_x+1] == 3 or key =="Right" and map_data[man_y][man_x+1] == 11 or key =="Right" and map_data[man_y][man_x+1] == 12:
man_x = man_x + 1
#Left
if key =="Left" and map_data[man_y][man_x-1] == 7 or key =="Left" and map_data[man_y][man_x-1] == 6 or key =="Left" and map_data[man_y][man_x-1] == 4 or key =="Left" and map_data[man_y][man_x-1] == 2 or key =="Left" and map_data[man_y][man_x-1] == 0:
man_x = man_x - 1
#skip
if key =="space":
man_x = 18
man_y = 4
#button
if map_data[man_y][man_x] == 3:
map_data[man_y][man_x] = 12
canvas.delete("man")
canvas.create_image(man_x*48+26,man_y*48+26,image=images[11],tag="man_e")
root.after(300,button_used)
#key収集
if map_data[man_y][man_x] == 6:
key_flag = True
canvas.delete("man")
canvas.create_image(man_x*48+26,man_y*48+26,image=images[4],tag="man_e")
#goal
if map_data[man_y][man_x] == 7:
if key_flag == True:
goal()
endd = True
#grass_field
if map_data[man_y][man_x] == 2 or map_data[man_y][man_x] == 0:
canvas.delete("man")
canvas.create_image(man_x*48+26,man_y*48+26,image=images[8],tag="man")
#sand_field
if map_data[man_y][man_x] == 4 or map_data[man_y][man_x] == 6 or map_data[man_y][man_x] == 7:
canvas.delete("man")
canvas.create_image(man_x*48+26,man_y*48+26,image=images[9],tag="man")
root.after(200,main_proc)
#ウィンドウ作成
root = tkinter.Tk()
root.title("自由制作")
root.minsize(980,502)
root.bind("",key_down)
root.bind("",key_up)
#キャンバス作成
canvas = tkinter.Canvas(root,width=960,height=482)
canvas.place(x=10,y=10)
canvas.create_rectangle(0,0,960,482,fill="gray",tag="draw_field")
#画像データ読み込み
images=[tkinter.PhotoImage(file="images\\mapfield_grass.png"),
tkinter.PhotoImage(file="images\\mapwall_grass.png"),
tkinter.PhotoImage(file="images\\flower_grass.png"),
tkinter.PhotoImage(file="images\\button_grass.png"),
tkinter.PhotoImage(file="images\\mapfield_sand.png"),
tkinter.PhotoImage(file="images\\mapwall_sand.png"),
tkinter.PhotoImage(file="images\\mapkey_sand.png"),
tkinter.PhotoImage(file="images\\mapgoal_sand.png"),
tkinter.PhotoImage(file="images\\mapman_grass.png"),
tkinter.PhotoImage(file="images\\mapman_sand.png"),
tkinter.PhotoImage(file="images\\sea.png"),
tkinter.PhotoImage(file="images\\button_grass_effect.png"),
tkinter.PhotoImage(file="images\\button_grass_used.png"),
tkinter.PhotoImage(file="images\\mapfield_grass_ojama.png")]
map_data = [
[1,0,1,1,1,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],
[1,2,0,1,1,5,7,4,5,5,5,6,4,4,4,5,4,4,4,5],
[1,1,0,1,1,1,5,4,4,4,5,5,5,5,4,4,4,5,4,5],
[1,0,0,0,1,1,5,4,5,4,4,4,4,5,5,5,5,5,4,5],
[1,0,1,0,1,1,1,5,5,4,4,5,4,4,4,4,4,4,4,5],
[1,2,1,0,0,1,1,1,5,5,4,5,5,5,5,4,5,5,4,5],
[1,0,0,1,0,0,1,1,5,5,4,4,5,4,4,4,5,5,4,5],
[10,0,0,1,1,0,1,1,5,5,5,5,4,4,5,5,4,5,4,5],
[10,10,0,3,1,0,0,13,4,4,4,4,4,5,4,4,4,4,4,5],
[10,10,10,10,1,1,1,1,5,5,5,5,5,5,5,5,5,5,5,5]
]
man_x = 1
man_y = 0
key = ""
main_proc()
draw_map()
root.mainloop()