html
html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!-- Created by Ethan -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake Game</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<button id="walls">Walls</button>
<div class="game-container">
<canvas id="canvas"></canvas>
</div>
<div class="dpad">
<button id="up">▲</button>
<div class="horizontal-buttons">
<button id="left">◀</button>
<button id="right">▶</button>
</div>
<button id="down">▼</button>
</div>
<script src="script.js"></script>
</body>
</html>
Enter to Rename, Shift+Enter to Preview
css
css
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* Created by Ethan */
/* style.css */
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #333;
color: #fff;
font-family: Arial, sans-serif;
}
.game-container {
display: flex;
justify-content: center;
align-items: center;
border: 2px solid #444;
background-color: #222;
}
canvas {
background-color: #000;
}
.dpad {
Enter to Rename, Shift+Enter to Preview
js
js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Created by Ethan
onload = () => {
// shorten document.querySelector()
const $ = el => document.querySelector(el);
const canvas = $("#canvas");
const ctx = canvas.getContext("2d");
const TS = 20;
// size of canvas. modify multiplier to change
canvas.width = 18*TS;
canvas.height = 18*TS;
let snake = [{ x:5*TS, y:5*TS }];
let direction = { x:TS, y: 0 };
let nextDirection = { x:TS, y: 0 };
let food = { x:10*TS, y:10*TS };
let score = 0;
let lastTime = 0;
const speed = 250; // larger value is slower
let wallOn = false;
// arrow buttons
$("#up").addEventListener( "touchstart",
()=>setDirection(0, -1) );
$("#down").addEventListener( "touchstart",
()=>setDirection(0, 1) );
$("#left").addEventListener( "touchstart",
Enter to Rename, Shift+Enter to Preview
BROWSER
Console
Run