当前位置:   article > 正文

uni app 钓鱼小游戏

uni app 钓鱼小游戏

最近姑娘喜欢玩那个餐厅游戏里的钓鱼  ,经常让看广告,然后就点点点...

自己写个吧。小鱼的图片自己搞。 有问题自己改,不要私信我

  1. <template>
  2. <view class="page_main">
  3. <view class="top_linear">
  4. <view class="top_mean_left">
  5. <text>以钓到 </text>
  6. <text class="top_mean_tv">{{ydlist.length}} </text>
  7. </view>
  8. <view class="top_mean_center">
  9. <text> </text>
  10. <text class="top_mean_tv"></text>
  11. </view>
  12. <view class="top_mean_right">
  13. <text> </text>
  14. <text class="top_mean_tv"></text>
  15. </view>
  16. <view class="top_mean_right">
  17. <text> </text>
  18. <text class="top_mean_tv"> </text>
  19. </view>
  20. </view>
  21. <view class="contentone">
  22. <canvas class="canvas_cla" canvas-id="myCanvas" id="myCanvas"></canvas>
  23. </view>
  24. <view class="contenttwo">
  25. <canvas class="canvas_cla" canvas-id="myCanvass" @touchstart="touchstart" @touchend="touchend"
  26. id="myCanvass" @touchmove="touchmove"></canvas>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. data() {
  33. return {
  34. canvasWidth: 0,
  35. canvasHeight: 0,
  36. shuishen: 0,
  37. ydlist: [],
  38. dyl: {
  39. dylimg: '../static/ship.png',
  40. dylx: 0,
  41. dyly: 0,
  42. dylsize: 50,
  43. dqjl: 100,
  44. speedx: 2,
  45. yg: {
  46. image: "../static/yg.png",
  47. size: 15,
  48. sxspeedy: 5
  49. }
  50. },
  51. ydtimerX: '',
  52. ytimerX: '',
  53. ylist: [],
  54. yimages: ['../static/yu1.png', '../static/yu2.png', '../static/yu3.png',
  55. '../static/yu4.png', '../static/yu5.png', '../static/yu6.png',
  56. '../static/yu7.png', '../static/yu8.png', '../static/yu9.png',
  57. '../static/yu10.png', '../static/yu11.png', '../static/yu12.png', '../static/yu13.png',
  58. '../static/yu14.png', '../static/yu15.png', '../static/yu16.png', '../static/yu17.png',
  59. '../static/yu18.png'
  60. ],
  61. ygy: 100,
  62. istddyl: false,
  63. isxg: false,
  64. iswddl: false,
  65. };
  66. },
  67. onLoad() {
  68. this.dyl.yxstartx = this.dyl.dylx + this.dyl.dylsize
  69. this.dyl.dyly = this.canvasHeight - this.shuishen - this.dyl.dylsize
  70. this.dyl.yxstarty = this.dyl.dyly + this.dyl.dylsize - 10
  71. console.log(JSON.stringify(this.ylist))
  72. },
  73. onReady() {
  74. const ctx = uni.createCanvasContext('myCanvas');
  75. const ctxtwo = uni.createCanvasContext('myCanvass');
  76. this.ctx = ctx
  77. this.ctxtwo = ctxtwo
  78. const canvas = uni.createSelectorQuery().select('#myCanvas');
  79. canvas.boundingClientRect(rect => {
  80. this.canvasWidth = rect.width
  81. this.canvasHeight = rect.height
  82. this.shuishen = rect.height - 100
  83. this.addy()
  84. }).exec();
  85. this.drawyu()
  86. this.drawdyl()
  87. this.yddyl()
  88. this.ydy()
  89. },
  90. /**
  91. * 生命周期函数--监听页面卸载
  92. */
  93. onUnload: function() {
  94. },
  95. methods: {
  96. /**
  97. * 触摸屏幕开始 记录开始坐标
  98. */
  99. touchstart: function(e) {
  100. let startx = e.touches[0].x
  101. let starty = e.touches[0].y
  102. console.log(startx, starty)
  103. if (starty < this.canvasHeight - this.shuishen) {
  104. if (!this.isxg) {
  105. console.log("拖动钓鱼老")
  106. this.istddyl = true
  107. clearInterval(this.ydtimerX);
  108. this.dyl.dylx = startx
  109. this.drawdyl();
  110. }
  111. } else {
  112. if (!this.isxg) {
  113. this.ygy = starty
  114. this.isxg = true
  115. this.dyl.dqjl = this.getRandomInt(40, 100)
  116. }
  117. }
  118. },
  119. /**
  120. * 触摸屏幕结束 计算
  121. */
  122. touchend: function(e) {
  123. let x = e.changedTouches[0].x
  124. let y = e.changedTouches[0].y
  125. if (!this.isxg) {
  126. if (this.istddyl) {
  127. this.istddyl = false
  128. this.yddyl()
  129. }
  130. } else {
  131. }
  132. },
  133. /**
  134. * 触摸屏幕移动 画框
  135. */
  136. touchmove: function(e) {
  137. let x = e.touches[0].x
  138. let y = e.touches[0].y
  139. if (!this.isxg) {
  140. if (this.istddyl) {
  141. this.dyl.dylx = x
  142. this.drawdyl();
  143. }
  144. }
  145. },
  146. addy: function() {
  147. console.log("添加" + this.ylist.length)
  148. let count = 10 - this.ylist.length
  149. console.log("添加" + count)
  150. console.log(this.canvasHeight, this.shuishen, this.canvasHeight)
  151. // console.log(this.canvasHeight - this.shuishen + 50, this.canvasHeight - 50)
  152. for (var i = 0; i <= count; i++) {
  153. let bean = {}
  154. bean.size = this.getRandomInt(20, 40)
  155. bean.x = [0, this.canvasWidth - bean.size][this.getRandomInt(0, 1)]
  156. bean.y = this.getRandomInt(this.canvasHeight - this.shuishen + 50, this.canvasHeight - 80)
  157. bean.image = this.yimages[this.getRandomInt(0, this.yimages.length - 1)]
  158. bean.dqjl = this.getRandomInt(0, 100)
  159. bean.speedx = this.getRandomInt(1, 3)
  160. bean.isbdd = false
  161. this.ylist.push(bean)
  162. }
  163. },
  164. getRandomInt: function(min, max) {
  165. min = Math.ceil(min);
  166. max = Math.floor(max);
  167. return Math.floor(Math.random() * (max - min + 1)) + min;
  168. },
  169. drawdyl: function() {
  170. this.ctxtwo.drawImage("../static/shanshui.png", 0,
  171. 0, this.canvasWidth, this.canvasHeight - this.shuishen);
  172. var dyly = this.canvasHeight - this.shuishen - this.dyl.dylsize
  173. this.ctxtwo.drawImage(this.dyl.dylimg, this.dyl.dylx, dyly, this
  174. .dyl.dylsize, this.dyl.dylsize);
  175. this.ctxtwo.draw(); // 绘制到 canvas 上
  176. // 绘制鱼线(假设是简单的直线)
  177. this.ctxtwo.beginPath();
  178. this.ctxtwo.moveTo(this.dyl.dylx + this.dyl.dylsize, dyly + this.dyl.dylsize - 10); // 起点位置
  179. this.ctxtwo.lineTo(this.dyl.dylx + this.dyl.dylsize, this.ygy); // 终点位置
  180. this.ctxtwo.setStrokeStyle('#000000'); // 设置线条颜色
  181. this.ctxtwo.setLineWidth(2); // 设置线条宽度
  182. this.ctxtwo.stroke(); // 绘制线条
  183. this.ctxtwo.drawImage(this.dyl.yg.image, this.dyl.dylx + this.dyl.dylsize - this.dyl.yg.size / 3,
  184. this.ygy, this.dyl.yg.size,
  185. this.dyl.yg.size);
  186. // 将绘图上下文的内容绘制到 canvas 中
  187. this.ctxtwo.draw(true);
  188. },
  189. drawyu: function() {
  190. // 设置填充颜色
  191. this.ctx.setFillStyle('#1E90FF');
  192. // 绘制一个填充的长方形,参数分别为:x坐标,y坐标,宽度,高度
  193. this.ctx.fillRect(0, this.canvasHeight - this.shuishen, this.canvasWidth, this.shuishen);
  194. // 将绘制内容绘制到画布上
  195. for (var i = 0; i < this.ylist.length; i++) {
  196. var bean = this.ylist[i]
  197. this.ctx.drawImage(bean.image, bean.x, bean.y, bean.size, bean.size);
  198. }
  199. this.ctx.draw(); // 绘制到 canvas 上
  200. },
  201. ydy: function() {
  202. let that = this;
  203. let pzcount = 0
  204. // 在x轴上反弹
  205. this.ytimerX = setInterval(() => {
  206. for (var i = 0; i < this.ylist.length; i++) {
  207. var bean = this.ylist[i]
  208. if (bean.isbdd) {
  209. if (bean.y > 100) {
  210. bean.y = this.ygy - this.dyl.yg.sxspeedy
  211. } else {
  212. this.ydlist.push(JSON.parse(JSON.stringify(bean)))
  213. console.log("收线完成", this.ydlist.length)
  214. this.dyl.yg.sxspeedy = 5
  215. this.iswddl = false
  216. this.ylist.splice(i, 1)
  217. this.addy()
  218. }
  219. } else {
  220. bean.x = bean.x + bean.speedx
  221. if (bean.x < 0 || bean.x + bean.size > that.canvasWidth) {
  222. bean.speedx = -bean.speedx;
  223. // bean.isReversed = !bean.isReversed
  224. }
  225. }
  226. }
  227. that.drawyu();
  228. }, 100); //100毫秒更新一次位置
  229. },
  230. yddyl: function() {
  231. let that = this;
  232. let pzcount = 0
  233. // 在x轴上反弹
  234. this.ydtimerX = setInterval(() => {
  235. if (!that.isxg) {
  236. that.dyl.dylx = that.dyl.dylx + that.dyl.speedx;
  237. if (that.dyl.dylx < 0 || that.dyl.dylx + that.dyl.dylsize > that.canvasWidth) {
  238. that.dyl.speedx = -that.dyl.speedx;
  239. }
  240. } else {
  241. if (that.ygy > 100) {
  242. that.ygy = that.ygy - that.dyl.yg.sxspeedy
  243. if (!that.iswddl) {
  244. console.log("没调到")
  245. that.iswddl = that.isColliding()
  246. } else {
  247. console.log("调到")
  248. }
  249. } else {
  250. that.isxg = false
  251. that.ygy = 100
  252. that.iswddl = false
  253. }
  254. }
  255. that.drawdyl();
  256. }, 100); //100毫秒更新一次位置
  257. },
  258. isColliding: function() {
  259. let ygx = this.dyl.dylx + this.dyl.dylsize
  260. let ygy = this.ygy
  261. let ygsize = this.dyl.yg.size
  262. for (var i = 0; i < this.ylist.length; i++) {
  263. let bean = this.ylist[i]
  264. if (ygx < bean.x + bean.size && ygx + ygsize > bean.x && ygy < bean.y + bean
  265. .size && ygy + ygsize > bean.y) {
  266. if (this.dyl.dqjl + bean.dqjl >= 100) {
  267. bean.isbdd = true
  268. this.dyl.yg.sxspeedy = 45 - bean.size
  269. return true
  270. }
  271. }
  272. }
  273. return false
  274. },
  275. }
  276. };
  277. </script>
  278. <style scoped>
  279. .contentone {
  280. width: 100vw;
  281. height: 100vh;
  282. position: absolute;
  283. top: 80rpx;
  284. display: flex;
  285. justify-content: center;
  286. }
  287. .contenttwo {
  288. width: 100vw;
  289. height: 100vh;
  290. position: absolute;
  291. top: 80rpx;
  292. display: flex;
  293. justify-content: center;
  294. }
  295. .canvas_cla {
  296. width: 100vw;
  297. height: 100vh;
  298. }
  299. .top_linear {
  300. display: flex;
  301. flex-direction: row;
  302. justify-content: space-between;
  303. display: flex;
  304. flex-direction: row;
  305. height: 80rpx;
  306. }
  307. .top_mean_left {
  308. margin-left: 32rpx;
  309. }
  310. .top_mean_right {
  311. margin-right: 32rpx;
  312. }
  313. .top_mean_tv {
  314. color: #07a5a6;
  315. }
  316. .page_main {
  317. width: 100vw;
  318. height: 100vh;
  319. background-color: #efefef;
  320. }
  321. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/248174
推荐阅读
相关标签
  

闽ICP备14008679号