fl3kaucicpp/Snakes/snakes.cpp

89 lines
1.6 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-05-15 14:29:11 +00:00
using namespace std;
const int width = 80;
const int height = 20;
2024-05-21 08:34:15 +00:00
int snakeX = width/2;
int snakeY = height/2;
2024-05-15 14:29:11 +00:00
bool gameover;
void start(){
gameover=false;
}
void draw(){
//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-05-21 08:34:15 +00:00
void GameUpdate(int snakeXmv, WINDOW * win){
int c = getch();
printw("%d", c);
if(c == 97 || c == 68){
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));
}
}
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));
}
}
}
2024-05-15 14:29:11 +00:00
int main(){
2024-05-21 08:34:15 +00:00
int snakeXmv = snakeX;
while(!gameover){
start();
initscr();
WINDOW * win = newwin(height, width, 0, 0);
refresh();
box(win, 0, 0);
mvwprintw(win,snakeY,snakeXmv,"o");
wrefresh(win);
2024-05-15 14:29:11 +00:00
2024-05-21 08:34:15 +00:00
GameUpdate(snakeXmv, win);
cout << snakeXmv << endl;
getch();
endwin();
}
2024-05-15 14:29:11 +00:00
return 0;
}