Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

var rightPressed = false;
var leftPressed = false;
var upPressed = false;
var downPressed = false;

var foods = {
	x: Math.floor(Math.random() * 500),
	y: Math.floor(Math.random() * 500)
};

var snake = {
	x: 25,
	y: 25,
	width: 25,
	height: 25
};

function drawRect() {
	ctx.clearRect(0, 0, canvas.width, canvas.height);
	ctx.beginPath();
	ctx.rect(snake.x, snake.y, snake.width, snake.height)
	ctx.fillStyle = "#000000";
	ctx.fill();
	ctx.closePath();
}

function controls() {
	if(rightPressed) {
		snake.x += 1;
	}else if(leftPressed) {
		snake.x -= 1;
	}else if(upPressed) {
		snake.y -= 1;
	}else if(downPressed) {
		snake.y += 1;
	}
}

function collision() {
	if(snake.x >= canvas.width) {
		snake.x = canvas.width - canvas.width;
	}else if(snake.x < canvas.width - canvas.width) {
		snake.x = canvas.width;
	}else if(snake.y >= canvas.height) {
		snake.y = canvas.height - canvas.height;
	}else if(snake.y < canvas.height - canvas.height) {
		snake.y = canvas.height;
	}
}

document.addEventListener("keydown", keyDownHandler, false);
//document.addEventListener("keyup", keyUpHandler, false);

function keyDownHandler(e) {
    if(e.keyCode == 39) {
        rightPressed = true;
		leftPressed = false;
		upPressed =  false;
		downPressed = false;
    }
    else if(e.keyCode == 37) {
        leftPressed = true;
		rightPressed = false;
		upPressed = false;
		downPressed = false;
    }
	if(e.keyCode == 38) {
        upPressed = true;
		leftPressed = false;
		rightPressed = false;
		downPressed = false;
    }
    else if(e.keyCode == 40) {
        downPressed = true;
		rightPressed = false;
		leftPressed = false;
		upPressed = false;
    }
}

/*
function keyUpHandler(e) {
    if(e.keyCode == 39) {
        rightPressed = false;
    }
    else if(e.keyCode == 37) {
        leftPressed = false;
    }
	if(e.keyCode == 38) {
        upPressed = false;
    }
    else if(e.keyCode == 40) {
        downPressed = false;
    }
}
*/

function food() {
	ctx.beginPath();
	ctx.rect(foods.x, foods.y, 25, 25);
	ctx.fillStyle = "#8B0000";
	ctx.fill();
	ctx.closePath();
}





                                MY PROBLEM|
                                         
----------------------------------------------------------------------
function eatFood() {
	if(snake.x == foods.x && snake.y == foods.y) {
		console.log("collide");
	}
}
----------------------------------------------------------------------







function start() {
	drawRect();
	controls();
	collision();
	food();
	eatFood();
}


setInterval(start, 10);


What I have tried:

Ive tried multiple ways and if I could help that would be awesome.
Posted
Updated 6-Mar-18 16:04pm
v2
Comments
ZurdoDev 6-Mar-18 16:01pm    
Please do not expect anyone to try and read through all that code to figure out what you have done. You need to debug your code and narrow down where the problem is and then ask a specific question.
Member 13712171 6-Mar-18 16:39pm    
when I debug I get no errors and I'm honestly just not sure what to do.

Google is your friend...
If you want to learn how to develop games in JS, read this:
2D breakout game using pure JavaScript - Game development | MDN[^]
The 7th step is about collision detection...
 
Share this answer
 
Quote:
Im trying to get detection on when snake hits food

Like 'snakeByte' ? Snake Byte [Plums: 0] ATARI 800 (8-Bit) - 540 - YouTube[^]
Quote:
Ive tried multiple ways and if I could help that would be awesome.

First you need to tell us what is supposed to be the game and how it should work and animate.
Quote:
when I debug I get no errors and I'm honestly just not sure what to do.

You never get error by the debugger, the debugger only show you what your code is doing, it don't understand and don't know what your code is supposed to do.
In order to debug the code, you need to remove the animation and launch each screen refresh manually.
In short, we can't help you if we don't know the details.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900