赞
踩
利用这个小游戏可以巩固我们前面所学习的知识,使用easyx图形库可以使我们之前的终端输出变得更加有趣,我这里安装的是easyx大暑版(即最新版),大家可以到官网上下载,小游戏源代码就分享在这里了,大家有兴趣的话可以玩玩我的小游戏
- #include <graphics.h>
- #include<stdio.h>
- #include<conio.h>
- struct Ball
- {
- int x;
- int y;
- int r;
- int dx;
- int dy;
- };
- struct Ball ball1 = { 400,400,10,20,-15 };
- struct Ball ball2 = { 300,200,20,6,-20 };
- struct Ball ball3 = { 70,600,30,9,-20 };
- struct Ball myball = { 800, 650, 15, 20, 20 };//初始化所用到的球体参数
- void KeyDown()//实现键盘控制myball移动的函数
- {
- switch (_getch())
- {
- case 'W':
- case 'w':
- myball.y -= myball.dy;
- break;
- case 'S':
- case 's':
- myball.y += myball.dy;
- break;
- case 'A':
- case 'a':
- myball.x -= myball.dx;
- break;
- case 'D':
- case 'd':
- myball.x += myball.dx;
- break;
- }
- }
- void drawball(struct Ball ball1)//对障碍小球的画图操作
- {
- setfillcolor(WHITE);
- solidcircle(ball1.x, ball1.y, ball1.r);
- }
- void drawball2(struct Ball ball2)//对myball小球的画图操作
- {
- setfillcolor(RED);
- solidcircle(ball2.x, ball2.y, ball2.r);
- }
- void moveball1()//根据障碍小球不同的运动轨迹来实现障碍小球运动的函数,这是实现小球1运动的函数
- {
- if (ball1.x - ball1.r <= 0 || ball1.x + ball1.r >= 900||ball1.x>=200&&ball1.x<=300)
- {
- ball1.dx = -ball1.dx;
- }
- if (ball1.y - ball1.r <= 0 || ball1.y + ball1.r >= 700||ball1.y>=100&&ball1.y<=200)
- {
- ball1.dy = -ball1.dy;
- }
- ball1.x += ball1.dx;
- ball1.y += ball1.dy;
- }
- void moveball2()//实现障碍小球2运动的函数
- {
- if (ball2.x - ball2.r <= 0 || ball2.x + ball2.r >= 900 || ball2.x >= 100 && ball2.x <= 200)
- {
- ball2.dx = -ball2.dx;
- }
- if (ball2.y - ball2.r <= 0 || ball2.y + ball2.r >= 700 || ball2.y >= 600 && ball2.y <= 700)
- {
- ball2.dy = -ball2.dy;
- }
- ball2.x += ball2.dx;
- ball2.y += ball2.dy;
- }
- void moveball3()//实现障碍小球3运动的函数
- {
- if (ball3.x - ball3.r <= 0 || ball3.x + ball3.r >= 900 || ball3.x >= 600 && ball3.x <= 800)
- {
- ball3.dx = -ball3.dx;
- }
- if (ball3.y - ball3.r <= 0 || ball3.y + ball3.r >= 700 || ball3.y >= 200 && ball3.y <= 300)
- {
- ball3.dy = -ball3.dy;
- }
- ball3.x += ball3.dx;
- ball3.y += ball3.dy;
- }
- int main()
- {
- initgraph(1200, 700);//开启游戏起始界面的窗口
- IMAGE img1;
- ExMessage msg;
- loadimage(&img1, "./begin.jpg", 1200, 700);//使用此路径源文件要和图片在图一级目录下
- putimage(0, 0, &img1);
- while (1) {
- while (peekmessage(&msg))
- {
- if ((msg.message == WM_LBUTTONDOWN) && (msg.x <= 700 && msg.x >= 500) && (msg.y <= 700 && msg.y >= 630))//判断是否有鼠标点击开始游戏处
- {
- closegraph();
- initgraph(900, 700);//如果点击了开始游戏,那么开启游戏闯关界面
- while (1)//循环清屏和画图操作实现小球的动态移动效果
- {
- cleardevice();
- drawball(ball1);
- drawball(ball2);
- drawball(ball3);
- drawball2(myball);
- moveball1();
- moveball2();
- moveball3();
- if (_kbhit())//myball小球的控制
- {
- cleardevice();
- drawball(ball1);
- drawball(ball2);
- drawball(ball3);
- drawball2(myball);
- moveball1();
- moveball2();
- moveball3();
- KeyDown();
- }
- if (
- ((myball.x - ball1.x) * (myball.x - ball1.x) + (myball.y - ball1.y) * (myball.y - ball1.y)) <= (myball.r + ball1.r) * (myball.r + ball1.r)
- || ((myball.x - ball2.x) * (myball.x - ball2.x) + (myball.y - ball2.y) * (myball.y - ball2.y)) <= (myball.r + ball2.r) * (myball.r + ball2.r)
- || ((myball.x - ball3.x) * (myball.x - ball3.x) + (myball.y - ball3.y) * (myball.y - ball3.y)) <= (myball.r + ball3.r) * (myball.r + ball3.r)
- )//当myball和障碍小球发生触碰时,游戏失败
- {
- closegraph();
- initgraph(1200, 700);
- IMAGE img2;
- loadimage(&img2, "./over.jpg", 1200, 700);//使用该路径要使源文件和图片文件在同一级目录下
- putimage(0, 0, &img2);
- Sleep(5000);
- return 0;
- }
- if (myball.x < 15 && myball.y < 15)//当myball到达界面左上角时,闯关成功
- {
- closegraph();
- initgraph(1200, 700);
- IMAGE img3;
- loadimage(&img3, "./success.jpg", 1200, 700);
- putimage(0, 0, &img3);
- Sleep(5000);
- return 0;
- return 0;
- }
- Sleep(20);
- }
- closegraph();
- }
- }
- }
- return 0;
- }
-

这里给大家演示一下游戏画面:
其中红色小球是我们可以操作移动的小球,白色即为障碍小球
成功界面:
失败界面:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。