我曾介绍过,我是使用Mac mini m4 32G内存+256G硬盘,国补5969到手的电脑上本地部署了DeepSeek r1-32B模型,每秒大约8-10个tokens,能接受使用状态,我一直认为这个就是目前市面上能部署32b的最优方案。
今天我们来测试一下这个模型的代码能力。
问:帮忙用VUE写一个可以在本地运行的贪吃蛇小游戏
DeepSeek 自我思考中:
嗯,用户让我帮忙用Vue写一个可以在本地运行的贪吃蛇小游戏。
好的,我得先想想怎么开始。 首先,我应该确定贪吃蛇的基本功能。贪吃蛇通常有蛇身、食物、方向控制,还有游戏结束的条件。那在Vue中,这些元素该怎么实现呢?
我觉得可以用Canvas来画蛇和食物。这样比较灵活,也能控制细节。然后,用数据响应式地管理蛇的位置、食物的位置,以及得分。
接下来是结构。我应该创建一个Vue组件,里面有template部分,显示游戏区域和得分。在script里,初始化蛇、方向、食物等变量。还要写定时器来控制蛇的移动速度。 用户可能需要键盘事件来控制方向,所以得监听keydown事件,改变方向变量。同时要注意避免反向移动,比如当前往右,不能马上按左键让蛇掉头。 然后是碰撞检测,包括边界和自身。如果蛇碰到墙壁或者自己,游戏就结束,弹出提示并重置状态。 食物的生成也很重要,要确保生成的位置不在蛇身上。吃掉食物后,得分增加,蛇变长,并生成新的食物。 最后,用户可能需要一个开始/重新开始的功能,所以我得写个方法来初始化游戏变量,可能还有按钮来触发。
总结一下,步骤大概是:创建Vue组件,设置Canvas,管理数据状态,处理移动逻辑和碰撞检测,以及食物的生成。然后把这些组合在一起,测试是否能在本地运行。
DeepSeek正式回复:
以下是一个用 Vue 3 实现的贪吃蛇小游戏代码,包含了基本的功能如:蛇的移动、食物生成、得分统计等。
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Snake Game</title>
<script src="https://unpkg.com/vue@next"></script>
<style>
.game-container {
display: flex;
flex-direction: column;
align-items: center;
}
#game-board {
border: 2px solid black;
background-color: white;
}
.score {
margin-top: 10px;
font-size: 24px;
}
</style>
</head>
<body>
<div id="app">
<div class="game-container">
<canvas id="game-board" ref="board"></canvas>
<div class="score">Score: {{ score }}</div>
</div>
</div>
<script>
const { createApp } = Vue;
const app = createApp({
data() {
return {
snake: [
{ x: 5, y: 5 },
{ x: 4, y: 5 },
{ x: 3, y: 5 }
],
food: { x: 10, y: 10 },
direction: 'right',
score: 0,
gameBoardWidth: 20,
gameBoardHeight: 20,
gameSpeed: 100
};
},
mounted() {
this.initGame();
},
methods: {
initGame() {
const canvas = this.$refs.board;
const ctx = canvas.getContext('2d');
// 设置画布大小
canvas.width = 400;
canvas.height = 400;
// 游戏主循环
setInterval(() => {
this.move();
this.draw(ctx);
this.checkCollision();
this.generateFood();
}, this.gameSpeed);
// 添加键盘事件监听
document.addEventListener('keydown', (e) => {
switch(e.key) {
case 'ArrowUp':
if(this.direction !== 'down') this.direction = 'up';
break;
case 'ArrowDown':
if(this.direction !== 'up') this.direction = 'down';
break;
case 'ArrowLeft':
if(this.direction !== 'right') this.direction = 'left';
break;
case 'ArrowRight':
if(this.direction !== 'left') this.direction = 'right';
break;
}
});
},
move() {
const head = {...this.snake[0]};
switch(this.direction) {
case 'up':
head.y--;
break;
case 'down':
head.y++;
break;
case 'left':
head.x--;
break;
case 'right':
head.x++;
break;
}
this.snake.unshift(head);
if(this.isFoodEaten()) {
this.score += 10;
this.generateFood();
} else {
this.snake.pop();
}
},
draw(ctx) {
// 清空画布
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
// 绘制蛇
const snakeSize = ctx.canvas.width / this.gameBoardWidth;
this.snake.forEach((segment, index) => {
if(index === 0) {
ctx.fillStyle = 'red';
} else {
ctx.fillStyle = 'blue';
}
ctx.fillRect(segment.x * snakeSize, segment.y * snakeSize, snakeSize - 1, snakeSize - 1);
});
// 绘制食物
ctx.fillStyle = 'green';
ctx.fillRect(this.food.x * snakeSize, this.food.y * snakeSize, snakeSize - 1, snakeSize - 1);
},
generateFood() {
if(!this.isFoodEaten()) return;
let newFood;
do {
newFood = {
x: Math.floor(Math.random() * this.gameBoardWidth),
y: Math.floor(Math.random() * this.gameBoardHeight)
};
} while(this.snake.some(segment => segment.x === newFood.x && segment.y === newFood.y));
this.food = newFood;
},
isFoodEaten() {
return this.snake[0].x === this.food.x && this.snake[0].y === this.food.y;
},
checkCollision() {
const head = this.snake[0];
// 检查边界碰撞
if(head.x < 0 || head.x >= this.gameBoardWidth ||
head.y < 0 || head.y >= this.gameBoardHeight) {
alert('Game Over! Score: ' + this.score);
this.reset();
}
// 检查自身碰撞
for(let i = 1; i < this.snake.length; i++) {
if(head.x === this.snake[i].x && head.y === this.snake[i].y) {
alert('Game Over! Score: ' + this.score);
this.reset();
}
}
},
reset() {
this.snake = [
{ x: 5, y: 5 },
{ x: 4, y: 5 },
{ x: 3, y: 5 }
];
this.direction = 'right';
this.score = 0;
this.generateFood();
}
}
});
app.mount('#app');
</script>
</body>
</html>
要运行这个游戏:
- 将以上代码保存为一个 HTML 文件(例如 snake-game.html)
- 在浏览器中打开这个文件
游戏规则:
- 使用方向键控制蛇的移动
- 蛇吃到绿色的食物会变长并增加分数
- 蛇碰到边界或自身时游戏结束
- 得分会在右上角显示
主要功能包括:
- 蛇的移动和转向
- 食物生成(确保不会生成在蛇身上)
- 碰撞检测(边界和自身)
- 得分统计
- 游戏重置
你可以根据需要调整以下参数:
- gameBoardWidth 和 gameBoardHeight:游戏区域大小
- gameSpeed:蛇的移动速度
- 食物生成规则
- 初始蛇的位置和长度
大家觉得这个DeepSeek R1 32B本地部署的代码编写能力如何?我觉得AI,最大可能是先把程序员这个职业给干没了!
没有回复内容