Started Snake Game

This commit is contained in:
Ben
2018-06-15 11:24:22 +00:00
parent 51420dbd45
commit d70d1379e3
7 changed files with 3275 additions and 0 deletions

View File

@@ -0,0 +1 @@
const express = require('express');

3136
NodeJS/SimpleRESTapi/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,19 @@
{
"name": "simplerestapi",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon server.js"
},
"author": "Ben (plane000)",
"license": "MIT",
"devDependencies": {
"nodemon": "^1.17.5"
},
"dependencies": {
"express": "^4.16.3",
"mongoose": "^5.1.5"
}
}

View File

@@ -0,0 +1,14 @@
body {
background-color: black;
}
canvas {
padding: 0;
margin: auto;
display: block;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}

80
NodeJS/Snake/game.js Normal file
View File

@@ -0,0 +1,80 @@
let c = document.getElementById("canv");
let ctx = c.getContext("2d");
let screenHeight = 40;
let screenWidth = 40;
let grid = []; //0 = nothing, 1 = snake, 2 = apple
let direction = 0; //0 = up, 1 = right, 2 = down, 3 = left
let snakeX = 20;
let snakeY = 20;
window.addEventListener("keydown", onKeyDown, false);
for (let i = 0; i < screenHeight; i++) {
grid[i] = [];
for (let j = 0; j < screenWidth; j++) {
grid[i][j] = 0;
}
}
function onKeyDown(event) {
switch (event.key) {
case 'w':
case 'W':
direction = 0;
break;
case 'd':
case 'D':
direction = 1;
break;
case 's':
case 'S':
direction = 2;
break;
case 'a':
case 'A':
direction = 3;
break;
}
}
function gameLoop() {
for (let i = 0; i < screenHeight; i++) {
for (let j = 0; j < screenWidth; j++) {
grid[i][j] = 0;
}
}
if (direction == 0) {
snakeY--;
} else if (direction == 1) {
snakeX++;
} else if (direction == 2) {
snakeY++;
} else if (direction == 3) {
snakeX--;
}
grid[snakeX][snakeY] = 1;
render();
}
function render() {
for (let i = 0; i < screenHeight; i++) {
for (let j = 0; j < screenWidth; j++) {
if (grid[i][j] == 0) {
ctx.fillStyle="#FFFFF0";
} else if (grid[i][j] == 1) {
ctx.fillStyle="#FF0000";
} else {
ctx.fillStyle="#00FF00";
}
ctx.fillRect(i * 10, j * 10, 400 / screenHeight, 400 / screenWidth);
}
}
}
setInterval(gameLoop, 100);

14
NodeJS/Snake/index.html Normal file
View File

@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Snake game</title>
<meta name="viewport" content="width=device-width, insitial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="css/style.css" />
</head>
<body>
<canvas id="canv" width="400" height="400" onkeypress="onKeyPressed(event)"></canvas>
<script src="game.js"></script>
</body>
</html>

11
NodeJS/Snake/package.json Normal file
View File

@@ -0,0 +1,11 @@
{
"name": "snake",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ben (plane000)",
"license": "MIT"
}