vue3开发打砖块游戏的思路和方法是什么
Admin 2022-06-02 群英技术资讯 1096 次浏览
这篇文章主要讲解了“vue3开发打砖块游戏的思路和方法是什么”,文中的讲解内容简单、清晰、详细,对大家学习或是工作可能会有一定的帮助,希望大家阅读完这篇文章能有所收获。下面就请大家跟着小编的思路一起来学习一下吧。用vue3写了几个实例,感觉Vue3的composition Api设计得还是很不错,改变了一下习惯,但写多两个就好了。 这次写一个也是儿时很觉得很好玩的游戏-打砖块, 无聊的时候玩一下也觉得挺好玩,游戏性也挺高。这次我直接用vite+vue3打包尝试一下,vite也是开箱即用,特点是也是可以清除死代码,按需打包,所以打包速度也是非常快的。没用过的同学可以尝试用用。

<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>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章我们来了解Nodejs中的文件流,有很多新手对用户Nodejs中的文件流不是很理解的,对此下文给大家来详细的介绍一下,感兴趣的朋友不妨了解看看,对大家会有一定的帮助,那么接下来就跟随小编来一起学习一下吧!
ES6中Set和Map数据结构的简单讲解 目录 Set Map 总结 Set ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set本身是一个构造函数,用来生成 Set 数据结构. const s = new Set(); [2, 3, 5, 4, 5, 2, 2].forEach(x => s.add(x)); for (let i of s) { console.log(i); } // 2 3 5 4 上面代码通过add()方法向 Set 结构加入成员,结果表明 Set 结构不会添加 ...
目录写在前面javscript 中函数和对象的关系面向对象编程(OOP)继承多态封装函数编程编程(FP)闭包和高阶函数柯里化偏函数组合和管道函子写在最后写在前面浏览下文我觉得还是要有些基础的!下文涉及的知识点太多,基本上每一个拿出来都能写几篇文章,我在写文章的过程中只是做了简单的实现,我只是提供了一个思路,更多的细节还
JavaScript实现乘和加的方法:1、通过function创建add和take函数,并设置两个参数;2、设置相加和相乘的数学公式;3、输入参数并在浏览器中输出计算结果即可。
在JavaScript中也有this关键字,器作用是函数调用上下文,而this的行为是比较复杂,在JavaScript面试上常会遇到,也是很多朋友很难理解的一个知识点,这篇文章就给大家分享一些关于JS中this关键词的面试题,对大家学习和理解this有一定的参考价值。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008