Code GameDev

let snake;

function setup() {
createCanvas(500, 500);

snake = {
position: createVector(20, 20),
}
}

function draw() {
background(220);

noStroke();

fill(0);
rect(snake.position.x, snake.position.y,20,20);
}

function keyPressed() {
if (keyCode == LEFT_ARROW) {
// snake.position.x = snake.position.x – 1;
snake.position.x -= 1;
} else if (keyCode == RIGHT_ARROW) {
snake.position.x += 1;
} else if (keyCode == UP_ARROW) {
snake.position.y -= 1;
} else if (keyCode == DOWN_ARROW) {
snake.position.y += 1;
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *