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

2023-02-05 0 372

每晚两个C词汇小工程项目,提高你的程式设计潜能!

用VS写了两个小小格斗游戏,在介面左侧有运转时间,收到的陀螺特征值等重要信息,有 10 个陀螺行踪,玩者能掌控两个箱子以内终端(摇杆),飞来陀螺,按 Esc 键选择退出,最终会表明收到的陀螺的数量/平均分。

格斗游戏是那么单纯,但是也很挑战他们的化学反应潜能的哦!

格斗游戏运转截屏如下表所示:

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

单纯介绍格斗游戏后他们就来试一试吧!

本工程项目校对自然环境:Visual Studio 2019/2022,EasyX应用程序

标识符展现:

#include <graphics.h> #include <conio.h> #include <time.h> #include <stdio.h> // 表述自变量 #defineNUM 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[i].x = 16 + 45 * i; ball[i].y = 8+ rand() %32; ball[i].v = 1 + rand() % 5; } // 画球,并计算得分 void calculateScore(Ball ball[], int& score) { for (int i = 0; i < NUM; i++) { fillcircle(ball[i].x, ball[i].y, 8); if (ball[i].y >= 472) { ballRandom(ball, i);continue; } if (box_x + 8 <= ball[i].x && ball[i].x <= box_x + 72 && ball[i].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[i].y += ball[i].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++感兴趣能关注小编在后台私信我:【程式设计交流】一起来学习哦!能领取一些C/C++的工程项目学

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务