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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
生成AIを使ったコードレビューで定性的に品質カバー
chiilog
1
260
CSC307 Lecture 09
javiergs
PRO
1
830
IFSによる形状設計/デモシーンの魅力 @ 慶應大学SFC
gam0022
1
300
「ブロックテーマでは再現できない」は本当か?
inc2734
0
870
Lambda のコードストレージ容量に気をつけましょう
tattwan718
0
110
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
1k
高速開発のためのコード整理術
sutetotanuki
1
390
AI によるインシデント初動調査の自動化を行う AI インシデントコマンダーを作った話
azukiazusa1
1
700
Oxlint JS plugins
kazupon
1
840
Fluid Templating in TYPO3 14
s2b
0
130
AI Agent Tool のためのバックエンドアーキテクチャを考える #encraft
izumin5210
6
1.8k
Smart Handoff/Pickup ガイド - Claude Code セッション管理
yukiigarashi
0
130
Featured
See All Featured
Building Applications with DynamoDB
mza
96
6.9k
The Pragmatic Product Professional
lauravandoore
37
7.1k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
110
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
49
Un-Boring Meetings
codingconduct
0
200
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
Navigating Team Friction
lara
192
16k
Producing Creativity
orderedlist
PRO
348
40k
Chasing Engaging Ingredients in Design
codingconduct
0
110
Design in an AI World
tapps
0
140
Utilizing Notion as your number one productivity tool
mfonobong
3
220
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