C语言贪吃蛇打怪版本
废话不多说,直接上代码
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#define high 25
#define width 80
#define bullet_num 5
#define up 1
#define down 2
#define left 3
#define right 4
int pic[high][width]= {
0}; //画面大小,0:空格,-3:子弹,-7,-8,-2:食物?,!,$,-1:边框#,100:自己蛇头@,>100为自己蛇身*,0<为敌人的蛇<100,镭射激光为-9,-10
int direction;//上,下,左,右为1,2,3,4
int food_x,food_y;//加一分
int food_x1,food_y1;//10%死亡,30%加5分,60%加2分
int food_x2,food_y2;
int laser;//激光
int laser_s;//是否吃到!
int score;//分数
int i,j;//用于循环
int sign;//判断自己蛇的子弹是否射出
int death;//死亡参数
int enemydeath;//杀敌数
typedef struct//子弹参数
{
int x,y;//坐标
int direction;//方向
int exist;//是否存在
}Bullet;
Bullet bullet[bullet_num],bulletenemy[bullet_num];//定义自己子弹与AI蛇子弹
typedef struct//蛇
{
int x,y;//蛇头
int direction;//方向
int CD;//子弹CD
int snakespeed ;//蛇的速度
int oldtail_x,oldtail_y;//蛇尾
int color;//颜色
}Snake;
Snake my;//定义自己的蛇
typedef struct//蛇
{
int x,y;//蛇头
int ax[3],ay[3];//蛇身
int direction;//方向
int sign;//判断AI蛇的型号
}Snake1;
Snake1 enemy[2];//AI蛇
void explation();
void startup();//初始化
int color(int a);
void gotoxy(int x,int y);//光标移动到(x,y)位置
void HideCursor();//隐藏光标
void printsnake();
void show();//显示画面
void newsnake(Snake1 *enemy);//AI蛇的重置
void bulletmove();//自己蛇子弹的移动
void enemy_snakemove(Snake1 *enemy);//敌人蛇自动移动
void snakemove();//自己蛇自动移动
void bulletenemymove(Snake1 *enemy);//AI蛇子弹的移动
void updateWithInput();//与用户有关的更新
void updateWithoutInput();//与用户无关的更新
void welcometogame();
void reward();
void gold();
void less();
void random();
void choose();
void Lostdraw();
void endgame();//结束的东西
void startup()//初始化
{
system("cls");
enemydeath=0;
laser_s=0;//是否射击
laser=0;//激光不可发出
sign=1;//子弹可以射出
my.CD=8;//技能的CD
for(i=0;i<bullet_num;i++)
{
bullet[i].exist=0;//自己蛇子弹的存在为0
}
enemy[0].sign=0;//0号AI蛇的特殊标志
enemy[1].sign=1;//1号AI蛇的特殊标志
enemy[1].x=high/5*4;
enemy[1].y=width/4*3;
enemy[0].x=high/5;
enemy[0].y=width/4;//0,1号AI蛇的初始坐标
my.snakespeed=2;//自己蛇的速度
my.x=high/2;
my.y=width/2;//自己蛇的坐标
score=0;
for(i=0; i<high; i++)
{
pic[i][0]=-1;
pic[i][width-1]=-1;
}
for(j=0; j<width; j++)
{
pic[0][j]=-1;
pic[high-1][j]=-1;
}//定义边框
pic[my.x][my.y]=100;
pic[enemy[0].x][enemy[0].y]=1;
pic[enemy[1].x][enemy[1].y]=11;//蛇头赋值
for(j=1; j<4; j++)
{
pic[my.x][my.y-j]=j+100;
enemy[0].ax[j-1]=enemy[0].x;
enemy[0].ay[j-1]=enemy[0].y-j;
enemy[1].ax[j-1]=enemy[1].x;
enemy[1].ay[j-1]=enemy[1].y+j;
pic[enemy[0].ax[j-1]][enemy[0].ay[j-1]]=j+1;
pic[enemy[1].ax[j-1]][enemy[1].ay[j-1]]=j+11;
}//蛇身的二位数组的值
my.direction=4;
enemy[0].direction=4;
enemy[1].direction=3;//方向
srand((int)time(NULL));
food_x=rand()%(high-5)+4;
food_y=rand()%(width-5)+4;//随机生成食物
pic[food_x][food_y]=-2;
}
int color(int a)//颜色函数
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, a); //更改文字颜色
return 0;
}
void gotoxy(int x,int y)//光标移动到(x,y)位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(handle,pos);
}
void HideCursor()//隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {
1,0}; //第二个0表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void printsnake()//首页字体
{
printf("\n");
printf("\n");
printf("\n");
color(3);
printf(" ◆◆ ◆◆ ◆ ◆ \n");
printf(" ◆◆◆◆◆◆◆◆◆ ◆◆◆◆◆◆◆◆◆◆\n");
printf(" ◆ ◆◆ ◆ ◆ \n");
printf(" ◆◆◆ ◆◆ ◆ ◆ \n");
printf(" ◆◆◆◆◆◆◆◆◆ ◆◆◆◆◆◆◆◆◆◆◆\n");
printf(" ◆ ◆◆ ◆ ◆◆◆◆◆◆ ◆ ◆ ◆ \n");
printf(" ◆ ◆◆ ◆ ◆◆◆◆◆◆◆◆◆◆ ◆ ◆ ◆◆ \n");
printf(" ◆◆◆◆◆ ◆ ◆◆◆ ◆◆ ◆◆◆ ◆ ◆ ◆◆ \n");
printf(" ◆◆◆◆◆◆◆◆◆◆◆ ◆◆◆ ◆◆◆ ◆◆ ◆◆◆◆◆◆◆◆◆ \n");
printf(" ◆◆ ◆◆◆◆◆◆◆ ◆◆ ◆◆◆ ◆◆ \n");
printf(" ◆◆◆◆◆◆ ◆◆\n");
printf(" ◆◆◆◆ ◆◆◆\n");
printf(" ◆◆◆ ◆◆◆ \n");
printf(" ◆◆◆◆\n");
printf(" ◆◆◆◆\n\n");
color(4);
printf(" 贪 吃 蛇");
}
void reward()//抽奖函数
{
int i,j = 1;
int n,m;
system("cls");
color(15);
gotoxy(44,3);
printf("超级大乐透");
color(2);
for (i = 6; i <= 22; i++) //输出上下边框===
{
for (j = 20; j <= 76; j++) //输出左右边框||
{
gotoxy(j, i);
if (i == 6 || i == 20) printf("=");
else if (j == 20 || j == 75) printf("||");
}
}
color(11);
gotoxy(30,14);
printf("非酋大转盘[写下你的幸运数字]: \b\b");
color(14);
scanf("%d", &n);
srand((int)time(NULL));
m=rand()%100+1;
if(m>=1&&m<=5)
{
system("cls");
gold();
startup();
}
if(m>20)
{
system("cls");
random();
startup();
}
if(m<=20&&m>5)
{
system("cls");
less();
startup();
}
}
void gold()//金色传说
{
system("cls");
my.color=6;
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf(" ◆◆◆◆◆ ◆◆◆◆◆◆ ◆◆ ◆◆ ◆◆ ◆◆ ◆◆\n");
printf(" ◆◆◆◆ ◆◆◆◆ ◆◆◆ ◆◆ ◆◆◆◆◆◆◆◆◆ ◆◆◆ ◆ ◆◆ \n");
printf(" ◆◆◆◆◆◆◆◆◆◆◆ ◆◆◆◆◆◆◆◆◆ ◆◆ ◆ ◆◆◆◆◆◆\n");
printf(" ◆ ◆◆◆ ◆ ◆ ◆◆◆◆◆◆◆◆◆◆◆ ◆◆◆ ◆ ◆\n");
printf(" ◆◆◆◆◆◆◆◆◆ ◆ ◆ ◆ ◆◆ ◆◆ ◆ ◆ ◆◆\n");
printf(" ◆◆ ◆ ◆ ◆ ◆ ◆ ◆◆◆ ◆◆◆◆◆◆ ◆ ◆◆◆◆◆◆\n");
printf(" ◆◆ ◆ ◆◆ ◆◆◆◆◆◆◆◆ ◆ ◆◆ ◆ ◆ ◆ \n");
printf(" ◆◆ ◆ ◆◆ ◆ ◆ ◆ ◆◆◆◆◆ ◆◆◆◆ ◆ ◆\n");
printf(" ◆◆◆◆◆◆◆◆◆◆ ◆◆ ◆◆ ◆ ◆◆◆ ◆◆◆◆ ◆◆◆\n");
printf(" ◆◆◆◆◆◆◆◆◆ ◆ ◆◆ ◆◆ ◆◆◆\n");
Sleep(1000);
}
void random()//普通
{
system("cls");
my.color=15;
gotoxy(45,10);
printf("普通\n");
Sleep(1000);
}
void less()//稀有
{
system("cls");
my.color=5;
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf(" ◆◆ ◆ ◆ ◆ \n");
printf(" ◆◆◆◆◆◆◆◆◆◆◆ ◆◆ \n");
printf(" ◆ ◆◆◆◆ ◆◆◆◆◆◆◆◆◆◆◆◆◆◆\n");
printf(" ◆ ◆◆◆◆◆◆◆ ◆◆◆ \n");
printf(" ◆◆◆◆◆◆ ◆◆ ◆ ◆◆◆◆◆◆◆◆◆◆\n");
printf(" ◆◆◆◆◆◆◆◆◆◆◆◆ ◆◆◆◆ ◆\n");
printf(" ◆◆◆◆ ◆◆◆ ◆◆ ◆◆◆◆◆◆◆◆◆\n");
printf(" ◆◆◆◆◆◆◆◆◆◆◆ ◆ ◆\n");
printf(" ◆◆◆ ◆◆◆ ◆ ◆ ◆ ◆\n");
printf(" ◆ ◆ ◆ ◆ ◆ ◆ ◆◆◆◆◆◆◆◆◆\n");
printf(" ◆ ◆ ◆ ◆ ◆ ◆\n");
printf(" ◆ ◆ ◆ ◆ ◆ ◆\n");
printf(" ◆ ◆ ◆ ◆◆ ◆ ◆◆◆◆\n");
printf(" ◆\n");
Sleep