-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.cpp
More file actions
128 lines (116 loc) · 5.12 KB
/
Copy pathSnake.cpp
File metadata and controls
128 lines (116 loc) · 5.12 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
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
#include "Snake.h"
#include <algorithm>
Snake::Snake(Sound deathSound)
{
Vector2 tmp_pos = {GRID_WIDTH/2 * GRIDSIZE, GRID_HEIGHT/2 * GRIDSIZE};
this->positions.push_back(tmp_pos);
this->deathSound = deathSound;
}
void Snake::Draw()
{
for(int i = (int)positions.size() - 1; i >= 0; i--)
{
float EyeSize = GRIDSIZE/3;
Rectangle rec = {positions.at(i).x, positions.at(i).y, GRIDSIZE, GRIDSIZE};
Color col = {std::clamp(255 - (i * 20), 50, 255), 37, 17, 255};
DrawRectangleRounded(rec, 0.3f, 10, col);
DrawRectangleRoundedLines(rec, 0.3f, 10, 5, BLACK);
if(i == 0)
{
if(abs(direction.x))
{
if(direction.x == 1.0f)
{
Rectangle r = {positions.at(i).x + GRIDSIZE - EyeSize, positions.at(i).y, EyeSize, EyeSize};
DrawRectangleRounded(r, 0.3f, 10, WHITE);
DrawRectangleRoundedLines(r, 0.3f, 10, 5, BLACK);
r = {r.x + EyeSize/2, r.y + EyeSize/3, EyeSize/2, EyeSize/2};
DrawRectangleRounded(r, 0.9f, 10, BLACK);
r = {positions.at(i).x + GRIDSIZE - EyeSize, positions.at(i).y + (GRIDSIZE - EyeSize), EyeSize, EyeSize};
DrawRectangleRounded(r, 0.3f, 10, WHITE);
DrawRectangleRoundedLines(r, 0.3f, 10, 5, BLACK);
r = {r.x + EyeSize/2, r.y + EyeSize/3, EyeSize/2, EyeSize/2};
DrawRectangleRounded(r, 0.9f, 10, BLACK);
}else if(direction.x == -1.0f)
{
Rectangle r = {positions.at(i).x, positions.at(i).y, EyeSize, EyeSize};
DrawRectangleRounded(r, 0.3f, 10, WHITE);
DrawRectangleRoundedLines(r, 0.3f, 10, 5, BLACK);
r = {r.x, r.y + EyeSize/3, EyeSize/2, EyeSize/2};
DrawRectangleRounded(r, 0.9f, 10, BLACK);
r = {positions.at(i).x, positions.at(i).y + (GRIDSIZE - EyeSize), EyeSize, EyeSize};
DrawRectangleRounded(r, 0.3f, 10, WHITE);
DrawRectangleRoundedLines(r, 0.3f, 10, 5, BLACK);
r = {r.x, r.y + EyeSize/3, EyeSize/2, EyeSize/2};
DrawRectangleRounded(r, 0.9f, 10, BLACK);
}
}
if(abs(direction.y))
{
if(direction.y == 1.0f)
{
Rectangle r = {(positions.at(i).x), positions.at(i).y + GRIDSIZE - EyeSize, EyeSize, EyeSize};
DrawRectangleRounded(r, 0.3f, 10, WHITE);
DrawRectangleRoundedLines(r, 0.3f, 10, 5, BLACK);
r = {r.x + EyeSize/3.5, r.y + EyeSize/2, EyeSize/2, EyeSize/2};
DrawRectangleRounded(r, 0.9f, 10, BLACK);
r={(positions.at(i).x) + GRIDSIZE - EyeSize, positions.at(i).y + GRIDSIZE - EyeSize, EyeSize, EyeSize};
DrawRectangleRounded(r, 0.3f, 10, WHITE);
DrawRectangleRoundedLines(r, 0.3f, 10, 5, BLACK);
r = {r.x + EyeSize/3.5, r.y + EyeSize/2, EyeSize/2, EyeSize/2};
DrawRectangleRounded(r, 0.9f, 10, BLACK);
}else if(direction.y == -1.0f)
{
Rectangle r = {(positions.at(i).x), positions.at(i).y , EyeSize, EyeSize};
DrawRectangleRounded(r, 0.3f, 10, WHITE);
DrawRectangleRoundedLines(r, 0.3f, 10, 5, BLACK);
r = {r.x + EyeSize/3.5, r.y, EyeSize/2, EyeSize/2};
DrawRectangleRounded(r, 0.9f, 10, BLACK);
r={(positions.at(i).x) + GRIDSIZE - EyeSize, positions.at(i).y, EyeSize, EyeSize};
DrawRectangleRounded(r, 0.3f, 10, WHITE);
DrawRectangleRoundedLines(r, 0.3f, 10, 5, BLACK);
r = {r.x + EyeSize/3.5, r.y, EyeSize/2, EyeSize/2};
DrawRectangleRounded(r, 0.9f, 10, BLACK);
}
}
}
}
}
Vector2 Snake::get_head_position()
{
return positions.front();
}
Vector2 Snake::get_pos()
{
Vector2 tmp = {this->positions.front().x, this->positions.front().y};
return tmp;
}
void Snake::move()
{
Vector2 cur = this->get_head_position();
Vector2 new_pos = {fmod((cur.x+direction.x*GRIDSIZE),GAME_WIDTH), fmod(cur.y+direction.y*GRIDSIZE, GAME_HEIGHT)};
if (new_pos.x < 0)
new_pos.x = GAME_WIDTH - GRIDSIZE;
if (new_pos.y < 0)
new_pos.y = GAME_HEIGHT - GRIDSIZE;
for(int i = 0; i < this->positions.size(); i++)
{
if(new_pos.x == this->positions.at(i).x && new_pos.y == this->positions.at(i).y)
{
this->reset();
return;
}
}
this->positions.insert(this->positions.begin(), new_pos);
if (this->positions.size() >= this->length)
this->positions.pop_back();
}
void Snake::reset()
{
PlaySound(this->deathSound);
this->length = 1;
this->positions.clear();
Vector2 tmp_pos = {GRID_WIDTH/2 * GRIDSIZE, GRID_HEIGHT/2 * GRIDSIZE};
this->positions.push_back(tmp_pos);
this->score = 0;
}