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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
CSC307 Lecture 08
javiergs
PRO
0
670
15年続くIoTサービスのSREエンジニアが挑む分散トレーシング導入
melonps
2
190
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
550
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
370
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
200
開発者から情シスまで - 多様なユーザー層に届けるAPI提供戦略 / Postman API Night Okinawa 2026 Winter
tasshi
0
200
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
170
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
560
Implementation Patterns
denyspoltorak
0
280
Grafana:建立系統全知視角的捷徑
blueswen
0
330
360° Signals in Angular: Signal Forms with SignalStore & Resources @ngLondon 01/2026
manfredsteyer
PRO
0
120
izumin5210のプロポーザルのネタ探し #tskaigi_msup
izumin5210
1
110
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
42
2.9k
Bash Introduction
62gerente
615
210k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
80
Code Reviewing Like a Champion
maltzj
527
40k
Navigating Weather and Climate Data
rabernat
0
100
The browser strikes back
jonoalderson
0
370
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
1
99
How to make the Groovebox
asonas
2
1.9k
The Cost Of JavaScript in 2023
addyosmani
55
9.5k
Thoughts on Productivity
jonyablonski
74
5k
Site-Speed That Sticks
csswizardry
13
1.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