Browse Source

added snack generator and implemented teh snake-snack collision checker

pull/1/head
VissaMoutafis 5 years ago
parent
commit
d273b9ea34
  1. 1
      include/Controller.h
  2. 6
      include/Graphics.h
  3. 3
      include/Snack.h
  4. 14
      src/Controller.cpp
  5. 7
      src/Graphics.cpp
  6. 22
      src/Snack.cpp
  7. 5
      src/SnakeImpl.cpp

1
include/Controller.h

@ -8,6 +8,7 @@
class Controller{
private:
Snake *snake; //the figure the controler controls
Point snack;
graphics_input inpt;
public:
Controller(Snake* _snake);

6
include/Graphics.h

@ -11,9 +11,9 @@ typedef int graphics_input; //the type of the graphics input
#define GAME_TOP_WALL_Y 1
#define GAME_BOTTOM_WALL_Y LINES+1
#define GAME_LEFT_WALL_X -1
#define GAME_RIGHT_WALL_X COLS+1
#define GAME_BOTTOM_WALL_Y (LINES - 4)
#define GAME_LEFT_WALL_X 1
#define GAME_RIGHT_WALL_X (COLS - 2)
void initializeGraphics(void);
void endGraphics(void);

3
include/Snack.h

@ -4,3 +4,6 @@
#define SNACK '*'
//snack methods
void generateSnack(Point &snack);
unsigned int getSnackScore(void);

14
src/Controller.cpp

@ -4,13 +4,15 @@
using namespace std;
Controller::Controller()
:inpt{0}
: snack{SNACK} ,inpt{0}
{
snake = new Snake();
generateSnack(snack);
}
Controller::Controller(Snake *_snake)
: snake{_snake}, inpt{0} {
: snake{_snake}, snack{SNACK}, inpt{0}
{
assert(this->snake != NULL);
}
@ -18,6 +20,13 @@ Controller::Controller(Snake *_snake)
Controller::~Controller(){}
int Controller::act(void){
if (snake->hasBitSnack(this->snack.getY(), this->snack.getX()) == true)
{
generateSnack(this->snack);
this->snake->incSize();
}
switch (this->inpt)
{
case UP:
@ -36,6 +45,7 @@ int Controller::act(void){
snake->move();
break;
}
refreshScreen();
if(snake->isBitten())

7
src/Graphics.cpp

@ -1,8 +1,11 @@
#include "Graphics.h"
#include <chrono>
#include <thread>
#include <time.h>
#include <cstdlib>
///Utillities for the graphics
unsigned int sleepTime = 50000000;
unsigned int sleepTime = 40000000;
static WINDOW *_box = NULL;
static void createBox(void){
@ -17,6 +20,8 @@ static void destroyBox(void){
}
//Initialization function
void initializeGraphics(void){
srand((unsigned int) time(NULL));
initscr(); //initialize curses
cbreak(); //set line buffering to false
noecho(); //set input echo to false

22
src/Snack.cpp

@ -0,0 +1,22 @@
#include "Snack.h"
#include <cstdlib>
using namespace std;
const unsigned int snackScore = 10;
void generateSnack(Point &snack){
int x, y;
x = (rand() + 1) % GAME_RIGHT_WALL_X;
y = (rand() + 1) % GAME_BOTTOM_WALL_Y;
snack.setPoint(y, x);
printChar(y, x, snack.getImg());
}
unsigned int getSnackScore(void){
return snackScore;
}

5
src/SnakeImpl.cpp

@ -6,7 +6,7 @@ Snake::Snake(int headY, int headX)
:direction{LEFT}
{
snake.push_back(Point{headY, headX, '>'}); //add the head of the snake
for(int i=1; i<=7; i++)
for(int i=1; i<=3; i++)
snake.push_back(Point{headY, headX+i, BODY});
this->printSnake();
}
@ -28,7 +28,8 @@ bool Snake::isBitten(void){
}
bool Snake::hasBitSnack(int snackY, int snackX){
return readChar(snackY, snackX) == SNACK;
return snake.begin()->getY() == snackY
&& snake.begin()->getX() == snackX;
}
bool Snake::hasCrashedWall(void){

Loading…
Cancel
Save