網(wǎng)站首頁 編程語言 正文
今天花了兩個小時使用jQuery寫了一個小動畫游戲,如下圖所示,通過鼠標(biāo)點擊,發(fā)射球。
代碼:
<!DOCTYPE html> <html> ?? ?<head> ?? ??? ?<meta charset="utf-8"> ?? ??? ?<title></title> ?? ??? ?<style> ?? ??? ??? ?#main { ?? ??? ??? ??? ?width: 500px; ?? ??? ??? ??? ?height: 650px; ?? ??? ??? ??? ?border: 3px solid #efefef; ?? ??? ??? ??? ?margin: 30px auto; ?? ??? ??? ??? ?position: relative; ?? ??? ??? ?} ?? ??? ??? ?#fireSpace { ?? ??? ??? ??? ?width: 100%; ?? ??? ??? ??? ?height: 400px; ?? ??? ??? ??? ?position: absolute; ?? ??? ??? ??? ?top: 0; ?? ??? ??? ??? ?left: 0; ?? ??? ??? ?} ?? ??? ??? ?#gun { ?? ??? ??? ??? ?display: block; ?? ??? ??? ??? ?width: 60px; ?? ??? ??? ??? ?height: 60px; ?? ??? ??? ??? ?position: absolute; ?? ??? ??? ??? ?bottom: 20px; ?? ??? ??? ??? ?left: 50%; ?? ??? ??? ??? ?transform: translate(-50%, 0); ?? ??? ??? ?} ?? ??? ?</style> ?? ?</head> ?? ?<body> ?? ??? ?<div id="main"> ?? ??? ??? ?<div id="fireSpace"> ?? ??? ??? ?</div> ?? ??? ??? ?<img src="./gun.png" id="gun"> ?? ??? ?</div> ?? ?</body> </html> <script src="./jquery.js"></script> <script> ?? ?let initX = 0, ?? ??? ?initY = 300, ?? ??? ?initDeg = 90, ?? ??? ?thenDeg, gunX, gunY, boo, x = 0, ?? ??? ?y = 300, ?? ??? ?nx, ny, dg = 90, ?? ??? ?ndg, rdg, isLeft0, isLeft; ?? ?document.getElementById("fireSpace").onmousemove = function(e) { ?? ??? ?if (e.offsetX - 220 >= 0) { ?? ??? ??? ?// nx = e.offsetX - 220; ?? ??? ??? ?// ny = 600-e.offsetY; ?? ??? ??? ?gunX = e.offsetX - 220; ?? ??? ??? ?isLeft = false; ?? ??? ?} else if (e.offsetX - 220 <= 0) { ?? ??? ??? ?gunX = 220 - e.offsetX; ?? ??? ??? ?isLeft = true; ?? ??? ?} ?? ??? ?gunY = 650 - e.offsetY; ?? ??? ?if (e.offsetX - 220 == 0) { ?? ??? ??? ?thenDeg = 90; ?? ??? ?} else { ?? ??? ??? ?thenDeg = gunY - gunX >= 0 ? (90 - Math.asin(gunX / gunY) * 180 / Math.PI) : (Math.asin(gunY / gunX) * ?? ??? ??? ??? ?180 / Math.PI); ?? ??? ??? ?// thenDeg = Math.asin(gunY / gunX) * 180 / Math.PI; ?? ??? ?} ?? ??? ?if (initX - 220 == 0) { ?? ??? ??? ?initDeg = 90; ?? ??? ?} else { ?? ??? ??? ?initDeg = initY - initX >= 0 ? (90 - Math.asin(initX / initY) * 180 / Math.PI) : (Math.asin(initY / ?? ??? ??? ??? ??? ?initX) * ?? ??? ??? ??? ?180 / Math.PI); ?? ??? ?} ?? ??? ?if (initY <= 3) { ?? ??? ??? ?initDeg = 0; ?? ??? ?} ?? ??? ?if (gunY <= 3) { ?? ??? ??? ?thenDeg = 0; ?? ??? ?} ?? ??? ?if (!isLeft0 && isLeft) { ?? ??? ??? ?rdg = -(180 - initDeg - thenDeg); ?? ??? ?} else if (isLeft0 && !isLeft) { ?? ??? ??? ?rdg = 180 - initDeg - thenDeg; ?? ??? ?} else if (isLeft0 && isLeft) { ?? ??? ??? ?rdg = (thenDeg - initDeg) ?? ??? ?} else if (!isLeft0 && !isLeft) { ?? ??? ??? ?rdg = (initDeg - thenDeg) ?? ??? ?} ?? ??? ?document.getElementById("gun").style.transform = "translate(-50%, 0) rotate(" + rdg + "deg)"; ?? ??? ?x = nx; ?? ??? ?y = ny; ?? ??? ?isLeft0 = isLeft; ?? ?} ?? ?let fireX,fireY,iX=0,iY=0 ?? ?document.getElementById("fireSpace").onclick = function(e) { ?? ??? ?fireX = e.offsetX; ?? ??? ?fireY = e.offsetY; ?? ??? ?let boll = document.createElement("img"); ?? ??? ?boll.style.width = "50px"; ?? ??? ?boll.style.height = "50px"; ?? ??? ?boll.setAttribute("src", "./boll.png"); ?? ??? ?boll.style.position = "absolute"; ?? ??? ?boll.style.bottom = "0"; ?? ??? ?boll.style.left = "50%"; ?? ??? ?boll.style.transform = "translate(-40%,0)"; ?? ??? ?boll.style.zIndex = "-1"; ?? ??? ?document.getElementById("main").appendChild(boll); ?? ??? ?$(boll).animate({ ?? ??? ??? ?top: fireY, ?? ??? ??? ?left: fireX ?? ??? ?}, 1000); ?? ??? ?setTimeout(function() { ?? ??? ??? ?boll.parentNode.removeChild(boll); ?? ??? ?}, 1000); ?? ?} </script>
圖片素材:
感興趣的的小伙伴可以去試試。
原文鏈接:https://blog.csdn.net/qq_36604863/article/details/122474462
相關(guān)推薦
- 2024-01-27 什么是DTO ,DTO 有什么作用
- 2023-06-17 C語言中#define在多行宏定義出錯的原因及分析_C 語言
- 2024-03-17 Invoke-Expression : 無法將參數(shù)綁定到參數(shù)“Command”,因為該參數(shù)為空字符串
- 2023-02-10 docker實現(xiàn)跨宿主機的容器之間網(wǎng)絡(luò)互聯(lián)_docker
- 2022-08-10 Qt實現(xiàn)簡易計時器的示例代碼_C 語言
- 2022-02-18 解決上傳apk文件后為vnd.android.package-archive格式的問題
- 2023-12-13 python處理Excel文檔(一)合并Excel工作表
- 2022-08-19 Python運行時修改業(yè)務(wù)SQL代碼_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支