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
HTML5 canvas game
Search
HakumaNatata
May 02, 2011
Programming
0
69
HTML5 canvas game
Write a game using canvas
HakumaNatata
May 02, 2011
Tweet
Share
More Decks by HakumaNatata
See All by HakumaNatata
Introduction to NMAP
natata
0
59
SPDY
natata
0
110
How to trace code
natata
0
49
Introduction to HTML5
natata
0
38
PHP with Smarty
natata
0
35
Other Decks in Programming
See All in Programming
MUSUBIXとは
nahisaho
0
130
QAフローを最適化し、品質水準を満たしながらリリースまでの期間を最短化する #RSGT2026
shibayu36
2
4.3k
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
200
AI & Enginnering
codelynx
0
110
2026年 エンジニアリング自己学習法
yumechi
0
130
それ、本当に安全? ファイルアップロードで見落としがちなセキュリティリスクと対策
penpeen
7
2.4k
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
500
AI時代のキャリアプラン「技術の引力」からの脱出と「問い」へのいざない / tech-gravity
minodriven
20
6.8k
インターン生でもAuth0で認証基盤刷新が出来るのか
taku271
0
190
メルカリのリーダビリティチームが取り組む、AI時代のスケーラブルな品質文化
cloverrose
2
510
OSSとなったswift-buildで Xcodeのビルドを差し替えられるため 自分でXcodeを直せる時代になっている ダイアモンド問題編
yimajo
3
610
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
130
Featured
See All Featured
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
110
Navigating Team Friction
lara
192
16k
BBQ
matthewcrist
89
10k
We Are The Robots
honzajavorek
0
160
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.2k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
1
1.3k
エンジニアに許された特別な時間の終わり
watany
106
230k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.1k
How to build a perfect <img>
jonoalderson
1
4.9k
How to Talk to Developers About Accessibility
jct
2
130
Java REST API Framework Comparison - PWX 2021
mraible
34
9.1k
Transcript
資工3A 彭博涵
1 game 3 way to play
流程 畫面 聲音
JavaScript CSS <canvas> + JS JavaScript + music
None
¨ <canvas id=“demo” width=‘’ height=‘’></ canvas> ¨ 畫框 ¨ 提供讓JS作畫的地方
¨ var canvas = document.getElementById("demo"); ¡ 取得畫框 ¨ var ctx
= canvas. getContext("2d"); ¡ 放上白紙 ¡ Q: Is there a 3D canvas?
¨ 畫線 ¨ ctx.beginPath(); //拿起一枝筆 ¨ ctx.moveTo(10,10); //放到紙上某一點 ¨ ctx.lineTo(50,50);
//從目前位置畫到某一點 ¨ ctx.lineTo(10,50); ¨ ctx.closePath(); //將目前的點與原點連接起來 ¨ ctx.stroke(); //描外框 ¨ ctx.fillStyle = "rgb(R,G,B)"; //0~255 ¨ ctx.fill(); //填滿(自動執行 closePath())
¨ 畫方形 ¨ ctx.fillStyle = "rgba(R,G,B,A)"; a = 0~1 ¨
ctx.fillRect ( x1 , y1 , x2 , y2 ); ¨ ctx.clearRect ( x1 , y1 , x2 , y2 );
¨ 畫圓 ¨ arc(x,y,r,startAngle,closeAngle,anticlockwise) ¨ 弧度 弧度(逆時針) boolen ¨ ctx.beginPath();
¨ ctx.arc(275,300,100,Math.PI,Math.PI*3/2,true); ¨ ctx.lineTo(275,300); ¨ ctx.closePath(); ¨ ctx.stroke(); ¨ ctx.fill();
¨ https://developer.mozilla.org/en/ Canvas_tutorial
None
¨ Quick change alarge number of graphs. ¨ FPS (Frames
Per Second) ¨ setInterval( action , time); // millisecond
¨ var FPS = 30; ¨ setInterval(function() { update(); draw();
}, 1000/FPS);
player • 外觀 • 移動 • 射擊 bullet • 外觀 • 往上衝 enemy • 外觀 • 往下衝
¨ 事件 ¡ 子彈與敵方碰撞 ¡ 玩家與敵方碰撞
¨ Color ¨ Size ¨ Move ¨ Shoot ¨ Draw
¨ var player = { ¨ color: "#00A", ¨ x:
220, ¨ y: 270, ¨ width: 32, ¨ height: 32, ¨ draw: function() ¨ { ¨ canvas.fillStyle = this.color; ¨ canvas.fillRect(this.x, this.y, this.width, this.height); ¨ } ¨ };
¨ 前人種樹 後人乘涼 ¨ https://github.com/tzuryby/jquery.hotkeys ¨ keydown.left ¨ keydown.right ¨
keydown.space
¨ function update() { if (keydown.space) { player.shoot(); } if
(keydown.left) { player.x -= 5; } if (keydown.right) { player.x += 5; } player.x = Math.min(Math.max(player.x, 0), CANVAS_WIDTH - player.width); }
¨ canvas.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); ¨ player.draw();
¨ Active ¨ Speed ¨ Size ¨ Color ¨ inBound
¨ Update ¨ Draw
¨ player.shoot = function() { var bulletPosition = { x:
this.x + this.width/2, y: this.y + this.height/2 }; playerBullets.push(Bullet({ speed: 5, x: bulletPosition.x, y: bulletPosition.y })); };
¨ //更新每個子彈狀態 ¨ playerBullets.forEach( function(bullet) {bullet.update(); } ); ¨ //過濾已經不行的子彈
filter會回傳改過的陣列 而不會更動原本的陣列 ¨ playerBullets = playerBullets.filter( function(bullet) {return bullet.active; } );
¨ playerBullets.forEach(function(bullet) { bullet.draw(); });
¨ Active ¨ Size ¨ Color ¨ Speed ¨ inBound
¨ Update ¨ Draw
¨ enemies.forEach(function(enemy) { enemy.update(); }); ¨ enemies = enemies.filter(function(enemy) {
return enemy.active; }); ¨ if(Math.random() < 0.05) { enemies.push(Enemy()); }
¨ enemies.forEach(function(enemy) {enemy.draw(); });
¨ 前人種樹 後人乘涼 ¨ player.sprite = Sprite("player"); ¨ player.draw =
function() { this.sprite.draw(canvas, this.x, this.y); };
¨ function collides(a, b) { return a.x < b.x +
b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y; }
¨ function handleCollisions() { playerBullets.forEach(function(bullet) { enemies.forEach(function(enemy) { if (collides(bullet,
enemy)) { enemy.explode(); bullet.active = false; } }); }); enemies.forEach(function(enemy) { if (collides(enemy, player)) { enemy.explode(); } }); }
¨ handleCollisions();
¨ 前人種樹 後人乘涼 ¨ player.shoot = function() { Sound.play("shoot"); }
None