面试题之用Vue手写贪吃蛇
- 做了一半,还犯了一个致命问题
- 以下是试后自己整理的代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
}
#grid {
background-color: #ddd;
width: 500px;
height: 500px;
position: relative;
}
#grid .ele {
background-color: #000;
position: absolute;
border: 1px solid #ccc;
box-sizing: border-box;
width: 10px;
height: 10px;
}
#grid .food {
background-color: red;
position: absolute;
border: 1px solid #ccc;
box-sizing: border-box;
width: 10px;
height: 10px;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!--
50*50
5
-->
</head>
<body>
<div id="app">
<div id="grid">
<div class="ele" v-for="(item, index) in snake" :key="`snake${index}`" :style="locationCompute(item.x, item.y)"></div>
<div class="food" v-for="(item, index) in foods" :key="`food${index}`" :style="locationCompute(item.x, item.y)"></div>
</div>
<br>
<hr