Python 入門指南 5.0
exercise2605.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | # 引入 Point 定義 from exercise1810 import Point # 引入 Lion 定義 from exercise1812 import Lion # 引入 print_world() 定義 from exercise2602 import print_world # 引入 pclear_monitor() 定義 from exercise2604 import clear_monitor # 引入 move() 定義 from exercise2604 import move # 引入 time import time # 引入 os import os # 確認是否出界 def bound_check(animal, direction, world): # 設定邊界值 min_x = 0 max_x = len(world[min_x]) - 1 min_y = 0 max_y = len(world) - 1 # 回傳結果 match direction: case "up": if animal.point.y - 1 < min_y: return False case "down": if animal.point.y + 1 > max_y: return False case "right": if animal.point.x + 1 > max_x: return False case "left": if animal.point.x - 1 < min_x: return False return True # 執行部分 if __name__ == '__main__': # 建立獅子物件 lion = Lion(Point(1,1), "Red") # 設定地圖 world = [["口", "口", "口", "口"], ["口", "口", "口", "口"], ["口", "口", "口", "口"], ["口", "口", "口", "口"]] # 清空螢幕 clear_monitor() # 印出地圖 print_world(lion, world) # 設定移動指令 direction = ["up", "up", "right"] # 移動的迴圈 m = "" for d in direction: time.sleep(1) clear_monitor() if bound_check(lion, d, world): move(lion, d) print_world(lion, world) else: print("撞牆中") # 檔名: exercise2605.py # 說明:《Python入門指南》的練習 # 網站: http://kaiching.org # 作者: 張凱慶 # 時間: 2023 年 10 月 |