snake works
This commit is contained in:
136
src/App.vue
136
src/App.vue
@@ -2,9 +2,10 @@
|
|||||||
import { Ref, ref } from 'vue';
|
import { Ref, ref } from 'vue';
|
||||||
|
|
||||||
|
|
||||||
const grid = ref(genGrid(10, 10))
|
const grid = ref(genGrid(30, 30))
|
||||||
const currentCoin = ref(spawnCoin(10, 10))
|
const currentCoin = ref(spawnCoin())
|
||||||
const direction: Ref<'left' | 'right' | 'top' | 'bottom'> = ref('right')
|
const direction: Ref<'left' | 'right' | 'top' | 'bottom' | undefined> = ref('right')
|
||||||
|
const snake = ref(spawnSnake())
|
||||||
|
|
||||||
interface Cell {
|
interface Cell {
|
||||||
x: number
|
x: number
|
||||||
@@ -12,57 +13,145 @@ interface Cell {
|
|||||||
color: 'white' | 'yellow' | 'green'
|
color: 'white' | 'yellow' | 'green'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Snake {
|
||||||
|
cell: Cell
|
||||||
|
history: Cell[]
|
||||||
|
length: number
|
||||||
|
}
|
||||||
|
|
||||||
function genGrid(length: number, height: number) {
|
function genGrid(length: number, height: number) {
|
||||||
const grid: Cell[][] = []
|
const grid: Cell[][] = []
|
||||||
for (let i = 0; i < height; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
const currentRow: Cell[] = []
|
const currentRow: Cell[] = []
|
||||||
for (let j = 0; j < length; j++) {
|
for (let j = 0; j < height; j++) {
|
||||||
currentRow.push({
|
currentRow[j] = {
|
||||||
x: j,
|
x: i,
|
||||||
y: i,
|
y: j,
|
||||||
color: 'white'
|
color: 'white'
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
grid.push(currentRow)
|
grid[i] = currentRow
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
console.log(grid)
|
||||||
|
|
||||||
return grid
|
return grid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function spawnCoin(length: number, height: number) {
|
function spawnCoin() {
|
||||||
const cell = randomCell(length, height)
|
const cell = randomCell()
|
||||||
cell.color = 'yellow'
|
cell.color = 'yellow'
|
||||||
return cell
|
return cell
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function spawnSnake(): Snake {
|
||||||
|
const row = random(grid.value)
|
||||||
|
const cell = random(row)
|
||||||
|
|
||||||
function randomCell(length: number, height: number) {
|
cell.color = 'green'
|
||||||
const y = Math.floor(Math.random() * (height - 1));
|
return {
|
||||||
const x = Math.floor(Math.random() * (length - 1));
|
cell: cell,
|
||||||
|
history: [cell],
|
||||||
return grid.value[y][x]
|
length: 3
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function random<T>(array: Array<T>) {
|
||||||
|
return array[Math.floor(Math.random() * array.length)]
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function randomCell() {
|
||||||
|
const randomRow = random(grid.value)
|
||||||
|
const randomCell = random(randomRow)
|
||||||
|
|
||||||
|
return randomCell
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveSnake(x: number, y: number) {
|
||||||
|
const current = snake.value.cell
|
||||||
|
const newCell = grid.value[current.x + x][current.y + y]
|
||||||
|
|
||||||
|
newCell.color = 'green'
|
||||||
|
snake.value.cell = newCell
|
||||||
|
snake.value.history.push(newCell)
|
||||||
|
|
||||||
|
|
||||||
|
if (snake.value.history.length > length) {
|
||||||
|
const currentLength = snake.value.history.length
|
||||||
|
const allowedLength = snake.value.length
|
||||||
|
const toDelete = snake.value.history.slice(0, currentLength - allowedLength)
|
||||||
|
for (const cell of toDelete) {
|
||||||
|
cell.color = 'white'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
setInterval(() => {
|
||||||
|
|
||||||
|
if (direction.value === undefined) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (direction.value) {
|
||||||
|
case 'left':
|
||||||
|
moveSnake(-1, 0)
|
||||||
|
break
|
||||||
|
case 'right':
|
||||||
|
moveSnake(1, 0)
|
||||||
|
break
|
||||||
|
case 'top':
|
||||||
|
moveSnake(0, -1)
|
||||||
|
break
|
||||||
|
case 'bottom':
|
||||||
|
moveSnake(0, 1)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (currentCoin.value.x === snake.value.cell.x && currentCoin.value.y === snake.value.cell.y) {
|
||||||
|
snake.value.length++
|
||||||
|
currentCoin.value = spawnCoin()
|
||||||
|
}
|
||||||
|
|
||||||
|
}, 500);
|
||||||
|
|
||||||
|
|
||||||
|
function keydown(event: KeyboardEvent) {
|
||||||
|
switch (event.key) {
|
||||||
|
case 'ArrowRight': direction.value = 'right'
|
||||||
|
break
|
||||||
|
case 'ArrowLeft': direction.value = 'left'
|
||||||
|
break
|
||||||
|
case 'ArrowUp': direction.value = 'top'
|
||||||
|
break
|
||||||
|
case 'ArrowDown': direction.value = 'bottom'
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('keydown', keydown)
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="grid">
|
<div class="grid" @keyup="keydown($event)">
|
||||||
<div class="row" v-for="row in grid">
|
<div class="row" v-for="row in grid">
|
||||||
<div class="cell" v-for="cell in row" :style="{backgroundColor: cell.color}">
|
<div class="cell" :class="`cell-${cell.y}-${cell.x}`" v-for="cell in row"
|
||||||
|
:style="{ backgroundColor: cell.color }">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|
||||||
.grid {
|
.grid {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
flex-direction: column;
|
|
||||||
border: 1px solid white;
|
border: 1px solid white;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -70,6 +159,7 @@ function randomCell(length: number, height: number) {
|
|||||||
.row {
|
.row {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
|
flex-direction: column;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,6 +168,4 @@ function randomCell(length: number, height: number) {
|
|||||||
width: 10px;
|
width: 10px;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
Reference in New Issue
Block a user