.
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								Snakes/snakes
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Snakes/snakes
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							@@ -2,21 +2,21 @@
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <ncurses.h>
 | 
			
		||||
#include <thread>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
const int width = 80;
 | 
			
		||||
const int height = 20;
 | 
			
		||||
int snakeX = width/2;
 | 
			
		||||
int snakeY = height/2;
 | 
			
		||||
int X = width/2;
 | 
			
		||||
int Y = height/2;
 | 
			
		||||
int sDir = 0;
 | 
			
		||||
int score = -1;
 | 
			
		||||
int count = 0;
 | 
			
		||||
int tail = 1;
 | 
			
		||||
bool gameover;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void start(){
 | 
			
		||||
    gameover=false;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
void draw(){
 | 
			
		||||
WINDOW * win;
 | 
			
		||||
WINDOW * win2;
 | 
			
		||||
/*void draw(){
 | 
			
		||||
//Change "clear" to "cls" if compiling for Windows
 | 
			
		||||
    system("clear");
 | 
			
		||||
for(int i=0; i<width; i++){
 | 
			
		||||
@@ -38,52 +38,117 @@ for(int i=0; i<width; i++){
 | 
			
		||||
    cout << "#";
 | 
			
		||||
}
 | 
			
		||||
cout<<endl;
 | 
			
		||||
}*/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void start(){
 | 
			
		||||
    gameover=false;
 | 
			
		||||
    }
 | 
			
		||||
void gover(){
 | 
			
		||||
    initscr();
 | 
			
		||||
    noecho();
 | 
			
		||||
    curs_set(0);
 | 
			
		||||
    win = newwin(height, width, 0, 0);
 | 
			
		||||
    box(win, 0, 0);
 | 
			
		||||
    mvwprintw(win,10,35,"GAME OVER");
 | 
			
		||||
    wrefresh(win);
 | 
			
		||||
    wgetch(win);
 | 
			
		||||
    endwin();
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void GameUpdate(int snakeXmv, WINDOW * win){
 | 
			
		||||
int c = getch();
 | 
			
		||||
printw("%d", c);
 | 
			
		||||
if(c == 97 || c == 68){
 | 
			
		||||
    for(int i; i < snakeX; snakeXmv--){
 | 
			
		||||
void UserInput(int tmpv) {
 | 
			
		||||
    nodelay(stdscr, TRUE);
 | 
			
		||||
    tmpv = getch();
 | 
			
		||||
    if (tmpv != ERR) {
 | 
			
		||||
        sDir = tmpv;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void fruit(int &fX, int &fY){ 
 | 
			
		||||
    if (score != -1) {
 | 
			
		||||
    mvwprintw(win, fY, fX, "F");
 | 
			
		||||
  }
 | 
			
		||||
    if (score == -1 || (fX == X && fY == Y)){
 | 
			
		||||
        score++;
 | 
			
		||||
        count++;
 | 
			
		||||
        wrefresh(win2);
 | 
			
		||||
        fX = 3 + (rand() % 75);
 | 
			
		||||
        fY = 3 + (rand() % 15);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RenderField(){
 | 
			
		||||
    initscr();
 | 
			
		||||
    noecho();
 | 
			
		||||
    curs_set(0);
 | 
			
		||||
        mvwprintw(win,snakeY,snakeXmv,"o");
 | 
			
		||||
        mvwprintw(win,snakeY,snakeXmv+1," ");
 | 
			
		||||
    win = newwin(height, width, 0, 0);
 | 
			
		||||
    win2 =newwin(3,width,20,0);
 | 
			
		||||
    box(win2,20,0);
 | 
			
		||||
    box(win, 0, 0);
 | 
			
		||||
    wrefresh(win);
 | 
			
		||||
        this_thread::sleep_for(chrono::milliseconds(1000));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
if(c == 67 || c == 100){
 | 
			
		||||
    for(int i; i < snakeX; snakeXmv++){
 | 
			
		||||
        curs_set(0);
 | 
			
		||||
        mvwprintw(win,snakeY,snakeXmv,"o");
 | 
			
		||||
        mvwprintw(win,snakeY,snakeXmv-1," ");
 | 
			
		||||
        wrefresh(win);
 | 
			
		||||
        this_thread::sleep_for(chrono::milliseconds(1000));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    wrefresh(win2);
 | 
			
		||||
   
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void GameUpdate(int &diff, int &fX, int &fY){
 | 
			
		||||
 | 
			
		||||
switch(sDir){
 | 
			
		||||
case 97:
 | 
			
		||||
case 68:
 | 
			
		||||
        X--;
 | 
			
		||||
        if (X <= 0)
 | 
			
		||||
            gameover=true;
 | 
			
		||||
        break;
 | 
			
		||||
case 100:
 | 
			
		||||
case 67:
 | 
			
		||||
        X++;
 | 
			
		||||
         if (X >= 80)
 | 
			
		||||
            gameover=true;
 | 
			
		||||
        break;
 | 
			
		||||
case 119:
 | 
			
		||||
case 65:
 | 
			
		||||
        Y--;
 | 
			
		||||
         if (Y <= 0)
 | 
			
		||||
            gameover=true;
 | 
			
		||||
        break;
 | 
			
		||||
case 115:
 | 
			
		||||
case 66:
 | 
			
		||||
        Y++;
 | 
			
		||||
         if (Y >= 20)
 | 
			
		||||
            gameover=true;
 | 
			
		||||
        break;    
 | 
			
		||||
}
 | 
			
		||||
mvwprintw(win, Y, X, "o");
 | 
			
		||||
mvwprintw(win2, 1, 3, "Score: %d", score);
 | 
			
		||||
fruit(fX, fY);
 | 
			
		||||
wrefresh(win2);
 | 
			
		||||
wrefresh(win);
 | 
			
		||||
if (count >= 5 && diff != 50){
 | 
			
		||||
    count = 0;
 | 
			
		||||
    diff -= 10;}
 | 
			
		||||
this_thread::sleep_for(chrono::milliseconds(diff));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(){
 | 
			
		||||
int snakeXmv = snakeX;
 | 
			
		||||
int tmpv;
 | 
			
		||||
int fX;
 | 
			
		||||
int fY;
 | 
			
		||||
int diff = 400;
 | 
			
		||||
start();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
while(!gameover){
 | 
			
		||||
    start();
 | 
			
		||||
    initscr();
 | 
			
		||||
    WINDOW * win = newwin(height, width, 0, 0);
 | 
			
		||||
    refresh();
 | 
			
		||||
    UserInput(tmpv);
 | 
			
		||||
    RenderField();
 | 
			
		||||
    GameUpdate(diff,fX,fY);
 | 
			
		||||
    //cout<<sDir<<endl;
 | 
			
		||||
           
 | 
			
		||||
    box(win, 0, 0);
 | 
			
		||||
    mvwprintw(win,snakeY,snakeXmv,"o");
 | 
			
		||||
    wrefresh(win);
 | 
			
		||||
    
 | 
			
		||||
    GameUpdate(snakeXmv, win);
 | 
			
		||||
    cout << snakeXmv << endl;
 | 
			
		||||
    getch();
 | 
			
		||||
    endwin();
 | 
			
		||||
}
 | 
			
		||||
gover();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
return 0;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user