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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
200
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
230
IFSによる形状設計/デモシーンの魅力 @ 慶應大学SFC
gam0022
1
300
Automatic Grammar Agreementと Markdown Extended Attributes について
kishikawakatsumi
0
180
CSC307 Lecture 07
javiergs
PRO
0
550
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
420
なるべく楽してバックエンドに型をつけたい!(楽とは言ってない)
hibiki_cube
0
140
Oxlintはいいぞ
yug1224
5
1.3k
QAフローを最適化し、品質水準を満たしながらリリースまでの期間を最短化する #RSGT2026
shibayu36
2
4.3k
AgentCoreとHuman in the Loop
har1101
5
220
Honoを使ったリモートMCPサーバでAIツールとの連携を加速させる!
tosuri13
1
170
CSC307 Lecture 01
javiergs
PRO
0
690
Featured
See All Featured
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Un-Boring Meetings
codingconduct
0
200
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
55k
Music & Morning Musume
bryan
47
7.1k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
220
The Cult of Friendly URLs
andyhume
79
6.8k
SEO for Brand Visibility & Recognition
aleyda
0
4.2k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
91
Embracing the Ebb and Flow
colly
88
5k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
1
1.4k
Docker and Python
trallard
47
3.7k
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