8000 Update and rename README.md to Snake.html · TheCodingRocket/Snake-Ruby@2fd53c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2fd53c0

Browse files
Update and rename README.md to Snake.html
1 parent 543e037 commit 2fd53c0

File tree

2 files changed

+186
-1
lines changed

2 files changed

+186
-1
lines changed

README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

Snake.html

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# Snake-2D-
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<title>Pancake - Snake</title>
6+
</head>
7+
<body>
8+
<script src="../build/pancake.js"></script>
9+
<canvas id="canvas" onclick="pancake.graphics.toggleFullscreen();"></canvas>
10+
<script>
11+
if (window.localStorage === undefined) {
12+
alert("Browser does not support offline localStorage,So online version will open");
13+
pancake.browser.open("https://rabios.github.io/Pancake/games/snake.html");
14+
}
15+
16+
// Create context and set it external canvas with id "canvas"
17+
// Set created context index to 0
18+
// Set canvas index to 0,Since custom canvases can't have index
19+
pancake.context.use(document.getElementById("canvas"), 0);
20+
21+
// Resize canvas to fit the window
22+
pancake.canvas.resize(pancake.canvas.compatible_width, pancake.canvas.compatible_height, 0);
23+
24+
// Set background color
25+
pancake.graphics.setBackgroundColor("black");
26+
27+
var grid_size = 32;
28+
var count = 0, score = 0, highscore = Number(pancake.storage.load("highscore")) || 0;
29+
30+
var snake = {
31+
x: 0, y: 0,
32+
// Snake velocity. Moves one grid_size length every frame in either the x or y direction
33+
vx: grid_size, vy: 0,
34+
35+
// Keep track of all grid_sizes the snake body occupies
36+
cells: [],
37+
38+
// Length of the snake.Grows when eating an apple
39+
maxCells: 1
40+
10000 };
41+
42+
var apple = { x: 120, y: 120 };
43+
44+
function game() {
45+
window.animate(game, 120);
46+
47+
// Slow game loop to 30 fps instead of 120 (120 / 30 = 4)
48+
if (++count < 4) {
49+
return;
50+
}
51+
52+
count = 0;
53+
pancake.graphics.clear();
54+
55+
// Update snake position
56+
snake.x += snake.vx;
57+
snake.y += snake.vy;
58+
59+
// Wrap snake position horizontally on edge of screen
60+
if (snake.x < 0) snake.x = canvas.width - grid_size;
61+
else if (snake.x >= canvas.width) snake.x = 0;
62+
63+
// Wrap snake position vertically on edge of screen
64+
if (snake.y < 0) snake.y = canvas.height - grid_size;
65+
else if (snake.y >= canvas.height) snake.y = 0;
66+
67+
// Keep track of where snake has been. Front of the array is always the head
68+
snake.cells.unshift({ x: snake.x, y: snake.y });
69+
70+
// Remove cells as we move away from them
71+
if (snake.cells.length > snake.maxCells) snake.cells.pop();
72+
73+
pancake.graphics.mode = pancake.graphics.FILL;
74+
75+
// Draw apple
76+
pancake.graphics.color("blue");
77+
pancake.graphics.square(apple.x, apple.y, grid_size);
78+
79+
// Draw snake one cell at a time
80+
pancake.graphics.color("dodgerblue");
81+
snake.cells.forEach(function(cell, index) {
82+
pancake.graphics.square(cell.x, cell.y, grid_size);
83+
84+
// Snake ate apple
85+
if (pancake.physics.checkCollisionRect(cell.x, cell.y, 32, 32, apple.x, apple.y, 32, 32)) {
86+
snake.maxCells++;
87+
score = snake.maxCells - 1;
88+
apple.x = pancake.util.random(Math.floor(pancake.canvas.compatible_width / 32)) * grid_size;
89+
apple.y = pancake.util.random(Math.floor(pancake.canvas.compatible_height / 32)) * grid_size;
90+
}
91+
92+
// Check collision with all cells after this one (modified bubble sort)
93+
for (var i = index + 1; i < snake.cells.length; i++) {
94+
95+
// Snake occupies same space as a body part. Reset game
96+
if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
97+
snake.x = snake.y = 320;
98+
snake.cells = [], snake.maxCells = 1;
99+
snake.vx = grid_size, snake.vy = 0;
100+
101+
// Storage functionality to save highscore and reset score
102+
if (score > Number(pancake.storage.load("highscore"))) pancake.storage.save("highscore", Number(score));
103+
score = 0;
104+
105+
apple.x = pancake.util.random(Math.floor(pancake.canvas.compatible_width / 32)) * grid_size;
106+
apple.y = pancake.util.random(Math.floor(pancake.canvas.compatible_height / 32)) * grid_size;
107+
108+
}
109+
}
110+
});
111+
112+
if (score == (pancake.graphics.context.canvas.width) * (pancake.graphics.context.canvas.height)) {
113+
alert("Congrats! You won the game!!!");
114+
pancake.game.restart();
115+
}
116+
117+
// Draw score
118+
pancake.graphics.color("blue");
119+
pancake.graphics.setFont("Monospace", 64);
120+
pancake.graphics.text(score, 32, 64);
121+
122+
// Get storage, Get canvas width
123+
// Draw highscore text
124+
pancake.graphics.text(Number(pancake.storage.load("highscore")), pancake.graphics.context.canvas.width - 156, 64);
125+
126+
// Input
127+
if (pancake.input.keydown(pancake.input.key.UP) || pancake.input.keydown(pancake.input.key.W) || pancake.input.swipe("UP")) {
128+
snake.vy = -grid_size, snake.vx = 0;
129+
}
130+
131+
if (pancake.input.keydown(pancake.input.key.DOWN) || pancake.input.keydown(pancake.input.key.S) || pancake.input.swipe("DOWN")) {
132+
snake.vy = grid_size, snake.vx = 0;
133+
}
134+
135+
if (pancake.input.keydown(pancake.input.key.LEFT) || pancake.input.keydown(pancake.input.key.A) || pancake.input.swipe("LEFT")) {
136+
snake.vx = -grid_size, snake.vy = 0;
137+
}
138+
139+
if (pancake.input.keydown(pancake.input.key.RIGHT) || pancake.input.keydown(pancake.input.key.D) || pancake.input.swipe("RIGHT")) {
140+
snake.vx = grid_size, snake.vy = 0;
141+
}
142+
143+
// Check gamepad support and first gamepad connected
144+
if (pancake.browser.support.GAMEPAD() && pancake.input.gamepadConnected(0)) {
145+
// Analog controls
146+
if (pancake.input.gamepadAnalogMoved(0, pancake.input.GAMEPAD_MOVE_ANALOG, pancake.input.GAMEPAD_ANALOG_UP, "UP")) {
147+
snake.vy = -grid_size, snake.vx = 0;
148+
}
149+
150+
if (pancake.input.gamepadAnalogMoved(0, pancake.input.GAMEPAD_MOVE_ANALOG, pancake.input.GAMEPAD_ANALOG_DOWN, "DOWN")) {
151+
snake.vy = grid_size, snake.vx = 0;
152+
}
153+
154+
if (pancake.input.gamepadAnalogMoved(0, pancake.input.GAMEPAD_MOVE_ANALOG, pancake.input.GAMEPAD_ANALOG_LEFT, "LEFT")) {
155+
snake.vx = -grid_size, snake.vy = 0;
156+
}
157+
158+
if (pancake.input.gamepadAnalogMoved(0, pancake.input.GAMEPAD_MOVE_ANALOG, pancake.input.GAMEPAD_ANALOG_RIGHT, "RIGHT")) {
159+
snake.vx = grid_size, snake.vy = 0;
160+
}
161+
162+
// DPAD Controls
163+
if (pancake.input.gamepadButtonPressed(pancake.input.button.UP, 0)) {
164+
snake.vy = -grid_size, snake.vx = 0;
165+
}
166+
167+
if (pancake.input.gamepadButtonPressed(pancake.input.button.DOWN, 0)) {
168+
snake.vy = grid_size, snake.vx = 0;
169+
}
170+
171+
if (pancake.input.gamepadButtonPressed(pancake.input.button.LEFT, 0)) {
172+
snake.vx = -grid_size, snake.vy = 0;
173+
}
174+
175+
if (pancake.input.gamepadButtonPressed(pancake.input.button.RIGHT, 0)) {
176+
snake.vx = grid_size, snake.vy = 0;
177+
}
178+
}
179+
180+
pancake.input.preventLoop();
181+
}
182+
183+
window.animate(game, 120);
184+
</script>
185+
</body>
186+
</html>

0 commit comments

Comments
 (0)
0