-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.cpp
101 lines (89 loc) · 2.54 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
#include "screen.h"
#include "snake.h"
#include "particle.h"
#include <iostream>
using namespace std;
using namespace SDL_Graphics;
Snake::Snake(){
internalBuffer = new int[screen::HEIGHT*screen::WIDTH];
image = new Uint32[screen::HEIGHT*screen::WIDTH];
length = 15;
x = 0;
y = 0;
direction = 0;
lastPosition = -1;
lastDirection = -1;
score = 0;
particle0 = new particle(screen::WIDTH,screen::HEIGHT);
}
void Snake::setDirection(int dir){
if(!((dir+lastDirection)%2 == 0)){
direction = dir;
}
}
void Snake::move(){
if(x<=-1||x>=1){
x = -x;
}
if(y<=-1||y>=1){
y = -y;
}
if(direction == 0){
x +=0.001;
}else if(direction == 1){
y -=0.001;
}else if(direction == 2){
x -= 0.001;
}else if(direction == 3){
y += 0.001;
}
lastDirection = direction;
for (int i=0;i<screen::WIDTH*screen::HEIGHT;i++){
if(internalBuffer[i] > 0){
(internalBuffer[i])--;
}else if(internalBuffer[i] < 0){
(internalBuffer[i])++;
}
}
int real_x = (x+1)*(screen::WIDTH/2);
int real_y = (y+1)*(screen::HEIGHT/2);
int position = real_x+(real_y*screen::WIDTH);
if( (position != lastPosition) && (internalBuffer[position]>0)){
throw -1;
}
//if(position == (particle0->x)+(particle0->y*screen::WIDTH)){
if(image[position] == 0xffff0000){
length += 10;
score += length*10;
particle0->renew();
}
if(direction==1 || direction==3){
for(int i=-1;i<=1;i++){
internalBuffer[(position+i)] = length*-1;
}
}else if(direction==0 || direction==2){
for(int i=-1;i<=1;i++){
internalBuffer[position+(i*screen::WIDTH)] = length*-1;
}
}
internalBuffer[position] = length;
lastPosition = position;
//cout << "position" << position << endl;
//cout << "x: " << real_x << " y: " << real_y <<endl;
}
void Snake::updateSnake(){
memset(image , 0 , screen::WIDTH*screen::HEIGHT*sizeof(Uint32));
for(int i=-1;i<=1;i++){
image[((particle0->x)+(particle0->y*screen::WIDTH))+i] = 0xffff0000;
}
for (int i = -1; i <= 1; i+=2)
{
image[((particle0->x)+(particle0->y*screen::WIDTH))+(i*screen::WIDTH)] = 0xffff0000;
}
for(int i=0;i<screen::WIDTH*screen::HEIGHT;i++){
if(internalBuffer[i]!=0){
image[i] = 0xff000000;
}
}
cout << particle0->x << " " << particle0->y << " position in buffer: " << (particle0->x)+(particle0->y*screen::WIDTH) << endl;
}