### ex6-1 ### import tkinter map_data = [ # 用二維 list 定義迷宮 [0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0], [0, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 0], [0, 3, 0, 0, 3, 3, 3, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 0, 0, 3, 1, 1, 3, 0], [0, 3, 2, 2, 3, 0, 0, 3, 2, 2, 3, 0], [0, 3, 0, 0, 3, 1, 1, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 3, 3, 3, 1, 1, 3, 0], [0, 2, 3, 3, 2, 0, 0, 2, 3, 3, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] def draw_screen(): # 繪製遊戲畫面的函式 for y in range(9): for x in range(12): canvas.create_image(x * 60 + 30, y * 60 + 30, image = img_bg[map_data[y][x]]) root = tkinter.Tk() root.title('提心吊膽企鵝迷宮') root.resizable(False, False) canvas = tkinter.Canvas(width = 720, height = 540) canvas.pack() img_bg = [ tkinter.PhotoImage(file = 'images/chip00.png'), tkinter.PhotoImage(file = 'images/chip01.png'), tkinter.PhotoImage(file = 'images/chip02.png'), tkinter.PhotoImage(file = 'images/chip03.png') ] draw_screen() root.mainloop() ### ex6-2 ### import tkinter key = '' koff = False def key_down(e): global key, koff key = e.keysym koff = False def key_up(e): global koff koff = True DIR_UP = 0 # 定義角色方向的變數 (向上) DIR_DOWN = 1 # 定義角色方向的變數 (向下) DIR_LEFT = 2 # 定義角色方向的變數 (向左) DIR_RIGHT = 3 # 定義角色方向的變數 (向右) pen_x = 90 # penpen 的 X 座標 pen_y = 90 # penpen 的 Y 座標 map_data = [ [0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0], [0, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 0], [0, 3, 0, 0, 3, 3, 3, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 0, 0, 3, 1, 1, 3, 0], [0, 3, 2, 2, 3, 0, 0, 3, 2, 2, 3, 0], [0, 3, 0, 0, 3, 1, 1, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 3, 3, 3, 1, 1, 3, 0], [0, 2, 3, 3, 2, 0, 0, 2, 3, 3, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] def draw_screen(): # 繪製遊戲畫面的函式 canvas.delete('SCREEN') # 暫時刪除全部的影像 for y in range(9): for x in range(12): canvas.create_image(x * 60 + 30, y * 60 + 30, image = img_bg[map_data[y][x]], tag = 'SCREEN') # 繪製迷宮 canvas.create_image(pen_x, pen_y, image = img_pen, tag = 'SCREEN') # 繪製 penpen def check_wall(cx, cy, di): # 確認指定方向是否有牆壁的函式 chk = False # 把 False 代入 chk (False 代表能走) if di == DIR_UP: # 向上時 mx = int(cx / 60) # 把調查 map_data 向上的值 my = int((cy - 60) / 60) # 代入 mx 與 my if map_data[my][mx] <= 1: # 如果是牆壁 chk = True # 把 True 帶入 chk (True 代表牆壁,不能走) if di == DIR_DOWN: # 向下時 mx = int(cx / 60) my = int((cy + 60) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_LEFT: # 向左時 mx = int((cx - 60) / 60) my = int(cy / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_RIGHT: # 向右時 mx = int((cx + 60) / 60) my = int(cy / 60) if map_data[my][mx] <= 1: chk = True return chk # 把 chk (能走或不能走) 的值當作回傳值 def move_penpen(): # 移動 penpen 的函式 global pen_x, pen_y if key == 'Up': # 按下向上按鍵時 if check_wall(pen_x, pen_y, DIR_UP) == False: # 如果不是牆壁 pen_y = pen_y - 60 # 減少 Y 座標往上移動 if key == 'Down': # 按下向下按鍵時 if check_wall(pen_x, pen_y, DIR_DOWN) == False: pen_y = pen_y + 60 if key == 'Left': # 按下向左按鍵時 if check_wall(pen_x, pen_y, DIR_LEFT) == False: pen_x = pen_x - 60 if key == 'Right': # 按下向右按鍵時 if check_wall(pen_x, pen_y, DIR_RIGHT) == False: pen_x = pen_x + 60 def main(): global key, koff draw_screen() move_penpen() if koff == True: key = '' koff = False root.after(300, main) root = tkinter.Tk() img_bg = [ tkinter.PhotoImage(file = 'images/chip00.png'), tkinter.PhotoImage(file = 'images/chip01.png'), tkinter.PhotoImage(file = 'images/chip02.png'), tkinter.PhotoImage(file = 'images/chip03.png') ] img_pen = tkinter.PhotoImage(file = 'images/pen03.png') root.title('提心吊膽企鵝迷宮') root.resizable(False, False) root.bind('', key_down) root.bind('', key_up) canvas = tkinter.Canvas(width = 720, height = 540) canvas.pack() main() root.mainloop() ### ex6-3 ### import tkinter key = '' koff = False def key_down(e): global key, koff key = e.keysym koff = False def key_up(e): global koff koff = True DIR_UP = 0 DIR_DOWN = 1 DIR_LEFT = 2 DIR_RIGHT = 3 ANIMATION = [0, 1, 0, 2] # 定義動畫編號的 list tmr = 0 # 計時器 pen_x = 90 pen_y = 90 pen_d = 0 # penpen 的方向 pen_a = 0 # penpen 的影像編號 map_data = [ [0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0], [0, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 0], [0, 3, 0, 0, 3, 3, 3, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 0, 0, 3, 1, 1, 3, 0], [0, 3, 2, 2, 3, 0, 0, 3, 2, 2, 3, 0], [0, 3, 0, 0, 3, 1, 1, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 3, 3, 3, 1, 1, 3, 0], [0, 2, 3, 3, 2, 0, 0, 2, 3, 3, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] def draw_screen(): canvas.delete('SCREEN') for y in range(9): for x in range(12): canvas.create_image(x * 60 + 30, y * 60 + 30, image = img_bg[map_data[y][x]], tag = 'SCREEN') canvas.create_image(pen_x, pen_y, image = img_pen[pen_a], tag = 'SCREEN') # 顯示 penepn,增加 penpen 的動畫 def check_wall(cx, cy, di): chk = False if di == DIR_UP: mx = int(cx / 60) my = int((cy - 60) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_DOWN: mx = int(cx / 60) my = int((cy + 60) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_LEFT: mx = int((cx - 60) / 60) my = int(cy / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_RIGHT: mx = int((cx + 60) / 60) my = int(cy / 60) if map_data[my][mx] <= 1: chk = True return chk def move_penpen(): global pen_x, pen_y, pen_d, pen_a if key == 'Up': # 按下向上鍵時 pen_d = DIR_UP # penpen 朝上 if check_wall(pen_x, pen_y, pen_d) == False: # 如果不是牆壁 pen_y = pen_y - 60 if key == 'Down': # 按下向下鍵時 pen_d = DIR_DOWN if check_wall(pen_x, pen_y, pen_d) == False: pen_y = pen_y + 60 if key == 'Left': # 按下向左鍵時 pen_d = DIR_LEFT if check_wall(pen_x, pen_y, pen_d) == False: pen_x = pen_x - 60 if key == 'Right': # 按下向右鍵時 pen_d = DIR_RIGHT if check_wall(pen_x, pen_y, pen_d) == False: pen_x = pen_x + 60 pen_a = pen_d * 3 + ANIMATION[tmr % 4] # 計算 penpen 的動畫 (影像) 編號 def main(): global key, koff, tmr tmr = tmr + 1 # tmr 的值加 1 draw_screen() # 繪製遊戲畫面 move_penpen() # 移動 penpen if koff == True: key = '' koff = False root.after(300, main) root = tkinter.Tk() img_bg = [ tkinter.PhotoImage(file = 'images/chip00.png'), tkinter.PhotoImage(file = 'images/chip01.png'), tkinter.PhotoImage(file = 'images/chip02.png'), tkinter.PhotoImage(file = 'images/chip03.png') ] img_pen = [ # 載入 penpen 影像的 list tkinter.PhotoImage(file = 'images/pen00.png'), tkinter.PhotoImage(file = 'images/pen01.png'), tkinter.PhotoImage(file = 'images/pen02.png'), tkinter.PhotoImage(file = 'images/pen03.png'), tkinter.PhotoImage(file = 'images/pen04.png'), tkinter.PhotoImage(file = 'images/pen05.png'), tkinter.PhotoImage(file = 'images/pen06.png'), tkinter.PhotoImage(file = 'images/pen07.png'), tkinter.PhotoImage(file = 'images/pen08.png'), tkinter.PhotoImage(file = 'images/pen09.png'), tkinter.PhotoImage(file = 'images/pen10.png'), tkinter.PhotoImage(file = 'images/pen11.png') ] root.title('提心吊膽企鵝迷宮') root.resizable(False, False) root.bind('', key_down) root.bind('', key_up) canvas = tkinter.Canvas(width = 720, height = 540) canvas.pack() main() root.mainloop() ### ex6-4 ### import tkinter key = '' koff = False def key_down(e): global key, koff key = e.keysym koff = False def key_up(e): global koff koff = True DIR_UP = 0 DIR_DOWN = 1 DIR_LEFT = 2 DIR_RIGHT = 3 ANIMATION = [0, 1, 0, 2] tmr = 0 pen_x = 90 pen_y = 90 pen_d = 0 pen_a = 0 map_data = [ [0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0], [0, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 0], [0, 3, 0, 0, 3, 3, 3, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 0, 0, 3, 1, 1, 3, 0], [0, 3, 2, 2, 3, 0, 0, 3, 2, 2, 3, 0], [0, 3, 0, 0, 3, 1, 1, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 3, 3, 3, 1, 1, 3, 0], [0, 2, 3, 3, 2, 0, 0, 2, 3, 3, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] def draw_screen(): canvas.delete('SCREEN') for y in range(9): for x in range(12): canvas.create_image(x * 60 + 30, y * 60 + 30, image=img_bg[map_data[y][x]], tag = 'SCREEN') canvas.create_image(pen_x, pen_y, image = img_pen[pen_a], tag = 'SCREEN') def check_wall(cx, cy, di, dot): # 增加 chk = False if di == DIR_UP: mx = int((cx - 30) / 60) my = int((cy - 30 - dot) / 60) if map_data[my][mx] <= 1: chk = True mx = int((cx + 29) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_DOWN: mx = int((cx - 30) / 60) my = int((cy + 29 + dot) / 60) if map_data[my][mx] <= 1: chk = True mx = int((cx + 29) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_LEFT: mx = int((cx - 30 - dot) / 60) my = int((cy - 30) / 60) if map_data[my][mx] <= 1: chk = True my = int((cy + 29) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_RIGHT: mx = int((cx + 29 + dot) / 60) my = int((cy - 30) / 60) if map_data[my][mx] <= 1: chk = True my = int((cy + 29) / 60) if map_data[my][mx] <= 1: chk = True return chk def move_penpen(): global pen_x, pen_y, pen_d, pen_a if key == 'Up': pen_d = DIR_UP if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_y = pen_y - 20 if key == 'Down': pen_d = DIR_DOWN if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_y = pen_y + 20 if key == 'Left': pen_d = DIR_LEFT if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_x = pen_x - 20 if key == 'Right': pen_d = DIR_RIGHT if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_x = pen_x + 20 pen_a = pen_d * 3 + ANIMATION[tmr % 4] def main(): global key, koff, tmr tmr = tmr + 1 draw_screen() move_penpen() if koff == True: key = '' koff = False root.after(100, main) root = tkinter.Tk() img_bg = [ tkinter.PhotoImage(file = 'images/chip00.png'), tkinter.PhotoImage(file = 'images/chip01.png'), tkinter.PhotoImage(file = 'images/chip02.png'), tkinter.PhotoImage(file = 'images/chip03.png') ] img_pen = [ tkinter.PhotoImage(file = 'images/pen00.png'), tkinter.PhotoImage(file = 'images/pen01.png'), tkinter.PhotoImage(file = 'images/pen02.png'), tkinter.PhotoImage(file = 'images/pen03.png'), tkinter.PhotoImage(file = 'images/pen04.png'), tkinter.PhotoImage(file = 'images/pen05.png'), tkinter.PhotoImage(file = 'images/pen06.png'), tkinter.PhotoImage(file = 'images/pen07.png'), tkinter.PhotoImage(file = 'images/pen08.png'), tkinter.PhotoImage(file = 'images/pen09.png'), tkinter.PhotoImage(file = 'images/pen10.png'), tkinter.PhotoImage(file = 'images/pen11.png') ] root.title('提心吊膽企鵝迷宮') root.resizable(False, False) root.bind('', key_down) root.bind('', key_up) canvas = tkinter.Canvas(width = 720, height = 540) canvas.pack() main() root.mainloop() ### ex6-5 ### import tkinter key = '' koff = False def key_down(e): global key, koff key = e.keysym koff = False def key_up(e): global koff koff = True DIR_UP = 0 DIR_DOWN = 1 DIR_LEFT = 2 DIR_RIGHT = 3 ANIMATION = [0, 1, 0, 2] tmr = 0 score = 0 # 分數 pen_x = 90 pen_y = 90 pen_d = 0 pen_a = 0 map_data = [ [0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0], [0, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 0], [0, 3, 0, 0, 3, 3, 3, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 0, 0, 3, 1, 1, 3, 0], [0, 3, 2, 2, 3, 0, 0, 3, 2, 2, 3, 0], [0, 3, 0, 0, 3, 1, 1, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 3, 3, 3, 1, 1, 3, 0], [0, 2, 3, 3, 2, 0, 0, 2, 3, 3, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] def draw_txt(txt, x, y, siz, col): # 顯示陰影文字的函式 fnt = ('Times New Roman', siz, 'bold') # 顯示字體 canvas.create_text(x + 2, y + 2, text = txt, fill = 'black', font = fnt, tag = 'SCREEN') # 文字的陰影 (偏移 2 像素以黑色顯示) canvas.create_text(x, y, text = txt, fill = col, font = fnt, tag = 'SCREEN') # 用設定的顏色顯示文字 def draw_screen(): canvas.delete('SCREEN') for y in range(9): for x in range(12): canvas.create_image(x * 60 + 30, y * 60 + 30, image = img_bg[map_data[y][x]], tag = 'SCREEN') canvas.create_image(pen_x, pen_y, image = img_pen[pen_a], tag = 'SCREEN') draw_txt('SCORE ' + str(score), 200, 30, 30, 'white') # 顯示分數 def check_wall(cx, cy, di, dot): chk = False if di == DIR_UP: mx = int((cx - 30) / 60) my = int((cy - 30 - dot) / 60) if map_data[my][mx] <= 1: chk = True mx = int((cx + 29) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_DOWN: mx = int((cx - 30) / 60) my = int((cy + 29 + dot) / 60) if map_data[my][mx] <= 1: chk = True mx = int((cx + 29) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_LEFT: mx = int((cx - 30 - dot) / 60) my = int((cy - 30) / 60) if map_data[my][mx] <= 1: chk = True my = int((cy + 29) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_RIGHT: mx = int((cx + 29 + dot) / 60) my = int((cy - 30) / 60) if map_data[my][mx] <= 1: chk = True my = int((cy + 29) / 60) if map_data[my][mx] <= 1: chk = True return chk def move_penpen(): global score, pen_x, pen_y, pen_d, pen_a # 全域變數新增 score if key == 'Up': pen_d = DIR_UP if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_y = pen_y - 20 if key == 'Down': pen_d = DIR_DOWN if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_y = pen_y + 20 if key == 'Left': pen_d = DIR_LEFT if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_x = pen_x - 20 if key == 'Right': pen_d = DIR_RIGHT if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_x = pen_x + 20 pen_a = pen_d * 3 + ANIMATION[tmr % 4] mx = int(pen_x / 60) # 把用來調查 penpen 所在位置的 lisst 值 my = int(pen_y / 60) # 代入 mx 與 my if map_data[my][mx] == 3: # 進入糖果網格後 score = score + 100 # 增加分數 map_data[my][mx] = 2 # 刪除糖果 def main(): global key, koff, tmr tmr = tmr + 1 draw_screen() move_penpen() if koff == True: key = '' koff = False root.after(100, main) root = tkinter.Tk() img_bg = [ tkinter.PhotoImage(file = 'images/chip00.png'), tkinter.PhotoImage(file = 'images/chip01.png'), tkinter.PhotoImage(file = 'images/chip02.png'), tkinter.PhotoImage(file = 'images/chip03.png') ] img_pen = [ tkinter.PhotoImage(file = 'images/pen00.png'), tkinter.PhotoImage(file = 'images/pen01.png'), tkinter.PhotoImage(file = 'images/pen02.png'), tkinter.PhotoImage(file = 'images/pen03.png'), tkinter.PhotoImage(file = 'images/pen04.png'), tkinter.PhotoImage(file = 'images/pen05.png'), tkinter.PhotoImage(file = 'images/pen06.png'), tkinter.PhotoImage(file = 'images/pen07.png'), tkinter.PhotoImage(file = 'images/pen08.png'), tkinter.PhotoImage(file = 'images/pen09.png'), tkinter.PhotoImage(file = 'images/pen10.png'), tkinter.PhotoImage(file = 'images/pen11.png') ] root.title('提心吊膽企鵝迷宮') root.resizable(False, False) root.bind('', key_down) root.bind('', key_up) canvas = tkinter.Canvas(width = 720, height = 540) canvas.pack() main() root.mainloop() ### ex6-6 ### import tkinter import random # 匯入 random 模組 key = '' koff = False def key_down(e): global key, koff key = e.keysym koff = False def key_up(e): global koff koff = True DIR_UP = 0 DIR_DOWN = 1 DIR_LEFT = 2 DIR_RIGHT = 3 ANIMATION = [0, 1, 0, 2] tmr = 0 score = 0 pen_x = 90 pen_y = 90 pen_d = 0 pen_a = 0 red_x = 630 # red 的 X 座標 red_y = 450 # red 的 Y 座標 red_d = 0 # red 的方向 red_a = 0 # red 的影像編號 map_data = [ [0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0], [0, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 0], [0, 3, 0, 0, 3, 3, 3, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 0, 0, 3, 1, 1, 3, 0], [0, 3, 2, 2, 3, 0, 0, 3, 2, 2, 3, 0], [0, 3, 0, 0, 3, 1, 1, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 3, 3, 3, 1, 1, 3, 0], [0, 2, 3, 3, 2, 0, 0, 2, 3, 3, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] def draw_txt(txt, x, y, siz, col): fnt = ('Times New Roman', siz, 'bold') canvas.create_text(x + 2, y + 2, text = txt, fill = 'black', font = fnt, tag = 'SCREEN') canvas.create_text(x, y, text = txt, fill = col, font = fnt, tag = 'SCREEN') def draw_screen(): canvas.delete('SCREEN') for y in range(9): for x in range(12): canvas.create_image(x * 60 + 30, y * 60 + 30, image = img_bg[map_data[y][x]], tag = 'SCREEN') canvas.create_image(pen_x, pen_y, image = img_pen[pen_a], tag = 'SCREEN') canvas.create_image(red_x, red_y, image = img_red[red_a], tag = 'SCREEN') # 顯示 red draw_txt('SCORE ' + str(score), 200, 30, 30, 'white') def check_wall(cx, cy, di, dot): chk = False if di == DIR_UP: mx = int((cx - 30) / 60) my = int((cy - 30 - dot) / 60) if map_data[my][mx] <= 1: chk = True mx = int((cx + 29) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_DOWN: mx = int((cx - 30) / 60) my = int((cy + 29 + dot) / 60) if map_data[my][mx] <= 1: chk = True mx = int((cx + 29) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_LEFT: mx = int((cx - 30 - dot) / 60) my = int((cy - 30) / 60) if map_data[my][mx] <= 1: chk = True my = int((cy + 29) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_RIGHT: mx = int((cx + 29 + dot) / 60) my = int((cy - 30) / 60) if map_data[my][mx] <= 1: chk = True my = int((cy + 29) / 60) if map_data[my][mx] <= 1: chk = True return chk def move_penpen(): global score, pen_x, pen_y, pen_d, pen_a if key == 'Up': pen_d = DIR_UP if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_y = pen_y - 20 if key == 'Down': pen_d = DIR_DOWN if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_y = pen_y + 20 if key == 'Left': pen_d = DIR_LEFT if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_x = pen_x - 20 if key == 'Right': pen_d = DIR_RIGHT if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_x = pen_x + 20 pen_a = pen_d * 3 + ANIMATION[tmr % 4] mx = int(pen_x / 60) my = int(pen_y / 60) if map_data[my][mx] == 3: score = score + 100 map_data[my][mx] = 2 def move_enemy(): # 移動敵人 red 的函式 global red_x, red_y, red_d, red_a # 變成全域變數 speed = 10 # red 的移動速度 if red_x % 60 == 30 and red_y % 60 == 30: # 剛好在網格上時 red_d = random.randint(0, 3) # 隨機改變方向 if red_d == DIR_UP: # red 朝上時 if check_wall(red_x, red_y, red_d, speed) == False: # 如果不是牆壁 red_y = red_y - speed # 減少 Y 座標往上移動 if red_d == DIR_DOWN: # red 朝下時 if check_wall(red_x, red_y, red_d, speed) == False: # 如果不是牆壁 red_y = red_y + speed # 增加 Y 座標往下移動 if red_d == DIR_LEFT: # red 朝左時 if check_wall(red_x, red_y, red_d, speed) == False: # 如果不是牆壁 red_x = red_x - speed # 減少 X 座標往左移動 if red_d == DIR_RIGHT: # red 朝右時 if check_wall(red_x, red_y, red_d, speed) == False: # 如果不是牆壁 red_x = red_x + speed # 增加 X 座標往右移動 red_a = red_d * 3 + ANIMATION[tmr % 4] # 計算 red 的動畫 (影像) 編號 def main(): global key, koff, tmr tmr = tmr + 1 draw_screen() move_penpen() move_enemy() # 移動 red if koff == True: key = '' koff = False root.after(100, main) root = tkinter.Tk() img_bg = [ tkinter.PhotoImage(file = 'images/chip00.png'), tkinter.PhotoImage(file = 'images/chip01.png'), tkinter.PhotoImage(file = 'images/chip02.png'), tkinter.PhotoImage(file = 'images/chip03.png') ] img_pen = [ # 載入 red 影像的 list tkinter.PhotoImage(file = 'images/pen00.png'), tkinter.PhotoImage(file = 'images/pen01.png'), tkinter.PhotoImage(file = 'images/pen02.png'), tkinter.PhotoImage(file = 'images/pen03.png'), tkinter.PhotoImage(file = 'images/pen04.png'), tkinter.PhotoImage(file = 'images/pen05.png'), tkinter.PhotoImage(file = 'images/pen06.png'), tkinter.PhotoImage(file = 'images/pen07.png'), tkinter.PhotoImage(file = 'images/pen08.png'), tkinter.PhotoImage(file = 'images/pen09.png'), tkinter.PhotoImage(file = 'images/pen10.png'), tkinter.PhotoImage(file = 'images/pen11.png') ] img_red = [ tkinter.PhotoImage(file = 'images/red00.png'), tkinter.PhotoImage(file = 'images/red01.png'), tkinter.PhotoImage(file = 'images/red02.png'), tkinter.PhotoImage(file = 'images/red03.png'), tkinter.PhotoImage(file = 'images/red04.png'), tkinter.PhotoImage(file = 'images/red05.png'), tkinter.PhotoImage(file = 'images/red06.png'), tkinter.PhotoImage(file = 'images/red07.png'), tkinter.PhotoImage(file = 'images/red08.png'), tkinter.PhotoImage(file = 'images/red09.png'), tkinter.PhotoImage(file = 'images/red10.png'), tkinter.PhotoImage(file = 'images/red11.png') ] root.title('提心吊膽企鵝迷宮') root.resizable(False, False) root.bind('', key_down) root.bind('', key_up) canvas = tkinter.Canvas(width = 720, height = 540) canvas.pack() main() root.mainloop() ### ex6-7 ### import tkinter import random key = '' koff = False def key_down(e): global key, koff key = e.keysym koff = False def key_up(e): global koff koff = True DIR_UP = 0 DIR_DOWN = 1 DIR_LEFT = 2 DIR_RIGHT = 3 ANIMATION = [0, 1, 0, 2] idx = 0 # 索引 tmr = 0 score = 0 candy = 0 # 每個關卡的糖果數量 pen_x = 0 pen_y = 0 pen_d = 0 pen_a = 0 red_x = 0 red_y = 0 red_d = 0 red_a = 0 map_data = [] def set_stage(): # 設定關卡資料的函式 global map_data, candy # 變成全域變數 map_data = [ # 把迷宮資料放入二維 list 內 [0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0], [0, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 0], [0, 3, 0, 0, 3, 3, 3, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 0, 0, 3, 1, 1, 3, 0], [0, 3, 2, 2, 3, 0, 0, 3, 2, 2, 3, 0], [0, 3, 0, 0, 3, 1, 1, 3, 0, 0, 3, 0], [0, 3, 1, 1, 3, 3, 3, 3, 1, 1, 3, 0], [0, 2, 3, 3, 2, 0, 0, 2, 3, 3, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] candy = 32 # 糖果的數量 def set_chara_pos(): # 設定角色起始位置的函式 global pen_x, pen_y, pen_d, pen_a # 變成全域變數 global red_x, red_y, red_d, red_a pen_x = 90 # 代入 penpen 的 (x, y) 座標 pen_y = 90 pen_d = DIR_DOWN # penpen 朝下 pen_a = 3 # 代入 penpen 的影像編號 red_x = 630 # 代入 red 的 (x, y) 座標 red_y = 450 red_d = DIR_DOWN # red 朝下 red_a = 3 # 代入 red 的影像編號 def draw_txt(txt, x, y, siz, col): fnt = ('Times New Roman', siz, 'bold') canvas.create_text(x + 2, y + 2, text = txt, fill = 'black', font = fnt, tag = 'SCREEN') canvas.create_text(x, y, text = txt, fill = col, font = fnt, tag = 'SCREEN') def draw_screen(): canvas.delete('SCREEN') for y in range(9): for x in range(12): canvas.create_image(x * 60 + 30, y * 60 + 30, image = img_bg[map_data[y][x]], tag = 'SCREEN') canvas.create_image(pen_x, pen_y, image = img_pen[pen_a], tag = 'SCREEN') canvas.create_image(red_x, red_y, image = img_red[red_a], tag = 'SCREEN') draw_txt('SCORE ' + str(score), 200, 30, 30, 'white') def check_wall(cx, cy, di, dot): chk = False if di == DIR_UP: mx = int((cx - 30) / 60) my = int((cy - 30 - dot) / 60) if map_data[my][mx] <= 1: chk = True mx = int((cx + 29) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_DOWN: mx = int((cx - 30) / 60) my = int((cy + 29 + dot) / 60) if map_data[my][mx] <= 1: chk = True mx = int((cx + 29) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_LEFT: mx = int((cx - 30 - dot) / 60) my = int((cy - 30) / 60) if map_data[my][mx] <= 1: chk = True my = int((cy + 29) / 60) if map_data[my][mx] <= 1: chk = True if di == DIR_RIGHT: mx = int((cx+29+dot)/60) my = int((cy - 30) / 60) if map_data[my][mx] <= 1: chk = True my = int((cy + 29) / 60) if map_data[my][mx] <= 1: chk = True return chk def move_penpen(): global score, candy, pen_x, pen_y, pen_d, pen_a # 新增 candy if key == 'Up': pen_d = DIR_UP if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_y = pen_y - 20 if key == 'Down': pen_d = DIR_DOWN if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_y = pen_y + 20 if key == 'Left': pen_d = DIR_LEFT if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_x = pen_x - 20 if key == 'Right': pen_d = DIR_RIGHT if check_wall(pen_x, pen_y, pen_d, 20) == False: pen_x = pen_x + 20 pen_a = pen_d * 3 + ANIMATION[tmr % 4] mx = int(pen_x / 60) my = int(pen_y / 60) if map_data[my][mx] == 3: score = score + 100 map_data[my][mx] = 2 candy = candy - 1 def move_enemy(): global idx, tmr, red_x, red_y, red_d, red_a # 新增 idx, tmr speed = 10 if red_x % 60 == 30 and red_y % 60 == 30: red_d = random.randint(0, 6) if red_d >= 4: # 亂數為 4 以上時 if pen_y < red_y: # penpen 如果在上方 red_d = DIR_UP # red 朝上 if pen_y > red_y: # penpen 如果在下方 red_d = DIR_DOWN # red 朝下 if pen_x < red_x: # penpen 如果在左方 red_d = DIR_LEFT # red 朝左 if pen_x > red_x: # penpen 如果在右方 red_d = DIR_RIGHT # red 朝右 if red_d == DIR_UP: if check_wall(red_x, red_y, red_d, speed) == False: red_y = red_y - speed if red_d == DIR_DOWN: if check_wall(red_x, red_y, red_d, speed) == False: red_y = red_y + speed if red_d == DIR_LEFT: if check_wall(red_x, red_y, red_d, speed) == False: red_x = red_x - speed if red_d == DIR_RIGHT: if check_wall(red_x, red_y, red_d, speed) == False: red_x = red_x + speed red_a = red_d * 3 + ANIMATION[tmr % 4] if abs(red_x - pen_x) <= 40 and abs(red_y - pen_y) <= 40: # 判斷是否與 penpen 接觸 idx = 2 # 如果接觸到,idx 變成 2 tmr = 0 # tmr 變成 0,進行攻擊處理 def main(): global key, koff, idx, tmr, score tmr = tmr + 1 draw_screen() if idx == 0: # idx 為 0 時 (標題畫面) canvas.create_image(360, 200, image = img_title, tag = 'SCREEN') # 顯示標題 LOGO if tmr % 10 < 5: # tmr 除以 10 的餘數不超過 5 draw_txt('Press SPACE !', 360, 380, 30, 'yellow') # 顯示 Press SPACE! if key == 'space': # 按下空白鍵後 score = 0 # 分數變成 0 set_stage() # 設定關卡資料 set_chara_pos() # 每個角色回到起始位置 idx = 1 # idex 變成 1,遊戲開始 if idx == 1: # idx 為 1 時 (遊戲進行中的處理) move_penpen() # 移動 penpen move_enemy() # 移動 red if candy == 0: # 收集到全部的糖果後 idx = 4 # idx 變成 4 tmr = 0 # tmr 變成 0,過關 if idx == 2: # idx 為 2 時 (被敵人攻擊後的處理) draw_txt('GAME OVER', 360, 270, 40, 'red') # 顯示 GAME OVER if tmr == 50: # tmr 變成 50 時 idx = 0 # idx 變成 0,進入標題畫面 if idx == 4: # idx 為 4 時 (過關) draw_txt('STAGE CLEAR', 360, 270, 40, 'pink') # 顯示 STAGE CLEAR if tmr == 50: # tmr 變成 50 時 idx = 0 # idx 變成 0,進入標題畫面 if koff == True: key = '' koff = False root.after(100, main) root = tkinter.Tk() img_bg = [ tkinter.PhotoImage(file = 'images/chip00.png'), tkinter.PhotoImage(file = 'images/chip01.png'), tkinter.PhotoImage(file = 'images/chip02.png'), tkinter.PhotoImage(file = 'images/chip03.png') ] img_pen = [ tkinter.PhotoImage(file = 'images/pen00.png'), tkinter.PhotoImage(file = 'images/pen01.png'), tkinter.PhotoImage(file = 'images/pen02.png'), tkinter.PhotoImage(file = 'images/pen03.png'), tkinter.PhotoImage(file = 'images/pen04.png'), tkinter.PhotoImage(file = 'images/pen05.png'), tkinter.PhotoImage(file = 'images/pen06.png'), tkinter.PhotoImage(file = 'images/pen07.png'), tkinter.PhotoImage(file = 'images/pen08.png'), tkinter.PhotoImage(file = 'images/pen09.png'), tkinter.PhotoImage(file = 'images/pen10.png'), tkinter.PhotoImage(file = 'images/pen11.png') ] img_red = [ tkinter.PhotoImage(file = 'images/red00.png'), tkinter.PhotoImage(file = 'images/red01.png'), tkinter.PhotoImage(file = 'images/red02.png'), tkinter.PhotoImage(file = 'images/red03.png'), tkinter.PhotoImage(file = 'images/red04.png'), tkinter.PhotoImage(file = 'images/red05.png'), tkinter.PhotoImage(file = 'images/red06.png'), tkinter.PhotoImage(file = 'images/red07.png'), tkinter.PhotoImage(file = 'images/red08.png'), tkinter.PhotoImage(file = 'images/red09.png'), tkinter.PhotoImage(file = 'images/red10.png'), tkinter.PhotoImage(file = 'images/red11.png') ] img_title = tkinter.PhotoImage(file = 'images/title.png') # 載入標題 LOGO 的變數 root.title('提心吊膽企鵝迷宮') root.resizable(False, False) root.bind('', key_down) root.bind('', key_up) canvas = tkinter.Canvas(width = 720, height = 540) canvas.pack() set_stage() # 設定關卡資料 set_chara_pos() # 讓每個角色位於起始位置 main() root.mainloop()