用純C語言實現貪吃蛇遊戲

本文實例為大傢分享瞭C語言實現貪吃蛇遊戲的具體代碼,供大傢參考,具體內容如下

在讀大學生一枚,五一期間用四天時間寫一個小遊戲。

本貪吃蛇遊戲代碼共計531行,開發環境是VS,遊戲分為兩種模式,標準模式和超大地圖模式,詳細看以下源代碼,並進入遊戲體驗具體內容。

//貪吃蛇
 
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
//遊戲窗口
#define FrameX 5
#define FrameY 5
#define Frame_width 20
#define Frame_height 20
#define FrameX2 3
#define FrameY2 3
#define Frame_width2 55
#define Frame_height2 47
//全局變量
int i, j, p, q, m;int a[2];
//蛇的結構體
struct Snack{ 
int x[200];
//蛇的X坐標; 
int y[200];
//蛇的Y坐標; 
int count;
//蛇吃食物的數量; 
int length;
//蛇身體的長度; 
int speed;
//蛇移動的速度;
};
 
//食物的結構
struct Food{ 
 int x;
//食物的X坐標;
    int y;
//食物的Y坐標;
};
 
//光標的位置
void gotoxy(HANDLE hOut, int x, int y){ COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(hOut, pos);}
 
//遊戲菜單
void menu(){
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
void over_game();
void start_game();
void start_game2(); 
int n; 
gotoxy(hOut, 50, 14);
printf(" 超貪吃的蛇"); 
gotoxy(hOut, 50, 16); 
printf("正常遊戲模式【1】");
gotoxy(hOut, 50, 17);
printf("超大地圖模式【2】");
gotoxy(hOut, 50, 18);
printf(" 結束遊戲【3】");
scanf_s("%d", &n); 
switch(n) 
{ 
case 1: 
 system("cls"); 
 start_game(); 
 over_game(); 
 break; 
case 2: 
 system("cls"); 
 start_game2(); 
 over_game();
 break; 
case 3: 
 system("cls"); 
 over_game();
 break; 
default: 
 printf("你的輸入有誤,請重新輸入!"); 
 menu(); 
 break;
}}
//制作遊戲窗口2
void make_frame2()
{
 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
 //定義顯示器句柄變量 
 gotoxy(hOut, 24, 13);
 //定義障礙物
 printf("#");
 gotoxy(hOut, 14, 23);
 printf("#");
 gotoxy(hOut, 35, 20);
 //定義障礙物 
 printf("#");
 gotoxy(hOut, 40, 35);
 printf("#");
 gotoxy(hOut, 70, 6);
 //定義障礙物 
 printf("#");
 gotoxy(hOut, 80, 16);
 printf("#");
 gotoxy(hOut, 60, 45);
 //定義障礙物
 printf("#");
 gotoxy(hOut, 28, 8);
 printf("#");
for (i = 0; i < 2*Frame_width2+1;i++) 
{ gotoxy(hOut, FrameX2 + i, FrameY2);
//上邊框
printf("-"); gotoxy(hOut, FrameX2 + i, Frame_height2+FrameY2);
//下邊框 
printf("-"); } 
for (i = 0; i < Frame_height2-1; i++) 
{ gotoxy(hOut, FrameX2, FrameY2 + i+1);
//左邊框 
printf("|"); 
gotoxy(hOut, FrameX2+2*Frame_width2, FrameY2 + i+1);
//右邊框
printf("|"); }}
//制作遊戲窗口
void make_frame()
{
 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
 //定義顯示器句柄變量
 gotoxy(hOut, 24, 13); 
 //定義障礙物
 printf("#"); 
 gotoxy(hOut, 14, 23); 
 printf("#"); 
 for (i = 0; i < 2 * Frame_width + 1; i++)
 { gotoxy(hOut, FrameX + i, FrameY);
 //上邊框 
 printf("-");
 gotoxy(hOut, FrameX + i, Frame_height + FrameY);
 //下邊框
 printf("-"); } 
 for (i = 0; i < Frame_height - 1; i++)
 { gotoxy(hOut, FrameX, FrameY + i + 1);
 //左邊框
 printf("|");
 gotoxy(hOut, FrameX + 2 * Frame_width, FrameY + i + 1);
 //右邊框 
 printf("|"); }}
 
