介个游戏是大二开学前小编泡了几天图书馆写的
肥肠肥肠粗糙了
(为了涨粉丧心病狂炒冷饭)
但是可以给初学者拿来练习练习面向对象的编程哦
(你连多态都没涉及到还好意思叫oop??)
有很多细节没有处理好,比如
明显闪烁
性能低下
界面丑陋
参数没有设置好导致难度极大
哎,先看看初始界面吧
哦,看呐
这简(zhi)洁(nan)的风(shen)格(mei)
(别说了,直男没有这么辣鸡的审美)
别别别,先别着急退出呀
我们进游戏看看
咳咳。果然和真正的“绝地求生”还是有点区别的
但是,该游戏中体现出的不屈不挠、置之死地而后生的精神
和绝地求生是一样的。。。
hhhh来看代码叭
使用的图形库是easyx
一般的IDE理论上说不会自带,需要自己下载
我下载的版本自带中文注释,仿佛知道了不被自带的原因
使用的编辑环境是Visual Studio 2017
新建头文件circle.h
源文件circle.cpp源.cpp
首先是头文件circle.h
1#include2#include3#definegraphWidth 8004#definegraphHeight 5005#defineSTARTX 3806#defineSTARTY 2507#defineEXITX 3808#defineEXITY 3009#defineORIGINALR 1010#defineCIRCLECOUNT 511#defineMAX 10012#pragmaonce13class Circle14{15private:16friendclassmyCircle;17intradius, x, y, color,dx,dy;18public:19Circle(intX,intY,intR,intColor);20Circle(intX,intY,intR,intColor,intdX,intdY);21~Circle();22intgetRadius();23voiddraw();24voidmove();25intgetX();26intgetY();27};2829class myCircle:publicCircle {30private:31intMax;32public:33myCircle(intX,intY,intR,intColor,intMaxsize);34voidresize();35voidmove( MOUSEMSG mousemsg);36intgetMax();37};
大家应该都知道把数值定义成宏的好处吧
直观明了,增加代码的可读性,也方便以后的参数调整
头文件中已经出现了一些easyx中的类了,
可以直接跳转到easyx.h中查看这个类的函数
可以看到头文件中只有类的声明,
成员函数的实现都在circle.cpp中
1#include"circle.h"2#include34//Circle 定义区******************************5Circle::Circle(intX,intY,intR,intColor):6x(X),y(Y),radius(R),color(Color){7draw();8}9Circle::Circle(intX,intY,intR,intColor,intdX,intdY) :10x(X), y(Y), radius(R),color(Color), dx(dX), dy(dY) {11draw();12}13Circle::~Circle(){14clearcircle(x, y, radius);15}16voidCircle::draw() {17setfillcolor(color);18solidcircle(x, y, radius);19}20intCircle::getRadius() {21returnradius;22}23intCircle::getX() {24returnx;25}26intCircle::getY() {27returny;28}29voidCircle::move(){30clearcircle(x, y, radius);31if(x + radius >= graphWidth || x - radius <=0) {32dx = -dx;33}34if(y + radius >= graphHeight || y - radius <=0) {35dy = -dy;36}37x += dx;38y += dy;39draw();40}41//*******************************************4243//myCircle 定义区*****************************44myCircle::myCircle(intX,intY,intR,intColor,intMaxsize) :45Circle(X, Y, R,Color),Max(Maxsize){}46void myCircle::resize() {47if(radius <50) {48clearcircle(x, y, radius);49radius +=1;50draw();51}52}53void myCircle::move(MOUSEMSG mousemsg) {54clearcircle(x,y,radius);55x = mousemsg.x;56y = mousemsg.y;57draw();58}59intmyCircle::getMax() {60returnMax;61}62//**********************************************
将类的实现和调用分离,用头文件作为中间连接的桥梁
这样能减小单个文件的大小
方便以后的调试和debug
接下来是最重要的main()所在的源文件了
源.cpp主要写了游戏的初始界面和游戏中的操作
1#include"circle.h"2#include3#include4#include56usingnamespacestd;7intversusEnermy(myCircle *user, Circle *enermy){8if(sqrt(pow(user->getX() - enermy->getX(),2) +pow(user->getY() - enermy->getY(),2)) - user->getRadius() - enermy->getRadius() <=0.00001) {9if(user->getRadius() > enermy->getRadius()) {10deleteenermy;11user->resize();12return1;13}14else{15deleteuser;16return0;17}18}19else{20return2;21}22}2324intLarger(Circle **enermy,myCircle *user){25intn =0;26for(inti =0; i < CIRCLECOUNT; i++) {27if(enermy[i] !=NULL) {28if(enermy[i]->getRadius() >= user->getRadius()) {29n++;30}31}32}33returnn;34}3536intSmaller(Circle **enermy,myCircle *user){37intn =0;38for(inti =0; i < CIRCLECOUNT; i++) {39if(enermy[i] !=NULL) {40if(enermy[i]->getRadius() < user->getRadius()) {41n++;42}43}44}45returnn;46}4748Circle *newEnermy(myCircle *user,Circle **enermyArr){49srand((unsigned)time(NULL));50intenermyR;51if(Smaller(enermyArr,user) ==0) {52enermyR = rand() % (user->getRadius() -5) +5;53}54elseif(Larger(enermyArr,user) ==0) {55enermyR = rand() % (user->getMax() - user->getRadius()) + user->getRadius();56}57else{58if(rand() %2) {59enermyR = rand() % (user->getRadius() -5) +5;60}61else{62enermyR = rand() % (user->getMax() - user->getRadius()) + user->getRadius();63}64}65Circle *enermy =newCircle(rand() % (graphWidth - enermyR *2) + enermyR, rand() % (graphHeight - enermyR *2) + enermyR, enermyR, rand() %16777216, rand() % (graphWidth /100) +1, rand() % (graphHeight /100) +1);66returnenermy;67}6869voidstartGame(){70/*这是正式的写游戏过程的函数*/71cleardevice();72srand((unsigned)time(NULL));73MOUSEMSG mousemsg = GetMouseMsg();74myCircle *user =newmyCircle(mousemsg.x, mousemsg.y, ORIGINALR, MAGENTA,MAX);75Circle *enermy[CIRCLECOUNT];76for(inti =0; i < CIRCLECOUNT /2; i++) {77intenermyR = rand() % (ORIGINALR -5) +5;78enermy[i] =newCircle(rand() % (graphWidth - enermyR *2) + enermyR, rand() % (graphHeight - enermyR *2) + enermyR, enermyR, rand()%16777216, rand() % (graphWidth /100), rand() % (graphHeight /100));79}80for(inti = CIRCLECOUNT /2; i < CIRCLECOUNT; i++) {81intenermyR = rand() % (user->getMax() - user->getRadius());82enermy[i] =newCircle(rand() % (graphWidth - enermyR *2) + enermyR, rand() % (graphHeight - enermyR *2) + enermyR, enermyR, rand()%16777216, rand() % (graphWidth /100), rand() % (graphHeight /100));83}84Sleep(1500);85while(true) {86if(MouseHit()) {87FlushMouseMsgBuffer();88mousemsg = GetMouseMsg();89user->move(mousemsg);90}91Sleep(5);//放缓游戏节奏,不然容易落地成盒92for(inti =0; i < CIRCLECOUNT; i++) {93if(enermy[i] !=NULL) {94enermy[i]->move();95intresult = versusEnermy(user, enermy[i]);96if(result ==0)gotooutside;97elseif(result ==1) {98enermy[i] =NULL;99enermy[i] = newEnermy(user, enermy);100}101}102}103}104outside:105for(inti =0; i <4; i++) {106if(enermy[i] !=NULL)107deleteenermy[i];108}109cleardevice();110}111112boolclick(MOUSEMSG mousemsg, LPCTSTR word){113//判断鼠标点击的是哪个字符串114boolresult =false;115shortx, y;116if(word == _T("start")||word == _T("restart")){117x = STARTX;118y = STARTY;119}120elseif(word == _T("exit")) {121x = EXITX;122y = EXITY;123}124if(mousemsg.x >= x && mousemsg.x <= x + textwidth(word)) {125if(mousemsg.y >= y && mousemsg.y <= y + textwidth(word)) {126result =true;127}128}129returnresult;130}131132intmain(intargc,char*argv[]){133initgraph(graphWidth,graphHeight/*,SHOWCONSOLE/**/);134setbkcolor(WHITE);135settextcolor(BLACK);136cleardevice();137138//颜色代码从左到右是BGR139CircleCircle1(420,125,50,0xe22b8a);140CircleCircle2(380,125,25,0xFFFF);141LPCTSTR start = _T("start");142LPCTSTRexit= _T("exit");143outtextxy(STARTX, STARTY, start);144outtextxy(EXITX, EXITY,exit);145structMOUSEMSGmousemsg;146while(true) {147if(MouseHit()) {148mousemsg = GetMouseMsg();149if(mousemsg.mkLButton && click(mousemsg, start)) {150startGame();151Circle1.draw();152Circle2.draw();153start = _T("restart");154outtextxy(STARTX, STARTY, start);155outtextxy(EXITX, EXITY,exit);156}elseif(mousemsg.mkLButton && click(mousemsg,exit)) {157FlushMouseMsgBuffer();158break;159}160}161}162FlushMouseMsgBuffer();163closegraph();164return0;165}
分享这个bug百出的小游戏主要是因为这是我第一个
通过参考库文件的声明、分离类和主函数、用C++写GUI的程序
比较有纪念意义
实质性的教导意义呢
emmm
朋友们一条语句太长的时候记得回车换行
习从来不是一个人的事情,要有个相互监督的伙伴,工作需要学习C/C++或者为了入行、转行学习C/C++的伙伴可以私信回复小编“学习”领取全套免费C/C++学习资料、视频