如何用vue3实现打砖块小游戏
游戏需求
创建一个场景
创建一个球,创建一堆被打击方块
创建一个可以移动方块并可控制左右移动
当球碰撞左右上边界及移动方块回弹
挡球碰撞下边界游戏结束
完整代码
<template> <button @click="stop">停止</button> <button @click="start">游戏开始</button> <div style="color: red; text-align: center;font-size: 25px">score:{{scroce}}</div> <div class="box" :style="{width :boxWidth +"px", height:boxHeight +"px"}"> <div class="str">{{str}}</div> <div class="kuaiBox"> <div class="kuai" v-for="(item,index) in arr" :key="index" :style="{opacity :item.active ? "0":"1"}"></div> </div> <div class="ball" :style="{left :x + "px", top : y + "px", width : ball +"px", height: ball+"px"}"></div> <div class="bottomMove" :style="{left :mx + "px" , top : my + "px",width :moveBottomW +"px",height : moveBottomH+"px" }"></div> </div> </template> <script setup> import {onMounted, onUnmounted, reactive, toRefs} from "vue" const boxWidth = 500, // 场景宽度 boxHeight = 300, // 场景高度 ball = 10,//小球的宽高 moveBottomH = 5,//移动方块高度 moveBottomW = 100//移动方块快读 const strArr = "恭喜你,挑战成功!!" //用reactive 保存一些可观察信息 const state = reactive({ x: boxWidth / 2 - ball / 2, // 小球x轴位置信息 计算默认位置在中间 y: boxHeight - ball - moveBottomH, // 小球Y轴的位置信息 计算默认位置在中间 mx: boxWidth / 2 - moveBottomW / 2, //移动方块的位置信息 计算默认位置在中间 my: boxHeight - moveBottomH, // 移动方块y轴的的位置信息 计算默认位置在中间 // 被打击方块的数组 arr: Array.from({length: 50}, (_, index) => { return { index, active: false } }), str: "", // 返回挑战成功字眼 scroce: 0 // 分数 }) // 用toRefs将观察对象的信息解构出来供模板使用 const {x, y, mx, my, arr, str, scroce} = toRefs(state) let timer = null, // 小球定时器 speed = 3,// 小球速度 map = {x: 10, y: 10}, timer2 = null, // 挑战成功字眼显示定时器 index = 0//挑战成功字眼续个显示的索引值 // 挑战成功字眼续个显示的方法 const strFun = () => { if (strArr.length === index) clearInterval(timer2) state.str += strArr.substr(index, 1) index++ } //移动小球的方法 // 1.这里同过变量map 对象来记录坐标信息, 确定小球碰到 左右上 及移动方块是否回弹 // 2.循环砖块检测小球碰撞到砖块消失 const moveBall = () => { const {offsetTop, offsetHeight, offsetLeft, offsetWidth} = document.querySelector(".bottomMove") if (state.x <= 0) { map.x = speed } else if (state.x > boxWidth - ball) { map.x = -speed } if (state.y <= 0) { map.y = speed } if (state.y >= offsetTop - offsetHeight && state.y <= offsetTop + offsetHeight && state.x >= offsetLeft && state.x < offsetLeft + offsetWidth) { map.y = -speed } if (state.y > boxHeight) { clearInterval(timer) alert("game over") window.location.reload() } Array.from(state.arr).forEach((item, index) => { const { offsetLeft, offsetTop, offsetWidth, offsetHeight } = document.querySelectorAll(".kuai")[index] if (state.x > offsetLeft && state.x < offsetLeft + offsetWidth && state.y > offsetTop && state.y < offsetTop + offsetHeight) { if (!state.arr[index].active) { state.scroce += 100 } state.arr[index].active = true } }) if (Array.from(state.arr).every(item => item.active)) { clearInterval(timer) timer2 = setInterval(strFun, 1000) } state.x = state.x += map.x state.y = state.y += map.y } //移动方块左右移动方法 ,接住小球 const bottomMove = ev => { if (ev.code === "Space") clearInterval(timer) switch (ev.key) { case "ArrowRight": state.mx += 100 break case "ArrowLeft": state.mx -= 100 break } state.mx = state.mx < 0 ? 0 : state.mx state.mx = state.mx > boxWidth - moveBottomW ? boxWidth - moveBottomW : state.mx } // 暂停游戏 const stop = () => { clearInterval(timer) } // 开始游戏 const start = () => { timer = setInterval(moveBall, 20) } // 绑定移动方块事件 onMounted(() => { document.addEventListener("keyup", bottomMove) }) // 移动出移动方块事件 onUnmounted(() => { clearInterval(timer) }) </script> <style> .bottomMove { width: 100px; height: 10px; background: red; position: absolute; transition-duration: 100ms; transition-timing-function: ease-out; } .ball { width: 20px; height: 20px; background-color: red; border-radius: 50%; position: absolute; } .kuaiBox { display: flex; flex-wrap: wrap; } .kuai { width: 30px; height: 10px; background: red; margin: 10px; transition-duration: 100ms; transition-timing-function: ease-in; } .str { text-align: center; font-size: 50px; color: red; } .box { justify-content: center; width: 500px; height: 500px; margin: 0 auto; position: relative; border: 5px solid red; overflow: hidden; } .picker { width: 50px; height: 50px; } </style>
以上是如何用vue3实现打砖块小游戏的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

