$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
HTML5 canvas game
Search
HakumaNatata
May 02, 2011
Programming
0
68
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
57
SPDY
natata
0
100
How to trace code
natata
0
48
Introduction to HTML5
natata
0
37
PHP with Smarty
natata
0
34
Other Decks in Programming
See All in Programming
WebRTC と Rust と8K 60fps
tnoho
2
1k
「文字列→日付」の落とし穴 〜Ruby Date.parseの意外な挙動〜
sg4k0
0
320
Why Kotlin? 電子カルテを Kotlin で開発する理由 / Why Kotlin? at Henry
agatan
1
310
無秩序からの脱却 / Emergence from chaos
nrslib
2
10k
AWS CDKの推しポイントN選
akihisaikeda
1
220
AIエージェントでのJava開発がはかどるMCPをAIを使って開発してみた / java mcp for jjug
kishida
4
830
CSC305 Lecture 15
javiergs
PRO
0
210
MAP, Jigsaw, Code Golf 振り返り会 by 関東Kaggler会|Jigsaw 15th Solution
hasibirok0
0
160
【レイトレ合宿11】kagayaki_v4
runningoutrate
0
110
AI時代もSEOを頑張っている話
shirahama_x
0
190
Atomics APIを知る / Understanding Atomics API
ssssota
1
230
生成AIを活用したリファクタリング実践 ~コードスメルをなくすためのアプローチ
raedion
0
160
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
11
950
The Pragmatic Product Professional
lauravandoore
36
7k
Building Adaptive Systems
keathley
44
2.8k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
We Have a Design System, Now What?
morganepeng
54
7.9k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.5k
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.8k
Designing Experiences People Love
moore
142
24k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
31
2.7k
What's in a price? How to price your products and services
michaelherold
246
12k
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