大概就是昨晚,字节发布了全新的AI IDE,名为Trae。经过一晚上的使用,真的有焦虑住,甚至都在反思未来还需要程序员吗?对于刚入行的新手们,甚至都没有新手保护期了。。
话不多说
访问
https://www.trae.com.cn/
按您所用的平台,按需选择安装包
主界面大概是这样:
右边分别显示chat,builder模式
我们测试下它的代码能力,选择builder模式,让它给我们写个贪食蛇
然后它就会库库写代码~,最后帮你启动测试服务器,我浅玩了下,真的焦虑住
以下是主要的game.js
const CELL_SIZE = 20;
const GAME_SIZE = 400;
const DIRECTIONS = {
LEFT: 0,
RIGHT: 1,
UP: 2,
DOWN: 3
};
class SnakeGame {
constructor() {
this.canvas = document.getElementById('gameCanvas');
this.ctx = this.canvas.getContext('2d');
this.scoreElement = document.getElementById('score');
this.reset();
this.bindControls();
this.setupButtons();
}
reset() {
this.snake = [{x: 5, y: 5}];
this.direction = DIRECTIONS.RIGHT;
this.food = this.generateFood();
this.score = 0;
this.gameLoop = null;
this.isPaused = false;
this.updateScore();
}
generateFood() {
return {
x: Math.floor(Math.random() * (GAME_SIZE/CELL_SIZE)),
y: Math.floor(Math.random() * (GAME_SIZE/CELL_SIZE))
};
}
bindControls() {
document.addEventListener('keydown', (e) => {
switch(e.key) {
case 'ArrowLeft': this.changeDirection(DIRECTIONS.LEFT); break;
case 'ArrowRight': this.changeDirection(DIRECTIONS.RIGHT); break;
case 'ArrowUp': this.changeDirection(DIRECTIONS.UP); break;
case 'ArrowDown': this.changeDirection(DIRECTIONS.DOWN); break;
}
});
}
setupButtons() {
document.getElementById('startBtn').addEventListener('click', () => {
if(!this.gameLoop) this.startGame();
});
document.getElementById('pauseBtn').addEventListener('click', () => {
this.togglePause();
});
}
changeDirection(newDir) {
if(
(this.direction === DIRECTIONS.LEFT && newDir === DIRECTIONS.RIGHT) ||
(this.direction === DIRECTIONS.RIGHT && newDir === DIRECTIONS.LEFT) ||
(this.direction === DIRECTIONS.UP && newDir === DIRECTIONS.DOWN) ||
(this.direction === DIRECTIONS.DOWN && newDir === DIRECTIONS.UP)
) return;
this.direction = newDir;
}
updateScore() {
this.scoreElement.textContent = this.score;
}
startGame() {
this.reset();
this.gameLoop = setInterval(() => this.update(), 150);
}
togglePause() {
this.isPaused = !this.isPaused;
document.getElementById('pauseBtn').textContent =
this.isPaused ? '继续' : '暂停';
}
update() {
if(this.isPaused) return;
const head = {...this.snake[0]};
switch(this.direction) {
case DIRECTIONS.LEFT: head.x--; break;
case DIRECTIONS.RIGHT: head.x++; break;
case DIRECTIONS.UP: head.y--; break;
case DIRECTIONS.DOWN: head.y++; break;
}
// 碰撞检测
if(
head.x < 0 || head.x >= GAME_SIZE/CELL_SIZE ||
head.y < 0 || head.y >= GAME_SIZE/CELL_SIZE ||
this.snake.some(segment => segment.x === head.x && segment.y === head.y)
) {
alert('游戏结束!得分:' + this.score);
clearInterval(this.gameLoop);
this.gameLoop = null;
return;
}
this.snake.unshift(head);
// 吃到食物
if(head.x === this.food.x && head.y === this.food.y) {
this.score += 10;
this.updateScore();
this.food = this.generateFood();
} else {
this.snake.pop();
}
this.draw();
}
draw() {
this.ctx.clearRect(0, 0, GAME_SIZE, GAME_SIZE);
// 绘制食物
this.ctx.fillStyle = '#e74c3c';
this.ctx.fillRect(
this.food.x * CELL_SIZE,
this.food.y * CELL_SIZE,
CELL_SIZE - 2,
CELL_SIZE - 2
);
// 绘制蛇
this.snake.forEach((segment, index) => {
this.ctx.fillStyle = index === 0 ? '#2ecc71' : '#27ae60';
this.ctx.fillRect(
segment.x * CELL_SIZE,
segment.y * CELL_SIZE,
CELL_SIZE - 2,
CELL_SIZE - 2
);
});
}
}
// 初始化游戏
new SnakeGame();