snake can now die, score is available
This commit is contained in:
45
src/App.vue
45
src/App.vue
@@ -1,11 +1,11 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { Ref, ref } from 'vue';
|
import { Ref, ref } from 'vue';
|
||||||
|
|
||||||
|
|
||||||
const grid = ref(genGrid(30, 30))
|
const grid = ref(genGrid(30, 30))
|
||||||
const currentCoin = ref(spawnCoin())
|
const currentCoin = ref(spawnCoin())
|
||||||
const direction: Ref<'left' | 'right' | 'top' | 'bottom' | undefined> = ref('right')
|
const direction: Ref<'left' | 'right' | 'top' | 'bottom' | undefined> = ref(undefined)
|
||||||
const snake = ref(spawnSnake())
|
const snake = ref(spawnSnake())
|
||||||
|
const score = ref(0)
|
||||||
|
|
||||||
interface Cell {
|
interface Cell {
|
||||||
x: number
|
x: number
|
||||||
@@ -72,13 +72,17 @@ function randomCell() {
|
|||||||
|
|
||||||
function moveSnake(x: number, y: number) {
|
function moveSnake(x: number, y: number) {
|
||||||
const current = snake.value.cell
|
const current = snake.value.cell
|
||||||
const newCell = grid.value[current.x + x][current.y + y]
|
const newCell = getCell(current.x + x, current.y + y);
|
||||||
|
|
||||||
|
if (newCell === undefined || newCell.color === 'green') {
|
||||||
|
death()
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
newCell.color = 'green'
|
newCell.color = 'green'
|
||||||
snake.value.cell = newCell
|
snake.value.cell = newCell
|
||||||
snake.value.history.push(newCell)
|
snake.value.history.push(newCell)
|
||||||
|
|
||||||
|
|
||||||
if (snake.value.history.length > length) {
|
if (snake.value.history.length > length) {
|
||||||
const currentLength = snake.value.history.length
|
const currentLength = snake.value.history.length
|
||||||
const allowedLength = snake.value.length
|
const allowedLength = snake.value.length
|
||||||
@@ -90,6 +94,36 @@ function moveSnake(x: number, y: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function getCell(x: number, y: number) {
|
||||||
|
try {
|
||||||
|
return grid.value[x][y]
|
||||||
|
} catch {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function death() {
|
||||||
|
killSnake()
|
||||||
|
killCoin()
|
||||||
|
currentCoin.value = spawnCoin()
|
||||||
|
snake.value = spawnSnake()
|
||||||
|
direction.value = undefined;
|
||||||
|
score.value = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
function killSnake() {
|
||||||
|
snake.value.history
|
||||||
|
for (const cell of snake.value.history) {
|
||||||
|
cell.color = 'white'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function killCoin() {
|
||||||
|
const coinCell = grid.value[currentCoin.value.x][currentCoin.value.y]
|
||||||
|
coinCell.color = 'white'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
|
|
||||||
if (direction.value === undefined) {
|
if (direction.value === undefined) {
|
||||||
@@ -115,8 +149,8 @@ setInterval(() => {
|
|||||||
if (currentCoin.value.x === snake.value.cell.x && currentCoin.value.y === snake.value.cell.y) {
|
if (currentCoin.value.x === snake.value.cell.x && currentCoin.value.y === snake.value.cell.y) {
|
||||||
snake.value.length++
|
snake.value.length++
|
||||||
currentCoin.value = spawnCoin()
|
currentCoin.value = spawnCoin()
|
||||||
|
score.value++
|
||||||
}
|
}
|
||||||
|
|
||||||
}, 500);
|
}, 500);
|
||||||
|
|
||||||
|
|
||||||
@@ -138,6 +172,7 @@ window.addEventListener('keydown', keydown)
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
<h1>Score {{ score }}</h1>
|
||||||
<div class="grid" @keyup="keydown($event)">
|
<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" :class="`cell-${cell.y}-${cell.x}`" v-for="cell in row"
|
<div class="cell" :class="`cell-${cell.y}-${cell.x}`" v-for="cell in row"
|
||||||
|
|||||||
Reference in New Issue
Block a user