Skip to content

Commit

Permalink
Implemented growth mechanics
Browse files Browse the repository at this point in the history
  • Loading branch information
SL-Pirate committed Jan 18, 2023
1 parent dda82c0 commit edacac7
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 46 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
/.vscode/

#dev vlogs
/DevVlogs/
/DevVlogs/

#TODO NOTES
TODO
7 changes: 6 additions & 1 deletion include/graphic_handler.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "main.hpp"
class Entity;

class GraphicItem{
private:
Expand All @@ -12,9 +12,11 @@ class GraphicItem{
SDL_Rect *org = nullptr;
SDL_Texture *texture = nullptr;
ID id;
Entity *parent = nullptr;

GraphicItem();
GraphicItem(SDL_Texture *texture, int y, int x, ID id);
GraphicItem(SDL_Texture *texture, int y, int x, ID id, Entity *parent);
// ~GraphicItem();

SDL_Rect *getDst();
Expand All @@ -38,4 +40,7 @@ class GraphicItem{
divided by size of the graphic item
*/
int *getPos();

//risky

};
10 changes: 9 additions & 1 deletion include/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@
#define texHeight 16
#define rows 608 / 16 //resolves to number y
#define cols 608 / 16 //resolves to number x

//debug
#define DEBUG

#ifdef DEBUG
#define realP "/home/slpirate/Commons/programming/C++/snake/"
#else
#define realP ""
#endif

//globals vars
enum ID{WALL, SNAKE, FOOD};
enum ID{WALL, SNAKE, FOOD};
18 changes: 16 additions & 2 deletions include/snake.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,39 @@ class Entity{
SDL_Texture *texture = nullptr;
//GraphicItem *arr[rows][cols];
GraphicItem ***arr;
//used for passing food entities
GraphicItem *item = nullptr;

public:
Entity();

//used for passing food entities
virtual void clearItem() = 0;

//just to make this class abstract
//virtual void placeholderFunc();
};


class Snake : public Entity {
GraphicItem *snake[rows*cols];
int length = 0;
Dir dir = right;
int score = 0;

void ate(GraphicItem *nextItem);
void clearItem();

public:
Snake(Window *gameWin, GraphicItem ***arr);
bool addToArr();
void addToArr();
bool move();
bool move(Dir direction);
};


class Wall : public Entity {
void clearItem();
public:
Wall(Window *gameWin, GraphicItem ***arr);
};
Expand All @@ -44,5 +57,6 @@ class Food : public Entity {
public:
Food(Window *gameWin, GraphicItem ***arr);
void genFood();
void refresh();

void clearItem();
};
21 changes: 12 additions & 9 deletions src/food.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ Food::Food(Window *gameWin, GraphicItem ***arr){
}

void Food::genFood(){
genCords();
if (arr[cords[0]][cords[1]] == nullptr){
refresh();
if(item == nullptr){
genCords();
if (arr[cords[0]][cords[1]] == nullptr){
item = new GraphicItem(texture, cords[0], cords[1], FOOD, this);
arr[cords[0]][cords[1]] = item;
}
else{
genFood();
}
}
else{
genFood();
}

}

void Food::genCords(){
cords[0] = random() % cols;
cords[1] = random() % rows;
}

void Food::refresh(){
arr[cords[0]][cords[1]] = new GraphicItem(texture, cords[0], cords[1], FOOD);
void Food::clearItem(){
item = nullptr;
std::cout << "cleared: " << item << std::endl;
}
22 changes: 20 additions & 2 deletions src/graphic_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "graphic_handler.hpp"
#include "main.hpp"
#include "snake.hpp"

GraphicItem::GraphicItem(SDL_Texture *texture, int x, int y, ID id){
this->texture = texture;
Expand All @@ -19,6 +18,25 @@ GraphicItem::GraphicItem(SDL_Texture *texture, int x, int y, ID id){
setPos(x, y);
}

GraphicItem::GraphicItem(SDL_Texture *texture, int x, int y, ID id, Entity *parent){
this->texture = texture;
this->id = id;
this->parent = parent;

org = new SDL_Rect();
org->h = 16;
org->w = 16;
org->x = 0;
org->y = 0;

dst.w = 16;
dst.h = 16;
dst.x = x * dst.w;
dst.y = y * dst.h;

setPos(x, y);
}

SDL_Rect *GraphicItem::getDst() {
return &dst;
}
Expand Down
43 changes: 24 additions & 19 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ int main(int argc, char **argv){
bool gameRunning = true;
bool isKeyPressed = false;
bool isPaused = false;
const int numFoodItems = 3;

SDL_Event evnt;
Dir dir;
Expand All @@ -38,8 +39,10 @@ int main(int argc, char **argv){

//implementing snake & food
Snake snake = Snake(gameWin, arr);
Food food1 = Food(gameWin, arr);
Food food2 = Food(gameWin, arr);
Food *foods[numFoodItems];
for (int i = 0; i < numFoodItems; i++){
foods[i] = new Food(gameWin, arr);
}

while(gameRunning) {
while (SDL_PollEvent(&evnt)){
Expand Down Expand Up @@ -75,22 +78,23 @@ int main(int argc, char **argv){
}
gameWin->clearRen();

//cleaning the arr
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
if (arr[i][j] != nullptr){
if(arr[i][j]->id == WALL){
delete(arr[i][j]);
}
}
arr[i][j] = nullptr;
}
}

//poppulating the array w/ walls
Wall wall = Wall(gameWin, arr);
// //cleaning the arr
// for (int i = 0; i < rows; i++){
// for (int j = 0; j < cols; j++){
// if (arr[i][j] != nullptr){
// if(arr[i][j]->id == WALL){
// delete(arr[i][j]);
// }
// }
// arr[i][j] = nullptr;
// }
// }

// //poppulating the array w/ walls
// Wall wall = Wall(gameWin, arr);

//move the snake
//TODO: fix recursion
if (isKeyPressed){
if (!isPaused){
if(!snake.move(dir)){
Expand All @@ -106,8 +110,9 @@ int main(int argc, char **argv){
}

//refresh food
food1.refresh();
food2.refresh();
for (int i = 0; i < numFoodItems; i++){
foods[i]->genFood();
}

//displaying everything on screen
gameWin->render(background);
Expand All @@ -120,7 +125,7 @@ int main(int argc, char **argv){
}
}
gameWin->display();
std::this_thread::sleep_for(std::chrono::milliseconds(250));
std::this_thread::sleep_for(std::chrono::milliseconds(150));
}

delete(gameWin);
Expand Down
63 changes: 52 additions & 11 deletions src/snake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,45 @@ Snake::Snake(Window *gameWin, GraphicItem ***arr){
length = 2;
}

bool Snake::addToArr(){
void Snake::addToArr(){
for (int i = 0; i < length; i++){
arr[snake[i]->getPos()[0]][snake[i]->getPos()[1]] = snake[i];
}
}

bool Snake::move(){
//deleting the last square where the snake's tail was last in
arr[snake[length - 1]->getPos()[0]][snake[length - 1]->getPos()[1]] = nullptr;

//collision detection
if (arr[snake[0]->getPos()[0]][snake[0]->getPos()[1]] != nullptr){
if(arr[snake[0]->getPos()[0]][snake[0]->getPos()[1]]->id == WALL || arr[snake[0]->getPos()[0]][snake[0]->getPos()[1]]->id == SNAKE){
GraphicItem *nextItem = nullptr;
switch (dir){
case up:
nextItem = arr[snake[0]->getPos()[0]][snake[0]->getPos()[1] - 1];
break;
case down:
nextItem = arr[snake[0]->getPos()[0]][snake[0]->getPos()[1] + 1];
break;
case left:
nextItem = arr[snake[0]->getPos()[0] - 1][snake[0]->getPos()[1]];
break;
case right:
nextItem = arr[snake[0]->getPos()[0] + 1][snake[0]->getPos()[1]];
}
if (nextItem != nullptr){
if(nextItem->id == WALL || nextItem->id == SNAKE){
return false;
}
else if(nextItem->id == FOOD){
ate(nextItem);
}
}
else{
for (int i = 0; i < length; i++){
arr[snake[i]->getPos()[0]][snake[i]->getPos()[1]] = snake[i];
}
delete(snake[length - 1]);
snake[length - 1] = nullptr;
}
return true;
}

bool Snake::move(){
for (int i = length; i >= 1; i--){
for (int i = length - 1; i >= 1; i--){
snake[i] = snake[i - 1];
}
switch (dir){
Expand All @@ -45,7 +67,9 @@ bool Snake::move(){
snake[0] = new GraphicItem(texture, snake[1]->getPos()[0] + 1, snake[1]->getPos()[1], SNAKE);
}

return addToArr();
addToArr();

return true;
}

bool Snake::move(Dir direction){
Expand All @@ -64,3 +88,20 @@ bool Snake::move(Dir direction){

return move();
}

void Snake::ate(GraphicItem *nextItem){
length++;
score++;

//delete the food item
arr[nextItem->getPos()[0]][nextItem->getPos()[1]]->parent->clearItem();
delete(arr[nextItem->getPos()[0]][nextItem->getPos()[1]]);

//output the score
system("clear");
std::cout << "Your score: " << score << std::endl;
}

void Snake::clearItem(){
item = nullptr;
}
4 changes: 4 additions & 0 deletions src/wall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ Wall::Wall(Window *gameWin, GraphicItem ***arr){
}
}
}

void Wall::clearItem(){
item = nullptr;
}

0 comments on commit edacac7

Please sign in to comment.