首页 > 编程语言 > 用纯C语言实现贪吃蛇游戏
2021
11-16

用纯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();
 //结束游戏
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