@Ibishka

Как изменить направление змеи?

const canvas = document.querySelector("#canvas"),
  ctx = canvas.getContext("2d");
// Set canvas width & height;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

ctx.fillRect(0, 0, canvas.width, canvas.height);
// Create snake
let x = canvas.width / 2 - 50,
  y = canvas.height / 2 - 10,
  w = 150;

// set endlessly move of snake
const moveEndlessly = () => {
  ctx.fillStyle = "#000";
  ctx.fillRect(0, 0, canvas.width, canvas.height);

  ctx.fillStyle = "#59982f";
  x -= 5;
  ctx.fillRect(x, y, w, 20);
  window.requestAnimationFrame(moveEndlessly);
};

moveEndlessly();

// set movement of snake on keydown
document.addEventListener("keydown", e => {
  if (e.keyCode == 37) {
  }
  if (e.keyCode == 38) {
// What to do
  }
  if (e.keyCode == 39) {
  }
  if (e.keyCode == 40) {
  }
});

Как поварачивать вверх например?
  • Вопрос задан
  • 144 просмотра
Решения вопроса 1
twobomb
@twobomb
Не увидел в вашем примере змейки, поэтому решил добавить
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
sfrancisco
@sfrancisco
CodePen

условно можно так
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы