javaScript實(shí)現(xiàn)網(wǎng)頁(yè)版的彈球游戲
利用javeScript對(duì)象以及方法實(shí)現(xiàn)的網(wǎng)頁(yè)彈球游戲,供大家參考,具體內(nèi)容如下
<!DOCTYPE html><html><head><tilie>呼呼哈嘿的網(wǎng)頁(yè)彈球</title></head><body><canvas id='canvas'width='400'height='400'></canvas> <script src='https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js'></script><script>var canv=document.getElementById('canvas');var ctx=canv.getContext('2d');//創(chuàng)建一個(gè)小球?qū)ο髒ar ball={x: 100,y: 100,xSpeed: -5,ySpeed: -5};//定義繪制小球的方法ball.draw=function(){ ctx.beginPath(); ctx.arc(this.x,this.y,10,0,Math.PI*2,false); ctx.fill();};//定義小球運(yùn)動(dòng)的方法ball.move=function(){ this.x =this.x+this.xSpeed; this.y =this.y+this.ySpeed;};//邊界判斷ball.checkCanvas=function(panelStart,panelEnd){ if(this.x<0||this.x>400) this.xSpeed=-this.xSpeed;if(this.y<0) this.ySpeed=-this.ySpeed;if(this.y>390){ if(this.x>panelStart && this.x<panelEnd) this.ySpeed=-this.ySpeed; else{ alert('GAME OVER!!!'); this.x= 50; this.y=100;}}};//定時(shí)功能使得小球動(dòng)起來(lái)setInterval(function(){ ctx.clearRect(0,0,400,400); ball.draw(); panel.draw(); ball.move(); ball.checkCanvas(panel.x,panel.x+panel.xSize); ctx.strokeRect(0,0,400,400);},30); //定時(shí)函數(shù),每30ms執(zhí)行一次大括號(hào)中的代碼;//創(chuàng)建擋板的對(duì)象;var panel ={ x:200, y:390, xSize: 50, ySize: 5};//擋板移動(dòng)方法panel.draw=function(){ ctx.fillRect(this.x,this.y,this.xSize,this.ySize);};//利用jquery實(shí)現(xiàn)按鍵交互;$('body').keydown(function(event){console.log(event.keyCode);if(event.keyCode==37){ panel.x=panel.x-5; if(panel.x<0){panel.x=0; } }if(event.keyCode==39){ panel.x=panel.x+5;if(panel.x>400-50){panel.x=350; } }} );</script></body></html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 詳解Android studio 動(dòng)態(tài)fragment的用法2. 編程語(yǔ)言PHP在Web開(kāi)發(fā)領(lǐng)域的優(yōu)勢(shì)在哪?3. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)4. 什么是python的自省5. 解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題6. Android如何加載Base64編碼格式圖片7. 基于android studio的layout的xml文件的創(chuàng)建方式8. Springboot Druid 自定義加密數(shù)據(jù)庫(kù)密碼的幾種方案9. 圖文詳解vue中proto文件的函數(shù)調(diào)用10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備