This repository was archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobjects.cpp
More file actions
170 lines (148 loc) · 5.41 KB
/
objects.cpp
File metadata and controls
170 lines (148 loc) · 5.41 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "defines.h"
#include "snake.hpp"
#include "objects.hpp"
#include "graphics.hpp"
// Class FRAGMENT
Fragment::Fragment(int newX, int newY, SDL_Renderer* newRenderer) {
this-> x = newX;
this-> y = newY;
this-> next = NULL;
this-> renderer = newRenderer;
this-> headTexture = loadSDLImg("sprites/head_c.png", this-> renderer);
this-> bodyTexture = loadSDLImg("sprites/body_c.png", this-> renderer);
this-> L_bodyTexture = loadSDLImg("sprites/L_body_c.png", this-> renderer);
this-> tailTexture = loadSDLImg("sprites/tail_c.png", this-> renderer);
}
Fragment::~Fragment() {
SDL_DestroyTexture(this-> headTexture);
SDL_DestroyTexture(this-> bodyTexture);
SDL_DestroyTexture(this-> L_bodyTexture);
SDL_DestroyTexture(this-> tailTexture);
if (next != NULL) delete next;
}
void Fragment::createFragment(int newX, int newY) {
if (next != NULL) next-> createFragment(newX, newY);
else this-> next = new Fragment(newX, newY, this->renderer);
}
void Fragment::printAndNext(int angle) {
if (next != NULL) {
// formule compliquée pour avoir le prochain angle en fonction des coordonnées
int nextAngle = 90*((this->x-this->next->x-1)*(this->x-this->next->x)+this->y-this->next->y);
if (this->x==this->next->x && this->y==this->next->y)
this-> next-> printAndNext(angle);
else if (nextAngle != angle) {
if ((nextAngle+360)%360 - angle == 90) angle -= 90;
printImgOnRenderer(this-> L_bodyTexture, this->renderer, {this->x, this->y}, angle);
this-> next-> printAndNext(nextAngle);
} else {
printImgOnRenderer(this-> bodyTexture,this->renderer, {this->x, this->y}, angle);
this-> next-> printAndNext(nextAngle);
}
} else {
printImgOnRenderer(this-> tailTexture, this-> renderer, {this->x, this->y}, angle);
}
}
void Fragment::printHeadSkin(int angle) {
printImgOnRenderer(this-> headTexture, this-> renderer, {this->x, this->y}, angle);
}
void Fragment::move(int newX, int newY) {
if (this-> next != NULL) {
if (this-> x == this-> next-> x && this-> y == this-> next-> y);
else next-> move(this-> x, this-> y);
}
this-> x = newX;
this-> y = newY;
}
bool Fragment::checkCollision(int newX, int newY) {
if (this-> x == newX && this-> y == newY) return true;
if (this-> next != NULL) return this-> next-> checkCollision(newX, newY);
return false;
}
// Class FRUIT
Fruit::Fruit(SDL_Renderer* renderer) {
this-> appleTexture = loadSDLImg("sprites/apple.png", renderer);
this-> jamTexture = loadSDLImg("sprites/jam.png", renderer);
this-> shieldTexture = loadSDLImg("sprites/shield.png", renderer);
this-> poopTexture = loadSDLImg("sprites/poop.png", renderer);
}
Fruit::~Fruit() {
SDL_DestroyTexture(this-> appleTexture);
SDL_DestroyTexture(this-> jamTexture);
SDL_DestroyTexture(this-> shieldTexture);
SDL_DestroyTexture(this-> poopTexture);
}
void Fruit::relocate(Fragment* Head, Fragment* Tail) {
if (rand()%10 == 0) this-> type = FRUIT_SHIELD;
else if (rand()%5 == 0) this-> type = FRUIT_JAM;
else this-> type = FRUIT_APPLE;
for (int i = 0; i == 0 || (i < 10 && Head->checkCollision(this-> x, this-> y)); i++) {
this-> x = rand() % GRID_WIDTH;
this-> y = rand() % GRID_HEIGHT;
} if (Head->checkCollision(this-> x, this-> y)) {
this-> type = FRUIT_POOP;
this-> x = Tail-> x;
this-> y = Tail-> y;
}
}
void Fruit::print(SDL_Renderer* renderer) {
if (this-> type == FRUIT_APPLE)
printImgOnRenderer(this-> appleTexture, renderer, {this->x, this->y});
else if (this-> type == FRUIT_JAM)
printImgOnRenderer(this-> jamTexture, renderer, {this->x, this->y});
else if (this-> type == FRUIT_SHIELD)
printImgOnRenderer(this-> shieldTexture, renderer, {this->x, this->y});
else
printImgOnRenderer(this-> poopTexture, renderer, {this->x, this->y});
}
// Class SNAKE
Snake::Snake(int newX, int newY, int dir, SDL_Renderer* newRenderer) {
changeDir(dir);
this-> renderer = newRenderer;
this-> Head = new Fragment(newX, newY, this-> renderer);
this-> Head-> createFragment(newX - this-> dirX, newY - this-> dirY);
this-> Tail = Head-> next;
this-> actualLenght = 2;
this-> shield = false;
this-> score = -10;
}
Snake::~Snake() {
delete this-> Head;
}
void Snake::changeDir(int dir) {
dirX = (1-dir%2)*(1-dir);
dirY = -(dir%2)*(dir-2);
}
void Snake::move(int dir) {
changeDir(dir);
this-> Head-> move(this-> Head->x + this-> dirX, this-> Head->y + this-> dirY);
}
void Snake::eat(Fruit* whatever) {
int size_gain = 1;
this-> score += 10;
if (whatever-> type == FRUIT_JAM) {
size_gain = 3;
this-> score += 20;
} else if (whatever-> type == FRUIT_SHIELD) {
this-> shield = true;
this-> score += 65;
}
whatever-> relocate(this-> Head, this-> Tail);
for (int i = 0; i < size_gain; i++) {
this-> Head-> createFragment(this-> Tail-> x, this-> Tail-> y);
this-> Tail = this-> Tail -> next;
this-> actualLenght++;
}
}
void Snake::printEntireSnake() {
this-> Head -> next-> printAndNext(90*((this->Head->x-this->Head->next->x-1)*(this->Head->x-this->Head->next->x)+this->Head->y-this->Head->next->y));
this-> Head -> printHeadSkin(90*((dirX-1)*dirX+dirY));
}
bool Snake::hitAWallOrHimself() {
return this-> Head-> next-> checkCollision(this-> Head-> x, this-> Head->y)
|| this-> Head-> x > GRID_WIDTH || this-> Head-> x < 0
|| this-> Head-> y > GRID_HEIGHT || this-> Head-> y < 0;
}