-----------------------------------------
圖片放大特效
table樣式
 <style scoped> img[alt="Alt1"]{ background: rgba(255, 255, 255, 0); position: absolute; top: 10%; left: 10%; /* transform: translate(-50%, -50%); */ width: 200px; /* 調整圖片寬度 */ height: auto; /* 保持原始比例 */ padding:0; margin:0; z-index: 1; } img[alt="Alt2"]{ background: rgba(255, 255, 255, 0); position: absolute; top: 10%; left: 75%; /* transform: translate(-50%, -50%); */ width: 200px; /* 調整圖片寬度 */ height: auto; /* 保持原始比例 */ padding:0; margin:0; z-index: 2; } </style>
- [選項一:基礎練習](#選項一基礎練習-適合初學者) - [選項二:進階應用](#選項二進階應用-有點挑戰性) - [選項三:創意發揮](#選項三創意發揮-自由度高)
--- #### 💡 生活範例 - 計算平均分數: ```python print("📊 計算學生的平均分數") # 初始化變數 total_score = 0 student_count = 0 # 使用while迴圈輸入分數 while True: score = input("請輸入學生分數 (輸入'q'結束): ") if score.lower() == 'q': # 結束輸入 break try: score = float(score) total_score += score student_count += 1 except ValueError: print("⚠️ 請輸入有效的數字!") # 計算平均分數 if student_count > 0: average_score = total_score / student_count print(f"學生總數: {student_count}") print(f"平均分數: {average_score:.2f}") else: print("未輸入任何分數!") ```
--- # 小練習 3 #### 🧮 練習題目:費氏數列生成器 請寫一個程式,生成指定數量的費氏數列。 ```python # 你的程式碼在這裡 n = int(input("請輸入要生成的費氏數列項數: ")) # 提示:使用while迴圈 # a, b = 0, 1 # count = 0 # while count < n: # print(a, end=" ") # a, b = b, a + b # count += 1 ``` #### 💭 思考時間: - 費氏數列的遞推公式是什麼? - 如果輸入0或負數會發生什麼?
```python # 你的程式碼在這裡 start = int(input("請輸入倒數開始的秒數: ")) # 提示:使用while迴圈 # while start > 0: # print(f"剩餘: {start} 秒") # start -= 1 # print("倒數結束!🎉") ```
--- # 小練習 2 #### 🎲 練習題目:寶物篩選系統 建立一個程式來篩選有價值的寶物: ```python all_items = ["石頭", "金幣", "樹枝", "銀幣", "垃圾", "鑽石", "破布", "紅寶石"] valuable_items = ["金幣", "銀幣", "鑽石", "紅寶石"] # 任務1:使用for迴圈找出所有有價值的物品 print("🔍 搜尋有價值的寶物...") my_treasures = [] # 你的程式碼: # for item in all_items: # if item in valuable_items: # my_treasures.append(item) # print(f"發現寶物: {item} ✨") # 任務2:計算寶物價值 values = {"金幣": 100, "銀幣": 50, "鑽石": 500, "紅寶石": 300} total_value = 0 # 你的程式碼: # for treasure in my_treasures: # total_value += values[treasure] print(f"總價值: {total_value} 金幣 💰") ```
# 尋寶冒險遊戲實作 現在讓我們整合所有學過的迴圈技巧,打造一個簡單的尋寶遊戲!🎮 --- #### 🎯 遊戲設計架構: ```python import random class TreasureHunter: def __init__(self): self.player_name = "" self.health = 100 self.score = 0 self.inventory = [] self.game_running = True def start_game(self): print("🏴☠️ 歡迎來到尋寶冒險世界!") self.player_name = input("請輸入你的冒險者名字: ") print(f"歡迎,勇敢的 {self.player_name}!") # 遊戲主循環 while self.game_running and self.health > 0: self.show_status() self.explore_area() if self.score >= 1000: print("🎉 恭喜!你已經成為傳奇尋寶獵人!") break def show_status(self): print(f"\n📊 === {self.player_name} 的狀態 ===") print(f"❤️ 生命值: {self.health}") print(f"⭐ 分數: {self.score}") print(f"🎒 背包: {self.inventory}") ``` --- # while迴圈製作遊戲主循環 使用while迴圈打造遊戲的核心邏輯: ```python def explore_area(self): print("\n🌍 你來到了一個神秘區域...") # 隨機事件列表 events = ["寶箱", "怪物", "商人", "陷阱", "神秘泉水"] event = random.choice(events) if event == "寶箱": self.find_treasure() elif event == "怪物": self.fight_monster() elif event == "商人": self.meet_merchant() elif event == "陷阱": self.encounter_trap() elif event == "神秘泉水": self.find_spring() # 詢問是否繼續冒險 choice = input("\n🤔 要繼續探險嗎?(y/n): ").lower() if choice == 'n': self.game_running = False print("感謝遊玩!下次見!👋") def find_treasure(self): treasures = [ ("金幣", 50), ("銀幣", 25), ("鑽石", 200), ("紅寶石", 150), ("魔法書", 300), ("神秘藥水", 100) ] treasure, value = random.choice(treasures) print(f"✨ 你發現了 {treasure}!獲得 {value} 分!") self.inventory.append(treasure) self.score += value ``` --- # for迴圈處理道具清單 使用for迴圈來管理遊戲中的各種物品和操作: ```python def fight_monster(self): monsters = ["哥布林", "骷髏兵", "野狼", "黑暗法師"] monster = random.choice(monsters) print(f"⚔️ 遭遇 {monster}!準備戰鬥!") # 戰鬥回合 for round_num in range(1, 4): print(f"\n🥊 第 {round_num} 回合") # 玩家可選擇的行動 actions = ["攻擊", "防守", "使用道具"] print("選擇你的行動:") for i, action in enumerate(actions, 1): print(f"{i}. {action}") choice = input("請選擇 (1-3): ") if choice == "1": # 攻擊 damage = random.randint(15, 30) print(f"💥 你對 {monster} 造成 {damage} 點傷害!") if random.random() > 0.3: # 70%機率勝利 print(f"🎉 你擊敗了 {monster}!獲得經驗值!") self.score += 75 return elif choice == "2": # 防守 print("🛡️ 你選擇防守,減少傷害!") damage = random.randint(5, 15) else: # 使用道具 self.use_item_in_battle() continue # 怪物反擊 self.health -= damage print(f"💔 你受到 {damage} 點傷害!") if self.health <= 0: print("💀 你被擊敗了!遊戲結束!") return print("😰 戰鬥結束,你成功逃脫了!") ``` --- # 計分系統與遊戲存檔 實作完整的計分系統和存檔功能: ```python def calculate_final_score(self): """計算最終分數""" base_score = self.score health_bonus = self.health * 2 # 生命值獎勵 inventory_bonus = len(self.inventory) * 25 # 道具獎勵 # 特殊道具額外獎勵 special_items = ["魔法書", "神秘藥水", "鑽石"] special_bonus = 0 for item in self.inventory: if item in special_items: special_bonus += 100 total_score = base_score + health_bonus + inventory_bonus + special_bonus print(f"\n🏆 === 最終計分 ===") print(f"基礎分數: {base_score}") print(f"生命值獎勵: {health_bonus}") print(f"道具獎勵: {inventory_bonus}") print(f"特殊道具獎勵: {special_bonus}") print(f"總分: {total_score}") return total_score def save_game_record(self): """儲存遊戲記錄""" final_score = self.calculate_final_score() # 判定等級 if final_score >= 1500: rank = "傳奇尋寶大師 🏆" elif final_score >= 1000: rank = "專業探險家 ⭐" elif final_score >= 500: rank = "新手冒險者 🌟" else: rank = "初學者 🔰" print(f"\n🎖️ 你的冒險等級: {rank}") # 簡單的「存檔」功能 game_record = { "玩家": self.player_name, "分數": final_score, "等級": rank, "道具數量": len(self.inventory), "剩餘生命": self.health } print("\n💾 遊戲記錄已保存!") for key, value in game_record.items(): print(f"{key}: {value}") # 啟動遊戲 if __name__ == "__main__": game = TreasureHunter() game.start_game() game.save_game_record() ``` --- #### 🎯 遊戲設計架構: ```python import random def start_game(): print("🏴☠️ 歡迎來到尋寶冒險世界!") player_name = input("請輸入你的冒險者名字: ") print(f"歡迎,勇敢的 {player_name}!") health = 100 score = 0 inventory = [] game_running = True while game_running and health > 0: show_status(player_name, health, score, inventory) health, score, inventory, game_running = explore_area(health, score, inventory, game_running) if score >= 1000: print("🎉 恭喜!你已經成為傳奇尋寶獵人!") break save_game_record(player_name, health, score, inventory) ``` --- ```python def show_status(player_name, health, score, inventory): print(f"\n📊 === {player_name} 的狀態 ===") print(f"❤️ 生命值: {health}") print(f"⭐ 分數: {score}") print(f"🎒 背包: {inventory}") def explore_area(health, score, inventory, game_running): print("\n🌍 你來到了一個神秘區域...") events = ["寶箱", "怪物", "商人", "陷阱", "神秘泉水"] event = random.choice(events) if event == "寶箱": score, inventory = find_treasure(score, inventory) elif event == "怪物": health, score = fight_monster(health, score) elif event == "商人": print("🛒 你遇到了一位商人,但他今天沒有帶任何商品。") elif event == "陷阱": health = encounter_trap(health) elif event == "神秘泉水": health = find_spring(health) choice = input("\n🤔 要繼續探險嗎?(y/n): ").lower() if choice == 'n': game_running = False print("感謝遊玩!下次見!👋") return health, score, inventory, game_running ``` --- ```python def find_treasure(score, inventory): treasures = [ ("金幣", 50), ("銀幣", 25), ("鑽石", 200), ("紅寶石", 150), ("魔法書", 300), ("神秘藥水", 100) ] treasure, value = random.choice(treasures) print(f"✨ 你發現了 {treasure}!獲得 {value} 分!") inventory.append(treasure) score += value return score, inventory ``` --- ```python def fight_monster(health, score): monsters = ["哥布林", "骷髏兵", "野狼", "黑暗法師"] monster = random.choice(monsters) print(f"⚔️ 遭遇 {monster}!準備戰鬥!") for round_num in range(1, 4): print(f"\n🥊 第 {round_num} 回合") actions = ["攻擊", "防守", "逃跑"] for i, action in enumerate(actions, 1): print(f"{i}. {action}") choice = input("請選擇 (1-3): ") if choice == "1": # 攻擊 damage = random.randint(15, 30) print(f"💥 你對 {monster} 造成 {damage} 點傷害!") if random.random() > 0.3: print(f"🎉 你擊敗了 {monster}!獲得經驗值!") score += 75 return health, score elif choice == "2": # 防守 print("🛡️ 你選擇防守,減少傷害!") damage = random.randint(5, 15) else: # 逃跑 print("😰 你成功逃脫了!") return health, score health -= damage print(f"💔 你受到 {damage} 點傷害!") if health <= 0: print("💀 你被擊敗了!遊戲結束!") return health, score print("😰 戰鬥結束,你成功逃脫了!") return health, score ``` --- ```python def encounter_trap(health): damage = random.randint(10, 30) print(f"💣 你踩到了陷阱!損失 {damage} 點生命值!") health -= damage return health def find_spring(health): heal = random.randint(20, 50) print(f"💧 你發現了一個神秘泉水,恢復了 {heal} 點生命值!") health += heal return health ``` --- ```python def save_game_record(player_name, health, score, inventory): base_score = score health_bonus = health * 2 inventory_bonus = len(inventory) * 25 special_items = ["魔法書", "神秘藥水", "鑽石"] special_bonus = sum(100 for item in inventory if item in special_items) total_score = base_score + health_bonus + inventory_bonus + special_bonus print(f"\n🏆 === 最終計分 ===") print(f"基礎分數: {base_score}") print(f"生命值獎勵: {health_bonus}") print(f"道具獎勵: {inventory_bonus}") print(f"特殊道具獎勵: {special_bonus}") print(f"總分: {total_score}") if total_score >= 1500: rank = "傳奇尋寶大師 🏆" elif total_score >= 1000: rank = "專業探險家 ⭐" elif total_score >= 500: rank = "新手冒險者 🌟" else: rank = "初學者 🔰" print(f"\n🎖️ 你的冒險等級: {rank}") print("\n💾 遊戲記錄已保存!") print(f"玩家: {player_name}") print(f"分數: {total_score}") print(f"等級: {rank}") print(f"道具數量: {len(inventory)}") print(f"剩餘生命: {health}") if __name__ == "__main__": start_game() ```
**提交要求:** - 程式碼檔案 (.py) - 執行截圖 - 簡短心得(100字內)
5. **存檔功能**:將遊戲狀態寫入檔案
**範例架構:** ```python class AdvancedTreasureHunter: def __init__(self): self.level = 1 self.exp = 0 self.health = 100 self.max_health = 100 # ... 其他屬性 def explore_map(self): # 3x3地圖探索 game_map = [ ["?", "?", "?"], ["?", "P", "?"], # P代表玩家位置 ["?", "?", "?"] ] # 你的實作... def battle_system(self): # 戰鬥系統實作 pass ```
**評分標準:** - **創意性** (30%):概念新穎有趣 - **技術實作** (40%):正確使用while/for/巢狀迴圈 - **實用性** (20%):真的有解決問題 - **程式品質** (10%):程式碼清晰、有註解
**提交格式:** - 完整程式碼 - 使用說明文件 - 功能展示影片或截圖 - 設計理念說明(300字內) **截止時間:下週上課前** ⏰ **提交方式:上傳至課程平台** 📤