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 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 \ 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 <iostream>
#include "SnakeGameState.h" #include "SnakeGameState.h"
#include "SnakeGameOverState.h"
CSnakeGameState CSnakeGameState::m_SnakeGameState; CSnakeGameState CSnakeGameState::m_SnakeGameState;
void CSnakeGameState::Init() void CSnakeGameState::Init()
{ {
srand(time(NULL)); srand(time(NULL));
BrdSet = new Snake; waitTime = new Thread;
BrdSet->newBody(); snake = new Snake;
snake->newBody();
input = 0; input = 0;
} }
...@@ -24,29 +26,34 @@ void CSnakeGameState::Resume() ...@@ -24,29 +26,34 @@ void CSnakeGameState::Resume()
void CSnakeGameState::Cleanup() void CSnakeGameState::Cleanup()
{ {
// cout << " SnakeState Cleanup" << endl; // cout << " SnakeState Cleanup" << endl;
delete waitTime;
waitTime = nullptr;
delete snake;
snake = nullptr;
} }
void CSnakeGameState::HandleEvents(CGameEngine* game) void CSnakeGameState::HandleEvents(CGameEngine* game)
{
waitTime.Sleep(1);
}
void CSnakeGameState::Update(CGameEngine* game)
{ {
if (game->newInput) if (game->newInput)
{ {
input = game->input; input = game->input;
} }
BrdSet->movement(input); }
void CSnakeGameState::Update(CGameEngine* game)
{
waitTime->Sleep(1);
snake->movement(input);
} }
void CSnakeGameState::Draw(CGameEngine* game) void CSnakeGameState::Draw(CGameEngine* game)
{ {
system("stty cooked"); system("stty cooked");
if (!(BrdSet->draw())) if (!(snake->draw()))
{ {
game->PopState(); game->HoldScore(snake->snekoScore());
game->ChangeState(CSnakeGameOverState::Instance());
} }
system("stty raw"); system("stty raw");
......
...@@ -26,8 +26,8 @@ class CSnakeGameState : public CGameState ...@@ -26,8 +26,8 @@ class CSnakeGameState : public CGameState
private: private:
static CSnakeGameState m_SnakeGameState; static CSnakeGameState m_SnakeGameState;
Snake* BrdSet; Snake* snake = nullptr;
Thread waitTime; Thread* waitTime = nullptr;
char input; char input;
}; };
......
#include <iostream> #include <iostream>
#include "SnakeMenuState.h" #include "SnakeMenuState.h"
#include "SnakeGameState.h" #include "SnakeGameState.h"
#include "SnakeScoreState.h"
#include <string> #include <string>
#include <fstream> #include <fstream>
...@@ -36,6 +37,7 @@ void CSnakeMenuState::Init() ...@@ -36,6 +37,7 @@ void CSnakeMenuState::Init()
// Set first input and draw the menu // Set first input and draw the menu
input = ' '; input = ' ';
menuPos = 0;
Draw(nullptr); Draw(nullptr);
} }
...@@ -85,7 +87,7 @@ void CSnakeMenuState::Update(CGameEngine* game) ...@@ -85,7 +87,7 @@ void CSnakeMenuState::Update(CGameEngine* game)
break; break;
case 1: //Snake case 1: //Snake
//game->PushState(CHighScore::Instance()); game->PushState(CSnakeScoreState::Instance());
break; break;
case 2: //Quit 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 ...@@ -13,3 +13,6 @@ NKV 5453
KAL 2434 KAL 2434
KOM 1404 KOM 1404
POP 636 POP 636
kom 428
asd 68
dad 36
NAME SCORE NAME SCORE
nge 654346 aaa 50
vfd 45365 aaa 40
cdn 21474 aaa 30
ooo 10000 aaa 25
LOP 10000 aaa 20
vfr 9999 aaa 15
ddd 9999 aaa 10
Kyl 9999 aaa 5
PPP 9999 kom 5
bbb 9999 aaa 1
vfd 4636 kom 1
vev 4536 qwe 1
DVS 3423 kom 0
goo 1000 kom 0
vfd 1000
efd 1000
dew 343
bth 34
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
using namespace std; using namespace std;
bool hi = true; bool hi = true;
Snake bdy; Snake bdy;
vector <Body> parts;
void Snake::movement(char input){ void Snake::movement(char input){
...@@ -52,7 +51,7 @@ void Snake::movement(char input){ ...@@ -52,7 +51,7 @@ void Snake::movement(char input){
for (int count = 0; count < parts.size(); count++){ for (int count = 0; count < parts.size(); count++){
if (count != 0) 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 ...@@ -118,11 +117,11 @@ if (grid [x][y] == grid[h][o] ){ // randomizes the apples
h = (rand() % 26) + 1; h = (rand() % 26) + 1;
o = (rand() % 57) + 1; o = (rand() % 57) + 1;
score++; score++;
bdy.newBody(); newBody();
bonusBody = bonusBody + 5; bonusBody = bonusBody + 5;
}else if (bonusBody > 0){ // adds body parts every loop after picking up apples }else if (bonusBody > 0){ // adds body parts every loop after picking up apples
bdy.newBody(); newBody();
bonusBody--; bonusBody--;
} }
...@@ -207,9 +206,9 @@ if (grid [x][y] == grid[h][o] ){ // randomizes the apples ...@@ -207,9 +206,9 @@ if (grid [x][y] == grid[h][o] ){ // randomizes the apples
return lastY; return lastY;
} }
void Body::follow(int count){ void Body::follow(int count, Snake* snake){
lastX = x; lastX = x;
lastY = y; lastY = y;
x = parts[count - 1].getlastX(); x = snake->getPart(count - 1).getlastX();
y = parts[count - 1].getlastY(); y = snake->getPart(count - 1).getlastY();
} }
\ No newline at end of file
...@@ -2,7 +2,30 @@ ...@@ -2,7 +2,30 @@
#define SNAKE_H #define SNAKE_H
#include <iostream> #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 { class Snake {
...@@ -18,6 +41,7 @@ class Snake { ...@@ -18,6 +41,7 @@ class Snake {
void newBody(); void newBody();
void GameOver(); void GameOver();
bool fillspace(); bool fillspace();
Body getPart(int index) {return parts.at(index); }
private: private:
...@@ -27,29 +51,6 @@ class Snake { ...@@ -27,29 +51,6 @@ class Snake {
char whtSpace = ' '; char whtSpace = ' ';
int direction = 5; int direction = 5;
int bonusBody = 0; int bonusBody = 0;
}; std::vector <Body> parts;
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;
}; };
#endif #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