//初始化蛇
void intt_snack(struct Snack* snack)
{ 
 snack->x[0] = FrameX + 5;
 //蛇的橫坐標;
 snack->y[0] = FrameY + 6;
 //蛇的縱坐標; 
 snack->speed = 200;
 //蛇的速度
 snack->length = 3;
 //蛇的長度; 
 snack->count = 0; 
 for (i = 1; i < snack->length; i++) 
    { 
 snack->x[i] = snack->x[i - 1]+2; 
    snack->y[i] = snack->y[i - 1];
    }
}
 
//初始化蛇2
void intt_snack2(struct Snack* snack)
{ snack->x[0] = FrameX2 + 5;
//蛇的橫坐標; 
snack->y[0] = FrameY2 + 6;
//蛇的縱坐標;
snack->speed = 200;
//蛇的速度; 
snack->length = 3;
//蛇的長度; 
snack->count = 0;
for (i = 1; i < snack->length; i++) 
{ snack->x[i] = snack->x[i - 1] + 2;
snack->y[i] = snack->y[i - 1]; }}
 
//移動蛇
void move_snack(HANDLE hOut,struct Snack* snack)
{ gotoxy(hOut, snack->x[0], snack->y[0]); printf(" "); 
//清除蛇尾;
for (i = 1; i < snack->length; i++) 
{ 
  snack->x[i - 1] = snack->x[i];
  snack->y[i - 1] = snack->y[i]; 
}}
 
//打印蛇
void print_snack(HANDLE hOut, struct Snack* snack)
{ 
 for (i = 0; i < snack->length; i++) 
{ 
  gotoxy(hOut, snack->x[i], snack->y[i]); 
if (i == 0) 
{ 
 printf("*");
} 
else if 
(i < snack->length - 1) 
{
 printf("0"); 
} 
else 
{ 
 printf("@"); 
} 
 }
}
 
//打印食物
void print_food(HANDLE hOut, struct Snack*snack,struct Food* food){ srand((unsigned)time(0));
while (1) 
{ 
food->x = rand() % 2*Frame_width+1; food->y = rand() % Frame_height;
if (food->x == 0 || food->y == 0) { continue; }
food->x = food->x+FrameX;
food->y = food->y + FrameY;
for (i = 0; i < snack->length; i++) 
{ 
 if (food->x == snack->x[i] && food->y == snack->y[i]) 
{ 
  break;
 } 
} 
if (i == snack->length) 
{ 
 gotoxy(hOut, food->x, food->y);
printf("$"); 
break;
} 
}
}
 
//打印食物2
void print_food2(HANDLE hOut, struct Snack* snack, struct Food* food)
{ srand((unsigned)time(0));
while (1) 
{ 
 food->x = (rand() % (3*Frame_width2 + 1))+FrameX2;
food->y = (rand() % Frame_height2)+FrameY2; 
if (food->x == 0 || food->y == 0) 
{ 
 continue; 
} 
for (i = 0; i < snack->length; i++)
{ 
 if (food->x == snack->x[i] && food->y == snack->y[i])
{
  break; 
 } 
} 
if (i == snack->length)
{ 
 gotoxy(hOut, food->x, food->y); printf("$");
break; 
}
}
}
 
//吃食物
void eat_food(HANDLE hOut, struct Snack *snack, struct Food *food)
{ if (snack->x[snack->length-1] == food->x && snack->y[snack->length-1] == food->y)
{ snack->length++; 
for (i = snack->length; i >= 1; i--) 
{ snack->x[i] = snack->x[i - 1]; 
snack->y[i] =snack->y[i - 1]; } snack->x[0] = a[0]; 
snack->y[0] = a[1]; print_food(hOut,snack,food); 
snack->count++; if (snack->count % 5 == 0) snack->speed -= 20; }}
 
//穿墻
void through_wall(HANDLE hOut, struct Snack* snack, char ch)
{ if (ch == 72 && snack->y[snack->length - 1] == FrameY) 
{ snack->y[snack->length - 1] =Frame_height+4; 
//如果蛇在上框且向上移動,穿墻 
} if (ch == 80 && snack->y[snack->length - 1] == FrameY + Frame_height)
{ snack->y[snack->length - 1] = FrameY+1; 
//如果蛇在下框且向下移動,穿墻
} if (ch == 75 && snack->x[snack->length - 1] == FrameX-1) 
{ snack->x[snack->length - 1] =2*Frame_width + FrameX-1;
//如果蛇在左框且向左移動,穿墻 
} if (ch == 77 && snack->x[snack->length - 1] == 2*Frame_width+FrameX+1)
{ snack->x[snack->length - 1] = FrameX+1; 
//如果蛇在右框且向右移動,穿墻
}}
 
