Ask your own question, for FREE!
Computer Science 67 Online
Aratox:

Hey guys, there's a project I've been working on for a Create Task on code.org, and I've been having a few problems with it. This is the code, and I seriously need help FAST, because this is due today at 11:59 PM 😅

Aratox:

```javascript window.onload = function() { var canvas = document.getElementById('snakeCanvas'); var ctx = canvas.getContext('2d'); var snakeSize = 20; var snake = [ { x: 40, y: 40 }, { x: 60, y: 40 } ]; var direction = 'right'; var food = { x: Math.floor(Math.random() * canvas.width / snakeSize) * snakeSize, y: Math.floor(Math.random() * canvas.height / snakeSize) * snakeSize }; function drawSnake() { ctx.fillStyle = 'green'; snake.forEach(function(segment) { ctx.fillRect(segment.x, segment.y, snakeSize, snakeSize); }); } function drawFood() { ctx.fillStyle = 'red'; ctx.fillRect(food.x, food.y, snakeSize, snakeSize); } function checkCollision() { if (snake[0].x < 0 || snake[0].x >= canvas.width || snake[0].y < 0 || snake[0].y >= canvas.height) { return true; } for (var i = 1; i < snake.length ; i++) { if (snake.x === snake[0].x && snake.y === snake[0].y) { return true; } } return false; } function update() { var head = { x: snake[0].x, y: snake[0].y }; if (direction === 'up') head.y -= snakeSize; if (direction === 'down') head.y += snakeSize; if (direction === 'left') head.x -= snakeSize; if (direction === 'right') head.x += snakeSize; snake.unshift(head); if (head.x === food.x && head.y === food.y) { food.x = Math.floor(Math.random() * canvas.width / snakeSize) * snakeSize; food.y = Math.floor(Math.random() * canvas.height / snakeSize) * snakeSize; } else { snake.pop(); } if (checkCollision()) { alert('Game over!'); window.location.reload(); } } function loop() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawSnake(); drawFood(); update(); setTimeout(function() { loop(); }, 100); } document.addEventListener('keydown', function(event) { if (event.key === 'ArrowUp' && direction !== 'down') { direction = 'up'; } if (event.key === 'ArrowDown' && direction !== 'up') { direction = 'down'; } if (event.key === 'ArrowLeft' && direction !== 'right') { direction = 'left'; } if (event.key === 'ArrowRight' && direction !== 'left') { direction = 'right'; } }); loop(); }; ```

Rosee5656:

I'm sorry toxy, but I have absolutely no clue man..

Rosee5656:

@extrinix

ShadowKid3:

Idk, Jay or ext can probably help you though. @Jaynator495 @extentrix

Rosee5656:

@shadowkid3 wrote:
Idk, Jay or ext can probably help you though. @Jaynator495 @extentrix
@extrinix *

ShadowKid3:

@ultrilliam

Limitless2:

i failed u as ur apprentice (i take coding) but sorry i havent got this advanced at coding just yet

qiqi:

It would be best if you also state the problem you are facing and what kind of help you need?

Aratox:

@qiqi wrote:
It would be best if you also state the problem you are facing and what kind of help you need?
The code's just not showing up in the editor... I mean, I have everything set up, no errors as far as I can see, but there may be some thing I overlooked.

1 attachment
Spectrum:

test the code and let me see what's going on.

Spectrum:

Try plugging it into mine and see if it works, https://coyote-code-editor.rf.gd

Aratox:

@spectrum wrote:
test the code and let me see what's going on.
It's being run in the image that I posted above, and other code editors don't work - by the way, this is supposed to be solely JavaScript.

Spectrum:

Well it's not running because you don't have an html interface to show it on.

Spectrum:

Add this to your HTML file, then try again. ``` <canvas id="snakeCanvas"></canvas> ```

Spectrum:

Oh...

Spectrum:

I FOUND THE PROBLEM!

Spectrum:

It's the checkCollision function. In the loop where you're checking for collisions with the snake itself, you're comparing snake.x and snake.y, which aren't properties of the snake array. You should be comparing snake.x and snake.y.

Spectrum:

try adding a border around your game too, so the player knows the bounds...

Spectrum:

