




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、JavaScript小游戲?qū)嵗汉唵蔚逆I盤練習(xí)鍵盤是一種常用的輸入設(shè)備, 靈活熟練地使用鍵盤進(jìn)行輸入是計算機(jī)用戶需掌握的一門基本功。下面我們編寫一個簡單的鍵盤練習(xí)游戲。1刺破氣泡交互式小動畫在編寫簡單的鍵盤練習(xí)游戲之前,先設(shè)計一個簡單地刺破氣泡交互式小動畫。在面板底部逐個上升一些氣泡, 用鼠標(biāo)在某個氣泡上單擊, 該氣泡被刺破, 刺破后的小氣泡逐漸消散在面板中。交互式效果如圖 1 所示。圖 1刺破氣泡交互式動畫一個氣泡可分為兩個狀態(tài): ( 1)氣泡從面板底部上升; ( 2)氣泡被鼠標(biāo)單擊刺破成小氣泡或氣泡上升越過了面板頂部消散了。為此抽象出兩個對象類: Bubbles 和 miniBubbles
2、 。其中, Bubbles 用于表示一個未被刺破的氣泡, miniBubbles 用于表示一個氣泡刺破后得到的逐漸消散的小氣泡。Bubbles 對象類定義 6 個屬性:表示氣泡圓心的坐標(biāo)( x,y)、氣泡的半徑時垂直方向的位移改變量 ySpeed、氣泡上升加速度 gravity 和氣泡顏色 color 。坐標(biāo)屬性值 y的初始值取畫布的高度, 表示氣泡從游戲面板底部開始上升,的初始值采用隨機(jī)數(shù)確定或直接指定。具體定義如下:radius、上升其余各屬性function Bubbles()this.x = rand(30,canvas.width - 30);this.y = canvas.heig
3、ht;this.radius = rand(15, 30);this.color =rgba(255, 255, 255, 0.75);this.ySpeed= Math.random() * 2;this.gravity = 0.01;Bubbles 對象類定義 2 個方法:繪制氣泡的方法draw() 、氣泡上升時坐標(biāo)改變方法update()。miniBubbles 對象類定義8 個屬性: 表示小氣泡圓心的坐標(biāo) ( x,y )、小氣泡半徑radius、散開時水平和垂直方向的位移改變量xSpeed 和 ySpeed、小氣泡的填充color 、小氣泡的減速度 gravity 、小氣泡的存活時間t
4、imeToLive 。具體定義如下:function miniBubbles(x,y,radius)this.x = x;this.y = y;this.radius = radius;this.color = rgba(255, 255, 255, 0.5);this.xSpeed=(Math.random() - 0.5) * 0.6;this.ySpeed=(Math.random() - 1) * 0.5;this.gravity = -0.03;this.timeToLive = 100;miniBubbles 對象類定義2 個方法:繪制小氣泡的方法draw() 、小氣泡位置改變改變
5、方法 update()。小氣泡每次改變位置后, timeToLive 減 1,當(dāng) timeToLive 值等于 0 時,從小氣泡數(shù)組中刪除該小氣泡,表示該小氣泡已消亡。定義兩個數(shù)組var bubbles = ; 和 var minibubbles = ;分別存儲未刺破的大氣泡對象和大氣泡刺破后散開的小氣泡。為畫布添加鼠標(biāo)按下事件監(jiān)控canvas.addEventListener(mousedown, function(); ,在事件處理函數(shù)中查找鼠標(biāo)單擊的氣泡,然后將該氣泡從bubbles 數(shù)組中刪除, 向 minibubbles數(shù)組中添加若干個散開的小氣泡。完整的 HTML 代碼如下。 刺破
6、氣泡小游戲var canvas = document.getElementById(myCanvas);var ctx = canvas.getContext(2d);canvas.height = innerHeight;canvas.width = innerWidth;function rand(min, max)return Math.floor(Math.random() * (max - min + 1) + min);function Bubbles()this.x = rand(30,canvas.width - 30);this.y = canvas.height;this.
7、radius = rand(15, 30);this.color =rgba(255, 255, 255, 0.75);this.ySpeed= Math.random() * 2;this.gravity = 0.01;Btotype.draw = function ()ctx.beginPath();ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);ctx.fillStyle = this.color;ctx.fill();ctx.closePath();Btotype.updat
8、e = function ()this.y -= this.ySpeed;if (this.y - this.radius 0)this.ySpeed += this.gravity;this.draw();function miniBubbles(x,y,radius)this.x = x;this.y = y;this.radius = radius;this.color = rgba(255, 255, 255, 0.5);this.xSpeed=(Math.random() - 0.5) * 0.6;this.ySpeed=(Math.random() - 1) * 0.5;this.
9、gravity = -0.03;this.timeToLive = 100;miniBtotype.draw = function () ctx.beginPath();ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);ctx.fillStyle = this.color;ctx.fill();ctx.closePath();miniBtotype.update = function () if (this.y - this.radius 0)this.ySpeed += this.g
10、ravity;this.x += this.xSpeed;this.y += this.ySpeed;this.timeToLive -;this.draw();var backgroundGradient = ctx.createLinearGradient(0, 0, 0, canvas.height);backgroundGradient.addColorStop(0, #009cff)backgroundGradient.addColorStop(1, #007bff) var bubbles = ;var minibubbles = ;var timer = 0;var spawnR
11、ate = 70;function animate()requestAnimationFrame(animate);ctx.fillStyle = backgroundGradient;ctx.fillRect(0, 0, canvas.width, canvas.height);for (var i=bubbles.length-1;i=0;i-)bubblesi.update();if (bubblesi.y=0;i-)minibubblesi.update();if (minibubblesi.timeToLive = 0)minibubbles.splice(i, 1);timer+;
12、if (timer=spawnRate)bubbles.push(new Bubbles();timer=0;spawnRate = rand(50, 100);canvas.addEventListener(mousedown, function()var x = event.pageX - canvas.getBoundingClientRect().left;var y = event.pageY - canvas.getBoundingClientRect().top;查找被單擊的氣泡for (var i=bubbles.length-1; i=0; i-)var bubble = b
13、ubblesi;var dist = Math.sqrt(Math.pow(bubble.x-x,2)+ Math.pow(bubble.y- y,2);if (dist= bubble.radius)var mx = bubble.x;var my = bubble.y;var mr = rand(2,5);bubbles.splice(i, 1)for (var k = 0; k bubble.radius/mr; k+)minibubbles.push(new miniBubbles(mx,my, mr);return;);animate();2.簡單的鍵盤練習(xí)小游戲有了上面的基礎(chǔ), 我
14、們可以編寫一個簡單的鍵盤練習(xí)小游戲,大小寫字母出現(xiàn)在游戲面板中,按鍵盤某個字母鍵后,對應(yīng)的字母消失。游戲過程如圖2 所示。圖 2 鍵盤練習(xí)小游戲在 Bubbles 對象類中增加一個屬性 letter ,表示氣泡中顯示的字母。為Windows 添加鍵盤按下 keypress 事件監(jiān)聽,處理鍵盤按鍵。完整的 HTML 代碼如下。 簡單鍵盤練習(xí) var canvas = document.getElementById(myCanvas);var ctx = canvas.getContext(2d);canvas.height = innerHeight;canvas.width = innerWi
15、dth;var str=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz;var cnt1=0;var cnt2=0;var cnt3=0;function rand(min, max)return Math.floor(Math.random() * (max - min + 1) + min);function Bubbles()this.x = rand(30,canvas.width - 30);this.y = canvas.height;this.radius = 20;this.color =rgba(255, 255, 2
16、55, 0.75);this.ySpeed= Math.random() * 2;this.gravity = 0.01;this.letter=str.charAt(rand(0,str.length-1);Btotype.draw = function ()ctx.beginPath();ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);ctx.fillStyle = this.color;ctx.fill();ctx.closePath();ctx.font = Bold 20px Georgia;
17、ctx.fillStyle = Black;ctx.textAlign = center;ctx.baseline = middle;ctx.fillText(this.letter,this.x, this.y);Btotype.update = function ()this.y -= this.ySpeed;if (this.y - this.radius 0)this.ySpeed += this.gravity;this.draw();function miniBubbles(x,y,radius)this.x = x;this.y = y;this.radius
18、 = radius;this.color = rgba(255, 255, 255, 0.5);this.xSpeed=(Math.random() - 0.5) * 0.6;this.ySpeed=(Math.random() - 1) * 0.5;this.gravity = -0.03;this.timeToLive = 100;miniBtotype.draw = function () ctx.beginPath();ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);ctx.fillStyle
19、= this.color;ctx.fill();ctx.closePath();miniBtotype.update = function () if (this.y - this.radius 0)this.ySpeed += this.gravity;this.x += this.xSpeed;this.y += this.ySpeed;this.timeToLive -;this.draw();var backgroundGradient = ctx.createLinearGradient(0, 0, 0, canvas.height);backgroundGrad
20、ient.addColorStop(0, #009cff)backgroundGradient.addColorStop(1, #007bff) var bubbles = ;var minibubbles = ;var timer = 0;var spawnRate = 70;function animate()requestAnimationFrame(animate);ctx.fillStyle = backgroundGradient;ctx.fillRect(0, 0, canvas.width, canvas.height);ctx.font = Bold 30px Georgia;ctx.fillStyle = Black;ctx.textAlign = center;ctx.baseline = middle;var mess=正確按鍵次數(shù): +cnt1+無效
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年合同到期解約申請模板
- 2025年藥店店員合同模板
- 一年級下冊數(shù)學(xué)教案-兩位數(shù)加減整十?dāng)?shù)、一位數(shù)的口算 (20)-西師大版
- 分?jǐn)?shù)的初步認(rèn)識(一)練習(xí)十一(教案)2024-2025學(xué)年數(shù)學(xué)三年級上冊 蘇教版
- 2024年人工種植牙項目投資申請報告代可行性研究報告
- 2025年杭州科技職業(yè)技術(shù)學(xué)院單招職業(yè)傾向性測試題庫1套
- 2025屆黑龍江省“六校聯(lián)盟”高三上學(xué)期聯(lián)考化學(xué)試題及答案
- 2025年度教師專業(yè)成長路徑規(guī)劃聘用合同
- 2025年度養(yǎng)老產(chǎn)業(yè)簡易版股份轉(zhuǎn)讓合同模板
- 2025年度文化旅游產(chǎn)業(yè)合作授權(quán)委托書
- 生產(chǎn)車間布局優(yōu)化與現(xiàn)場改善的策略研究
- 三方公司合作協(xié)議書范本
- 護(hù)理責(zé)任組長續(xù)聘競聘
- 2024-2025學(xué)年第二學(xué)期教學(xué)教研工作安排表
- 2025年貴州云上產(chǎn)業(yè)服務(wù)有限公司招聘筆試參考題庫含答案解析
- 2025年南京信息職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性測試近5年??及鎱⒖碱}庫含答案解析
- 2025-2030年中國天然氣行業(yè)發(fā)展分析及發(fā)展趨勢預(yù)測報告
- 《雷達(dá)信號處理基礎(chǔ)》課件
- 2025屆貴州省興義市三年級數(shù)學(xué)第一學(xué)期期末達(dá)標(biāo)檢測試題含解析
- 人教版地理七年級下冊7.1.2 亞洲的自然環(huán)境(課件39張)
- 2025年交通運(yùn)輸部廣州打撈局招聘事業(yè)編制人員13人歷年管理單位筆試遴選500模擬題附帶答案詳解
評論
0/150
提交評論