Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snake Game #2546

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<html lang="pt-br">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>HTML + CSS</title>

<link rel="stylesheet" href="styles.css" />

<script defer src="index.js"></script>

<title>Snake Game</title>
</head>
<body>
<h1>This is a simple HTML + CSS template!</h1>
<h1>Snake Game!</h1>
<canvas id="snake" width="512" height="512"></canvas>

<footer>
"Cobrinha Games Ltda."
</footer>
</body>
</html>
84 changes: 84 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//declarar variaveis
let canvas = document.getElementById("snake");
let context = canvas.getContext("2d");
let box = 32;
let snake = [{ x: 8 * box, y: 8 * box }];
let direction = "right";
let food = {
x: Math.floor(Math.random() * 15 + 1) * box,
y: Math.floor(Math.random() * 15 + 1) * box
};
//criar background
function createBG() {
context.fillStyle = "pink";
context.fillRect(0, 0, 16 * box, 16 * box);
}
//criar cobrinha
function createSnake() {
for (let i in snake) {
context.fillStyle = "green";
context.fillRect(snake[i].x, snake[i].y, box, box);
}
}
//criar comida
function createFood() {
context.fillStyle = "red";
context.fillRect(food.x, food.y, box, box);
}
//update
function update(event) {
if (event.keyCode == 37 && direction != "right") {
direction = "left";
} else if (event.keyCode == 38 && direction != "down") {
direction = "up";
} else if (event.keyCode == 39 && direction != "left") {
direction = "right";
} else if (event.keyCode == 40 && direction != "up") {
direction = "down";
}
}
//começar o jogo
function startGame() {
if (snake[0].x > 15 * box && direction == "right") snake[0].x = 0;
if (snake[0].x < 0 && direction == "left") snake[0].x = 15 * box;
if (snake[0].y > 15 * box && direction == "down") snake[0].y = 0;
if (snake[0].y < 0 && direction == "up") snake[0].y = 15 * box;
//colisão
for (let i = 1; i < snake.length; i++) {
if (snake[0].x === snake[i].x && snake[0].y === snake[i].y) {
alert("Game Over!");
clearInterval(game); //interrompe o jogo
}
}
//chamada das funções
createBG();
createSnake();
createFood();
//inicialização coordenadas da cabeça
let snakeX = snake[0].x;
let snakeY = snake[0].y;

if (direction == "right") snakeX += box;
if (direction == "left") snakeX -= box;
if (direction == "up") snakeY -= box;
if (direction == "down") snakeY += box;
//colisão com a comida
if (snakeX !== food.x || snakeY !== food.y) {
snake.pop();
} else {
food = {
x: Math.floor(Math.random() * 15 + 1) * box,
y: Math.floor(Math.random() * 15 + 1) * box
};
}

let newHead = {
x: snakeX,
y: snakeY
};

snake.unshift(newHead);
}
document.addEventListener("keydown", update);

let game = setInterval(startGame, 100); //atualização do canvas
36 changes: 34 additions & 2 deletions styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
h1 {
text-decoration: underline;
@import url("https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap");

body {
height: 95vh;
width: 95vw;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #9d40af;
color: #466e58;
font-family: "Press Start 2P", cursive;
}

canvas {
margin-bottom: 50px;
}

footer {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
text-align: center;
font-size: 15px;
margin-top: 25px;
margin-bottom: 15px;
font-family: "Roboto", sans-serif;
}

footer a {
text-decoration: none;
color: #fff;
}