Spaces:
Running
Running
Update index.html
Browse files- index.html +179 -18
index.html
CHANGED
@@ -1,19 +1,180 @@
|
|
1 |
-
<!
|
2 |
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
<html>
|
3 |
+
<head>
|
4 |
+
<meta charset="utf-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Nokia Snake Game</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
display: flex;
|
10 |
+
flex-direction: column; /* Stack elements vertically */
|
11 |
+
justify-content: center;
|
12 |
+
align-items: center;
|
13 |
+
height: 100vh;
|
14 |
+
margin: 0;
|
15 |
+
background-color: #1a1a1a;
|
16 |
+
}
|
17 |
+
h1 {
|
18 |
+
color: #ffffff;
|
19 |
+
margin-bottom: 10px;
|
20 |
+
}
|
21 |
+
canvas {
|
22 |
+
border: 10px solid #879372;
|
23 |
+
border-radius: 10px;
|
24 |
+
}
|
25 |
+
</style>
|
26 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.min.js"></script>
|
27 |
+
</head>
|
28 |
+
<body>
|
29 |
+
<h1>Team 9 - P5.JS - Spaces Test</h1> <!-- Added heading -->
|
30 |
+
<script>
|
31 |
+
let snake;
|
32 |
+
let food;
|
33 |
+
let gridSize = 10;
|
34 |
+
let gridWidth, gridHeight;
|
35 |
+
let score = 0;
|
36 |
+
|
37 |
+
function setup() {
|
38 |
+
createCanvas(240, 240);
|
39 |
+
gridWidth = floor(width / gridSize);
|
40 |
+
gridHeight = floor(height / gridSize);
|
41 |
+
frameRate(8);
|
42 |
+
snake = new Snake();
|
43 |
+
foodLocation();
|
44 |
+
}
|
45 |
+
|
46 |
+
function draw() {
|
47 |
+
background('#9CA777');
|
48 |
+
drawGrid();
|
49 |
+
snake.update();
|
50 |
+
snake.show();
|
51 |
+
|
52 |
+
if (snake.eat(food)) {
|
53 |
+
foodLocation();
|
54 |
+
score++;
|
55 |
+
}
|
56 |
+
|
57 |
+
fill('#4A5F35');
|
58 |
+
rect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
|
59 |
+
|
60 |
+
if (snake.checkCollision()) {
|
61 |
+
gameOver();
|
62 |
+
}
|
63 |
+
|
64 |
+
// Display score
|
65 |
+
fill('#4A5F35');
|
66 |
+
textSize(16);
|
67 |
+
textAlign(LEFT, TOP);
|
68 |
+
text('Score: ' + score, 5, 5);
|
69 |
+
}
|
70 |
+
|
71 |
+
function drawGrid() {
|
72 |
+
stroke('#AEBAA1');
|
73 |
+
strokeWeight(1);
|
74 |
+
for (let i = 0; i < width; i += gridSize) {
|
75 |
+
line(i, 0, i, height);
|
76 |
+
}
|
77 |
+
for (let j = 0; j < height; j += gridSize) {
|
78 |
+
line(0, j, width, j);
|
79 |
+
}
|
80 |
+
}
|
81 |
+
|
82 |
+
function foodLocation() {
|
83 |
+
food = createVector(floor(random(gridWidth)), floor(random(gridHeight)));
|
84 |
+
// Make sure food doesn't appear on snake
|
85 |
+
while (snake.body.some(part => part.x === food.x && part.y === food.y)) {
|
86 |
+
food = createVector(floor(random(gridWidth)), floor(random(gridHeight)));
|
87 |
+
}
|
88 |
+
}
|
89 |
+
|
90 |
+
function keyPressed() {
|
91 |
+
if (keyCode === LEFT_ARROW && snake.xdir === 0) {
|
92 |
+
snake.setDir(-1, 0);
|
93 |
+
} else if (keyCode === RIGHT_ARROW && snake.xdir === 0) {
|
94 |
+
snake.setDir(1, 0);
|
95 |
+
} else if (keyCode === DOWN_ARROW && snake.ydir === 0) {
|
96 |
+
snake.setDir(0, 1);
|
97 |
+
} else if (keyCode === UP_ARROW && snake.ydir === 0) {
|
98 |
+
snake.setDir(0, -1);
|
99 |
+
}
|
100 |
+
}
|
101 |
+
|
102 |
+
function gameOver() {
|
103 |
+
noLoop();
|
104 |
+
background('#9CA777');
|
105 |
+
fill('#4A5F35');
|
106 |
+
textSize(24);
|
107 |
+
textAlign(CENTER, CENTER);
|
108 |
+
text('GAME OVER', width / 2, height / 2 - 20);
|
109 |
+
textSize(16);
|
110 |
+
text('Score: ' + score, width / 2, height / 2 + 20);
|
111 |
+
text('Press F5 to restart', width / 2, height / 2 + 50);
|
112 |
+
}
|
113 |
+
|
114 |
+
class Snake {
|
115 |
+
constructor() {
|
116 |
+
this.body = [];
|
117 |
+
this.body[0] = createVector(floor(gridWidth / 2), floor(gridHeight / 2));
|
118 |
+
this.xdir = 0;
|
119 |
+
this.ydir = 0;
|
120 |
+
this.growAmount = 0; // Control growth
|
121 |
+
}
|
122 |
+
|
123 |
+
setDir(x, y) {
|
124 |
+
this.xdir = x;
|
125 |
+
this.ydir = y;
|
126 |
+
}
|
127 |
+
|
128 |
+
update() {
|
129 |
+
let head = this.body[this.body.length - 1].copy();
|
130 |
+
head.x += this.xdir;
|
131 |
+
head.y += this.ydir;
|
132 |
+
|
133 |
+
// Wrap around edges
|
134 |
+
head.x = (head.x + gridWidth) % gridWidth;
|
135 |
+
head.y = (head.y + gridHeight) % gridHeight;
|
136 |
+
|
137 |
+
this.body.push(head);
|
138 |
+
|
139 |
+
if (this.growAmount > 0) {
|
140 |
+
this.growAmount--;
|
141 |
+
} else {
|
142 |
+
this.body.shift();
|
143 |
+
}
|
144 |
+
}
|
145 |
+
|
146 |
+
grow() {
|
147 |
+
this.growAmount++; // Increase growth amount
|
148 |
+
}
|
149 |
+
|
150 |
+
eat(pos) {
|
151 |
+
let x = this.body[this.body.length - 1].x;
|
152 |
+
let y = this.body[this.body.length - 1].y;
|
153 |
+
if (x === pos.x && y === pos.y) {
|
154 |
+
this.grow();
|
155 |
+
return true;
|
156 |
+
}
|
157 |
+
return false;
|
158 |
+
}
|
159 |
+
|
160 |
+
checkCollision() {
|
161 |
+
let head = this.body[this.body.length - 1];
|
162 |
+
for (let i = 0; i < this.body.length - 1; i++) {
|
163 |
+
let part = this.body[i];
|
164 |
+
if (part.x === head.x && part.y === head.y) {
|
165 |
+
return true;
|
166 |
+
}
|
167 |
+
}
|
168 |
+
return false;
|
169 |
+
}
|
170 |
+
|
171 |
+
show() {
|
172 |
+
for (let i = 0; i < this.body.length; i++) {
|
173 |
+
fill('#4A5F35');
|
174 |
+
rect(this.body[i].x * gridSize, this.body[i].y * gridSize, gridSize, gridSize);
|
175 |
+
}
|
176 |
+
}
|
177 |
+
}
|
178 |
+
</script>
|
179 |
+
</body>
|
180 |
+
</html>
|