Python 入門指南 5.0
exercise2609.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 | # 引入 Point 定義 from exercise1810 import Point # 引入 Lion 定義 from exercise1812 import Lion # 引入 print_world() 定義 from exercise2607 import print_world # 引入 clear_monitor() 定義 from exercise2604 import clear_monitor # 引入 move() 定義 from exercise2604 import move # 引入 bound_check() 定義 from exercise2605 import bound_check # 引入 animal_check() 定義 from exercise2608 import animal_check # 引入 time import time # 引入 os import os # 從 random 引入 randint from random import randint # 執行部分 if __name__ == '__main__': # 建立動物串列 animals = [Lion(Point(1,1), "Red"), Lion(Point(2,2), "Blue")] # 設定地圖 world = [["口", "口", "口", "口"], ["口", "口", "口", "口"], ["口", "口", "口", "口"], ["口", "口", "口", "口"]] # 清空螢幕 clear_monitor() # 顯示初始位置 print_world(animals, world) # 暫停一秒 time.sleep(1) # 設定移動指令 direction = ["up", "down", "right", "left"] # 移動的迴圈 for i in range(10): for animal in animals: clear_monitor() while True: d = direction[randint(0, 3)] if bound_check(animal, d, world): if animal_check(animal, animals, d, world): move(animal, d) break else: continue else: continue print_world(animals, world) time.sleep(1) # 檔名: exercise2609.py # 說明:《Python入門指南》的練習 # 網站: http://kaiching.org # 作者: 張凱慶 # 時間: 2023 年 10 月 |