File size: 5,243 Bytes
d3de7fb
9aa58d1
d3de7fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nokia Snake Game</title>
    <style>
        body {
            display: flex;
            flex-direction: column; /* Stack elements vertically */
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #1a1a1a;
        }
        h1 {
            color: #ffffff;
            margin-bottom: 10px;
        }
        canvas {
            border: 10px solid #879372;
            border-radius: 10px;
        }
    </style>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.min.js"></script>
</head>
<body>
    <h1>Team 9 - P5.JS - Spaces Test</h1> <!-- Added heading -->
    <script>
        let snake;
        let food;
        let gridSize = 10;
        let gridWidth, gridHeight;
        let score = 0;

        function setup() {
          createCanvas(240, 240);
          gridWidth = floor(width / gridSize);
          gridHeight = floor(height / gridSize);
          frameRate(8);
          snake = new Snake();
          foodLocation();
        }

        function draw() {
          background('#9CA777');
          drawGrid();
          snake.update();
          snake.show();
          
          if (snake.eat(food)) {
            foodLocation();
            score++;
          }
          
          fill('#4A5F35');
          rect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
          
          if (snake.checkCollision()) {
            gameOver();
          }

          // Display score
          fill('#4A5F35');
          textSize(16);
          textAlign(LEFT, TOP);
          text('Score: ' + score, 5, 5);
        }

        function drawGrid() {
          stroke('#AEBAA1');
          strokeWeight(1);
          for (let i = 0; i < width; i += gridSize) {
            line(i, 0, i, height);
          }
          for (let j = 0; j < height; j += gridSize) {
            line(0, j, width, j);
          }
        }

        function foodLocation() {
          food = createVector(floor(random(gridWidth)), floor(random(gridHeight)));
          // Make sure food doesn't appear on snake
          while (snake.body.some(part => part.x === food.x && part.y === food.y)) {
            food = createVector(floor(random(gridWidth)), floor(random(gridHeight)));
          }
        }

        function keyPressed() {
          if (keyCode === LEFT_ARROW && snake.xdir === 0) {
            snake.setDir(-1, 0);
          } else if (keyCode === RIGHT_ARROW && snake.xdir === 0) {
            snake.setDir(1, 0);
          } else if (keyCode === DOWN_ARROW && snake.ydir === 0) {
            snake.setDir(0, 1);
          } else if (keyCode === UP_ARROW && snake.ydir === 0) {
            snake.setDir(0, -1);
          }
        }

        function gameOver() {
          noLoop();
          background('#9CA777');
          fill('#4A5F35');
          textSize(24);
          textAlign(CENTER, CENTER);
          text('GAME OVER', width / 2, height / 2 - 20);
          textSize(16);
          text('Score: ' + score, width / 2, height / 2 + 20);
          text('Press F5 to restart', width / 2, height / 2 + 50);
        }

        class Snake {
          constructor() {
            this.body = [];
            this.body[0] = createVector(floor(gridWidth / 2), floor(gridHeight / 2));
            this.xdir = 0;
            this.ydir = 0;
            this.growAmount = 0; // Control growth
          }
          
          setDir(x, y) {
            this.xdir = x;
            this.ydir = y;
          }
          
          update() {
            let head = this.body[this.body.length - 1].copy();
            head.x += this.xdir;
            head.y += this.ydir;
            
            // Wrap around edges
            head.x = (head.x + gridWidth) % gridWidth;
            head.y = (head.y + gridHeight) % gridHeight;
            
            this.body.push(head);
            
            if (this.growAmount > 0) {
              this.growAmount--;
            } else {
              this.body.shift();
            }
          }
          
          grow() {
            this.growAmount++; // Increase growth amount
          }
          
          eat(pos) {
            let x = this.body[this.body.length - 1].x;
            let y = this.body[this.body.length - 1].y;
            if (x === pos.x && y === pos.y) {
              this.grow();
              return true;
            }
            return false;
          }
          
          checkCollision() {
            let head = this.body[this.body.length - 1];
            for (let i = 0; i < this.body.length - 1; i++) {
              let part = this.body[i];
              if (part.x === head.x && part.y === head.y) {
                return true;
              }
            }
            return false;
          }
          
          show() {
            for (let i = 0; i < this.body.length; i++) {
              fill('#4A5F35');
              rect(this.body[i].x * gridSize, this.body[i].y * gridSize, gridSize, gridSize);
            }
          }
        }
    </script>
</body>
</html>