用 Python 打造你的遊戲
2020
小草、RexWu
ctl12345
def greet(name):
print("Hello, " + str(name))
# main
n = input("請告訴我你的名字")
greet(n)
try:
x = int(input('請輸入 x :'))
y = int(input('請輸入 y :'))
print(x + y)
except:
print('輸入錯誤 請輸入整數')
所以要處理例外狀況
使用 「try」和 「except」
物件 - 概念
物件 - 實作
class 物件名稱:
建構
函數1
函數2
...
class Player:
def __init__(self, hp, atk, def_):
self.hp = hp
self.atk = atk
self.def_ = def_
def attack(self, target):
target.hp = target.def_ - self.atk
def greet(self):
print("Player 說:早安我的朋友!")
def __init__(self, hp, atk, def_):
物件本體
參數們
self.hp = hp
self.atk = atk
self.def_ = def_
設定玩家的 hp
設定玩家的 atk
設定玩家的 def
取得物件本身的值
e.g.
self.hp -> 取得該物件的 hp 變數
Player1 = Player(10, 5, 2)
# 新增一個 hp = 10 atk = 5 def_ = 2 的玩家
Player2 = Player(20, 10, 5)
# 新增一個 hp = 20 atk = 10 def_ = 5 的玩家
player1 = Player(10, 5, 2)
# 新增一個 hp = 10 atk = 5 def_ = 2 的玩家
player2 = Player(20, 10, 5)
# 新增一個 hp = 20 atk = 10 def_ = 5 的玩家
print(player1.hp) # 顯示 player1 的 hp
print(player2.hp) # 顯示 player2 的 hp
class Player:
def __init__(self, hp, atk, def_):
self.hp = hp
self.atk = atk
self.def_ = def_
def attack(self, target):
target.hp = target.def_ - self.atk
def greet(self):
print("Player 說:早安我的朋友!")
class Player:
# 略過上面的東西
def getHpVal(self):
return self.hp
終於看得到惹
import tkinter as tk
window = tk.Tk()
msg = tk.Label(master=window, text="你好!")
msg.pack()
window.mainloop()
import tkinter as tk
window = tk.Tk()
window.geometry("500x500")
msg = tk.Label(master=window, text="你好!")
msg.pack()
window.mainloop()
import tkinter as tk
window = tk.Tk()
window.geometry("500x500")
msg = tk.Label(master=window, text="你好!")
msg.pack()
window.mainloop()
import tkinter as tk
window = tk.Tk()
def clk():
print("尼亂按 ><")
btn = tk.Button(master=window, text="按我!", command=clk)
msg.pack()
window.mainloop()
import tkinter as tk
window = tk.Tk()
def clk():
btn["text"] = "不要亂按 ><"
btn = tk.Button(master=window, text="按我!", command=clk)
btn.pack()
window.mainloop()
import tkinter as tk
window = tk.Tk()
window.geometry("300x300")
label0 = tk.Label(text="我在最上面!")
label0.pack(side = tk.TOP)
label3 = tk.Label(text="我在左邊 ( ~'ω')~ ")
label3.pack(side = tk.LEFT)
label4 = tk.Label(text=" ~('ω'~ ) 窩在右邊")
label4.pack(side = tk.RIGHT)
window.mainloop()
import tkinter as tk
window = tk.Tk()
window.geometry("500x500")
frame = tk.Frame()
frame.grid(row=0, column=0)
label = tk.Label(master=frame, text="左上角")
label.pack()
frame = tk.Frame()
frame.grid(row=1, column=1)
label = tk.Label(master=frame, text="右下角")
label.pack()
window.mainloop()
import tkinter as tk
window = tk.Tk()
window.geometry("500x500")
def clk():
print("不要亂按><")
for i in range(3):
for j in range(3):
frame = tk.Frame()
frame.grid(row=i, column=j)
btn = tk.Button(master=frame, text=str(i) + str(j), width=5, command=clk)
btn.pack()
window.mainloop()
import tkinter as tk
window = tk.Tk()
window.geometry("500x500")
def clk():
print("不要亂按><")
for i in range(3):
for j in range(3):
frame = tk.Frame()
frame.grid(row=i, column=j)
btn = tk.Button(master=frame, text=str(i) + str(j), width=5, command=clk)
btn.pack()
window.mainloop()
按鈕好多 怎麼知道誰是誰?
from functools import partial
def f(x, y):
return x * y
# 將參數"凍結"
f = partial(f, 10, 10)
print(f())
for i in range(3):
for j in range(3):
frame = tk.Frame()
frame.grid(row=i, column=j)
btn = tk.Button(master=frame,
text=str(i) + str(j),
width=10,
height=10,
command=partial(clk, i, j)
)
btn.pack()
btnAry = []
for i in range(3):
row = []
for j in range(3):
frame = tk.Frame()
frame.grid(row=i, column=j)
btn = tk.Button( # 後面省略....
row.append(btn)
btn.pack()
btnAry.append(row)