贪吃蛇
# include <stdio.h>
# include <time.h>
# include <windows.h>
# include <stdlib.h>
# include <conio.h>
# define U 1
# define D 2
# define L 3
# define R 4
void file_out ( ) ;
void tips ( ) ;
void printscore ( ) ;
void createfood ( ) ;
typedef struct snake {
int x;
int y;
struct snake * next;
} snake;
typedef struct foodtype {
int x;
int y;
} foodtype;
int score = 0 ;
int add = 10 ;
int HighScore = 0 ;
int status= R;
int sleeptime = 150 ;
snake * head;
foodtype * food;
snake * q;
int gameover= 0 ;
HANDLE hOut;
void color ( unsigned char bg, unsigned char fg) {
WORD wColor = ( ( bg & 0x0F ) << 4 + ( fg & 0x0F ) ) ;
SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE) , wColor) ;
}
void textcolor ( char fg) {
SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE) , FOREGROUND_RED |
FOREGROUND_GREEN |
FOREGROUND_BLUE) ;
SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE) , fg) ;
}
void gotoxy ( int x, int y) {
COORD c;
c. X = x;
c. Y = y;
SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE) , c) ;
}
void close_cur ( ) {
hOut= GetStdHandle ( STD_OUTPUT_HANDLE) ;
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo ( hOut, & CursorInfo) ;
CursorInfo. bVisible= 0 ;
SetConsoleCursorInfo ( hOut, & CursorInfo) ;
}
void createmap ( ) {
int i, j;
color ( 5 , 0 ) ;
gotoxy ( 0 , 0 ) ;
for ( i = 0 ; i< 60 ; i++ ) printf ( " " ) ;
gotoxy ( 0 , 26 ) ;
for ( i = 0 ; i< 60 ; i++ ) printf ( " " ) ;
for ( i = 1 ; i< 26 ; i++ ) {
gotoxy ( 0 , i) ;
printf ( " " ) ;
gotoxy ( 58 , i) ;
printf ( " " ) ;
}
color ( 3 , 0 ) ;
for ( i= 2 ; i< 57 ; i+= 2 ) {
for ( j= 1 ; j< 26 ; j++ ) {
gotoxy ( i, j) ;
printf ( " " ) ;
}
}
tips ( ) ;
}
void tips ( ) {
gotoxy ( 70 , 4 ) ;
textcolor ( 11 ) ;
printf ( "最高记录: %d" , HighScore) ;
textcolor ( 13 ) ;
gotoxy ( 75 , 11 ) ;
printf ( "小提示" ) ;
gotoxy ( 62 , 13 ) ;
textcolor ( 6 ) ;
printf ( "++------------------------------++" ) ;
gotoxy ( 62 , 25 ) ;
printf ( "++------------------------------++" ) ;
textcolor ( 3 ) ;
gotoxy ( 66 , 14 ) ;
printf ( "每个食物的得分: %d分" , add) ;
gotoxy ( 66 , 16 ) ;
printf ( "不能撞墙,不能咬到自己" ) ;
gotoxy ( 66 , 18 ) ;
printf ( "用 ↑↓← →分别控制移动" ) ;
gotoxy ( 66 , 20 ) ;
printf ( "F1: 加速 F2: 减速" ) ;
gotoxy ( 66 , 22 ) ;
printf ( "space: 暂停游戏" ) ;
gotoxy ( 66 , 24 ) ;
printf ( "esc: 退出游戏" ) ;
printscore ( ) ;
}
void printscore ( ) {
gotoxy ( 72 , 6 ) ;
textcolor ( 14 ) ;
printf ( "得分: %d" , score) ;
}
void file_out ( ) {
FILE* fp;
fp = fopen ( "save.txt" , "a+" ) ;
fscanf ( fp, "%d" , & HighScore) ;
fclose ( fp) ;
}
void initsnake ( ) {
snake * tail;
int i;
tail = ( snake* ) malloc ( sizeof ( snake) ) ;
tail-> x = 24 ;
tail-> y = 5 ;
tail-> next = NULL ;
for ( i = 1 ; i<= 4 ; i++ ) {
head = ( snake* ) malloc ( sizeof ( snake) ) ;
head-> next = tail;
head-> x = 24 + 2 * i;
head-> y = 5 ;
tail = head;
}
while ( tail!= NULL ) {
gotoxy ( tail-> x, tail-> y) ;
color ( 14 , 0 ) ;
printf ( " " ) ;
tail= tail-> next;
}
createfood ( ) ;
}
void createfood ( ) {
int ifok= 0 , x= 0 , y= 0 ;
while ( ! ifok) {
x= ( rand ( ) % 28 + 1 ) * 2 ;
y= rand ( ) % 25 + 1 ;
q= head;
while ( q-> next!= NULL ) {
if ( q-> x== x && q-> y == y) break ;
q = q-> next;
}
if ( q-> next== NULL ) ifok= 1 ;
}
gotoxy ( x, y) ;
food-> x = x;
food-> y = y;
color ( 12 , 0 ) ;
printf ( " " ) ;
}
int biteself ( ) {
q = head-> next;
while ( q!= NULL ) {
if ( q-> x== head-> x && q-> y== head-> y) {
return 1 ;
}
q= q-> next;
}
return 0 ;
}
int cantcrosswall ( ) {
if ( head-> x < 2 || head-> x> 56 || head-> y< 1 || head-> y> 25 ) {
return 1 ;
}
return 0 ;
}
void speedup ( ) {
if ( sleeptime>= 50 ) {
sleeptime= sleeptime- 10 ;
add = add+ 2 ;
}
}
void speeddown ( ) {
if ( sleeptime< 200 ) {
sleeptime += 10 ;
add -= 2 ;
if ( add< 1 ) add = 1 ;
}
}
void eatfood ( snake* head) {
while ( head!= NULL ) {
gotoxy ( head-> x, head-> y) ;
color ( 14 , 0 ) ;
printf ( " " ) ;
head= head-> next;
}
score += add;
speedup ( ) ;
createfood ( ) ;
}
void donteatfood ( snake* head) {
while ( head-> next-> next!= NULL ) {
gotoxy ( head-> x, head-> y) ;
color ( 14 , 0 ) ;
printf ( " " ) ;
head= head-> next;
}
gotoxy ( head-> next-> x, head-> next-> y) ;
color ( 3 , 0 ) ;
printf ( " " ) ;
free ( head-> next) ;
head-> next = NULL ;
}
void snakemove ( ) {
snake* nexthead;
nexthead = ( snake* ) malloc ( sizeof ( snake) ) ;
nexthead-> next = head;
if ( status== U) {
nexthead-> x = head-> x;
nexthead-> y = head-> y- 1 ;
} else if ( status== D) {
nexthead-> x = head-> x;
nexthead-> y = head-> y+ 1 ;
} else if ( status== R) {
nexthead-> x = head-> x+ 2 ;
nexthead-> y = head-> y;
} else if ( status== L) {
nexthead-> x = head-> x- 2 ;
nexthead-> y = head-> y;
}
if ( nexthead-> x == food-> x && nexthead-> y == food-> y) {
eatfood ( nexthead) ;
printscore ( ) ;
}
else {
donteatfood ( nexthead) ;
}
head = nexthead;
if ( cantcrosswall ( ) ) {
gameover = 1 ;
}
if ( biteself ( ) == 1 ) {
gameover = 2 ;
}
}
void keyboardControl ( ) {
while ( ! gameover) {
if ( GetAsyncKeyState ( VK_UP) & 0x8000 && status!= D) {
status = U;
}
if ( GetAsyncKeyState ( VK_DOWN) & 0x8000 && status!= U) {
status = D;
}
if ( GetAsyncKeyState ( VK_LEFT) & 0x8000 && status!= R) {
status = L;
}
if ( GetAsyncKeyState ( VK_RIGHT) & 0x8000 && status!= L) {
status = R;
}
if ( GetAsyncKeyState ( VK_SPACE) & 0x8000 ) {
while ( 1 ) {
Sleep ( 300 ) ;
if ( GetAsyncKeyState ( VK_SPACE) & 0x8000 ) {
break ;
}
}
}
if ( GetAsyncKeyState ( VK_ESCAPE) & 0x8000 ) {
gameover = 3 ;
}
if ( GetAsyncKeyState ( VK_F1) & 0x8000 ) {
speedup ( ) ;
}
if ( GetAsyncKeyState ( VK_F2) & 0x8000 ) {
speeddown ( ) ;
}
Sleep ( sleeptime) ;
snakemove ( ) ;
}
}
void file_in ( ) {
FILE* fp;
fp = fopen ( "save.txt" , "w+" ) ;
fprintf ( fp, "%d" , score) ;
fclose ( fp) ;
}
void endgame ( ) {
int i, j;
textcolor ( 4 ) ;
system ( "cls" ) ;
gotoxy ( 30 , 5 ) ;
for ( i= 0 ; i< 40 ; i++ ) printf ( "+" ) ;
gotoxy ( 30 , 20 ) ;
for ( i= 0 ; i< 40 ; i++ ) printf ( "+" ) ;
for ( i= 6 ; i< 20 ; i++ ) {
gotoxy ( 30 , i) ;
printf ( "+" ) ;
gotoxy ( 69 , i) ;
printf ( "+" ) ;
}
textcolor ( 15 ) ;
gotoxy ( 45 , 8 ) ;
printf ( "游戏结束!" ) ;
if ( gameover== 1 ) {
gotoxy ( 43 , 10 ) ;
printf ( "您因撞墙而死!!" ) ;
} else {
gotoxy ( 41 , 10 ) ;
printf ( "您因咬到自己而死!!" ) ;
}
gotoxy ( 45 , 12 ) ;
printf ( "得分: %d" , score) ;
if ( score> HighScore) {
gotoxy ( 42 , 14 ) ;
printf ( "您已创新的记录!" ) ;
file_in ( ) ;
} else {
gotoxy ( 40 , 14 ) ;
printf ( "您距离最高记录还差:%d分" , HighScore- score) ;
}
}
int main ( ) {
close_cur ( ) ;
srand ( ( unsigned int ) time ( NULL ) ) ;
food = ( foodtype* ) malloc ( sizeof ( foodtype) ) ;
system ( "mode con cols=98 lines=27" ) ;
file_out ( ) ;
createmap ( ) ;
initsnake ( ) ;
keyboardControl ( ) ;
endgame ( ) ;
system ( "pause >nul" ) ;
return 0 ;
}