Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
大家來寫貪食蛇
Search
Weizhong Yang
August 10, 2013
Technology
7
4.3k
大家來寫貪食蛇
iOSDevCamp Taipei 2013 workshop
Weizhong Yang
August 10, 2013
Tweet
Share
More Decks by Weizhong Yang
See All by Weizhong Yang
導入 Flutter 前你應該知道的事
zonble
7
1.1k
那些年,被蘋果 Ban 掉的 API
zonble
0
85
為視障朋友打造行動應用
zonble
16
1.2k
Aspect Oriented Programming
zonble
2
170
Mac OS X 與 iOS 的 Audio API
zonble
11
920
Debug Debug
zonble
6
350
Retina Mac
zonble
3
200
HTML 5 Native Drag
zonble
3
490
Other Decks in Technology
See All in Technology
これからSREになる人と、これからもSREをやっていく人へ
masayoshi
6
4.1k
急成長する企業で作った、エンジニアが輝ける制度/ 20250214 Rinto Ikenoue
shift_evolve
2
880
APIファーストで実現する運用性の高い IoT プラットフォーム: SORACOMのアプローチ
soracom
PRO
0
240
Kubernetes x k6 で負荷試験基盤を開発して 負荷試験を民主化した話 / Kubernetes x k6
sansan_randd
2
730
『AWS Distinguished Engineerに学ぶ リトライの技術』 #ARC403/Marc Brooker on Try again: The tools and techniques behind resilient systems
quiver
0
130
自動テストの世界に、この5年間で起きたこと
autifyhq
10
7.1k
生成AIの利活用を加速させるための取り組み「prAIrie-dog」/ Shibuya_AI_1
visional_engineering_and_design
1
140
日経電子版 x AIエージェントの可能性とAgentic RAGによって提案書生成を行う技術
masahiro_nishimi
1
290
ハッキングの世界に迫る~攻撃者の思考で考えるセキュリティ~
nomizone
12
4.5k
まだ間に合う! エンジニアのための生成AIアプリ開発入門 on AWS
minorun365
PRO
4
580
現場で役立つAPIデザイン
nagix
29
10k
SA Night #2 FinatextのSA思想/SA Night #2 Finatext session
satoshiimai
1
100
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
67
4.6k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
29
4.6k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
40
2k
We Have a Design System, Now What?
morganepeng
51
7.4k
How to Ace a Technical Interview
jacobian
276
23k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
A Philosophy of Restraint
colly
203
16k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
540
Transcript
⼤大家來寫 貪⻝⾷食蛇! Saturday, August 10,
About me Saturday, August 10,
About me • Lead iOS/Mac Developer at KKBOX • Mac
developer since2005, iOS developer since2008 • Other projects: Yahoo KeyKey! Boshiamy X1, Hami Music for iOS, and so on. • I do C, Objective-C, and Python Saturday, August 10,
Saturday, August 10,
我覺得是不錯的練習 • MVC架構 • DTO (Data Transfer Objects) • Quartz
2D 平⾯面繪圖 • Delegate/Protocol 設計 • Queue資料結構 • NSTimer Saturday, August 10,
應該三個多⼩小時可以 寫出來… (我⾃自⼰己寫過⼀一遍) 還是⼀一邊看環法賽⼀一邊很不專⼼心的寫 Saturday, August 10,
Saturday, August 10,
KKBOX的iOS訓練 • Selector⇢⼩小算盤 • ⼿手動記憶體管理 • Category⇢字串反轉 • Delegate⇢API包裝/Unit Test
• Block⇢API包裝 • OperationQueue⇢API包裝 • Quartz 2D與Notification⇢填字遊戲 • NSCoder/Sqlite/Localization…⇢看書練習 • CoreAnimation⇢動態歌詞 • UIAutomation 與 Accessibility • Audio API⇢Audio Player ⼤大概是在 這個地⽅方 Saturday, August 10,
貪⻝⾷食蛇? • 蛇的⾝身體所在位置 • 蛇現在的⻑⾧長度?吃到⽔水 果之後,往那邊變⻑⾧長? • 蛇的現在的移動⽅方向? 怎麼改變⽅方向? •
蛇超出邊界該怎麼辦? 邊界有多⼤大? • ⽔水果的位置 • 是不是吃到了⽔水果 • 吃到⽔水果之後,下⼀一個 ⽔水果在哪裡? • 該怎麼畫蛇跟⽔水果? • 頭撞到⾝身體了嗎? 我們應該如何妥當處理這些東⻄西? Saturday, August 10,
物件封裝 • 我們在畫⾯面中有⼀一條蛇 • 屬性:⾝身體的位置、蛇的⽅方向 • ⽅方法:改變⽅方向、移動⼀一格 Saturday, August 10,
MVC架構 • Model:我們的蛇與⽔水果的Point • View:繪製蛇與⽔水果的View • Controller: • 分派⽔水果的位置 •
使⽤用 Timer更新蛇的狀態、檢查是否吃 到⽔水果與撞到⾝身體 • 接收改變蛇的⽅方向的Touch事件 Saturday, August 10,
實作流程 • 撰寫蛇的Model • 對蛇的Model做單元測試 • 撰寫繪製蛇與⽔水果的View • 撰寫Controller •
整合! Saturday, August 10,
Model Saturday, August 10,
我們應該如何描述蛇與 ⽔水果的位置? • 我們可以使⽤用CGPoint • 不過不是很好,CGPoint的x與y是float • 我們希望x與y是整數 Saturday, August
10,
Data Transfer Objects • 只有屬性,沒有⽅方法的物件 • 當成 Structure 來⽤用 •
在Cocoa裡頭,我們也可以將 Structure 包進NSValue裡頭就是了… • [NSValue valueWithBytes: objCType:]; Saturday, August 10,
蛇的⾝身體的資料結構 • 不要想得太複雜,蛇的移動並不是⾝身體 的每個點都在移動 • 其實就只是,在頭的地⽅方增加⼀一個點, 然後把尾巴那個點去掉 • 先進先出⇢Queue Saturday,
August 10,
Saturday, August 10,
蛇的⽅方向 • 蛇應該要知道⾃自⼰己的⽅方向 • 改變⽅方向只能90度: • 往左右⾛走,只能改成上下 • 往上下⾛走,只能改成左右 Saturday,
August 10,
單元測試 Saturday, August 10,
3A原則 • Arrange • Act • Assert Saturday, August 10,
單元測試 • 我們的DTO裡頭的屬性是否正確? • 建⽴立蛇之後,⻑⾧長度位置是否正確? • 移動蛇之後,位置是否正確? • 把蛇的⾝身體加⻑⾧長之後,是否有加對? •
蛇是否會正確撞到⾃自⼰己⾝身體? Saturday, August 10,
View Saturday, August 10,
View • View 要跟Controller詢問蛇與⽔水果在哪裡 • 我們可以把蛇與⽔水果變成View的屬性, 從Controller設過去,但是這樣Controller 就要定時更新View,這樣很醜 • ⽐比較好的作法是,把Controller變成View
的delegate Saturday, August 10,
View • Subclass ⼀一個UIView • 實作drawRect: • 在drawRect:裡頭問delegate蛇與⽔水果在哪 • ⽤用UIBezierPath畫就好
• 所以我們要設計⼀一個詢問蛇與⽔水果在哪 的protocol Saturday, August 10,
Delegation/Protocol • @property (weak) id <Protocol> delegate; • 然後設計⼀一組 Protocol
• 例 -(Snake *)viewDidRequestSnake:(View *)v; • Controller要去實作delegate methods Saturday, August 10,
Controller Saturday, August 10,
Controller • 成員變數 • 蛇的Model • ⽔水果的Model • 繪製蛇與⽔水果View •
遊戲歡迎與結束畫⾯面 • Timer Saturday, August 10,
Controller • 如何接收Swipe事件? • ⽤用UIGestureRecognizer Saturday, August 10,
Let’s Do It! Saturday, August 10,