Browse Source

fixed the collisions, TODO: Snack functions and snack collisions

pull/1/head
VissaMoutafis 5 years ago
parent
commit
b89aece3e6
  1. BIN
      bin/main
  2. 4
      include/Controller.h
  3. 4
      include/Graphics.h
  4. 6
      include/Snack.h
  5. 5
      include/Snake.h
  6. 10
      src/Controller.cpp
  7. 2
      src/Graphics.cpp
  8. 12
      src/SnakeImpl.cpp
  9. BIN
      src/a.out
  10. 5
      src/main.cpp

BIN
bin/main

4
include/Controller.h

@ -3,6 +3,8 @@
#include "Graphics.h" #include "Graphics.h"
#include "Snake.h" #include "Snake.h"
#define DEFEAT -1
class Controller{ class Controller{
private: private:
Snake *snake; //the figure the controler controls Snake *snake; //the figure the controler controls
@ -14,7 +16,7 @@ public:
graphics_input readInput(void); //basic input methods: sets inpt to what it read if i tread something graphics_input readInput(void); //basic input methods: sets inpt to what it read if i tread something
void act(void); //basic act method: acts like a joystick
int act(void); //basic act method: acts like a joystick
bool wantsToQuit(void); //returns true of the user wants to quit bool wantsToQuit(void); //returns true of the user wants to quit
}; };

4
include/Graphics.h

@ -10,6 +10,10 @@
typedef int graphics_input; //the type of the graphics input 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
void initializeGraphics(void); void initializeGraphics(void);
void endGraphics(void); void endGraphics(void);

6
include/Snack.h

@ -0,0 +1,6 @@
#pragma once
#include "Point.h"
#define SNACK '*'

5
include/Snake.h

@ -3,7 +3,8 @@
#include <list> #include <list>
#include "Point.h" //the library we need to implement the snake #include "Point.h" //the library we need to implement the snake
#define SNACK '*'
#include "Snack.h"
#define BODY 'o' #define BODY 'o'
class Snake{ class Snake{
private: private:
@ -24,6 +25,8 @@ public:
bool isBitten(void); //function to check if the snake bit its self bool isBitten(void); //function to check if the snake bit its self
bool hasBitSnack(int snackY, int snackX);//checks if the snake has bitten a snack bool hasBitSnack(int snackY, int snackX);//checks if the snake has bitten a snack
bool hasCrashedWall(void); // method to check if the snake crashed the walls
int getSize(void); //get the current length int getSize(void); //get the current length
void incSize(void); //function to increase the length void incSize(void); //function to increase the length

10
src/Controller.cpp

@ -1,6 +1,6 @@
#include "Controller.h" #include "Controller.h"
#include <assert.h> #include <assert.h>
#include <iostream>
using namespace std; using namespace std;
Controller::Controller() Controller::Controller()
@ -17,7 +17,7 @@ Controller::Controller(Snake *_snake)
Controller::~Controller(){} Controller::~Controller(){}
void Controller::act(void){
int Controller::act(void){
switch (this->inpt) switch (this->inpt)
{ {
case UP: case UP:
@ -37,6 +37,12 @@ void Controller::act(void){
break; break;
} }
refreshScreen(); refreshScreen();
if(snake->isBitten())
return DEFEAT;
if (snake->hasCrashedWall())
return DEFEAT;
return 0;
} }
graphics_input Controller::readInput(void){ graphics_input Controller::readInput(void){

2
src/Graphics.cpp

@ -61,9 +61,9 @@ void printMsg(int y, int x, char* str){
} }
char readChar(int y, int x){ char readChar(int y, int x){
mvwgetch(_box, y, x);
refresh(); refresh();
wrefresh(_box); wrefresh(_box);
return mvwgetch(_box, y, x);
} }
int readInpt(){ int readInpt(){

12
src/SnakeImpl.cpp

@ -6,7 +6,7 @@ Snake::Snake(int headY, int headX)
:direction{LEFT} :direction{LEFT}
{ {
snake.push_back(Point{headY, headX, '>'}); //add the head of the snake snake.push_back(Point{headY, headX, '>'}); //add the head of the snake
for(int i=1; i<=3; i++)
for(int i=1; i<=7; i++)
snake.push_back(Point{headY, headX+i, BODY}); snake.push_back(Point{headY, headX+i, BODY});
this->printSnake(); this->printSnake();
} }
@ -21,6 +21,7 @@ bool Snake::isBitten(void){
while(body_part != snake.end()){ while(body_part != snake.end()){
if(body_part->getX() == head.getX() && body_part->getY() == head.getY()) if(body_part->getX() == head.getX() && body_part->getY() == head.getY())
return true; return true;
body_part++;
} }
return false; return false;
@ -30,6 +31,15 @@ bool Snake::hasBitSnack(int snackY, int snackX){
return readChar(snackY, snackX) == SNACK; return readChar(snackY, snackX) == SNACK;
} }
bool Snake::hasCrashedWall(void){
Point &head = *snake.begin();
return (head.getY() < GAME_TOP_WALL_Y) ||
(head.getY() > GAME_BOTTOM_WALL_Y) ||
(head.getX() < GAME_LEFT_WALL_X) ||
(head.getX() > GAME_RIGHT_WALL_X);
}
int Snake::getSize(void){ int Snake::getSize(void){
return snake.size(); return snake.size();
} }

BIN
src/a.out

5
src/main.cpp

@ -8,11 +8,10 @@ int main() {
initializeGraphics(); initializeGraphics();
Controller c; Controller c;
/* CODE TO BE WRITTEN... */ /* CODE TO BE WRITTEN... */
graphics_input in;
while(c.wantsToQuit() == false){ while(c.wantsToQuit() == false){
c.readInput(); c.readInput();
c.act();
if(c.act() == DEFEAT)
break;
} }
endGraphics(); endGraphics();
return 0; return 0;

Loading…
Cancel
Save