鱼C论坛

 找回密码
 立即注册
查看: 9107|回复: 220

[技术交流] c语言飞机大战简易编码

  [复制链接]
发表于 2019-8-14 16:48:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
/*小游戏飞机大战   2019/7/14 开始   2019/7/16 完成*/
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>

struct GAME
{
        int x;
        int y;
        char shape;
        struct GAME *next;
};

struct GAME *planehead;
struct GAME obstacles[25];
struct GAME bullet[888];
int score=10;
int numb=0;
int style=0;


void Pos();//光标
void map();//地图
void planestyle();//飞机型号
void plane();//飞机
void planemove();//飞机移动
void obstacle();//障碍物
void obstaclehide();//障碍物碰撞
void finish();//结束标志
void jiemian();//界面设计
void pause();//暂停
void bullets();//子弹
void hide();//碰撞
void init();//初始化
void victory();//胜利

void Pos(int x, int y)
{
        COORD pos;
        pos.X = x;
        pos.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void bullets()
{
        int i;
        for(i=0;i<100;i++)
        {
                bullet.x=0;
                bullet.y=0;
                bullet.shape='I';
        }
}


void hide()
{
        int i,j;
        for(i=0;i<25;i++)
        {
                for(j=0;j<numb;j++)
                {
                        if(obstacles.x==bullet[j].x&&obstacles.y==bullet[j].y)
                        {
                                obstacles.x=0;
                                bullet[j].shape=' ';
                                score=score+10;
                                if(score==1000)
                                {
                                        victory();
                                }
                        }
                        if((bullet[j].x==90||bullet[j].x==92)&&bullet[j].y==12)
                        {
                                victory();
                        }
                }
        }
}

void map()
{
        int a,b;
        for(a=0;a<57;a=a+2)
        {
                Pos(a,0);
                printf("■");
                Pos(a,28);
                printf("■");
        }
        for(b=0;b<28;b++)
        {
                Pos(0,b);
                printf("■");
                Pos(56,b);
            printf("■");
        }
}


void planestyle()
{
        Pos(28,8);
        printf("请选择飞机类型                     ");
        Pos(16,14);
        printf("■");
        Pos(15,15);
        printf("■");
        Pos(14,16);
        printf("■");
        Pos(17,15);
        printf("■");
        Pos(16,16);
        printf("■");
        Pos(18,16);
        printf("■");
        Pos(16,18);
        printf("1");


        Pos(33,14);
        printf("■");
        Pos(32,15);
        printf("■");
        Pos(34,15);
        printf("■");
        Pos(32,16);
        printf("■");
        Pos(34,16);
        printf("■");
        Pos(33,18);
        printf("2");


        Pos(50,15);
        printf("■");
        Pos(48,14);
        printf("■");
        Pos(52,14);
        printf("■");
        Pos(48,16);
        printf("■");
        Pos(52,16);
        printf("■");
        Pos(51,18);
        printf("3                ");

        system("pause");
        system("CLS");
        Pos(28,14);
        printf("请输入你选的型号:");
        scanf("%d",&style);
        system("CLS");
}



void plane()
{
        int i;
        if(style==1)
        {
                struct GAME *temp,*p;
                temp=(struct GAME *)malloc(sizeof(struct GAME));
                temp->next=NULL;
                for(i=0;i<5;i++)
                {
                        p=temp;
                        temp=(struct GAME *)malloc(sizeof(struct GAME));
                        temp->next=p;
                }
                planehead=temp;
                temp->x=28;
                temp->y=24;
                temp->next->x=27;
                temp->next->y=25;
                temp->next->next->x=29;
                temp->next->next->y=25;
                temp->next->next->next->x=26;
                temp->next->next->next->y=26;
                temp->next->next->next->next->x=28;
                temp->next->next->next->next->y=26;
                temp->next->next->next->next->next->x=30;
                temp->next->next->next->next->next->y=26;
                }
        else if(style==2)
        {
                struct GAME *temp,*p;
                temp=(struct GAME *)malloc(sizeof(struct GAME));
                temp->next=NULL;
                for(i=0;i<4;i++)
                {
                        p=temp;
                        temp=(struct GAME *)malloc(sizeof(struct GAME));
                        temp->next=p;
                }
                planehead=temp;
                temp->x=28;
                temp->y=24;
                temp->next->x=27;
                temp->next->y=25;
                temp->next->next->x=29;
                temp->next->next->y=25;
                temp->next->next->next->x=27;
                temp->next->next->next->y=26;
                temp->next->next->next->next->x=29;
                temp->next->next->next->next->y=26;
        }
        else if(style==3)
        {
                struct GAME *temp,*p;
                temp=(struct GAME *)malloc(sizeof(struct GAME));
                temp->next=NULL;
                for(i=0;i<4;i++)
                {
                        p=temp;
                        temp=(struct GAME *)malloc(sizeof(struct GAME));
                        temp->next=p;
                }
                planehead=temp;
                temp->x=28;
                temp->y=24;
                temp->next->x=26;
                temp->next->y=23;
                temp->next->next->x=30;
                temp->next->next->y=23;
                temp->next->next->next->x=26;
                temp->next->next->next->y=25;
                temp->next->next->next->next->x=30;
                temp->next->next->next->next->y=25;
        }
}

void planemove()
{
        char key;
        int i;
        int a;
        struct GAME *temp,*head;
        bullets();
        while(1)
        {
                head=temp=planehead;
                map();
            jiemian();
            if(kbhit())
                {
                    key=getch();
                        if(key=='w'||key=='W')
                        {
                                while(temp!=NULL)
                                {
                                        temp->x=temp->x;
                                        temp->y=temp->y-1;
                                        temp=temp->next;
                                }
                        }
                        else if(key=='s'||key=='S')
                        {
                                while(temp!=NULL)
                                {
                                        temp->x=temp->x;
                                        temp->y=temp->y+1;
                                        temp=temp->next;
                                }
                        }
                        else if(key=='a'||key=='A')
                        {
                                while(temp!=NULL)
                                {
                                        temp->x=temp->x-2;
                                        temp->y=temp->y;
                                        temp=temp->next;
                                }
                        }
                        else if(key=='d'||key=='D')
                        {
                                while(temp!=NULL)
                                {
                                        temp->x=temp->x+2;
                                        temp->y=temp->y;
                                        temp=temp->next;
                                }
                        }
                        else if(key==27)
                        {
                                finish();
                        }
                        else if(key==32)
                        {
                                pause();
                        }
                        else if(key=='j'||key=='J')
                        {
                                bullet[numb].x=planehead->x;
                                bullet[numb].y=planehead->y;
                                numb++;
                        }
                }
                while(head!=NULL)
                {
                      Pos(head->x,head->y);
                     printf("■");
                     head=head->next;
                }
                for(a=0;a<numb;a++)
                {
                        Pos(bullet[a].x,bullet[a].y);
                        printf("%c",bullet[a].shape);
                        bullet[a].y--;
                        if(bullet[a].y==0)
                        {
                                bullet[a].shape=' ';
                        }
                }
                for(i=0;i<25;i++)
                {
                        Pos(obstacles.x,obstacles.y);
                        printf("oo");
                        obstacles.y++;
                        if(obstacles.y>28)
                        {
                                obstacles.x=rand()%56;
                        obstacles.y=rand()%8;
                        if(obstacles.x%2!=0)
                                {
                                obstacles.x=obstacles.x-1;
                                }
                        }
                }
                obstaclehide();
                hide();
                Sleep(10);
                system("CLS");
        }
}



void obstacle()
{
        int i;
        srand(time(NULL));
        for(i=0;i<25;i++)
        {
                obstacles.x=rand()%56;
                obstacles.y=rand()%14;
                if(obstacles.x%2!=0)
                {
                        obstacles.x=obstacles.x-1;
                }
        }
}


void obstaclehide()
{
        struct GAME *temp;
        int i;
        temp=planehead;
        while(temp!=NULL)
        {
                for(i=0;i<25;i++)
                {
                        if(temp->x==obstacles.x&&temp->y==obstacles.y)
                        {
                                finish();
                        }
                }
                temp=temp->next;
        }       
}


void finish()
{
        free(planehead);
        system("CLS");
        Pos(28,14);
        printf("你失败了!              ");
        system("pause");
        exit(0);
}

void victory()
{
        free(planehead);
        system("CLS");
        Pos(28,14);
        printf("牛逼!                ");
        system("pause");
        exit(0);
}

void pause()
{
        int pausekey;
        while(1)
        {
                Pos(28,14);
                printf("暂停中!");
                if(kbhit())
                {
                        pausekey=getch();
                        if(pausekey==32)
                        {
                                break;
                        }
                }
        }
}

void jiemian()
{
        Pos(70,10);
        printf("小游戏飞机大战!");
        Pos(70,12);
        printf("得分:  %d",score);
        Pos(90,12);
        printf("BUG");
        Pos(70,16);
        printf("目标1000分!");
        Pos(70,18);
        printf("W,S,A,D,控制方向 空格暂停,esc退出");
        Pos(70,20);
        printf("j 发射子弹!");
}

void init()
{
        Pos(28,14);
        printf("飞机大战!               ");
        system("pause");
        system("CLS");
        Pos(28,14);
        printf("没人通关 除非BUG!          ");
        system("pause");
        system("CLS");
        Pos(28,14);
    printf("请把键盘改成大写键!          ");
        system("pause");
        system("CLS");
}




void main()
{
        init();
        planestyle();
        plane();
    planemove();
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-14 17:51:14 | 显示全部楼层
.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-15 14:24:36 | 显示全部楼层
,,,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-15 14:57:36 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-15 15:12:15 | 显示全部楼层
为啥我用devc++没法编译
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-15 15:59:26 | 显示全部楼层
看看学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-15 16:08:39 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-15 16:10:25 | 显示全部楼层
VC6.0++没法执行是怎麽回事,楼主用的是什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-15 16:35:31 | 显示全部楼层
133256 发表于 2019-8-15 16:10
VC6.0++没法执行是怎麽回事,楼主用的是什么

就是vc啊 ,是怎么提醒你  不能执行的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-15 17:22:58 | 显示全部楼层
随便看看用c语言是咋写的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-15 17:58:53 | 显示全部楼层
C:\Users\zwj\Desktop
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-15 18:07:41 | 显示全部楼层
学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-16 11:53:27 | 显示全部楼层
KANKAN
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-16 15:01:22 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-16 15:29:34 | 显示全部楼层
凑凑热闹哈哈哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-16 16:03:02 | 显示全部楼层
。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-16 17:16:25 From FishC Mobile | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-16 18:37:38 | 显示全部楼层
66
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-16 19:22:35 | 显示全部楼层
让我康康
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-17 12:07:52 From FishC Mobile | 显示全部楼层
想看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 07:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表