Here's your code (fixed): ``` window.onload = function() { var canvas = document.getElementById('snakeCanvas'); var ctx = canvas.getContext('2d'); var snakeSize = 20; var snake = [ { x: 40, y: 40 }, { x: 60, y: 40 } ]; var direction = 'right'; var food = { x: Math.floor(Math.random() * (canvas.width / snakeSize)) * snakeSize, y: Math.floor(Math.random() * (canvas.height / snakeSize)) * snakeSize }; function drawSnake() { ctx.fillStyle = 'green'; snake.forEach(function(segment) { ctx.fillRect(segment.x, segment.y, snakeSize, snakeSize); }); } function drawFood() { ctx.fillStyle = 'red'; ctx.fillRect(food.x, food.y, snakeSize, snakeSize); } function checkCollision() { if (snake[0].x < 0 || snake[0].x >= canvas.width || snake[0].y < 0 || snake[0].y >= canvas.height) { return true; } for (var i = 1; i < snake.length ; i++) { if (snake.x === snake[0].x && snake.y === snake[0].y) { return true; } } return false; } function update() { var head = { x: snake[0].x, y: snake[0].y }; if (direction === 'up') head.y -= snakeSize; if (direction === 'down') head.y += snakeSize; if (direction === 'left') head.x -= snakeSize; if (direction === 'right') head.x += snakeSize; snake.unshift(head); if (head.x === food.x && head.y === food.y) { food.x = Math.floor(Math.random() * (canvas.width / snakeSize)) * snakeSize; food.y = Math.floor(Math.random() * (canvas.height / snakeSize)) * snakeSize; } else { snake.pop(); } if (checkCollision()) { alert('Game over!'); window.location.reload(); } } function loop() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawSnake(); drawFood(); update(); setTimeout(function() { loop(); }, 100); } document.addEventListener('keydown', function(event) { if (event.key === 'ArrowUp' && direction !== 'down') { direction = 'up'; } if (event.key === 'ArrowDown' && direction !== 'up') { direction = 'down'; } if (event.key === 'ArrowLeft' && direction !== 'right') { direction = 'left'; } if (event.key === 'ArrowRight' && direction !== 'left') { direction = 'right'; } }); loop(); };

Spectrum:

Hope this helps! Tag me with @spectrum for more help. I'll try my best.

Aratox:

@spectrum wrote:
Hope this helps! Tag me with @spectrum for more help. I'll try my best.
Still nothing, it's a blank editor

Spectrum:

?

Spectrum:

Please show me what you're seeing @Aratox.

Aratox:

@spectrum wrote:
Please show me what you're seeing @Aratox.

1 attachment
Aratox:

I'm running it right now, the "Reset" button would just reset the editor (and nothing would change).

Spectrum:

if you have no access to html frontend add this to your JavaScript window.onload function: ``` const createCanvas = document.createElement('canvas') createCanvas.setAttribute('id', 'snakeCanvas') ```

Spectrum:

If not you need to find a new editor that allows you to add HTML frontend...

Aratox:

@spectrum wrote:
if you have no access to html frontend add this to your JavaScript window.onload function: ``` const createCanvas = document.createElement('canvas') createCanvas.setAttribute('id', 'snakeCanvas') ```
Oh yeah, I forgot to mention: const isn't supported 😭

Spectrum:

so use var

Spectrum:

or let

Aratox:

@spectrum wrote:
so use var
Yeah, that's what I was doing

Spectrum:

Is your code copy-pasted? Jay said it looked like it was. It could also be the program your using.

Spectrum:

From DMs:

@ultrilliam wrote:
Given the screenshot - I suspect that is not quite how this works - and he might be using copy-pasted code which is why it's not working. From the looks of it, it's a scratch type thing?

Aratox:

@spectrum wrote:
Is your code copy-pasted? Jay said it looked like it was. It could also be the program your using.
It's copy-pasted from my code.org project that I'm working on rn... also one of my friends just messaged me that I can use codepen.io, so I hope that it'll be easier on there

Aratox:

@spectrum wrote:
From DMs:
@ultrilliam wrote:
Given the screenshot - I suspect that is not quite how this works - and he might be using copy-pasted code which is why it's not working. From the looks of it, it's a scratch type thing?
@ultrilliam Scratch but way less cooperative 💀

qiqi:

@aratox wrote:
Oh yeah, I forgot to mention: const isn't supported 😭`
So I was looking at your editor from code.org, I think you just have to use createCanvas just so, without declaring it as const. Spectrum's explanation basically pointed out the problem. The missing part on your code is the creation of Canvas itself. You are calling and using a canvas with an id called `'snakeCanvas'` but base on your code, there is no creation of it (missing canvas height and width). I am assuming that you have created it using the UI tools on the website editor? Since I am also seeing "Design" button on your screen.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
lustful: Guys give me good aesthetics to make crrds from
7 minutes ago 2 Replies 0 Medals
REYSHAWN23134: THERE U HAPPY DUMB
1 hour ago 6 Replies 0 Medals
REYSHAWN23134: Does anyone want to vc?
1 hour ago 18 Replies 0 Medals
REYSHAWN23134: Who is bi in HERE
2 hours ago 0 Replies 0 Medals
Kaydi: Hey does anyone else have to do Aleks for their school
2 hours ago 9 Replies 3 Medals
hellokitty265: when ur pregnant and ur baby poops and pees does it stay inside of u or?
3 hours ago 19 Replies 1 Medal
ashley2345: What are the best baggy clothes to wear
3 hours ago 25 Replies 0 Medals
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!