Python 入門指南 5.0

exercise4409.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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
# 從 Brython 引入 document 並以 doc 為別名
from browser import document as doc
# 引入 Point 定義
from exercise1810 import Point
# 引入 Lion 定義
from exercise1812 import Lion
# 引入 Elephant 定義
from exercise2901 import Elephant
# 引入 Cat 定義
from exercise2902 import Cat
# 引入 Mouse 定義
from exercise2903 import Mouse
# 引入 move() 定義
from exercise2807 import move
# 引入 bound_check() 定義
from exercise2605 import bound_check
# 引入 animal_check() 定義
from exercise2809 import animal_check
# 引入 get_world_string() 定義
from exercise2906 import get_world_string

# 遊戲資料
user = Mouse(Point(3,0), "鼠")
animals = {}
animals["鼠"] = user
animals["象"] = Elephant(Point(0,0), "象")
animals["獅"] = Lion(Point(0,3), "獅")
animals["貓"] = Cat(Point(3,3), "貓")
world = [["口", "口", "口", "口"],
         ["口", "口", "口", "口"],
         ["口", "口", "口", "口"],
         ["口", "口", "口", "口"]]

# 印出鬥獸棋棋盤
def print_world(animals_list, world):
    doc["display"].clear()
    temp_list = get_world_string(animals_list, world).split("\n")
    for temp in temp_list:
        d = doc.createElement("div")
        d.style.border = "none"
        d.style.padding = "0"
        d.style.margin = "0"
        d.style.fontSize = "3em"
        d.text = temp
        doc["display"] <= d

# 初始設定
doc["button_up"].disabled = True
doc["button_left"].disabled = True
doc["button_right"].disabled = True
doc["button_down"].disabled = True
print_world(list(animals.values()), world)

# 進行動物移動
def animal_move(d, animal):
    # 宣告全域變數
    global animals, world
    # 進行移動
    if bound_check(animal, d, world):
        other = animal_check(animal, list(animals.values()), d, world)
        if other == "口":
            move(animal, d)
        else:
            pass
    # 印出地圖
    print_world(list(animals.values()), world)

# 進行按鈕上
def method_up(event):
    animal_move("up", user)

# 進行按鈕下
def method_down(event):
    animal_move("down", user)

# 進行按鈕左
def method_left(event):
    animal_move("left", user)

# 進行按鈕右
def method_right(event):
    animal_move("right", user)

# 啟動遊戲
def start_game(event):
    doc["button_up"].disabled = False
    doc["button_left"].disabled = False
    doc["button_right"].disabled = False
    doc["button_down"].disabled = False
    doc["run"].disabled = True

# 按鈕註冊事件
doc["button_up"].bind("click", method_up)
doc["button_down"].bind("click", method_down)
doc["button_left"].bind("click", method_left)
doc["button_right"].bind("click", method_right)
doc["run"].bind("click", start_game)

# 檔名: exercise4409.py
# 說明:《Python入門指南》的練習
# 網站: http://kaiching.org
# 作者: 張凱慶
# 時間: 2023 年 12 月

回到練習題目
上一頁 exercise4408.py
回 Python 入門指南 5.0 首頁
下一頁 exercise4410.py
回 Python 教材首頁
回程式語言教材首頁