Skip to content

Instantly share code, notes, and snippets.

@idwpan
Last active March 21, 2021 21:11
Show Gist options
  • Save idwpan/e30749fc5e016336ad3475eb7af625f4 to your computer and use it in GitHub Desktop.
Save idwpan/e30749fc5e016336ad3475eb7af625f4 to your computer and use it in GitHub Desktop.
Snake code
#include <ncurses.h>
#include <stdio.h>
int xPOS, yPOS;
void usleep(int i);
const char splashScreen[] =
" .o88b. d8b db .d8b. db dD d88888b \nd8P Y8 888o 88 d8' `8b 88 ,8P' 88' \n8P 88V8o 88 88ooo88 88,8P 88ooooo \n8b 88 V8o88 88~~~88 88`8b 88~~~~~ \nY8b d8 88 V888 88 88 88 `88. 88. \n `Y88P' VP V8P YP YP YP YD Y88888P ";
const char prompt[] = "Press any key to start.";
int kbhit(void) /* comment */
{
int ch, r;
// turn off getch() blocking and echo
nodelay(stdscr, TRUE);
noecho();
// check for input
ch = getch();
if( ch == ERR) // no input
{
r = FALSE;
}
else // input
{
r = TRUE;
ungetch(ch);
}
// restore block and echo
echo();
nodelay(stdscr, FALSE);
return(r);
}
void drawSnakePit()
{
mvhline(0, 1, ACS_HLINE, COLS-2);//draws top line
mvhline(1,1,ACS_BLOCK,COLS-2);//Draws # border top
mvhline(LINES-1,1,ACS_HLINE,COLS-2);//draws bottom line
mvhline(LINES-2,1,ACS_BLOCK,COLS-2);//Draws # border bottom
for(int i=1; i<LINES-1;i++)//draws left line
{
mvaddch(i,0,ACS_VLINE);
mvaddch(i,1,ACS_BLOCK); //Draws #border left
}
for(int i=1; i<LINES-1;i++)//draws right line
{
mvaddch(i,COLS-1,ACS_VLINE);
mvaddch(i,COLS-2,ACS_BLOCK);//Draws #border right
}
mvaddch(0,0,'+');//Draws upperleft corner
mvaddch(0,COLS-1,'+');//Draws upperRight corner
mvaddch(LINES-1,0,'+');//Draws lowerLeft corner
mvaddch(LINES-1,COLS-1,'+');//Draws lowerRight corner
}
void getInput(char input, bool gameRun)
{
if(input == 'w' || input == 'W')
{
yPOS--;
}
else if(input == 's' || input == 'S')
{
yPOS++;
}
else if(input == 'a' || input == 'A')
{
xPOS--;
}
else if(input == 'd' || input == 'D')
{
xPOS++;
}
else if(input == 'q' || input == 'Q')
{
gameRun = false;
}
}
int main ()
{
char input;
_Bool gameRun = true;//used to control main while loop for updating game
initscr();//starts ncurses
noecho();//prevents entered text from showing on the terminal screen
xPOS = COLS/2;//used to start snake in the middle of the window
yPOS = LINES/2;//
keypad(stdscr, TRUE);//Allows KeyBoard Input
mvprintw(0,0,splashScreen);
mvprintw(7,0,prompt);
refresh();
getch(); // start game
while(gameRun)//loop to make snake keep moving
{
clear(); //Clears the canvas
drawSnakePit(); //Draws the Snake Pit
if ( kbhit() )
{
//reads char into input var
input = getch();
}
// determines keypress and increments or decrements position values
getInput(input,gameRun);
// Draws the head of the snake
mvprintw( yPOS, xPOS, "X" );
// Need to add a function to store position of snakehead, probably will
// have to update it in the getinput() function as well
// Updates the screen
refresh();
// Check if reached edge
if ( ( xPOS <= 1 ) || \
( xPOS >= COLS - 1 ) || \
( yPOS <= 1 ) || \
( yPOS >= LINES - 1 ) )
{
gameRun = false;
}
// Sleeps for 300ms
usleep(300000);
}
clear();
printf("Game over!\n");
endwin();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment