IE盒子

搜索
查看: 101|回复: 0

C语言项目:接球小游戏(自制)!详细思路+源码分享

[复制链接]

4

主题

8

帖子

15

积分

新手上路

Rank: 1

积分
15
发表于 2023-4-9 20:25:50 | 显示全部楼层 |阅读模式
每天一个C语言小项目,提升你的编程能力!
用VS写了一个小小的游戏,在界面右侧有运行时间,接到的小球个数等信息,有 10 个小球下落,玩家可以控制一个盒子左右移动(方向键),来接小球,按 Esc 键退出,最后会显示接到的小球的数目/分数。
游戏就是这么简单,不过也很考验大家的反应能力的哦!
游戏运行截图如下:


简单了解游戏后我们就来试试吧!
本项目编译环境:Visual Studio 2019/2022,EasyX插件
代码展示:
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>

// 定义常量
#define NUM 10
#define        CMD_LEFT                1
#define        CMD_RIGHT                2
#define        CMD_QUIT                4

int box_x = 10;
int box_y = 420;

// 定义球的结构体
struct Ball
{
        int x, y, v;
};

// 获取用户控制
int GetCommand()
{
        int c = 0;
        if (GetAsyncKeyState(VK_LEFT) & 0x8000)                c |= CMD_LEFT;
        if (GetAsyncKeyState(VK_RIGHT) & 0x8000)        c |= CMD_RIGHT;
        if (GetAsyncKeyState(VK_ESCAPE) & 0x8000)        c |= CMD_QUIT;

        return c;
}

// 倒计时
int Time(int t)
{
        char strsec[10];
        int sec = 20 - (GetTickCount() - t) / 1000;
        itoa(sec, strsec, 10);
        outtextxy(570, 110, "      ");
        outtextxy(570, 110, strcat(strsec, "s"));
        return sec;
}

// 介绍
void menu()
{
        line(449, 0, 449, 480);
        char runTime[] = "游戏倒计时     : ",
                receiveBallNum[] = "接到的球的数量:", copyRight[] = "版权所有:C语言编程",
                finishWorkDate[] = "完成日期:2023年1月7日",
                introductiona[] = "按方向键控制盒子移动接住", introductionb[] = "小球,倒计时为0时游戏结束";

        settextcolor(GREEN);
        outtextxy(450, 10, introductiona);
        outtextxy(450, 30, introductionb);
        outtextxy(450, 110, runTime);
        outtextxy(450, 210, receiveBallNum);
        outtextxy(450, 310, copyRight);
        outtextxy(450, 410, finishWorkDate);
}

// 产生随机球
void ballRandom(Ball ball[], int i)
{
        ball.x = 16 + 45 * i;
        ball.y = 8 + rand() % 32;
        ball.v = 1 + rand() % 5;
}

// 画球,并计算得分
void calculateScore(Ball ball[], int& score)
{
        for (int i = 0; i < NUM; i++)
        {
                fillcircle(ball.x, ball.y, 8);
                if (ball.y >= 472)
                {
                        ballRandom(ball, i);
                        continue;
                }
                if (box_x + 8 <= ball.x && ball.x <= box_x + 72 && ball.y >= 412)
                {
                        score++;
                        ballRandom(ball, i);
                }
        }
}

// 主函数
int main()
{
        // 初始化
        initgraph(640, 480);
        srand(time(NULL));
        BeginBatchDraw();
        setlinecolor(GREEN);
        setfillcolor(WHITE);

        menu();

        Ball ball[NUM];
        int dx, i, c, score = 0;
        bool flag = true;

        for (i = 0; i < NUM; i++)
        {
                ballRandom(ball, i);
        }

        int t = GetTickCount();
        char strScore[10], str[] = "your score:";

        // 游戏主循环
        while (flag)
        {
                dx = 0;

                // 显示得分
                char strScore[10];
                itoa(score, strScore, 10);
                outtextxy(570, 210, strScore);

                // 画球,并计算得分
                calculateScore(ball, score);

                // 画盒子
                fillrectangle(box_x, box_y, box_x + 80, box_y + 60);
                FlushBatchDraw();

                // 获取用户控制命令
                c = GetCommand();
                if (c & CMD_LEFT)        dx = -10;
                if (c & CMD_RIGHT)        dx = 10;
                if (c & CMD_QUIT)        flag = false;
                if (!Time(t)) flag = false;

                // 延时
                Sleep(25);

                // 擦除游戏区
                clearrectangle(0, 0, 448, 480);

                // 计算球的新坐标
                for (i = 0; i < NUM; i++)
                {
                        ball.y += ball.v;
                }

                // 移动盒子
                box_x += dx;
                if (box_x < 0)   box_x = 0;
                if (box_x > 368) box_x = 368;
        }

        // 清空键盘缓冲区
        FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));

        // 输出游戏结果
        itoa(score, strScore, 10);
        outtextxy(222, 240, strcat(str, strScore));
        outtextxy(220, 300, "按任意键退出");
        EndBatchDraw();

        // 按任意键退出
        getch();
        closegraph();

        return 0;
}
大家赶紧去动手试试吧!
此外,我也给大家分享我收集的其他资源,从最零基础开始的教程到C语言C++项目案例,帮助大家在学习C语言的道路上披荆斩棘!
C语言C++编程学习视频分享:




整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)最重要的是你可以在群里面交流提问编程问题哦!
欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!
【C语言/C++/数据结构与算法编程学习】
回复

使用道具 举报

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

本版积分规则

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