Last active
November 3, 2020 22:38
-
-
Save supergoat/c0bbe32c6a036d890977bf9288df7b5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Snake Game</title> | |
</head> | |
<body> | |
<canvas id="gameCanvas" width="300" height="300"></canvas> | |
<script> | |
/** CONSTANTS **/ | |
const CANVAS_BORDER_COLOUR = 'black'; | |
const CANVAS_BACKGROUND_COLOUR = "white"; | |
const SNAKE_COLOUR = 'lightgreen'; | |
const SNAKE_BORDER_COLOUR = 'darkgreen'; | |
let snake = [ | |
{x: 150, y: 150}, | |
{x: 140, y: 150}, | |
{x: 130, y: 150}, | |
{x: 120, y: 150}, | |
{x: 110, y: 150} | |
] | |
// Horizontal velocity | |
let dx = 10; | |
// Vertical velocity | |
let dy = 0; | |
// Get the canvas element | |
var gameCanvas = document.getElementById("gameCanvas"); | |
// Return a two dimensional drawing context | |
var ctx = gameCanvas.getContext("2d"); | |
// Select the colour to fill the canvas | |
ctx.fillStyle = CANVAS_BACKGROUND_COLOUR; | |
// Select the colour for the border of the canvas | |
ctx.strokestyle = CANVAS_BORDER_COLOUR; | |
// Draw a "filled" rectangle to cover the entire canvas | |
ctx.fillRect(0, 0, gameCanvas.width, gameCanvas.height); | |
// Draw a "border" around the entire canvas | |
ctx.strokeRect(0, 0, gameCanvas.width, gameCanvas.height); | |
// Move on step to the right | |
advanceSnake() | |
// Change vertical velocity to 0 | |
dx = 0; | |
// Change horizontal velocity to 10 | |
dy = -10; | |
// Move one step up | |
advanceSnake(); | |
// Draw snake on the canvas | |
drawSnake(); | |
/** | |
* Advances the snake by changing the x-coordinates of its parts | |
* according to the horizontal velocity and the y-coordinates of its parts | |
* according to the vertical veolocity | |
*/ | |
function advanceSnake() { | |
const head = {x: snake[0].x + dx, y: snake[0].y + dy}; | |
snake.unshift(head); | |
snake.pop(); | |
} | |
/** | |
* Draws the snake on the canvas | |
*/ | |
function drawSnake() { | |
// loop through the snake parts drawing each part on the canvas | |
snake.forEach(drawSnakePart) | |
} | |
/** | |
* Draws a part of the snake on the canvas | |
* @param { object } snakePart - The coordinates where the part should be drawn | |
*/ | |
function drawSnakePart(snakePart) { | |
// Set the colour of the snake part | |
ctx.fillStyle = SNAKE_COLOUR; | |
// Set the border colour of the snake part | |
ctx.strokestyle = SNAKE_BORDER_COLOUR; | |
// Draw a "filled" rectangle to represent the snake part at the coordinates | |
// the part is located | |
ctx.fillRect(snakePart.x, snakePart.y, 10, 10); | |
// Draw a border around the snake part | |
ctx.strokeRect(snakePart.x, snakePart.y, 10, 10); | |
} | |
</script> | |
</body> | |
</html> |
forgive me if this is a stupid question, but how exactly is the snake supposed to MOVE?
nAH....AND SO IT TURNS OUT TO BE JUST AN IMAGE
WELL THE OUTPUT IS IN AN IMAGE FORM.....
@csg-gamerz101 I came here from a tutorial too, after looking for i could find another tutorial that works, if it helps this is the file "https://github.com/AlanGuth/SnakeGameJS/blob/master/SnakeFuncionalSiteCorreto.html", sorry for bad english
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I came here from your awesome tutorial in freeCodeCamp.
I guess the comments on line 49 and 51 are wrong.
By making
dx = 0;
you are actually making the horizontal velocity 0.And by making
dy = -10
a vertical velocity is getting introduced.If I am wrong, please correct me and explain me where I am going wrong.