-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnake.cpp
137 lines (128 loc) · 3.21 KB
/
snake.cpp
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
#include<iostream>
#include<SFML/Graphics.hpp>
#include<SFML/Audio.hpp>
#include<cstdio>
#include<string>
#include<sstream>
#include<cstdlib>
#include<ctime>
#include<deque>
#include"game.h"
#include"snake.h"
#include"snack.h"
using namespace std;
using namespace sf;
snake::snake()
{
RectangleShape head;
head.setPosition(600, 400);
head.setFillColor(Color::Red);
head.setSize(Vector2f(length, length));
head.setOutlineThickness(2);
head.setOutlineColor(Color::Yellow);
parts.push_back(head);
body_length = 1;
}
RectangleShape snake::get_head()
{
return parts[0];
}
void snake::update_motion(float move_in_x,float move_in_y)
{
int i;
for (i = parts.size() - 1; i > 0; i--)
{
parts[i].setPosition(parts[i - 1].getPosition().x, parts[i - 1].getPosition().y);
}
if (move_in_x == -1 && move_in_y == 0) // LEFT
{
parts[0].move(-length, 0);
}
else if (move_in_x == 1 && move_in_y == 0) //RIGHT
{
parts[0].move(length, 0);
}
else if (move_in_x == 0 && move_in_y == 1) // DOWN
{
parts[0].move(0, length);
}
else if (move_in_x == 0 && move_in_y == -1) //UP
{
parts[0].move(0, -length);
}
}
bool snake::collision_with_itself()
{
RectangleShape head = parts[0];
float i = 4;
while (i < parts.size())
{
if (head.getGlobalBounds().contains(parts[i].getPosition().x, parts[i].getPosition().y))
{
return true;
}
i++;
}
return false;
}
bool snake::collision_with_wall(RenderWindow& window)
{
float head_x = parts[0].getPosition().x;
float head_y = parts[0].getPosition().y;
float x = window.getSize().x;
float y = window.getSize().y;
if ((head_x + 15) >= x || head_x <= 0 || (head_y + 15) >= y || head_y <= 0)
{
return true;
}
return false;
}
void snake::update(RenderWindow& window,float move_in_x,float move_in_y)
{
body_length++;
float new_x = parts[parts.size() - 1].getPosition().x;
float new_y = parts[parts.size() - 1].getPosition().y;
RectangleShape part;
part.setSize(Vector2f(length, length));
part.setFillColor(Color::Green);
part.setOutlineThickness(2);
part.setOutlineColor(Color::Yellow);
if (move_in_x == -1 && move_in_y == 0) // LEFT
{
new_x += length;
}
if (move_in_x == 1 && move_in_y == 0) //RIGHT
{
new_x -= length;
}
if (move_in_x == 0 && move_in_y == 1) // DOWN
{
new_y -= length;
}
if (move_in_x == 0 && move_in_y == -1) //UP
{
new_y += length;
}
part.setPosition(new_x, new_y);
parts.push_back(part);
}
void snake::draw_body(RenderWindow& window)
{
int i;
for (i = 0; i < body_length; i++)
{
window.draw(parts[i]);
}
}
void snake::reset()
{
while (!parts.empty()) parts.pop_back();
RectangleShape head;
head.setPosition(600, 400);
head.setFillColor(Color::Red);
head.setSize(Vector2f(length, length));
head.setOutlineThickness(2);
head.setOutlineColor(Color::Yellow);
parts.push_back(head);
body_length = 1;
}