-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnake.cpp
118 lines (96 loc) · 2.45 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
#include "snake.h"
void Snake::init(){
//head
piece * head = new piece(16*25, 16*20, RIGHT, "head.png");
this->snake.push_back(head);
//body
for(int i=24; i>20; i--){
piece * body = new piece(16*i, 16*20, RIGHT, "piece.png");
this->snake.push_back(body);
}
}
Snake::Snake(){
this->init();
this->points = 0;
this->end = false;
}
Snake::~Snake(){
for(int i=0; i< (int) this->snake.size(); i++)
delete snake[i];
}
int Snake::getPoints() const{
return this->points;
}
piece * Snake::getTesta() const{
return this->snake[0];
}
int Snake::getDirection() const{
return (this->snake[0])->getDirection();
}
bool Snake::getEnd() const{
return this->end;
}
bool Snake::isThereOnAPoint(Point * asd) const{
bool sovrapposizione = false;
for(int i=0; i<(int)this->snake.size(); i++){
if (asd->getPosX() == (this->snake[i])->getPosX() && asd->getPosY() == (this->snake[i])->getPosY())
sovrapposizione = true;
}
return sovrapposizione;
}
void Snake::setEnd(bool e){
this->end = e;
}
void Snake::setPoints(int n){
this->points = n;
}
void Snake::addPoint(){
this->points++;
}
void Snake::addPoint(int n){
this->points += n;
}
void Snake::setDirection(int d){
(this->snake[0])->setDirection(d);
}
void Snake::pushPiece(){
int px,py,dir;
px = (this->snake[ (int)this->snake.size() - 1 ] )->getPosX();
py = (this->snake[ (int)this->snake.size() - 1 ] )->getPosY();
dir = (this->snake[ (int)this->snake.size() - 1 ] )->getDirection();
piece * coda = NULL;
switch(dir){
case UP:
coda = new piece(px,py+16,dir,"piece.png");
break;
case DOWN:
coda = new piece(px,py-16,dir,"piece.png");
break;
case RIGHT:
coda = new piece(px-16,py,dir,"piece.png");
break;
case LEFT:
coda = new piece(px+16,py,dir,"piece.png");
break;
}
this->snake.push_back(coda);
}
void Snake::move(){
for(int i = 0; i<(int)this->snake.size(); i++)
(this->snake[i])->move();
for(int i = (int)this->snake.size() - 1 ; i>0 ; i--)
(this->snake[i])->setDirection( (this->snake[i-1])->getDirection() );
for(int i = 0; i<(int)this->snake.size(); i++)
if((this->snake[i])->getDie())
this->end = true;
int px,py;
px = (this->snake[0])->getPosX();
py = (this->snake[0])->getPosY();
for(int i = 1; i<(int)this->snake.size(); i++)
if((this->snake[i])->getPosX() == px && (this->snake[i])->getPosY() == py)
this->end = true;
}
void Snake::draw(SDL_Surface * screen){
for(int i = 0; i<(int)this->snake.size(); i++)
(this->snake[i])->draw(screen);
}