linux下基于Ncurses的贪吃蛇
# include <stdio.h>
# include <curses.h>
# include <stdlib.h>
# include <pthread.h>
# include <unistd.h>
# define UP 1
# define DOWN - 1
# define LEFT 2
# define RIGHT - 2
struct Snake
{
int hang;
int lie;
struct Snake * next;
} ;
struct Snake food;
int key;
int dir;
int score = 0 ;
struct Snake * head;
struct Snake * tail;
void initsnakefood ( )
{
int x ;
int y ;
x= rand ( ) % 20 ;
y= rand ( ) % 20 ;
food. hang = x;
food. lie = y;
}
int judgesnakenode ( int i, int j)
{
struct Snake * p;
p = head;
while ( p!= NULL )
{
if ( p-> hang == i && p-> lie == j)
{
return 1 ;
}
p = p-> next;
}
return 0 ;
}
int hasfood ( int x, int y)
{
if ( food. hang == x && food. lie == y)
{
return 1 ;
}
return 0 ;
}
void initNcurses ( )
{
initscr ( ) ;
keypad ( stdscr , 1 ) ;
}
void GameMap ( )
{
int i, j;
move ( 0 , 0 ) ;
for ( i = 0 ; i < 20 ; i++ )
{
if ( i == 0 )
{
for ( j = 0 ; j < 20 ; j++ )
{
printw ( "--" ) ;
}
printw ( "\n" ) ;
}
if ( i>= 0 && i< 20 )
{
for ( j = 0 ; j <= 20 ; j++ )
{
if ( j== 0 || j == 20 )
{
printw ( "|" ) ;
}
else if ( judgesnakenode ( i, j) == 1 )
{
printw ( "[]" ) ;
}
else if ( hasfood ( i, j) == 1 )
{
printw ( "##" ) ;
}
else
{
printw ( " " ) ;
}
}
printw ( "\n" ) ;
}
if ( i== 19 )
{
for ( j = 0 ; j < 20 ; j++ )
{
printw ( "--" ) ;
}
printw ( "\n" ) ;
printw ( "By xiao yin student [2021-4-7]\n" ) ;
}
}
}
void addnode ( )
{
struct Snake * new = ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
new -> next = NULL ;
switch ( dir)
{
case UP:
new-> hang = tail-> hang - 1 ;
new -> lie = tail -> lie;
break ;
case DOWN:
new -> hang = tail -> hang + 1 ;
new -> lie = tail-> lie;
break ;
case LEFT:
new -> hang = tail-> hang;
new -> lie = tail -> lie- 1 ;
break ;
case RIGHT:
new -> hang = tail-> hang;
new -> lie = tail -> lie+ 1 ;
break ;
}
tail-> next = new;
tail = new;
}
/ 初始化蛇的节点
void initSnake ( )
{
struct Snake * p ;
dir = RIGHT;
while ( head!= NULL )
{
p = head;
head = head-> next ;
free ( p) ;
}
initsnakefood ( ) ;
head = ( struct Snake * ) malloc ( sizeof ( struct Snake ) ) ;
head -> hang = 2 ;
head -> lie = 1 ;
head -> next = NULL ;
tail = head;
}
void deletenode ( )
{
struct Snake * p ;
p= head ;
head = head -> next;
free ( p) ;
}
int ifsnakedie ( )
{
struct Snake * p ;
p = head;
if ( tail-> hang < 0 || tail -> hang == 20 || tail-> lie == 0 || tail -> lie == 20 )
{
return 1 ;
}
while ( p-> next!= NULL )
{
if ( p-> hang == tail -> hang && p-> lie == tail-> lie )
{
return 1 ;
}
p = p-> next ;
}
return 0 ;
}
void movesnake ( )
{
addnode ( ) ;
if ( hasfood ( tail-> hang, tail-> lie) == 1 )
{
initsnakefood ( ) ;
}
else
{
deletenode ( ) ;
}
if ( ifsnakedie ( ) )
{
initSnake ( ) ;
}
}
void * refreshMap ( )
{
while ( 1 )
{
movesnake ( ) ;
GameMap ( ) ;
refresh ( ) ;
usleep ( 100000 ) ;
}
}
void turn ( int direction)
{
if ( abs ( dir ) != abs ( direction) )
{
dir = direction;
}
}
void * changedir ( )
{
int key;
while ( 1 )
{
key = getch ( ) ;
switch ( key)
{
case KEY_DOWN :
turn ( DOWN) ;
break ;
case KEY_UP :
turn ( UP) ;
break ;
case KEY_LEFT :
turn ( LEFT) ;
break ;
case KEY_RIGHT :
turn ( RIGHT) ;
break ;
}
}
}
void startgame ( )
{
printw ( "#############################################################\n" ) ;
printw ( "| |\n" ) ;
printw ( "| Snake Game |\n" ) ;
printw ( "| |\n" ) ;
printw ( "| 1.Start 2. Over |\n" ) ;
printw ( "#############################################################\n" ) ;
printw ( "| |\n" ) ;
printw ( "|Author: xiao yin student [2021-4-7] 1.1.0 |\n" ) ;
printw ( "-------------------------------------------------------------\n" ) ;
}
int main ( )
{
pthread_t t1;
pthread_t t2;
initNcurses ( ) ;
initSnake ( ) ;
GameMap ( ) ;
pthread_create ( & t1, NULL , refreshMap, NULL ) ;
pthread_create ( & t2, NULL , changedir, NULL ) ;
while ( 1 ) ;
getch ( ) ;
endwin ( ) ;
return 0 ;
}