C语言修改了的飞机小游戏
前段时间,和室友看了一些C语言的小游戏,打算自己也玩一个,在网上找了一个模板。然后自己修改了一些我们自己觉得需要修改的漏洞,和一些不太完善的地方。
游戏画面
然后是代码
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
#include<conio.h>
#define MAX 100
long long int speed=0;
int position_x,position_y;
int high,width;
int bullet_x,bullet_y;
int enemy_x,enemy_y;
int map[MAX][MAX];
int score,n;
void starup()
{
high=20;
width=30;
position_x=high/2;
position_y=width/2;
bullet_x=0;
bullet_y=position_y;
enemy_x=2;
enemy_y=position_y-1;
score=0;
}
void startMap()
{
int i,j;
for(i=1;i<=high-1;i++)
{
map[i][1]=4;
for(j=2;j<=width-1;j++)
map[i][j]=0;
map[i][width]=4;
}
i=high;
for(j=1;j<=width;j++)
map[i][j]=3;
map[bullet_x][bullet_y]=5;
map[position_x-1][position_y]=1;
i=position_x;
for(j=position_y-2;j<=position_y+2;j++)
map[i][j]=1;
map[position_x+1][position_y-1]=1;
map[position_x+1][position_y+1]=1;
map[enemy_x][enemy_y]=2;
map[enemy_x-1][enemy_y-1]=2;
map[enemy_x-1][enemy_y+1]=2;
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void gotoxy(int x,int y)
{
HANDLE handle =GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(handle,pos);
}
void updateWithoutInput()
{
if(bullet_x>0)
bullet_x--;
if((bullet_x==enemy_x)&&(bullet_y==enemy_y))
{
score++;
enemy_x=0;
enemy_y=rand()%width;
bullet_x=0;
}
if((bullet_x==enemy_x-1)&&(bullet_y==enemy_y-1))
{
score++;
enemy_x=0;
enemy_y=rand()%width;
bullet_x=0;
}
if((bullet_x==enemy_x-1)&&(bullet_y==enemy_y+1))
{
score++;
enemy_x=0;
enemy_y=rand()%width;
bullet_x=0;
}
if((bullet_x==enemy_x+1)&&(bullet_y==enemy_y))
{
score++;
enemy_x=0;
enemy_y=rand()%width;
bullet_x=0;
}
if((bullet_x==enemy_x)&&(bullet_y==enemy_y-1))
{
score++;
enemy_x=0;
enemy_y=rand()%width;
bullet_x=0;
}
if((bullet_x==enemy_x)&&(bullet_y==enemy_y+1))
{
score++;
enemy_x=0;
enemy_y=rand()%width;
bullet_x=0;
}
if(enemy_x>high)
{
enemy_x=0;
enemy_y=rand()%width;
}
if(speed==1)
for(int i=1;i<=10000;i++)
{
for(int j=1;j<=10000;j++)
{
speed=1;//Sleep(500);
}
}
speed=0;
if(speed==0)
{
enemy_x++;
speed=1;
}
}
void updateWithInput()
{
char input;
if(kbhit())
{
input=getch();
if(input=='a')
position_y--;
if(input=='s')
position_x++;
if(input=='d')
position_y++;
if(input=='w')
position_x--;
if(input==' ')
{
bullet_x=position_x-1;
bullet_y=position_y;
}
}
}
void show()
{
gotoxy(0,0);
int i,j;
for(i=1;i<=high;i++)
{
for(j=1;j<=width;j++)
{
if(map[i][j]==0)
printf(" ");
if(map[i][j]==1)
printf("*");
if(map[i][j]==2)
printf("#");
if(map[i][j]==3)
printf("~");
if(map[i][j]==4)
printf("|");
if(map[i][j]==5)
printf("|");
}
printf("\n");
}
printf("\n你的得分:%d",score);
printf("\n死亡:%d\n\n",n);
printf("操作说明:ASDW分别操作 左下右上四个移动\n");
printf(" 空格是发射子弹");
printf(" 死亡5次游戏结束");
}
void death()
{
if((position_x==enemy_x)&&(position_y==enemy_y))//撞敌人死亡一次
{
n++;
position_x=16;
position_y=rand()%width; //随机生成自己
bullet_x=0;
}
if((position_x==enemy_x-1)&&(position_y==enemy_y-1))//撞敌人死亡一次
{
n++;
position_x=16;
position_y=rand()%width;
bullet_x=0;
}
if((position_x==enemy_x-1)&&(position_y==enemy_y+1))//撞敌人死亡一次
{
n++;
position_x=16;
position_y=rand()%width;
bullet_x=0;
}
if((position_x>=20)||(position_x<=0))//超出上下界面死亡一次
{
n++;
position_x=16;
position_y=rand()%width;
bullet_x=0;
}
if((position_y>=30)||(position_y<=0))//超出左右界面死亡一次
{
n++;
position_x=16;
position_y=rand()%width;
bullet_x=0;
}
if(n>=5)
{
system("CLS");
printf("\n\n\n\n游戏结束\n");
Sleep(1500);//睡眠函数
exit(-1);//结束执行框
}
}
void main()
{
starup();
while(1)
{
HideCursor();
startMap();
show();
updateWithoutInput();
updateWithInput();
death();
}
}
代码的主要内容是边缘和自身与敌机的形成,和光标的隐藏,以及一些飞机死亡的重生以及游戏结束。