tinymce是一个功能齐全的富文本编辑器插件,但在vue中引入tinymce并不像别的Vue富文本插件一样那么顺利,tinymce本身并不适配Vue,还需要引入@tinymce/tinymce-vue,并且它是国外的富文本插件,没有通过中文版本,需要在其官网下载翻译包(可能需要翻墙)。1、安装相关依赖npminstalltinymce-Snpminstall@tinymce/tinymce-vue-S2、下载中文包3.引入皮肤和汉化包在项目public文件夹下新建tinymce文件夹,将下载的

vue3+vite:src使用require动态导入图片报错和解决方法vue3+vite动态的导入多张图片vue3如果使用的是typescript开发,就会出现require引入图片报错,requireisnotdefined不能像使用vue2这样imgUrl:require(’…/assets/test.png’)导入,是因为typescript不支持require所以用import导入,下面介绍如何解决:使用awaitimport

想要实现页面的局部刷新,我们只需要实现局部组件(dom)的重新渲染。在Vue中,想要实现这一效果最简便的方式方法就是使用v-if指令。在Vue2中我们除了使用v-if指令让局部dom的重新渲染,也可以新建一个空白组件,需要刷新局部页面时跳转至这个空白组件页面,然后在空白组件内的beforeRouteEnter守卫中又跳转回原来的页面。如下图所示,如何在Vue3.X中实现点击刷新按钮实现红框范围内的dom重新加载,并展示对应的加载状态。由于Vue3.X中scriptsetup语法中组件内守卫只有o

Vue实现博客前端,需要实现markdown的解析,如果有代码则需要实现代码的高亮。Vue的markdown解析库有很多,如markdown-it、vue-markdown-loader、marked、vue-markdown等。这些库都大同小异。这里选用的是marked,代码高亮的库选用的是highlight.js。具体实现步骤如下:一、安装依赖库在vue项目下打开命令窗口,并输入以下命令npminstallmarked-save//marked用于将markdown转换成htmlnpmins

vue3+ts+axios+pinia实现无感刷新1.先在项目中下载aiXos和pinianpmipinia--savenpminstallaxios--save2.封装axios请求-----下载js-cookienpmiJS-cookie-s//引入aixosimporttype{AxiosRequestConfig,AxiosResponse}from"axios";importaxiosfrom'axios';import{ElMess

最终效果安装VueCropper组件yarnaddvue-cropper@next上面的安装值针对Vue3的,如果时Vue2或者想使用其他的方式引用,请访问它的npm官方地址:官方教程。在组件中引用使用时也很简单,只需要引入对应的组件和它的样式文件,我这里没有在全局引用,只在我的组件文件中引入import{userInfoByRequest}from'../js/api'import{VueCropper}from'vue-cropper&

vue3项目打包发布到服务器后访问页面显示空白1、处理vue.config.js文件中的publicPath处理如下:const{defineConfig}=require('@vue/cli-service')module.exports=defineConfig({publicPath:process.env.NODE_ENV==='production'?'./':'/&

前言无论是vue还是react,当遇到多处重复代码的时候,我们都会想着如何复用这些代码,而不是一个文件里充斥着一堆冗余代码。实际上,vue和react都可以通过抽组件的方式来达到复用,但如果遇到一些很小的代码片段,你又不想抽到另外一个文件的情况下,相比而言,react可以在相同文件里面声明对应的小组件,或者通过renderfunction来实现,如:constDemo:FC=({msg})=>{returndemomsgis{msg}}constApp:FC=()=>{return(
