代码来自《C语言项目开发全程实录》第二版
主函数部分
#include<stdio.h> //标准输入输出
#include"head.h" //头文件
#include"fstatement.h" //函数声明
/********主函数*******/
int main()
{
welcometogame();
File_out();
keyboardControl();
endgame();
printf("\n");
system("pause");
return 0;
}
函数声明部分
#pragma once
#ifndef _FSTATEMENT_H_
#define _FSTATEMENT_H_
/********函数声明**********/
void gotoxy(int x, int y); //设置光标位置
int color(int c); //更改文字颜色
void welcometogame(); //开始界面
void createMap(); //绘制地图
void scoreandtips(); //游戏界面右侧的得分和小提示
void initsnake(); //初始化蛇身
void createfood(); //创建并随机出现食物
int biteself(); //判断蛇是否咬到了自己
void cantcrosswall(); //设置蛇撞墙的情况
void speedup(); //加速
void speeddown(); //减速
void snakemove(); //移动
void keyboardControl(); //控制键盘按键
void Lostdraw(); //游戏结束界面
void endgame(); //游戏结束
void choose(); //游戏失败之后的选择
void File_out(); //读取最高分
void File_in(); //最高分写入文件
#endif // !_FSTATEMENT_H_
函数定义部分
#include<stdio.h>
#include<stdlib.h>
#include"head.h"
#include"fstatement.h"
/******宏定义******/
#define U 1
#define D 2
#define L 3
#define R 4 //蛇的状态,U上、D下、L左、R右
/*********定义全局变量*********/
typedef struct snake //蛇身的一个节点
{
int x; //节点x的坐标
int y; //节点y的坐标
struct snake *next; //蛇身的下一个节点
}snake;
int score = 0, add = 10; //总得分与每次吃的食物
int HighScore = 0; //最高分
int status, sleeptime = 200; //蛇前进的状态,每次运行的时间间隔
snake *head, *food; //蛇头指针,食物指针
snake *q; //遍历蛇的时候用的指针
int endgamestatus = 0; //游戏结束情况
HANDLE hout; //控制台句柄
/*****文字颜色color()函数*****/
int color(int c)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //更改颜色文字
return 0;
}
/******设置光标位置gotoxy()*******/
void gotoxy(int x,int y)
{
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
/*******开始界面*******/
void welcometogame()
{
int n;
int i, j = 1;
gotoxy(43, 7);
color(11);
printf("贪 吃 蛇 游 戏");
color(14);
for (i = 10; i <= 16; i++)
{
for (j = 27; j <= 74; j++)
{
gotoxy(j, i);
if (i == 10 ||i == 16)
{
printf("-");
}
else if (j == 27 || j == 74)
{
printf("|");
}
}
}
color(12);
gotoxy(45, 12);
printf("1.开始游戏");
gotoxy(45, 14);
printf("2.退出游戏");
gotoxy(43, 18);
color(3);
printf("请选择[1 2]:[ ]\b\b"); //\b为退格,使光标在[]之间
color(14);
scanf_s("%d", &n);
switch (n)
{
case 1:
system("cls");
createMap(); //打印地图
initsnake();
createfood();
break;
case 2:
exit(0);
default:
color(12);
gotoxy(40, 18);
printf("请输入1-2之间的数!");
getchar();
system("cls");
welcometogame();
}
}
/*******创建地图*******/
void createMap()
{
int i, j;
for (i = 0; i < 58; i += 2) //打印上下边框
{
gotoxy(i, 0);
color(5); //深紫色边框
printf("□");
gotoxy(i, 26);
printf("□");
}
for (i = 1; i < 26; i++) //打印左右边框
{
gotoxy(0,i);
printf("□");
gotoxy(56,i);
printf("□");
}
for (i = 2; i < 56; i += 2) //打印中间网格
{
for (j = 1; j < 26; j++)
{
gotoxy(i, j);
color(3);
printf("■\n\n"); //*******\n\n将光标移到最下面
}
}
}
/*********得分与提示*********/
void scoreandtips()
{
File_out(); //读取文件
gotoxy(64, 4);
color(11);
printf("☆最高纪录☆:%d", HighScore);
gotoxy(64, 8);
color(14);
printf("得分:%d", score);
color(13);
gotoxy(73, 11);
printf("小提示");
gotoxy(60, 13);
color(6);
printf("*----------------------------------*");
gotoxy(60, 25);
printf("*----------------------------------*");
color(3);
gotoxy(64, 14);
printf("每个食物得分:%d分", add);
gotoxy(64, 16);
printf("不能穿墙,不能咬到自己");
gotoxy(64, 18);
printf("用↑↓←→分别控制蛇的移动");
gotoxy(64, 20);
printf("F1为加速,F2为减速");
gotoxy(64, 22);
printf("space:暂停游戏");
gotoxy(64, 24);
printf("ESC:退出游戏");
}
/********在文件中读取最高分********/
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);
printf("★");
tail = tail->next;
}
}
/*******随机出现食物********/
void createfood()
{
snake *food_1;
srand((unsigned)time(NULL));
food_1 = (snake *)malloc(sizeof(snake));
while (food_1->x % 2 != 0)
{
food_1->x =rand()% 52 + 2;
}
food_1->y = rand() % 24 + 1;
q = head;
while (q->next==NULL)
{
if (q->x == food_1->x && q->y == food_1->y)
{
free(food_1);
createfood();
}
q = q->next;
}
gotoxy(food_1->x, food_1->y);
food = food_1;
color(12);
printf("●");
}
/*********判断是否咬到了自己********/
int biteself()
{
snake *self;
self = head->next;
while (self!=NULL)
{
if (self->x == head->x&&self->y == head->y)
{
return 1;
}
self = self->next;
}
return 0;
}
/********判断蛇撞墙*******/
void cantcrosswall()
{
if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26)
{
endgamestatus = 1;
endgame();
}
}
/************设置加速********/
void speedup()
{
if (sleeptime >= 50)
{
sleeptime = sleeptime - 10;
add = add + 2;
if (sleeptime == 320)
{
add = 2;
}
}
}
/************设置减速********/
void speeddown()
{
if (sleeptime < 350)
{
sleeptime = sleeptime + 30;
add = add - 2;
if (sleeptime == 350)
{
add = 1;
}
}
}
/********不按键时蛇的前进方向*******/
void snakemove()
{
snake *nexthead;
cantcrosswall();
nexthead = (snake *)malloc(sizeof(snake));
if (status == U)
{
nexthead->x = head->x;
nexthead->y = head->y-1;
nexthead->next = head;
head = nexthead;
q = head;
if (nexthead->x == food->x&&nexthead->y == food->y)
{
while (q != NULL)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q=q->next;
}
score = score + add;
speedup();
createfood();
}
else
{
while (q->next->next != NULL)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
gotoxy(q->next->x, q->next->y);
color(3);
printf("■");
free(q->next);
q->next = NULL;
}
}
if (status == D)
{
nexthead->x = head->x;
nexthead->y = head->y + 1;
nexthead->next = head;
head = nexthead;
q = head;
if (nexthead->x == food->x&&nexthead->y == food->y)
{
while (q != NULL)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
score = score + add;
speedup();
createfood();
}
else
{
while (q->next->next != NULL)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
gotoxy(q->next->x, q->next->y);
color(3);
printf("■");
free(q->next);
q->next = NULL;
}
}
if (status == L)
{
nexthead->x = head->x -2;
nexthead->y = head->y;
nexthead->next = head;
head = nexthead;
q = head;
if (nexthead->x == food->x&&nexthead->y == food->y)
{
while (q != NULL)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
score = score + add;
speedup();
createfood();
}
else
{
while (q->next->next != NULL)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
gotoxy(q->next->x, q->next->y);
color(3);
printf("■");
free(q->next);
q->next = NULL;
}
}
if (status == R)
{
nexthead->x = head->x + 2;
nexthead->y = head->y;
nexthead->next = head;
head = nexthead;
q = head;
if (nexthead->x == food->x&&nexthead->y == food->y)
{
while (q != NULL)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
score = score + add;
speedup();
createfood();
}
else
{
while (q->next->next != NULL)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
gotoxy(q->next->x, q->next->y);
color(3);
printf("■");
free(q->next);
q->next = NULL;
}
}
if (biteself() == 1)
{
endgamestatus = 2;
endgame();
}
}
/***********按键控制*********/
void keyboardControl()
{
status = R;
while (1)
{
scoreandtips();
if (GetAsyncKeyState(VK_UP) && status != D)
{
status = U;
}
else if(GetAsyncKeyState(VK_DOWN) && status != U)
{
status = D;
}
else if (GetAsyncKeyState(VK_LEFT) && status != R)
{
status = L;
}
else if (GetAsyncKeyState(VK_RIGHT) && status != L)
{
status = R;
}
if (GetAsyncKeyState(VK_SPACE))
{
while (1)
{
Sleep(300);
if (GetAsyncKeyState(VK_SPACE))
{
break;
}
}
}
else if (GetAsyncKeyState(VK_ESCAPE))
{
endgamestatus = 3;
break;
}
else if (GetAsyncKeyState(VK_F1))
{
speedup();
}
else if (GetAsyncKeyState(VK_F2))
{
if (sleeptime < 350)
{
sleeptime = sleeptime + 30;
add = add - 2;
if (sleeptime == 350)
{
add = 1;
}
}
}
Sleep(sleeptime);
snakemove();
}
}
/***********失败界面******/
void Lostdraw()
{
int i, j;
system("cls");
gotoxy(17, 5);
color(11);
printf("******************************************************************"); //和上面,商编宽
for (i = 6; i <= 19; i++)
{
gotoxy(17, i); //竖边框
printf("|");
gotoxy(82, i);
printf("|");
}
gotoxy(17, 20); //下边框
printf("******************************************************************");
}
/***********结束游戏***********/
void endgame()
{
system("cls");
if (endgamestatus == 1)
{
Lostdraw();
gotoxy(40, 8);
color(12);
printf("真垃圾,墙你都撞");
}
else if (endgamestatus == 2)
{
Lostdraw();
gotoxy(40, 8);
color(12);
printf("真垃圾,自个都咬");
}
else if (endgamestatus == 3)
{
Lostdraw();
gotoxy(40, 8);
color(12);
printf("放弃吧少年,没救了");
}
gotoxy(40, 12);
color(13);
printf("你的得分是%d", score);
if (score >= HighScore)
{
color(10);
gotoxy(40, 16);
printf("真牛逼,破纪录啦");
File_in();
}
else
{
color(10);
gotoxy(40, 16);
printf("真垃圾,记录都破不了");
}
choose();
}
/************写入最高分**********/
void File_in()
{
FILE *fp;
fp = fopen("save.txt", "w+");
fprintf(fp, "%d", score);
fclose(fp);
}
void choose()
{
int n;
gotoxy(30, 23);
color(12);
printf("1.我还玩");
gotoxy(57, 23);
printf("2.不玩了");
color(11);
gotoxy(43, 26);
printf("选择:");
scanf("%d", &n);
switch (n)
{
case 1:
system("cls");
score = 0;
sleeptime = 200;
add = 10;
welcometogame();
break;
case 2:
exit(0);
break;
default:
gotoxy(35, 27);
color(12);
printf("输错了,重新输");
system("pause>nul");
endgame();
choose();
break;
}
}