This commit is contained in:
fl3ka 2024-06-04 14:17:51 +02:00
parent f65249e597
commit a7502e370c
2 changed files with 112 additions and 47 deletions

Binary file not shown.

View File

@ -2,21 +2,21 @@
#include <cstdlib> #include <cstdlib>
#include <ncurses.h> #include <ncurses.h>
#include <thread> #include <thread>
using namespace std; using namespace std;
const int width = 80; const int width = 80;
const int height = 20; const int height = 20;
int snakeX = width/2; int X = width/2;
int snakeY = height/2; int Y = height/2;
int sDir = 0;
int score = -1;
int count = 0;
int tail = 1;
bool gameover; bool gameover;
WINDOW * win;
WINDOW * win2;
void start(){ /*void draw(){
gameover=false;
}
void draw(){
//Change "clear" to "cls" if compiling for Windows //Change "clear" to "cls" if compiling for Windows
system("clear"); system("clear");
for(int i=0; i<width; i++){ for(int i=0; i<width; i++){
@ -38,52 +38,117 @@ for(int i=0; i<width; i++){
cout << "#"; cout << "#";
} }
cout<<endl; 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){ void UserInput(int tmpv) {
int c = getch(); nodelay(stdscr, TRUE);
printw("%d", c); tmpv = getch();
if(c == 97 || c == 68){ if (tmpv != ERR) {
for(int i; i < snakeX; snakeXmv--){ sDir = tmpv;
curs_set(0);
mvwprintw(win,snakeY,snakeXmv,"o");
mvwprintw(win,snakeY,snakeXmv+1," ");
wrefresh(win);
this_thread::sleep_for(chrono::milliseconds(1000));
}
} }
if(c == 67 || c == 100){ }
for(int i; i < snakeX; snakeXmv++){
curs_set(0); void fruit(int &fX, int &fY){
mvwprintw(win,snakeY,snakeXmv,"o"); if (score != -1) {
mvwprintw(win,snakeY,snakeXmv-1," "); mvwprintw(win, fY, fX, "F");
wrefresh(win); }
this_thread::sleep_for(chrono::milliseconds(1000)); 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);
win = newwin(height, width, 0, 0);
win2 =newwin(3,width,20,0);
box(win2,20,0);
box(win, 0, 0);
wrefresh(win);
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 main(){
int snakeXmv = snakeX; int tmpv;
int fX;
int fY;
int diff = 400;
start();
while(!gameover){ while(!gameover){
start(); UserInput(tmpv);
initscr(); RenderField();
WINDOW * win = newwin(height, width, 0, 0); GameUpdate(diff,fX,fY);
refresh(); //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; return 0;
} }