//穿墻2
void through_wall2(HANDLE hOut, struct Snack* snack, char ch)
{ if (ch == 72 && snack->y[snack->length - 1] == FrameY2) 
{ snack->y[snack->length - 1] = Frame_height2 + 4;
//如果蛇在上框且向上移動,穿墻 
} if (ch == 80 && snack->y[snack->length - 1] == FrameY2 + Frame_height2)
{ snack->y[snack->length - 1] = FrameY2 + 1; 
//如果蛇在下框且向下移動,穿墻 
} if (ch == 75 && snack->x[snack->length - 1] == FrameX2 - 1) 
{ snack->x[snack->length - 1] = 2 * Frame_width2 + FrameX2 - 1; 
//如果蛇在左框且向左移動,穿墻 
} if (ch == 77 && snack->x[snack->length - 1] == 2 * Frame_width2 + FrameX2 + 1) 
{ snack->x[snack->length - 1] = FrameX2 + 1;
//如果蛇在右框且向右移動,穿墻 
}}
 
//判斷蛇是否死亡
int snack_die(struct Snack *snack){ 
 for (i = 0; i < snack->length - 1; i++) 
 { if (snack->x[snack->length - 1] == snack->x[i] && snack->y[snack->length - 1] == snack->y[i]) 
  return 0; 
 if (snack->x[snack->length - 1] == 24 && snack->y[snack->length - 1] == 13) 
  return 0;
 if (snack->x[snack->length - 1] == 14 && snack->y[snack->length - 1] == 23)
  return 0; } 
 return 1;}
 
//判斷蛇是否死亡2
int snack_die2(struct Snack* snack)
{ for (i = 0; i < snack->length - 1; i++) 
{ if (snack->x[snack->length - 1] == snack->x[i] && snack->y[snack->length - 1] == snack->y[i]) 
return 0;
if (snack->x[snack->length - 1] == 24 && snack->y[snack->length - 1] == 13) 
return 0;
if (snack->x[snack->length - 1] == 14 && snack->y[snack->length - 1] == 23) 
return 0; 
if (snack->x[snack->length - 1] == 35 && snack->y[snack->length - 1] == 20) 
return 0;
if (snack->x[snack->length - 1] == 40 && snack->y[snack->length - 1] == 35) 
return 0;
if (snack->x[snack->length - 1] == 70 && snack->y[snack->length - 1] == 6) 
return 0; 
if (snack->x[snack->length - 1] == 80 && snack->y[snack->length - 1] == 16) 
return 0; 
if (snack->x[snack->length - 1] == 60 && snack->y[snack->length - 1] == 45) 
return 0;
if (snack->x[snack->length - 1] == 28 && snack->y[snack->length - 1] == 8) 
return 0; } 
return 1;}
 
//開始遊戲
void start_game(){ unsigned char ch = 77;
//定義用於接收鍵盤輸入的字符變量
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
//定義顯示器句柄變量 
struct Snack s, * snack = &s;
struct Food f, * food = &f; make_frame(); intt_snack(snack);
print_food(hOut,snack, food);
while (1) 
{ a[0] = snack->x[0]; 
//記住蛇尾的橫坐標
a[1] = snack->y[0];
//記住蛇尾的豎坐標
 j = 0; 
if (_kbhit()) 
{
 /* 判斷是否按下鍵盤,如果按下,ch接收鍵盤輸入 */ 
ch = _getch(); 
if (_kbhit()) 
{
/* 如果長按鍵盤,則加速 */
 Sleep(20); 
j = 1; } }
 switch (ch) 
 { 
 case 72:
 {
  /* 向上移動 */
  move_snack(hOut, snack); 
  //移動蛇
  snack->y[snack->length - 1] -= 1; 
  //蛇頭的豎坐標向上移,即減1
 break;
 }
 case 80: 
 {
  /* 向下移動 */
  move_snack(hOut, snack); 
  //移動蛇 
  snack->y[snack->length - 1] += 1; 
  //蛇頭的豎坐標向下移,即加1
 break; 
 } 
 case 75: {
  /* 向左移動 */ 
  move_snack(hOut, snack); 
  //移動蛇 
  snack->x[snack->length - 1] -= 2;
  //蛇頭的橫坐標向左移,即減2 
 break;
 } 
 case 77: 
 {
  /* 向右移動 */
  move_snack(hOut, snack); 
  //移動蛇 
  snack->x[snack->length - 1] += 2;
  //蛇頭的橫坐標向右移,即加2 
  break; } } 
 through_wall(hOut, snack, ch); 
 eat_food(hOut, snack, food); 
 //吃食物 
 print_snack(hOut, snack); 
 //打印蛇
 if (snack_die(snack) == 0) 
 {
  /* 遊戲結束條件:1.蛇碰到自身 2.按Esc鍵 3.速度為50ms */
  gotoxy(hOut, 15, 15); 
  printf("Game Over"); Sleep(1000); 
  break; 
 }
 if(j == 0) 
 { 
  Sleep(snack->speed); 
 //延遲時間 
 } 
 else 
 {
  Sleep(10);
 } 
}
}
 
