-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake_Artifact.cpp
More file actions
139 lines (130 loc) · 4.52 KB
/
Copy pathSnake_Artifact.cpp
File metadata and controls
139 lines (130 loc) · 4.52 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
129
130
131
132
133
134
135
136
137
138
139
#include "Snake_Artifact.h"
#include<iostream>
//constructor
Snake_Artifact::Snake_Artifact(float sz)
:snake_size{sz}
{
sf::RectangleShape head{sf::RectangleShape(sf::Vector2f(snake_size,snake_size))};
head.setFillColor(head_color);
head.setPosition(0, sz);
data.push_back(head);
}
std::pair<float,float> Snake_Artifact::get_Position(){
sf::RectangleShape head = data.front();
sf::Vector2f head_pos = head.getPosition();
return std::pair<float,float>({head_pos.x,head_pos.y});
}
int Snake_Artifact::size(){
//head should not be counted as size
return data.size()-1;
}
//collison with window or with itself
bool Snake_Artifact::check_collision(Artifact_Map& Game_Artifacts,
sf::RenderWindow& window){
//check collision with Window
sf::RectangleShape head = data.front();
sf::Vector2f head_pos = head.getPosition();
sf::Vector2u window_sz = window.getSize();
if(head_pos.x < 0 || head_pos.x > (float)(window_sz.x-snake_size) ||
head_pos.y < 0 || head_pos.y > (float)(window_sz.y-snake_size)){
//std::cout<<"Collision detected\n";
return true;
}
//check collision with itself
std::deque<sf::RectangleShape>::iterator it = data.begin();
it++; sf::RectangleShape body;
for(;it != data.end();it++){
body = *it;
sf::Vector2f body_pos = body.getPosition();
if(head_pos.x == body_pos.x && head_pos.y == body_pos.y){
//std::cout<<"Collision with itself\n";
return true;
}
}
return false;
}
//collision with Apple
bool Snake_Artifact::eat_Apple(Artifact_Map& Game_Artifacts,
sf::RenderWindow& window){
std::pair<float,float> apple_pos = Game_Artifacts["Apple"]->get_Position();
sf::RectangleShape head = data.front();
sf::Vector2f head_pos = head.getPosition();
if(head_pos.x == apple_pos.first && head_pos.y == apple_pos.second)
return true;
return false;
}
//draw - implements snake draw
bool Snake_Artifact::draw(sf::RenderWindow& window){
for(sf::RectangleShape r : data){
window.draw(r);
}
return true;
}
//update - implements snake Artifact update
// snake update => snake movement
bool Snake_Artifact::update(Artifact_Map& Game_Artifacts,
sf::RenderWindow& window, Event_Vec& Events){
for(sf::Event event : Events){
switch(event.type)
{
case sf::Event::Closed:
window.close();
return false;
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Up && !(snake_dir == sf::Keyboard::Down))
snake_dir = event.key.code;
if(event.key.code == sf::Keyboard::Down && !(snake_dir == sf::Keyboard::Up))
snake_dir = event.key.code;
if(event.key.code == sf::Keyboard::Left && !(snake_dir == sf::Keyboard::Right))
snake_dir = event.key.code;
if(event.key.code == sf::Keyboard::Right && !(snake_dir == sf::Keyboard::Left))
snake_dir = event.key.code;
break;
}
}
//std::cout<<"Snake size is"<<data.size()<<"\n";
sf::RectangleShape& head = data.front();
sf::Vector2f head_pos = head.getPosition();
sf::RectangleShape new_head{sf::RectangleShape(sf::Vector2f(snake_size,snake_size))};
//create new head & attach to snake
switch(snake_dir){
case sf::Keyboard::Up:
new_head.setFillColor(head_color);
new_head.setPosition(head_pos.x,head_pos.y-snake_size);
head.setFillColor(body_color);
data.push_front(new_head);
if(!eat_Apple(Game_Artifacts,window)){
data.pop_back(); //remove tail of snake
}
break;
case sf::Keyboard::Down:
new_head.setFillColor(head_color);
new_head.setPosition(head_pos.x,head_pos.y+snake_size);
head.setFillColor(body_color);
data.push_front(new_head);
if(!eat_Apple(Game_Artifacts,window)){
data.pop_back(); //remove tail of snake
}
break;
case sf::Keyboard::Left:
new_head.setFillColor(head_color);
new_head.setPosition(head_pos.x-snake_size,head_pos.y);
head.setFillColor(body_color);
data.push_front(new_head);
if(!eat_Apple(Game_Artifacts,window)){
data.pop_back(); //remove tail of snake
}
break;
case sf::Keyboard::Right:
new_head.setFillColor(head_color);
new_head.setPosition(head_pos.x+snake_size,head_pos.y);
head.setFillColor(body_color);
data.push_front(new_head);
if(!eat_Apple(Game_Artifacts,window)){
data.pop_back(); //remove tail of snake
}
break;
}
//check collision
return !(check_collision(Game_Artifacts,window));
}