Commit 2aad108b authored by Nelson Phillips's avatar Nelson Phillips

autosave

parent 6ba21b5f
g++ *.cpp -o main.out -pthread -std=c++11 && ./main.outf
\ No newline at end of file
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include "Snake.h" #include "Snake.h"
using namespace std; using namespace std;
bool hi = true;
Snake bdy; Snake bdy;
vector <Body> parts; vector <Body> parts;
void Snake::movement(char input){ void Snake::movement(char input){
...@@ -54,48 +54,66 @@ vector <Body> parts; ...@@ -54,48 +54,66 @@ vector <Body> parts;
} }
void Snake::draw(){ bool Snake::draw(){
for (int i = 0; i < 14; i++){ for (int i = 0; i < 29; i++){
for (int q = 0; q < 30; q++){ for (int q = 0; q < 60; q++){
if (x < 1)bdy.over(); if (x < 1)return false;
if (y < 1)bdy.over(); if (y < 1)return false;
if (x > 11)bdy.over(); if (x > 26)return false;
if (y > 28)bdy.over(); if (y > 57)return false;
grid[i][q] = whtSpace; grid[i][q] = whtSpace;
if (h == i || o == q) if (h == i || o == q)
grid[h][o] = '*'; grid[h][o] = '*';
if (i == 12 || i == 0 || q == 0 || q == 29){ if (i == 27 || i == 0)
grid[i][q] = 'O'; grid[i][q] = '|';
} if(q == 0 || q == 59)
grid[i][q] = '_';
for ( int count = 0; count < parts.size(); count++){ for ( int count = 0; count < parts.size(); count++){
grid[parts[count].getX()][parts[count].getY()] = 'B'; if (grid[parts[0].getX()][parts[0].getY()] == '-') return false;
if (count == 0){
if( direction == 2)
grid[parts[count].getX()][parts[count].getY()] = '>';
else
if ( direction == 3)
grid[parts[count].getX()][parts[count].getY()] = 'v';
else
if( direction == 4)
grid[parts[count].getX()][parts[count].getY()] = '<';
else
if( direction == 1)
grid[parts[count].getX()][parts[count].getY()] = '^';
else
grid[parts[count].getX()][parts[count].getY()] = 'O';
}
else
grid[parts[count].getX()][parts[count].getY()] = '-';
} }
if (i == x && q == y)
grid[x][y] = 'S';
} }
} }
for (int i = 0; i < 14; i++){ for (int i = 0; i < 29; i++){
cout << endl; cout << endl;
for (int q = 0; q < 30; q++){ for (int q = 0; q < 60; q++){
cout << grid[i][q] << " "; cout << grid[i][q] << " ";
grid[i][q] == ' ';
} }
} }
if (grid [x][y] == grid[h][o] ){ if (grid [x][y] == grid[h][o] ){
h = (rand() % 11) + 1; h = (rand() % 26) + 1;
o = (rand() % 21) + 1; o = (rand() % 57) + 1;
score++; score++;
bdy.lengthed();
bdy.newBody(); bdy.newBody();
bonusBody = bonusBody + 2;
} else if (bonusBody > 0){
bdy.newBody();
bonusBody--;
} }
return true;
} }
int Snake::snekoScore(){ int Snake::snekoScore(){
return score; return score;
...@@ -113,25 +131,22 @@ if (grid [x][y] == grid[h][o] ){ ...@@ -113,25 +131,22 @@ if (grid [x][y] == grid[h][o] ){
int Snake::getY(){ int Snake::getY(){
return y; return y;
} }
int Snake::getLeng(){ void Snake::newBody(){
return length;
}
void Snake::lengthed(){
length++;
}
void Snake::newBody(){
Body segment (x,y); Body segment (x,y);
parts.push_back(segment); parts.push_back(segment);
} }
void Snake::over(){ void Snake::GameOver(){
quitbool = true; for (int p = 0; p < 40; p++){
cout << endl;
} }
bool Snake::quit(){ cout << " _____ ____"<< endl <<
" / ____| / __ | " << endl <<
return quitbool; " | | __ __ _ _ __ ___ ___ | | | |_ _____ _ __" << endl <<
} " | | |_ |/ _` | '_ ` _ | / _ | | | | || | / / _ | '__|" << endl <<
" | |__| | (_| | | | | | | __/ | |__| || V / __/ |" << endl <<
" |_____||__,_|_| |_| |_||___| |____/ |_/ |___|_|" << endl;
}
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
class Snake { class Snake {
public: public:
void movement(char input); void movement(char input);
void draw(); bool draw();
int snekoScore(); int snekoScore();
void clrScreen(); void clrScreen();
void bodied(); void bodied();
...@@ -16,20 +16,16 @@ class Snake { ...@@ -16,20 +16,16 @@ class Snake {
int getX(); int getX();
int getY(); int getY();
void newBody(); void newBody();
void lengthed(); void GameOver();
int getLeng();
void over();
bool quit();
private: private:
int x = 5, y = 5, i = 0, q = 0, score = 0,
h = (rand() % 14), o = (rand() % 14); int x = 13, y = 13, i = 0, q = 0, score = 0,
char grid[15][30]; h = 13, o = 23;
int randChar = (rand() % 5); char grid[30][60];
char whtSpace = ' '; char whtSpace = ' ';
int direction = 5; int direction = 5;
int length = -1; int bonusBody = 0;
bool quitbool = false;
}; };
......
...@@ -4,16 +4,17 @@ ...@@ -4,16 +4,17 @@
#include "Snake.h" #include "Snake.h"
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <time.h>
using namespace std; using namespace std;
bool hello = true;
Snake BrdSet; Snake BrdSet;
Thread Sneko; Thread Sneko;
ReadInput inputRead; ReadInput inputRead;
void charCall() { void charCall() {
while (!BrdSet.quit()){ while (hello){
BrdSet.input = inputRead.collectInput(); BrdSet.input = inputRead.collectInput();
if (BrdSet.input == 'q' || BrdSet.input == 'Q') if (BrdSet.input == 'q' || BrdSet.input == 'Q')
break; break;
...@@ -22,7 +23,7 @@ void charCall() { ...@@ -22,7 +23,7 @@ void charCall() {
} }
void snakePrint(){ void snakePrint(){
while (!BrdSet.quit()){ while (hello){
Sneko.Sleep(1); Sneko.Sleep(1);
system("stty cooked"); system("stty cooked");
...@@ -36,13 +37,16 @@ void charCall() { ...@@ -36,13 +37,16 @@ void charCall() {
cout << "Your score is : " << BrdSet.snekoScore() << endl; cout << "Your score is : " << BrdSet.snekoScore() << endl;
BrdSet.draw(); hello = BrdSet.draw();
if (hello == false)
BrdSet.GameOver();
system("stty raw"); system("stty raw");
} }
} }
int main() int main()
{ {
srand(time(NULL));
BrdSet.newBody(); BrdSet.newBody();
thread first (charCall); // spawn new thread that calls input() thread first (charCall); // spawn new thread that calls input()
......
...@@ -4,6 +4,6 @@ ...@@ -4,6 +4,6 @@
#include "Threads.h" #include "Threads.h"
void Thread::Sleep(float s){ void Thread::Sleep(float s){
int sec = int(s*250000); int sec = int(s*130000);
usleep(sec); usleep(sec);
} }
\ No newline at end of file
g++ *.cpp -o main.out -pthread -std=c++11 && ./main.out
\ 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