fl3kaucicpp/Snakes/snakes.cpp

155 lines
2.4 KiB
C++
Raw Normal View History

2024-05-15 14:29:11 +00:00
#include <iostream>
#include <cstdlib>
2024-05-21 08:34:15 +00:00
#include <ncurses.h>
#include <thread>
2024-06-04 12:17:51 +00:00
2024-05-15 14:29:11 +00:00
using namespace std;
2024-06-04 12:17:51 +00:00
2024-05-15 14:29:11 +00:00
const int width = 80;
const int height = 20;
2024-06-04 12:17:51 +00:00
int X = width/2;
int Y = height/2;
int sDir = 0;
int score = -1;
int count = 0;
int tail = 1;
2024-05-15 14:29:11 +00:00
bool gameover;
2024-06-04 12:17:51 +00:00
WINDOW * win;
WINDOW * win2;
/*void draw(){
2024-05-15 14:29:11 +00:00
//Change "clear" to "cls" if compiling for Windows
system("clear");
for(int i=0; i<width; i++){
cout << "#";
}
cout << endl;
for(int i=0; i<height; i++){
2024-05-21 08:34:15 +00:00
for(int j=0; j<width; j++){
if(j==0 || j==width-2){
2024-05-15 14:29:11 +00:00
cout<<"#";
}
if(j==snakeX && i==snakeY)
cout<<"o";
else
cout<<" ";
}cout<<endl;
}
for(int i=0; i<width; i++){
cout << "#";
}
cout<<endl;
2024-06-04 12:17:51 +00:00
}*/
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();
2024-05-15 14:29:11 +00:00
}
2024-06-04 12:17:51 +00:00
void UserInput(int tmpv) {
nodelay(stdscr, TRUE);
tmpv = getch();
if (tmpv != ERR) {
sDir = tmpv;
2024-05-21 08:34:15 +00:00
}
2024-06-04 12:17:51 +00:00
}
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);
2024-05-21 08:34:15 +00:00
}
}
2024-05-15 14:29:11 +00:00
2024-06-04 12:17:51 +00:00
void RenderField(){
2024-05-21 08:34:15 +00:00
initscr();
2024-06-04 12:17:51 +00:00
noecho();
curs_set(0);
win = newwin(height, width, 0, 0);
win2 =newwin(3,width,20,0);
box(win2,20,0);
2024-05-21 08:34:15 +00:00
box(win, 0, 0);
wrefresh(win);
2024-06-04 12:17:51 +00:00
wrefresh(win2);
2024-05-21 08:34:15 +00:00
}
2024-05-15 14:29:11 +00:00
2024-06-04 12:17:51 +00:00
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 tmpv;
int fX;
int fY;
int diff = 400;
start();
while(!gameover){
UserInput(tmpv);
RenderField();
GameUpdate(diff,fX,fY);
//cout<<sDir<<endl;
}
gover();
2024-05-15 14:29:11 +00:00
return 0;
2024-06-04 12:17:51 +00:00
}
2024-05-15 14:29:11 +00:00