Commit caa566d6 authored by Konrad McClure's avatar Konrad McClure

THE GAME IS LIKE, KINDA DONE

parent 8f98ca19
g++ -std=c++11 -pthread *.cpp ../2048/*.cpp ../Snake/Threads.cpp ../Snake/Snake.cpp ../Snake/ReadInput.cpp ../SaveLoad/SaveLoad.cpp ../SaveLoad/SL2048.cpp && ./a.out
\ No newline at end of file
g++ -std=c++11 -pthread *.cpp ../2048/*.cpp ../Snake/Threads.cpp ../Snake/Snake.cpp ../Snake/ReadInput.cpp ../SaveLoad/SaveLoad.cpp ../SaveLoad/SL2048.cpp ../SaveLoad/SLSnake.cpp && ./a.out
\ No newline at end of file
#include <iostream>
#include "SnakeGameOverState.h"
#include <string>
#include <fstream>
#include <streambuf>
using namespace std;
CSnakeGameOverState CSnakeGameOverState::m_SnakeGameOverState;
void CSnakeGameOverState::Init()
{
if (!isInit)
{
// Load in main menu title
ifstream fs("SnakeGameOverTitle.txt");
fs.seekg(0, ios::end);
menuTitle.reserve(fs.tellg());
fs.seekg(0, ios::beg);
menuTitle.assign((istreambuf_iterator<char>(fs)),
istreambuf_iterator<char>());
// Set name for debugging
stateName = "Snakegameover";
// Read in score data
highscores.LoadContainers("../SaveLoad/snakedata.txt");
isInit = true;
}
// Set first input and draw the menu
input = 0;
scoreSet = false;
Draw(nullptr);
}
void CSnakeGameOverState::Pause()
{
}
void CSnakeGameOverState::Resume()
{
input = 0;
}
void CSnakeGameOverState::Cleanup()
{
}
void CSnakeGameOverState::HandleEvents(CGameEngine* game)
{
if (!scoreSet)
{
system("stty cooked");
cout << "Press enter to continue" << endl;
game->newInput = false;
game->inputPause = true;
while (game->newInput == false) {}
highscores.userInputNameMakeScore(game->GetScore());
scoreSet = true;
system("stty raw");
game->inputPause = false;
}
else
{
while (game->newInput == false) {}
input = game->input;
game->newInput = false;
}
}
void CSnakeGameOverState::Update(CGameEngine* game)
{
if (scoreSet)
{
switch(input)
{
case 'x':
game->HoldScore(0); // Reset the engine's score value
game->PopState();
break;
default:
break;
}
}
}
void CSnakeGameOverState::Draw(CGameEngine* game)
{
system("stty cooked");
cout << menuTitle << endl;
cout << "\n\n";
highscores.displayScores();
highscores.updateFile("../SaveLoad/snakedata.txt");
system("stty raw");
}
#ifndef CSNAKEGAMEOVERSTATE_H
#define CSNAKEGAMEOVERSTATE_H
#include "GameState.h"
#include "../SaveLoad/SLSnake.h"
#include <iostream>
#include <string>
class CSnakeGameOverState : public CGameState
{
public:
void Init();
void Cleanup();
void Pause();
void Resume();
void HandleEvents(CGameEngine* game);
void Update(CGameEngine* game);
void Draw(CGameEngine* game);
static CSnakeGameOverState* Instance() { return &m_SnakeGameOverState; }
protected:
CSnakeGameOverState() {}
private:
static CSnakeGameOverState m_SnakeGameOverState;
char input;
string menuTitle;
SLSnake highscores;
bool isInit = false;
bool scoreSet = false;
};
#endif
=======================================================
|| _____ ____ ||
|| / ____| / __ \ ||
|| | | __ __ _ _ __ ___ ___ | | | |_ _____ _ __ ||
|| | | |_ |/ _` | '_ ` _ \ / _ \ | | | \ \ / / _ \ '__| ||
|| | |__| | (_| | | | | | | __/ | |__| |\ V / __/ | ||
|| \_____|\__,_|_| |_| |_|\___| \____/ \_/ \___|_| ||
|| ||
=======================================================
#include <iostream>
#include "SnakeGameState.h"
#include "SnakeGameOverState.h"
CSnakeGameState CSnakeGameState::m_SnakeGameState;
void CSnakeGameState::Init()
{
srand(time(NULL));
BrdSet = new Snake;
BrdSet->newBody();
waitTime = new Thread;
snake = new Snake;
snake->newBody();
input = 0;
}
......@@ -24,29 +26,34 @@ void CSnakeGameState::Resume()
void CSnakeGameState::Cleanup()
{
// cout << " SnakeState Cleanup" << endl;
delete waitTime;
waitTime = nullptr;
delete snake;
snake = nullptr;
}
void CSnakeGameState::HandleEvents(CGameEngine* game)
{
waitTime.Sleep(1);
}
void CSnakeGameState::Update(CGameEngine* game)
{
if (game->newInput)
{
input = game->input;
}
BrdSet->movement(input);
}
void CSnakeGameState::Update(CGameEngine* game)
{
waitTime->Sleep(1);
snake->movement(input);
}
void CSnakeGameState::Draw(CGameEngine* game)
{
system("stty cooked");
if (!(BrdSet->draw()))
if (!(snake->draw()))
{
game->PopState();
game->HoldScore(snake->snekoScore());
game->ChangeState(CSnakeGameOverState::Instance());
}
system("stty raw");
......
......@@ -26,8 +26,8 @@ class CSnakeGameState : public CGameState
private:
static CSnakeGameState m_SnakeGameState;
Snake* BrdSet;
Thread waitTime;
Snake* snake = nullptr;
Thread* waitTime = nullptr;
char input;
};
......
#include <iostream>
#include "SnakeMenuState.h"
#include "SnakeGameState.h"
#include "SnakeScoreState.h"
#include <string>
#include <fstream>
......@@ -36,6 +37,7 @@ void CSnakeMenuState::Init()
// Set first input and draw the menu
input = ' ';
menuPos = 0;
Draw(nullptr);
}
......@@ -85,7 +87,7 @@ void CSnakeMenuState::Update(CGameEngine* game)
break;
case 1: //Snake
//game->PushState(CHighScore::Instance());
game->PushState(CSnakeScoreState::Instance());
break;
case 2: //Quit
......
#include <iostream>
#include "SnakeScoreState.h"
#include <string>
#include <fstream>
#include <streambuf>
using namespace std;
CSnakeScoreState CSnakeScoreState::m_SnakeScoreState;
void CSnakeScoreState::Init()
{
if (!isInit)
{
// Load in main menu title
ifstream fs("SnakeScoreTitle.txt");
fs.seekg(0, ios::end);
menuTitle.reserve(fs.tellg());
fs.seekg(0, ios::beg);
menuTitle.assign((istreambuf_iterator<char>(fs)),
istreambuf_iterator<char>());
// Set name for debugging
stateName = "Snakescore";
// Read in score data
highscores.LoadContainers("../SaveLoad/snakedata.txt");
isInit = true;
}
// Set first input and draw the menu
input = 0;
Draw(nullptr);
}
void CSnakeScoreState::Pause()
{
}
void CSnakeScoreState::Resume()
{
input = 0;
}
void CSnakeScoreState::Cleanup()
{
}
void CSnakeScoreState::HandleEvents(CGameEngine* game)
{
while (game->newInput == false)
{
input = ' ';
}
input = game->input;
game->newInput = false;
}
void CSnakeScoreState::Update(CGameEngine* game)
{
switch(input)
{
case 'x':
// Erase Scores
break;
default:
game->PopState();
break;
}
}
void CSnakeScoreState::Draw(CGameEngine* game)
{
system("stty cooked");
cout << menuTitle << endl;
highscores.displayScores();
system("stty raw");
}
#ifndef CSNAKESCORESTATE_H
#define CSNAKESCORESTATE_H
#include "GameState.h"
#include "../SaveLoad/SLSnake.h"
#include <iostream>
#include <string>
class CSnakeScoreState : public CGameState
{
public:
void Init();
void Cleanup();
void Pause();
void Resume();
void HandleEvents(CGameEngine* game);
void Update(CGameEngine* game);
void Draw(CGameEngine* game);
static CSnakeScoreState* Instance() { return &m_SnakeScoreState; }
protected:
CSnakeScoreState() {}
private:
static CSnakeScoreState m_SnakeScoreState;
char input;
string menuTitle;
SLSnake highscores;
bool isInit = false;
};
#endif
\ No newline at end of file
......@@ -13,3 +13,6 @@ NKV 5453
KAL 2434
KOM 1404
POP 636
kom 428
asd 68
dad 36
NAME SCORE
nge 654346
vfd 45365
cdn 21474
ooo 10000
LOP 10000
vfr 9999
ddd 9999
Kyl 9999
PPP 9999
bbb 9999
vfd 4636
vev 4536
DVS 3423
goo 1000
vfd 1000
efd 1000
dew 343
bth 34
aaa 50
aaa 40
aaa 30
aaa 25
aaa 20
aaa 15
aaa 10
aaa 5
kom 5
aaa 1
kom 1
qwe 1
kom 0
kom 0
......@@ -7,7 +7,6 @@
using namespace std;
bool hi = true;
Snake bdy;
vector <Body> parts;
void Snake::movement(char input){
......@@ -52,7 +51,7 @@ void Snake::movement(char input){
for (int count = 0; count < parts.size(); count++){
if (count != 0)
parts[count].follow(count);
parts[count].follow(count, this);
}
}
......@@ -118,11 +117,11 @@ if (grid [x][y] == grid[h][o] ){ // randomizes the apples
h = (rand() % 26) + 1;
o = (rand() % 57) + 1;
score++;
bdy.newBody();
newBody();
bonusBody = bonusBody + 5;
}else if (bonusBody > 0){ // adds body parts every loop after picking up apples
bdy.newBody();
newBody();
bonusBody--;
}
......@@ -207,9 +206,9 @@ if (grid [x][y] == grid[h][o] ){ // randomizes the apples
return lastY;
}
void Body::follow(int count){
void Body::follow(int count, Snake* snake){
lastX = x;
lastY = y;
x = parts[count - 1].getlastX();
y = parts[count - 1].getlastY();
x = snake->getPart(count - 1).getlastX();
y = snake->getPart(count - 1).getlastY();
}
\ No newline at end of file
......@@ -2,7 +2,30 @@
#define SNAKE_H
#include <iostream>
#include <vector>
class Snake;
class Body {
public:
Body(int xCord, int yCord);
int getX();
int getY();
void changeX(bool updown);
void changeY(bool updown);
void follow(int count, Snake* snake);
int getlastX();
int getlastY();
void setLast(int count);
private:
int x, y;
int lastX, lastY;
char piece;
};
class Snake {
......@@ -18,6 +41,7 @@ class Snake {
void newBody();
void GameOver();
bool fillspace();
Body getPart(int index) {return parts.at(index); }
private:
......@@ -27,29 +51,6 @@ class Snake {
char whtSpace = ' ';
int direction = 5;
int bonusBody = 0;
};
class Body {
public:
Body(int xCord, int yCord);
int getX();
int getY();
void changeX(bool updown);
void changeY(bool updown);
void follow(int count);
int getlastX();
int getlastY();
void setLast(int count);
private:
int x, y;
int lastX, lastY;
char piece;
std::vector <Body> parts;
};
#endif
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment