# Minecraft Python 4A2
## 🎯 學習重點
* Python 的輸出
* Minecraft 座標系統
* `getTilePos()`:取得玩家位置
* `setBlock()`:放置方塊
---
## 💬 標準輸出 vs. Minecraft 聊天輸出
```python
message = "Hello Python"
print(message) # 標準輸出
mc.postToChat(message) # Minecraft 聊天視窗輸出
```
---
## 🧭 Minecraft 座標系統
* 世界由 **x, y, z 三維座標**表示
* 可按 **F3** 檢視當前座標與方向
---
## 📍 取得自己的座標
```python
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
pos = mc.player.getTilePos()
mc.postToChat(pos)
```
* `pos` 物件存放 `(x, y, z)`
* 透過 `mc.postToChat()` 在 Minecraft 聊天顯示座標
---
## 📝 課堂範例 1:我在哪?
```python
# 匯入需要的工具
import mcpi.minecraft as minecraft
# 連結 Minecraft 世界
mc = minecraft.Minecraft.create()
# 取得目前位置
pos = mc.player.getTilePos()
# 輸出到 Minecraft 世界
mc.postToChat("Hello Minecraft!")
mc.postToChat(pos)
```
---
## 💡 延伸練習 1:What’s your name?
* 建立一個 Python 檔案
* 在 Minecraft 世界中顯示 **自己的英文名字**
---
## 🧱 用 Python 製造方塊
### API
```python
mc.setBlock(x, y, z, Block_ID, data)
```
* `(x, y, z)`:方塊座標
* `Block_ID`:方塊 ID(例如 20=玻璃, 79=冰塊)
* `data`:屬性(可省略)
### 範例:在玩家四周放玻璃
```python
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
pos = mc.player.getTilePos()
mc.setBlock(pos.x + 1, pos.y, pos.z, 20)
mc.setBlock(pos.x - 1, pos.y, pos.z, 20)
mc.setBlock(pos.x, pos.y, pos.z + 1, 20)
mc.setBlock(pos.x, pos.y, pos.z - 1, 20)
```
---
## 🏠 課堂範例 2:迷你冰屋
* 建立新 Python 檔案
* 用冰塊(ID=79)將自己完全封起來
* 提醒:Minecraft 人物高度 = **2 方塊**
```python
#匯入需要的工具
import mcpi.minecraft as minecraft
import mcpi.block as block
#連結Minecraft世界
mc = minecraft.Minecraft.create()
#取得目前位置
pos = mc.player.getTilePos()
mc.setBlock(pos.x + 1, pos.y, pos.z, 79)
mc.setBlock(pos.x - 1, pos.y, pos.z, 79)
mc.setBlock(pos.x, pos.y, pos.z + 1, 79)
mc.setBlock(pos.x, pos.y, pos.z - 1, 79)
#y+1 往上蓋一層,因為人物的身高是2個方塊高度
mc.setBlock(pos.x + 1, pos.y + 1, pos.z, 79)
mc.setBlock(pos.x - 1, pos.y + 1, pos.z, 79)
mc.setBlock(pos.x, pos.y + 1, pos.z + 1, 79)
mc.setBlock(pos.x, pos.y + 1, pos.z - 1, 79)
#把上面封起來
mc.setBlock(pos.x, pos.y + 2, pos.z, 79)
#把下面封起來
mc.setBlock(pos.x, pos.y - 1, pos.z, 79)
```
## 🧪 觀念實作複習:位置 → 放 TNT
```python
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
# 1) 在聊天視窗印出目前位置
pos = mc.player.getTilePos()
mc.postToChat(f"My position: {pos}")
# 2) 在目前位置生成一顆 TNT(ID 46)
TNT = 46
mc.setBlock(pos.x, pos.y, pos.z, TNT)
```
---
## 🌀 新觀念 1:用 Python 瞬間移動
API:
```python
mc.player.setTilePos(x, y, z)
```
範例:往 +x 方向瞬移 10 格
```python
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
#取得角色位置
pos = mc.player.getTilePos()
#將角色傳送到指定座標。新座標距離原座標10格
mc.player.setTilePos(pos.x + 10, pos.y, pos.z)
```
> `pos` 是含有 `x,y,z` 的物件;`pos.x` 代表 x 座標。  
---
## 🧩 新觀念 2:Python 特色(縮排)
* 從圖形拖拉式 → 文字式:邏輯相通,但 Python 更強大
* **縮排(4 空白或 1 個 TAB)代表區塊**,對應 Scratch 的插槽  
---
## 📦 新觀念 3:作用域(Effective Range)
區塊內的程式才屬於該結構(如 `while`)的作用域:
```python
import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft.create()
while True:
pos = mc.player.getTilePos()
mc.postToChat(pos)
print("I'm not in the effective range.")
```
> 迴圈外的 `print()` 不屬於 `while` 的作用域。  
---
## 📝 新觀念 4:註解(單行、多行)
```python
while True:
"""
這是多行註解:
下面會將你的所在位置印在聊天視窗中
"""
# 這是單行註解
pos = mc.player.getTilePos()
mc.postToChat(pos)
```
---
## 📚 新觀念 5:匯入函式庫(packages)
```python
import mcpi.minecraft as minecraft
import mcpi.block as block
import mcpi.minecraftturtle as minecraftturtle
# import the files you need
mc = minecraft.Minecraft.create()
pos = mc.player.getTilePos()
```
---
## 🥷 課堂範例 1:像忍者一樣瞬移
```python
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
#取得角色位置
pos = mc.player.getTilePos()
#將當前位置傳送到Minecraft對話框
mc.postToChat(pos)
#傳送到距離初始位置x+10、z+10的地方
mc.player.setTilePos(pos.x + 10, pos.y, pos.z + 10)
#重新取得角色位置
pos = mc.player.getTilePos()
#將當前位置傳送到Minecraft對話框
mc.postToChat(pos)
```
---
## 🧭 延伸練習 1:多軸位移
需求:`move | start` → 瞬移到 `x+10, y+2, z-5` → `move | end`
```python
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
start = mc.player.getTilePos()
mc.postToChat(f"move | start: {start}")
mc.player.setTilePos(start.x + 10, start.y + 2, start.z - 5)
end = mc.player.getTilePos()
mc.postToChat(f"move | end: {end}")
```
---
## 🧾 新觀念 6:變數(宣告、賦值、遞增/遞減)
```python
a = 99.9 # a: 99.9
a = 0 # a: 0
num = a + 1 # num: 1
num += 1 # num: 2
num -= 5 # num: -3
```
命名規則(示例):
```python
clientNum = 10 # OK(駝峰命名)
phoneNum1 = "0800" # OK
age = 5 # OK
Xxx = "kite" # 語法有效但不具意義
# a@/ = 5.5 # 不可:非法字元
# 1ab = 5 # 不可:數字開頭
```
---
## 🧪 新觀念 7:資料型別(type)
基本型別:
```python
# int / float / str / bool
i = 3
f = 0.125
s = "Hello"
b = True
print(type(i)) # <class 'int'>
print(type(f)) # <class 'float'>
print(type(s)) # <class 'str'>
print(type(b)) # <class 'bool'>
```
自訂型別(由套件提供):
```python
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
pos = mc.player.getTilePos()
print(type(mc)) # e.g., <class 'Minecraft'>
print(type(pos)) # e.g., <class 'Vec3'>
```
---
## 🧑💻 課堂範例 2:用 `type()` 看型別
```python
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
pos = mc.player.getTilePos()
print(type(mc))
print(type(pos))
```
---
## ➕ 延伸練習 2:同值不同型
```python
a = 100 # int
b = '100' # str
print(type(a)) # <class 'int'>
print(type(b)) # <class 'str'>
```
---
## ⌨️ `input()` 與 `int()`:輸入字串、轉整數
```python
age = int(input("輸入你的年齡: "))
print(age)
```
> `input()` 讀進來是 **字串**,若要做數值運算需用 `int()` 轉為 **整數**。  
---
## 🧩 回家作業
需求:
1. 先輸出原始座標
2. 依序讀入 `x,y,z`(提示文字)
3. 三個座標各自 **+1** 後輸出(格式:`x , y , z`)
參考骨架:
~~import mcpi.minecraft as minecraft~~
~~mc = minecraft.Minecraft.create()~~
~~pos = mc.player.getTilePos()
print(f"原始座標:{pos.x} , {pos.y} , {pos.z}")~~
~~x = int(input("請輸入 x 座標:"))
y = int(input("請輸入 y 座標:"))
z = int(input("請輸入 z 座標:"))~~
~~x += 1
y += 1
z += 1~~
~~print(x, ",", y, ",", z)~~
> 提醒:`input()` 的回傳值是字串,需 `int()` 轉型。  
---
## 🧷 小抄:常用方塊 ID(片段)
```text
TNT = 46
GLASS = 20
STONE = 1
GRASS = 2
```
---