Trae真的让我焦虑住

大概就是昨晚,字节发布了全新的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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彪悍大蓝猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值