# include <stdio.h>
# include <easyx.h>
# include <iostream>
# include <math.h>
# include <stdlib.h>
# include <conio.h>
# include <time.h>
# define PI 3.14
# define NODE_WIDTH 40
typedef struct {
int x;
int y;
} node;
void paintGrid ( ) {
for ( int y = 0 ; y < 600 ; y += NODE_WIDTH) {
line ( 0 , y, 800 , y) ;
}
for ( int x = 0 ; x < 800 ; x += NODE_WIDTH) {
line ( x, 0 , x, 600 ) ;
}
}
enum direction {
eUp,
eDown,
eLeft,
eRight
} ;
void paintSnake ( node* snake, int n) {
int left, top, right, bottom;
for ( int i = 0 ; i < n; i++ ) {
left = snake[ i] . x * NODE_WIDTH;
top = snake[ i] . y * NODE_WIDTH;
right = ( snake[ i] . x + 1 ) * NODE_WIDTH;
bottom = ( snake[ i] . y + 1 ) * NODE_WIDTH;
solidrectangle ( left, top, right, bottom) ;
}
}
node snakeMove ( node* snake, int length, int direction) {
node tail = snake[ length - 1 ] ;
for ( int i = length - 1 ; i > 0 ; i-- ) {
snake[ i] = snake[ i - 1 ] ;
}
node newHead;
newHead = snake[ 0 ] ;
if ( direction == eUp) {
newHead. y-- ;
}
else if ( direction == eDown) {
newHead. y++ ;
}
else if ( direction == eLeft) {
newHead. x-- ;
}
else {
newHead. x++ ;
}
snake[ 0 ] = newHead;
return tail;
}
void changeDirection ( enum direction * pD) {
if ( _kbhit ( ) != 0 ) {
char c = _getch ( ) ;
switch ( c) {
case 'w' :
if ( * pD != eDown)
* pD = eUp;
break ;
case 's' :
if ( * pD != eUp)
* pD = eDown;
break ;
case 'a' :
if ( * pD != eRight)
* pD = eLeft;
break ;
case 'd' :
if ( * pD != eLeft)
* pD = eRight;
break ;
}
}
}
void paintFood ( node food) {
int left, top, right, bottom;
left = food. x * NODE_WIDTH;
top = food. y * NODE_WIDTH;
right = ( food. x + 1 ) * NODE_WIDTH;
bottom = ( food. y + 1 ) * NODE_WIDTH;
setfillcolor ( YELLOW) ;
solidrectangle ( left, top, right, bottom) ;
setfillcolor ( WHITE) ;
}
node createFood ( node* snake, int length) {
node food;
while ( 1 ) {
food. x = rand ( ) % ( 800 / NODE_WIDTH) ;
food. y = rand ( ) % ( 600 / NODE_WIDTH) ;
int i;
for ( i = 0 ; i < length; i++ ) {
if ( snake[ i] . x == food. x && snake[ i] . y == food. y) {
break ;
}
}
if ( i < length) {
continue ;
}
else {
break ;
}
}
return food;
}
bool isGameOver ( node* snake, int length) {
if ( snake[ 0 ] . x < 0 || snake[ 0 ] . x > 800 / NODE_WIDTH) {
return true ;
}
if ( snake[ 0 ] . y < 0 || snake[ 0 ] . y > 600 / NODE_WIDTH) {
return true ;
}
for ( int i = 1 ; i < length; i++ ) {
if ( snake[ 0 ] . x == snake[ i] . x && snake[ 0 ] . y == snake[ i] . y) {
return true ;
}
return false ;
}
}
void reset ( node* snake, int * pLength, enum direction * d) {
snake[ 0 ] = node{ 5 , 7 } ;
snake[ 1 ] = node{ 4 , 7 } ;
snake[ 2 ] = node{ 3 , 7 } ;
snake[ 3 ] = node{ 2 , 7 } ;
snake[ 4 ] = node{ 1 , 7 } ;
* pLength = 5 ;
* d = eRight;
}
int main ( )
{
initgraph ( 800 , 600 ) ;
setbkcolor ( RGB ( 164 , 225 , 202 ) ) ;
cleardevice ( ) ;
node snake[ 100 ] = { { 5 , 7 } , { 4 , 7 } , { 3 , 7 } , { 2 , 7 } , { 1 , 7 } } ;
int length = 5 ;
enum direction d = eRight;
srand ( unsigned int ( time ( NULL ) ) ) ;
node food = createFood ( snake, length) ;
while ( 1 ) {
cleardevice ( ) ;
paintGrid ( ) ;
paintSnake ( snake, length) ;
paintFood ( food) ;
Sleep ( 1000 ) ;
changeDirection ( & d) ;
node lastTail = snakeMove ( snake, length, d) ;
if ( snake[ 0 ] . x == food. x && snake[ 0 ] . y == food. y) {
if ( length < 100 ) {
snake[ length] = lastTail;
length++ ;
}
food = createFood ( snake, length) ;
}
if ( isGameOver ( snake, length) == true ) {
reset ( snake, & length, & d) ;
food = createFood ( snake, length) ;
}
}
getchar ( ) ;
closegraph ( ) ;
return 0 ;
}