void start_game2(){ unsigned char ch = 77; 
//定義用於接收鍵盤輸入的字符變量 
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//定義顯示器句柄變量 
struct Snack s, * snack = &s; struct Food f, * food = &f; 
make_frame2();
intt_snack2(snack);
print_food(hOut, snack, food);
while (1) { a[0] = snack->x[0]; 
//記住蛇尾的橫坐標 
a[1] = snack->y[0];
//記住蛇尾的豎坐標 
j = 0; if (_kbhit()) 
{
 /* 判斷是否按下鍵盤,如果按下,ch接收鍵盤輸入 */ 
 ch = _getch(); 
 if (_kbhit()) {
 /* 如果長按鍵盤,則加速 */
 Sleep(20); j = 1; } } 
switch (ch) { 
case 72: {
 /* 向上移動 */
 move_snack(hOut, snack);
 //移動蛇
 snack->y[snack->length - 1] -= 1; 
 //蛇頭的豎坐標向上移,即減1
break;
} case 80: {
 /* 向下移動 */
 move_snack(hOut, snack);
 //移動蛇 
 snack->y[snack->length - 1] += 1; 
 //蛇頭的豎坐標向下移,即加1 
break; } case 75: {
 /* 向左移動 */ 
 move_snack(hOut, snack);
 //移動蛇
 snack->x[snack->length - 1] -= 2; 
 //蛇頭的橫坐標向左移,即減2
break; } case 77: {
 /* 向右移動 */ 
 move_snack(hOut, snack);
 //移動蛇 
 snack->x[snack->length - 1] += 2;
 //蛇頭的橫坐標向右移,即加2 
 break; } } 
through_wall2(hOut, snack, ch); 
eat_food(hOut, snack, food); 
//吃食物 
print_snack(hOut, snack); 
//打印蛇 
if (snack_die2(snack) == 0) 
{
 /* 遊戲結束條件:1.蛇碰到自身 2.按Esc鍵 3.速度為50ms */ 
 gotoxy(hOut, 30, 18); 
 printf("Game Over"); 
 Sleep(1000);
 break; } 
if (j == 0) { Sleep(snack->speed); 
//延遲時間 
}
else { Sleep(10); } }}
void choose()
{ HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
void start_game();
 void start_game2();
 void over_game();
 int n; 
gotoxy(hOut, 10, 20); 
printf("Continue [1]");
gotoxy(hOut, 30, 20);
 printf("Continue [2]");
gotoxy(hOut, 50, 20);
 printf("Exit [3]");
 gotoxy(hOut, 70, 20); 
printf("選擇:"); 
scanf_s("%d", &n); 
switch (n) 
{ case 1: system("cls"); 
start_game(); 
over_game();
break; 
case 2: 
system("cls"); 
start_game2(); 
over_game(); 
break; 
case 3:
exit(0); 
break;
default:
 printf("※※您的輸入有誤,請重新輸入※※");
 system("pause >nul"); over_game(); 
 choose(); break; }}
 
/******結束菜單*******************************************************************/
void over_game()
{
 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
 system("cls");
 printf("\n\n\n\n\n\n\n\n\t\t\t\t遊戲結束\n\n\n");
 choose();
 system("pause");
}
void main(){
 menu(); 
 start_game(); 
 //開始遊戲 
 start_game2();
 //開始遊戲 
 over_game();
 //結束遊戲
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: