这篇文章主要为大家详细介绍了C语言实现——《打砖块项目》,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下!
游戏介绍:
在游戏中,玩家通过按住并滑动挡板下的圆点控制挡板左右移动,接住击打砖块而改变飞行轨迹掉落下来的小球。在游戏界面的左侧有个速度控制器,玩家可一边接球,一边控制它。上下滑动调整小球的飞行速度。速度越快风险越大,当然奖励和风险是成正比的。越快的速度得分会越多,反之速度越慢得分会越少。(本项目并没有设计速度调整这一块,大家可以自己完善一下)
项目技术:
主要是数组、结构体、绘图技术、按键操作和定时器等,对逻辑也是有一定的要求,但是这些在我们项目源码里面都会有注释,大家到时候学习的时候千万不要忽略注释,注释可以更好地帮你理解代码,尤其是C语言初学者。
本项目编译环境:VS2019/VS2013;
插件:图形库插件easyX,涉及图片素材可以自行百度找也可以关注文末领取;
源代码示例:
//画砖块int map[5][8]; //描述整个地图HWND hwnd = NULL;//用1-3 给数组赋值void initMap(){for (int i = 0; i < 5; i++){for (int j = 0; j < 8; j++){map[i][j] = rand() % 3 + 1;}}}void drawMap(){setlinestyle(PS_SOLID, 2);setlinecolor(WHITE);for (int i = 0; i < 5; i++){for (int j = 0; j < 8; j++){int x = 100*j ; //j=x/100int y = 25*i ; //i=y/iswitch (map[i][j]) //map[i][j]!=0{case 0: //做消除用的break;case 1:setfillcolor(YELLOW);fillrectangle(x, y, x + 100, y + 25);break;case 2:setfillcolor(LIGHTBLUE);fillrectangle(x, y, x + 100, y + 25);break;case 3:setfillcolor(LIGHTGREEN);fillrectangle(x, y, x + 100, y + 25);break;}}}}//木板的过程struct Board{int x;int y;int speed;COLORREF color;int width;int height;};//struct Board board = { 300, 800 - 25,1, WHITE, 200, 25 };struct Board* createBoard(int x, int y, int speed, COLORREF color, int width, int height){struct Board* pBoard = (struct Board*)malloc(sizeof(struct Board));//结构体指针->成员 ->指针指向运算符//(*指针).成员;pBoard->x = x;pBoard->y = y;pBoard->speed = speed;pBoard->color = color;//结构体变量.成员(*pBoard).width = width;(*pBoard).height = height;return pBoard;}void drawBoard(struct Board* pBoard){setfillcolor(pBoard->color);fillrectangle(pBoard->x, pBoard->y,pBoard->x + pBoard->width, pBoard->y + pBoard->height);}//木板的按键操作void keyDown(struct Board* pBoard){//C语言: scanf函数 getch() getchar() gets()//异步的按键操作if (GetAsyncKeyState('A') || GetAsyncKeyState(VK_LEFT)&&pBoard->x>=0){pBoard->x -= pBoard->speed;}if (GetAsyncKeyState('D') || GetAsyncKeyState(VK_RIGHT)&&pBoard->x<=800-200){pBoard->x += pBoard->speed;}}//球:struct Ball{int x;int y;int r; //半径int dx;int dy;COLORREF color;};struct Ball* createBall(int x, int y, int r, int dx, int dy, COLORREF color){struct Ball* pBall = (struct Ball*)malloc(sizeof(struct Ball));pBall->x = x;pBall->y = y;pBall->r = r;pBall->dx = dx;pBall->dy = dy;pBall->color = color;return pBall;}void drawBall(struct Ball* pBall){setfillcolor(pBall->color);solidcircle(pBall->x, pBall->y, pBall->r);}//1.反射//2.撞击木板int hitBoard(struct Ball* pBall, struct Board* pBoard){if (pBall->y + pBall->r == pBoard->y) //y满足{if (pBall->x >= pBoard->x && pBall->x <= pBoard->x + pBoard->width){return 1;}}return 0;}int die(struct Ball* pBall){if (pBall->y > 800 - 25){return 1;}return 0;}//3.撞击砖块int hitBricks(struct Ball* pBall){//1.算出球的行的列是属于地图int ballJ = pBall->x / 100;int ballI = (pBall->y - pBall->r) / 25;//2.当前下标下,数组中不等于表示有砖块需要反射if (ballJ < 8 && ballI < 5 && map[ballI][ballJ] != 0){map[ballI][ballJ] = 0;return 1;}return 0;}void moveBall(struct Ball* pBall,struct Board* pBoard){if (pBall->x - pBall->r <= 0 || pBall->x + pBall->r >= 800){pBall->dx = -pBall->dx;}if (pBall->y - pBall->r <= 0 || hitBoard(pBall,pBoard)|| hitBricks(pBall)){pBall->dy = -pBall->dy;}pBall->x += pBall->dx;pBall->y += pBall->dy;}//4.收尾工作 :游戏结束//5.定时器int Timer(time_t num, int id){static time_t start[10];time_t end = clock();if (end - start[id]>num){start[id] = end;return 1;}return 0;}int gameOver(){for (int i = 0; i < 5; i++){for (int j = 0; j < 8; j++){if (map[i][j] != 0){return 0;}}}return 1;}int main(){srand((unsigned int)time(0)); //设置随机数的范围跟随时间改变而改变hwnd=initgraph(800, 800);struct Board* pBoard = createBoard(300, 800 - 25,5, WHITE, 200, 25);struct Ball* pBall = createBall(400, 600, 15, 5, -5, RED);initMap();BeginBatchDraw();while (1){cleardevice();drawMap();drawBoard(pBoard);drawBall(pBall);if(Timer(10,0))moveBall(pBall,pBoard);keyDown(pBoard);if (die(pBall)){MessageBox(hwnd, "you die", "gameOver", MB_OK);exit(0);}if (gameOver()){MessageBox(hwnd, "win game", "gameOver", MB_OK);exit(0);}FlushBatchDraw();}EndBatchDraw();closegraph();return 0;}
全部0条评论
快来发表一下你的评论吧 !