前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一个好玩的小游戏(纯C语言编写)

一个好玩的小游戏(纯C语言编写)

作者头像
全栈程序员站长
发布2022-08-31 15:23:55
6470
发布2022-08-31 15:23:55
举报

大家好,又见面了,我是你们的朋友全栈君。

最近在看知乎是发现了一个这一个专栏 https://zhuanlan.zhihu.com/c2game 从中获取的许多知识,本文中的游戏也是从里面学到的,不过本人又自己加了一些功能。 这是一个类似于飞机大战的游戏,不过目前代码量比较小,所以看起来非常简陋游戏界面如下 更新日志,本人将原来的原来的代码有进一步的优化了一下,之前是只有一个非常小的战机现在更新后可以产生一个非常大的战机(看起来也更有气势了~~)和敌人的战机,不过死亡的判定条件和边境的判断条件还没有做好,等下次再继续加油。2017.3.12更新 就是这样一个简陋的游戏(实在惭愧,本人目前能力有限) 如下图:

这里写图片描述
这里写图片描述

完整的代码如下:

代码语言:javascript
复制
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>
#include<stdlib.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];
/*0表示空白,1表示战机*的区域,2表示敌人战机的位置。 3表示上下围墙,4表示左右围墙,5表示子弹的位置*/
int score;
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 (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 <= 1000; j++)
            {
                speed = 1;
            }
        }
    speed = 0;
    if (speed == 0)
    {
        enemy_x++;
        speed = 1;
    }
}
void updateWithInput()//与输入有关的更新 
{
    char input;
    if (kbhit())//在VC6.0++下,为_kbhit()
    {
        input = getch();//在VC6.0++下为_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\n\n", score);
    printf("操作说明: ASDW分别操作 左下右上四个的移动\n");
    printf("**空格是发出子弹**\n");
}
int main()
{
    starup();
    while (1)
    {
        HideCursor();
        startMap();
        show();
        updateWithoutInput();
        updateWithInput();
    }
    return 0;
}

注意107行和109行的kbhit()和getch() 如果你看不明白,我建议你先去上面的那个连接中看看,他会教你如何一步步的进行最后做成一个完整的游戏。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/144281.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档