The Nature of Code閱讀心得與Python實作:1.3 Vector Addition

閱讀時間約 5 分鐘

假設向量w=(wx, wy),這時候wx、wy就稱為w的分量(component)。如果要更清楚地區分指的是wx還是wy,會把wx叫做x分量(x-component),而wy就叫做y分量(y-component)。在pygame中,Vector2()所建造的向量vec,其x、y分量就寫成vec.xvec.y

現在來看看向量的加法要怎麼算。假設u=(ux, uy)、v=(vx, vy),則

u + v = (ux+vx, uy+vy)

也就是說,向量的加法,就只是把分量相加而已。

向量加法跟實數加法一樣,都滿足交換律(commutative rule)和結合律(associative rule):

交換律:u + v = v + u
結合律:u + (v + w) = (u + v) + w

這個用大白話來說就是:誰排前面誰排後面,誰先加誰後加,都不影響最後的結果。

綜合上節和這節的結果,1.1節Example 1.1的程式,可以改寫成如下使用向量的版本。

Example 1.2: Bouncing Ball with Vectors!

# python version 3.10.9
import sys

import pygame # version 2.3.0


pygame.init()

pygame.display.set_caption("Example 1.2: Bouncing Ball with Vectors")

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

RADIUS = 24 # 球的半徑

screen_size = WIDTH, HEIGHT = 640, 360
screen = pygame.display.set_mode(screen_size)

FPS = 60
frame_rate = pygame.time.Clock()

position = pygame.Vector2(100, 100)
velocity = pygame.Vector2(2.5, 2)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

screen.fill(WHITE)

# 依據球的速度計算球的位置來移動球
position += velocity

# 檢查是不是碰到左、右邊界回彈
if (position.x > WIDTH) or (position.x < 0):
velocity.x = -velocity.x

# 檢查是不是碰到上、下邊界回彈
if (position.y > HEIGHT) or (position.y < 0):
velocity.y = -velocity.y

# 在位置(x, y)顯示球
pygame.draw.circle(screen, BLACK, position, RADIUS)

pygame.display.update()
frame_rate.tick(FPS)


Exercise 1.1

把Example 0.1改成用向量來寫。Walker這個class改寫為

class Walker:
def __init__(self, x, y):
self.position = pygame.Vector2(x, y)

def show(self, screen, color=(0, 0, 0)):
pt = (int(self.position.x), int(self.position.y))
screen.set_at(pt, color)

def step(self):
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
self.position += pygame.Vector2(random.choice(directions))


Exercise 1.2

Exercise 1.3

假設球是在長、寬、高分別為LENGTHWIDTHHEIGHT的方盒內運動。由於pygame並不支援3D繪圖,所以無法顯示動畫效果。不過,利用pygame提供的3D向量功能,還是可以計算出球的位置,以及是否碰到方盒邊界而回彈,這部分的程式如下:

# 檢查是不是碰到左、右邊界回彈
if (position.x > HEIGHT) or (position.x < 0):
velocity.x = -velocity.x

# 檢查是不是碰到前、後邊界回彈
if (position.y > WIDTH) or (position.y < 0):
velocity.y = -velocity.y

# 檢查是不是碰到上、下邊界回彈
if (position.z > HEIGHT) or (position.z < 0):
velocity.z = -velocity.z


13會員
94內容數
寫點東西自娛娛人
留言0
查看全部
發表第一個留言支持創作者!
你可能也想看
創作者要怎麼好好休息 + 避免工作過量?《黑貓創作報#4》午安,最近累不累? 這篇不是虛假的關心。而是《黑貓創作報》發行以來可能最重要的一篇。 是的,我們這篇講怎麼補充能量,也就是怎麼休息。
Thumbnail
avatar
黑貓老師
2024-06-29
The Evolution of Mobile App Development Frameworks in 2024As we navigate through 2024, the mobile app development landscape continues to evolve, driven by changing consumer behaviors...
Thumbnail
avatar
Digiworld
2024-04-26
The benefits on exercise_2024.04.25複習今日所學英文~有關於運動~ 內容:成長過程中對運動的興趣、運動的正面影響。從小時候習慣了每天運動、運動選擇的改變、現在面臨的挑戰~
Thumbnail
avatar
天上的一片雲
2024-04-25
《Compassion》The sorrow of trees - a message from nature"You live for yourselves, not for humans." About trees and their feelings of sadness due to human interference.
Thumbnail
avatar
靈魂共感療域 EmpathyCave
2023-11-12
自然死亡,很難嗎? Allowing the mother nature taking over our deathNow I understand: there is no need for futile treatment or ineffective care, allowing our mother nature to take over may be the kindest approach for t
avatar
雙魚鏡方格子檔案館
2023-07-13
《The First Slam Dunk》:即使心臟噗通噗通跳,也要表現得若無其事  簡直像站在球場中看著真實的比賽讓人目不轉睛,呼吸為之屏息——傳說中灌籃高手動畫版的遺珠之憾:湘北對山王之戰終於出世。當年所有人期盼卻沒盼得的最重要的一場比賽,在生父手下脫胎而出,讓這部作品畫下奇蹟的句點。  
Thumbnail
avatar
薄荷糖
2023-01-28
The Writer And Her Story 遇見香港漫畫家智海《The Writer And Her Story》是香港漫畫家智海在1999年的作品,黑白色調的漫畫,描寫一個抑鬱的作者,被自己腦袋裡沉重的想法壓駝了背,寫下作品想為腦袋「減重」,卻被各種瑣碎問題所困,就連繪者也彷彿以上帝之手,延伸再延伸,給他沒有盡頭的樓梯和前路...
Thumbnail
avatar
林倚
2023-01-03
The Innocents Abroad👉跟馬克•吐溫遊歐亞在海上看美景,也不忘笑話別人的英語口音👉「It's gorgis, ain't it?」一起來領略一世紀前馬克•吐溫的不倒幽默!
Thumbnail
avatar
Mary Ventura
2022-09-11
the benefit of the doubt? 「懷疑的好處」是什麼語感來著?英文有一些慣用的句子,字面上怎麼讀都參不透,因為不知道背後隱含的思考邏輯。「給他懷疑的好處(give him the benefit of the doubt)」這種話在中文的邏輯中根本不成文也說不通,到底是在講什麼鬼?你只能用外國人的邏輯來想!
Thumbnail
avatar
你的英日語自學導師 譯難忘  ོꦿ༄꧁꧂
2021-06-23
《The Queen's Gambit》 你是一個奇蹟,但要學習如何生活《Queen’s Gambit》第一眼看會以為又是另一個關於天才兒童戰無不勝或者自甘墮落的故事。其實這一套劇集,講的是一個高敏感又內向的女生,如何在女權崛起之前的冷戰年代,以人生輸家之姿,晉身頂級棋手的故事,同時講述一個女孩子的前半生。
Thumbnail
avatar
Kayla 陳韻如
2021-03-19
avatar
神勇火球
2007-08-16