C语言贪吃蛇小游戏
视频正在随缘策划中。。。。
废话在后面直接上程序
该程序在VS2019上可完美运行。
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<time.h>
#include<conio.h>
constexpr auto maphigh = 28, mapwide = 84;
struct virus
{
int x;
int y;
}virus;
unsigned short snakesize = 50, speed = 300, len = 4;
struct snake
{
int x[50];
int y[50];
int len;//蛇的长度
int speed;//蛇的速度
}snake;
int key = 'w';//初始化方向
void gotoxy(int x, int y);
void drawmap();
void keydown();
void creatvirus();
int snakestatus();
void startgame();
int menuselect();
void goodbye();
void introduce();
void edition();
int setup();
void respect();
int setup1();
int modifydiffculty();
void diffculty();
int customize();
void customize1();
int main()
{
for (;;)
{
system("cls");//清屏
switch (menuselect())
{
case 1://开始游戏
startgame();
break;
case 2://介绍
introduce();
break;
case 3://版本
edition();
break;
case 4://设置
setup1();
break;
case 5:
goodbye();
return 0;
default:
break;
}
}
}
void drawmap()//1.画地图
{
//⊙:病毒 █:蛇身(占用两个字符)
srand((unsigned int)time(NULL));//随机病毒出现的位置
int i, k;
for (i = 0; i <= maphigh; i++)
{
gotoxy(0, i);
printf("█");//打印左边框
gotoxy(mapwide, i);
printf("█");//打印右边框
}
for (i = 0; i <= mapwide; i += 2)//+=2因为 █占两个字节
{
gotoxy(i, 0);
printf("█");//打印下边框
gotoxy(i, maphigh);
printf("█");//打印上边框
}
//画蛇
snake.len = len;
snake.speed = speed;
//初始化蛇的位置
snake.x[0] = mapwide / 2;
snake.y[0] = maphigh / 2;//[0]为蛇头的位置
//画蛇头
gotoxy(snake.x[0], snake.y[0]);
printf("█");
//画蛇身
for (k = 1; k < snake.len; k++)
{
snake.x[k] = snake.x[k - 1] + 2;
snake.y[k] = snake.y[k - 1];
gotoxy(snake.x[k], snake.y[k]);
printf("█");
}
while (1)
{
virus.x = rand() % (mapwide - 4) + 2;//+2,+1这个与█所占的字符有关,长占两个字符宽1个
virus.y = rand() % (maphigh - 2) + 1;//画个图,显而易见
if (virus.x % 2 == 0)
break;
}
gotoxy(virus.x, virus.y);
printf("⊙");
gotoxy(mapwide + 4, maphigh);
printf("⊙:%d", snakesize - snake.len);
}
void creatvirus()//2.产生病毒
{
if (snake.x