-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
67 lines (50 loc) · 1.57 KB
/
snake.cpp
File metadata and controls
67 lines (50 loc) · 1.57 KB
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
#include "snake.h"
#include <vector>
using namespace std;
Snake::Snake(Coords position, int velocity) {
this->position = position;
this->velocity = velocity;
snake_length = 1;
direction = 'n'; // no direction assigned at start
body.push_back(position);
}
Coords Snake::getPosition() { return position; }
vector<Coords> Snake::getBody() { return body; }
void Snake::changeDirection(char direction) { this->direction = direction; }
void Snake::growSnake() { snake_length++; }
void Snake::moveSnake() {
switch(direction) {
case 'u':
position.y -= velocity;
break;
case 'd':
position.y += velocity;
break;
case 'l':
position.x -= velocity;
break;
case 'r':
position.x += velocity;
break;
}
// Add this position to the vector
body.push_back(position);
// If the position is greater than the current length of the snake,
// remove the "tail" element of the snake (the first element)
if (body.size() > snake_length) body.erase(body.begin());
}
bool Snake::collided() {
// Check if we have collided with the side of the board
if (position.x < 1 || position.x > WIDTH - 2 || position.y < 1 || position.y > HEIGHT - 2) return true;
// // Check if we have collided with any part of the snake's own body
// if (body.size() > 1) {
// for (auto b : body) {
// if (position.x == b.x || position.y == b.y) return true;
// }
// }
return false;
}
bool Snake::foodEaten(Coords food_position) {
if (position.x == food_position.x && position.y == food_position.y) return true;
return false;
}