├── BallBattle ├── BallBattle.pro ├── BallBattle.pro.user ├── ai.cpp ├── ai.h ├── battlefield.cpp ├── battlefield.h ├── battlefield.ui ├── camera.cpp ├── camera.h ├── config.h ├── food.cpp ├── food.h ├── main.cpp ├── maingamer.cpp ├── maingamer.h ├── mainwindow.cpp ├── mainwindow.h ├── mainwindow.ui ├── mydock.cpp ├── mydock.h ├── object.cpp ├── object.h ├── res.qrc ├── res │ ├── ai.cpp │ ├── ball │ │ ├── role_01.png │ │ ├── role_02.png │ │ ├── role_03.png │ │ ├── role_04.png │ │ ├── role_05.png │ │ └── role_06.png │ ├── floor │ │ ├── floor_01.png │ │ ├── floor_02.png │ │ └── role.png │ └── food │ │ └── food_01.png └── test.h └── README.md /BallBattle/BallBattle.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2022-07-05T20:32:38 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = BallBattle 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which has been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | 26 | SOURCES += \ 27 | main.cpp \ 28 | mainwindow.cpp \ 29 | battlefield.cpp \ 30 | object.cpp \ 31 | camera.cpp \ 32 | maingamer.cpp \ 33 | mydock.cpp \ 34 | food.cpp \ 35 | ai.cpp 36 | 37 | HEADERS += \ 38 | mainwindow.h \ 39 | battlefield.h \ 40 | config.h \ 41 | object.h \ 42 | camera.h \ 43 | maingamer.h \ 44 | mydock.h \ 45 | food.h \ 46 | ai.h 47 | 48 | FORMS += \ 49 | mainwindow.ui \ 50 | battlefield.ui 51 | 52 | RESOURCES += \ 53 | res.qrc 54 | -------------------------------------------------------------------------------- /BallBattle/BallBattle.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {e7afd24d-ea4e-4298-be77-2b7a5f3633ce} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | true 45 | 0 46 | 8 47 | true 48 | 1 49 | true 50 | true 51 | true 52 | false 53 | 54 | 55 | 56 | ProjectExplorer.Project.PluginSettings 57 | 58 | 59 | 60 | ProjectExplorer.Project.Target.0 61 | 62 | Desktop Qt 5.9.4 MinGW 32bit 63 | Desktop Qt 5.9.4 MinGW 32bit 64 | qt.594.win32_mingw53_kit 65 | 1 66 | 0 67 | 0 68 | 69 | D:/QTcode/build-BallBattle-Desktop_Qt_5_9_4_MinGW_32bit-Debug 70 | 71 | 72 | true 73 | qmake 74 | 75 | QtProjectManager.QMakeBuildStep 76 | true 77 | 78 | false 79 | false 80 | false 81 | 82 | 83 | true 84 | Make 85 | 86 | Qt4ProjectManager.MakeStep 87 | 88 | false 89 | 90 | 91 | 92 | 2 93 | 构建 94 | 95 | ProjectExplorer.BuildSteps.Build 96 | 97 | 98 | 99 | true 100 | Make 101 | 102 | Qt4ProjectManager.MakeStep 103 | 104 | true 105 | clean 106 | 107 | 108 | 1 109 | 清理 110 | 111 | ProjectExplorer.BuildSteps.Clean 112 | 113 | 2 114 | false 115 | 116 | Debug 117 | 118 | Qt4ProjectManager.Qt4BuildConfiguration 119 | 2 120 | true 121 | 122 | 123 | D:/QTcode/build-BallBattle-Desktop_Qt_5_9_4_MinGW_32bit-Release 124 | 125 | 126 | true 127 | qmake 128 | 129 | QtProjectManager.QMakeBuildStep 130 | false 131 | 132 | false 133 | false 134 | false 135 | 136 | 137 | true 138 | Make 139 | 140 | Qt4ProjectManager.MakeStep 141 | 142 | false 143 | 144 | 145 | 146 | 2 147 | 构建 148 | 149 | ProjectExplorer.BuildSteps.Build 150 | 151 | 152 | 153 | true 154 | Make 155 | 156 | Qt4ProjectManager.MakeStep 157 | 158 | true 159 | clean 160 | 161 | 162 | 1 163 | 清理 164 | 165 | ProjectExplorer.BuildSteps.Clean 166 | 167 | 2 168 | false 169 | 170 | Release 171 | 172 | Qt4ProjectManager.Qt4BuildConfiguration 173 | 0 174 | true 175 | 176 | 177 | D:/QTcode/build-BallBattle-Desktop_Qt_5_9_4_MinGW_32bit-Profile 178 | 179 | 180 | true 181 | qmake 182 | 183 | QtProjectManager.QMakeBuildStep 184 | true 185 | 186 | false 187 | true 188 | false 189 | 190 | 191 | true 192 | Make 193 | 194 | Qt4ProjectManager.MakeStep 195 | 196 | false 197 | 198 | 199 | 200 | 2 201 | 构建 202 | 203 | ProjectExplorer.BuildSteps.Build 204 | 205 | 206 | 207 | true 208 | Make 209 | 210 | Qt4ProjectManager.MakeStep 211 | 212 | true 213 | clean 214 | 215 | 216 | 1 217 | 清理 218 | 219 | ProjectExplorer.BuildSteps.Clean 220 | 221 | 2 222 | false 223 | 224 | Profile 225 | 226 | Qt4ProjectManager.Qt4BuildConfiguration 227 | 0 228 | true 229 | 230 | 3 231 | 232 | 233 | 0 234 | 部署 235 | 236 | ProjectExplorer.BuildSteps.Deploy 237 | 238 | 1 239 | 在本地部署 240 | 241 | ProjectExplorer.DefaultDeployConfiguration 242 | 243 | 1 244 | 245 | 246 | false 247 | false 248 | 1000 249 | 250 | true 251 | 252 | false 253 | false 254 | false 255 | false 256 | true 257 | 0.01 258 | 10 259 | true 260 | 1 261 | 25 262 | 263 | 1 264 | true 265 | false 266 | true 267 | valgrind 268 | 269 | 0 270 | 1 271 | 2 272 | 3 273 | 4 274 | 5 275 | 6 276 | 7 277 | 8 278 | 9 279 | 10 280 | 11 281 | 12 282 | 13 283 | 14 284 | 285 | 2 286 | 287 | BallBattle 288 | 289 | Qt4ProjectManager.Qt4RunConfiguration:D:/QTcode/BallBattle/BallBattle.pro 290 | true 291 | 292 | BallBattle.pro 293 | false 294 | 295 | D:/QTcode/build-BallBattle-Desktop_Qt_5_9_4_MinGW_32bit-Release 296 | 3768 297 | false 298 | true 299 | false 300 | false 301 | true 302 | 303 | 1 304 | 305 | 306 | 307 | ProjectExplorer.Project.TargetCount 308 | 1 309 | 310 | 311 | ProjectExplorer.Project.Updater.FileVersion 312 | 18 313 | 314 | 315 | Version 316 | 18 317 | 318 | 319 | -------------------------------------------------------------------------------- /BallBattle/ai.cpp: -------------------------------------------------------------------------------- 1 | #include "ai.h" 2 | #include"config.h" 3 | AI::AI() 4 | { 5 | 6 | } 7 | 8 | void AI::move(){ 9 | 10 | if(abs(speedX)>=6){speedX=6*(speedX)/abs(speedX);} 11 | if(abs(speedY)>=6){speedY=6*(speedY)/abs(speedY);} 12 | absX+=speedX; 13 | absY+=speedY; 14 | if(absX>=BATTLE_FIELD_WIDTH*2/3-Pic.width()/2){ 15 | absX = BATTLE_FIELD_WIDTH*2/3-Pic.width()/2; 16 | speedX = 0; 17 | } 18 | if(absX<=-BATTLE_FIELD_WIDTH/3-50){ 19 | absX = -BATTLE_FIELD_WIDTH/3-50; 20 | speedX = 0; 21 | } 22 | if(absY>=BATTLE_FIELD_HEIGHT*2/3-Pic.height()/2){ 23 | absY = BATTLE_FIELD_HEIGHT*2/3-Pic.height()/2; 24 | speedY = 0; 25 | } 26 | if(absY<=-BATTLE_FIELD_HEIGHT/3-50){ 27 | absY = -BATTLE_FIELD_HEIGHT/3-50; 28 | speedY = 0; 29 | } 30 | 31 | } 32 | void AI::chageSpeed(){ 33 | speedX+=(double) rand()/RAND_MAX>=0.5 ? 0.5:-0.5; 34 | speedY+=(double) rand()/RAND_MAX>=0.5 ? 0.5:-0.5; 35 | } 36 | 37 | void AI::collisionCheck(std::vector foodset) 38 | { 39 | double centerX = absX+Pic.width()/2; 40 | double centerY = absY+Pic.height()/2; 41 | int i = 0; 42 | for(auto tmp:foodset){ 43 | if(tmp==nullptr){ 44 | i++; 45 | continue; 46 | } 47 | double fcX = tmp->absX+tmp->picW/2; 48 | double fcY = tmp->absY+tmp->picH/2; 49 | double dis =sqrt(pow(centerX-fcX,2)+pow(centerY-fcY,2)); 50 | if(dis<=std::min(tmp->picW/2,tmp->picH/2)+std::min(Pic.width()/2,Pic.height()/2)){ 51 | tmp->absX = (double)rand()/RAND_MAX-BATTLE_FIELD_WIDTH/3; 52 | tmp->absY = (double)rand()/RAND_MAX-BATTLE_FIELD_HEIGHT/3; 53 | score+=10; 54 | } 55 | i++; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /BallBattle/ai.h: -------------------------------------------------------------------------------- 1 | #ifndef AI_H 2 | #define AI_H 3 | #include "object.h" 4 | #include "food.h" 5 | class AI : public Object{ 6 | public: 7 | AI(); 8 | ~AI(); 9 | void move(); 10 | void collisionCheck(std::vector foodset); 11 | void chageSpeed(); 12 | int score = 0; 13 | double speedX = 0; 14 | double speedY = 0; 15 | }; 16 | 17 | #endif // AI_H 18 | -------------------------------------------------------------------------------- /BallBattle/battlefield.cpp: -------------------------------------------------------------------------------- 1 | #include "battlefield.h" 2 | #include "ui_battlefield.h" 3 | #include "maingamer.h" 4 | #include "config.h" 5 | #include "mydock.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | Battlefield::Battlefield(QWidget *parent) : 11 | QWidget(parent), 12 | ui(new Ui::Battlefield) 13 | { 14 | p = new myCamera(0,0); 15 | ui->setupUi(this); 16 | resize(INITWIDTH,INITHEIGHT); 17 | dock= new myDock(this); 18 | dock->initDock(this->width()-dock->width(),0); 19 | dock->setParent(this); 20 | timer.setInterval(1000/64); 21 | speedtimer.setInterval(500); 22 | mainGamer::getInstance()->start(INITPIC); 23 | for(int i = 0;i<50;i++){ 24 | auto tmp = new food; 25 | tmp->absX = (double)rand()/RAND_MAX*BATTLE_FIELD_WIDTH-BATTLE_FIELD_WIDTH/3; 26 | tmp->absY = (double)rand()/RAND_MAX*BATTLE_FIELD_HEIGHT-BATTLE_FIELD_HEIGHT/3; 27 | foodset.push_back(tmp); 28 | } 29 | auto player = mainGamer::getInstance(); 30 | double centerX = player->absX+player->Pic.width(); 31 | double centerY = player->absY+player->Pic.height(); 32 | for(int i = 0;i<3;i++){ 33 | auto tmp = new AI; 34 | tmp->absX = (double)rand()/RAND_MAX*BATTLE_FIELD_WIDTH-BATTLE_FIELD_WIDTH/3; 35 | tmp->absY = (double)rand()/RAND_MAX*BATTLE_FIELD_HEIGHT-BATTLE_FIELD_HEIGHT/3; 36 | QString qrs = ":/res/ball/role_0"; 37 | int index = floor((double)rand()/RAND_MAX*5+1); 38 | QString qrst = QString::fromStdString(std::to_string(index)); 39 | qrs.append(qrst); 40 | qrs.append(".png"); 41 | tmp->Pic.load(qrs); 42 | tmp->picH = tmp->Pic.height(); 43 | tmp->picW = tmp->Pic.width(); 44 | double fcX = tmp->absX+tmp->picW/2; 45 | double fcY = tmp->absY+tmp->picH/2; 46 | double dis =sqrt(pow(centerX-fcX,2)+pow(centerY-fcY,2)); 47 | while(dis<=std::min(tmp->picW/2,tmp->picH/2)+std::min(player->Pic.width()/2,player->Pic.height()/2)){ 48 | tmp->absX = (double)rand()/RAND_MAX*BATTLE_FIELD_WIDTH-BATTLE_FIELD_WIDTH/3; 49 | tmp->absY = (double)rand()/RAND_MAX*BATTLE_FIELD_HEIGHT-BATTLE_FIELD_HEIGHT/3; 50 | } 51 | AIset.push_back(tmp); 52 | } 53 | gameBegin(); 54 | } 55 | 56 | Battlefield::~Battlefield() 57 | { 58 | delete ui; 59 | } 60 | 61 | void Battlefield::paintEvent(QPaintEvent *event) 62 | { QPainter qp(this); 63 | QPixmap l1; 64 | QPixmap l2; 65 | l1.load(FLOOR); 66 | l2.load(":/res/food/food_01.png"); 67 | auto player = mainGamer::getInstance(); 68 | for(double x = -BATTLE_FIELD_WIDTH/3-p->posX;x<=BATTLE_FIELD_WIDTH/3*2-p->posX;){ 69 | for(double y = -BATTLE_FIELD_WIDTH/3-p->posY;y<=BATTLE_FIELD_HEIGHT/3*2-p->posY;){ 70 | qp.drawPixmap(x,y,l1.width(),l1.height(),l1); 71 | y+=l1.height(); 72 | } 73 | x+=l1.width(); 74 | } 75 | for(auto tmp:foodset){ 76 | int x = tmp->absX-p->posX; 77 | int y = tmp->absY-p->posY; 78 | tmp->picW = l2.width(); 79 | tmp->picH = l2.height(); 80 | qp.drawPixmap(x,y,l2.width(),l2.height(),l2); 81 | } 82 | for(auto tmp:AIset){ 83 | int x = tmp->absX-p->posX; 84 | int y = tmp->absY-p->posY; 85 | qp.drawPixmap(x,y,tmp->picW,tmp->picH,tmp->Pic); 86 | } 87 | 88 | qp.drawPixmap(player->x,player->y,player->Pic.width(),player->Pic.height(),player->Pic); 89 | } 90 | void Battlefield::gameBegin(){ 91 | timer.start(); 92 | speedtimer.start(); 93 | connect(&speedtimer,&QTimer::timeout,[=]{ 94 | auto player = mainGamer::getInstance(); 95 | player->speedX = player->speedX*0.7; 96 | player->speedY = player->speedY*0.7; 97 | for(auto tmp:AIset){ 98 | tmp->chageSpeed(); 99 | } 100 | }); 101 | connect(&timer,&QTimer::timeout,[=]{ 102 | p->move(); 103 | auto player = mainGamer::getInstance(); 104 | player->move(p); 105 | player->collisionCheck(foodset); 106 | for(auto tmp:AIset){ 107 | tmp->move(); 108 | tmp->collisionCheck(foodset); 109 | } 110 | auto qstr = QString::fromStdString(std::to_string(mainGamer::getInstance()->score)); 111 | dock->score->setText(qstr); 112 | update(); 113 | }); 114 | } 115 | 116 | void Battlefield::keyPressEvent(QKeyEvent *event){ 117 | auto player = mainGamer::getInstance(); 118 | if(event->key()==Qt::Key_W){ 119 | player->speedY-=0.5; 120 | } 121 | if(event->key()==Qt::Key_S){ 122 | player->speedY+=0.5; 123 | } 124 | if(event->key()==Qt::Key_A){ 125 | player->speedX-=0.5; 126 | } 127 | if(event->key()==Qt::Key_D){ 128 | player->speedX+=0.5; 129 | } 130 | } 131 | 132 | -------------------------------------------------------------------------------- /BallBattle/battlefield.h: -------------------------------------------------------------------------------- 1 | #ifndef BATTLEFIELD_H 2 | #define BATTLEFIELD_H 3 | #include "camera.h" 4 | #include "food.h" 5 | #include "mydock.h" 6 | #include "ai.h" 7 | #include 8 | #include 9 | #include 10 | 11 | namespace Ui { 12 | class Battlefield; 13 | } 14 | 15 | class Battlefield : public QWidget 16 | { 17 | Q_OBJECT 18 | 19 | public: 20 | explicit Battlefield(QWidget *parent = 0); 21 | ~Battlefield(); 22 | void paintEvent(QPaintEvent *event); 23 | void gameBegin(); 24 | void keyPressEvent(QKeyEvent *event); 25 | 26 | myDock * dock; 27 | QTimer timer; 28 | QTimer speedtimer; 29 | std::vector foodset; 30 | std::vector AIset; 31 | 32 | private: 33 | Ui::Battlefield *ui; 34 | myCamera* p; 35 | }; 36 | 37 | #endif // BATTLEFIELD_H 38 | -------------------------------------------------------------------------------- /BallBattle/battlefield.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Battlefield 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | Form 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /BallBattle/camera.cpp: -------------------------------------------------------------------------------- 1 | #include "camera.h" 2 | #include "maingamer.h" 3 | #include "config.h" 4 | 5 | myCamera::myCamera(double x,double y):posX(x),posY(y) 6 | { 7 | 8 | } 9 | 10 | void myCamera::move() 11 | { 12 | movableX = true; 13 | auto gamer = mainGamer::getInstance(); 14 | posX+=gamer->speedX*0.8; 15 | posY+=gamer->speedY*0.8; 16 | if(posX<=-BATTLE_FIELD_WIDTH/3){ 17 | movableX = false; 18 | posX = -BATTLE_FIELD_WIDTH/3; 19 | } 20 | if(posX>=BATTLE_FIELD_WIDTH/3){ 21 | movableX = false; 22 | posX = BATTLE_FIELD_WIDTH/3; 23 | } 24 | if(posY<=-BATTLE_FIELD_HEIGHT/3){ 25 | movableY = false; 26 | posY = -BATTLE_FIELD_HEIGHT/3; 27 | } 28 | if(posY>=BATTLE_FIELD_HEIGHT/3){ 29 | movableY = false; 30 | posY = BATTLE_FIELD_HEIGHT/3; 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /BallBattle/camera.h: -------------------------------------------------------------------------------- 1 | #ifndef CAMERA_H 2 | #define CAMERA_H 3 | class myCamera 4 | { 5 | public: 6 | myCamera(double x,double y); 7 | ~myCamera(); 8 | void move(); 9 | public: 10 | double posX;//此处代表镜头左上角位置 11 | double posY; 12 | bool movableX = true;//是否可以移动X轴 13 | bool movableY = true;//是否可以移动Y轴 14 | double index = 1;//缩放系数 15 | }; 16 | 17 | #endif // CAMERA_H 18 | -------------------------------------------------------------------------------- /BallBattle/config.h: -------------------------------------------------------------------------------- 1 | #ifndef CONFIG_H 2 | #define CONFIG_H 3 | #define INITWIDTH 1024 4 | #define INITHEIGHT 1024 5 | #define BATTLE_FIELD_WIDTH INITWIDTH*3 6 | #define BATTLE_FIELD_HEIGHT INITHEIGHT*3 7 | #define FLOOR ":/res/floor/floor_01.png" 8 | #define RATE_HIGH 1000/64 9 | #define RATE_LOW 1000/32 10 | #define INITPIC ":/res/ball/role_01.png" 11 | #endif // CONFIG_H 12 | -------------------------------------------------------------------------------- /BallBattle/food.cpp: -------------------------------------------------------------------------------- 1 | #include "food.h" 2 | 3 | food::food() 4 | { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /BallBattle/food.h: -------------------------------------------------------------------------------- 1 | #ifndef FOOD_H 2 | #define FOOD_H 3 | #include "object.h" 4 | 5 | class food : public Object{ 6 | public: 7 | food(); 8 | }; 9 | 10 | #endif // FOOD_H 11 | -------------------------------------------------------------------------------- /BallBattle/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | #include "maingamer.h" 4 | #include "camera.h" 5 | mainGamer* mainGamer::gamePointer = NULL; 6 | int main(int argc, char *argv[]) 7 | { 8 | QApplication a(argc, argv); 9 | MainWindow w; 10 | w.show(); 11 | 12 | return a.exec(); 13 | } 14 | -------------------------------------------------------------------------------- /BallBattle/maingamer.cpp: -------------------------------------------------------------------------------- 1 | #include "maingamer.h" 2 | #include "camera.h" 3 | #include "config.h" 4 | mainGamer::mainGamer(int x) 5 | { 6 | 7 | } 8 | 9 | void mainGamer::start(QString path) 10 | { 11 | Pic.load(path); 12 | absX =INITWIDTH/2-Pic.width()/2; 13 | absY = INITHEIGHT/2-Pic.height()/2; 14 | x = absX; 15 | y = absY; 16 | } 17 | void mainGamer::move(myCamera *cam){ 18 | if(abs(speedX)>=6){speedX=6*(speedX)/abs(speedX);} 19 | if(abs(speedY)>=6){speedY=6*(speedY)/abs(speedY);} 20 | absX+=speedX; 21 | absY+=speedY; 22 | if(absX>=BATTLE_FIELD_WIDTH*2/3-Pic.width()/2){ 23 | absX = BATTLE_FIELD_WIDTH*2/3-Pic.width()/2; 24 | speedX = 0; 25 | } 26 | if(absX<=-BATTLE_FIELD_WIDTH/3-50){ 27 | absX = -BATTLE_FIELD_WIDTH/3-50; 28 | speedX = 0; 29 | } 30 | if(absY>=BATTLE_FIELD_HEIGHT*2/3-Pic.height()/2){ 31 | absY = BATTLE_FIELD_HEIGHT*2/3-Pic.height()/2; 32 | speedY = 0; 33 | } 34 | if(absY<=-BATTLE_FIELD_HEIGHT/3-50){ 35 | absY = -BATTLE_FIELD_HEIGHT/3-50; 36 | speedY = 0; 37 | } 38 | 39 | 40 | x = absX-cam->posX; 41 | y = absY-cam->posY; 42 | } 43 | 44 | void mainGamer::collisionCheck(std::vector foodset) 45 | { 46 | double centerX = absX+Pic.width()/2; 47 | double centerY = absY+Pic.height()/2; 48 | int i = 0; 49 | for(auto tmp:foodset){ 50 | if(tmp==nullptr){ 51 | i++; 52 | continue; 53 | } 54 | double fcX = tmp->absX+tmp->picW/2; 55 | double fcY = tmp->absY+tmp->picH/2; 56 | double dis =sqrt(pow(centerX-fcX,2)+pow(centerY-fcY,2)); 57 | if(dis<=std::min(tmp->picW/2,tmp->picH/2)+std::min(Pic.width()/2,Pic.height()/2)){ 58 | tmp->absX = (double)rand()/RAND_MAX-BATTLE_FIELD_WIDTH/3; 59 | tmp->absY = (double)rand()/RAND_MAX-BATTLE_FIELD_HEIGHT/3; 60 | score+=10; 61 | } 62 | i++; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /BallBattle/maingamer.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINGAMER_H 2 | #define MAINGAMER_H 3 | #include "object.h" 4 | #include "camera.h" 5 | #include "food.h" 6 | #include 7 | class mainGamer:public Object{ 8 | 9 | private: 10 | mainGamer(int x); 11 | ~mainGamer(); 12 | public: 13 | 14 | static mainGamer* getInstance(){ 15 | if(gamePointer==nullptr){ 16 | gamePointer = new mainGamer(1); 17 | } 18 | return gamePointer; 19 | } 20 | void start(QString path); 21 | void move(myCamera *cam); 22 | void collisionCheck(std::vector foodset); 23 | private: 24 | static mainGamer* gamePointer; 25 | public: 26 | double speedX = 0;//速度 27 | double speedY = 0; 28 | int score = 0; 29 | 30 | }; 31 | 32 | #endif // MAINGAMER_H 33 | -------------------------------------------------------------------------------- /BallBattle/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | #include "battlefield.h" 4 | #include "config.h" 5 | MainWindow::MainWindow(QWidget *parent) : 6 | QMainWindow(parent), 7 | ui(new Ui::MainWindow) 8 | { 9 | ui->setupUi(this); 10 | resize(INITWIDTH,INITHEIGHT); 11 | connect(ui->start,QPushButton::pressed,this,[=]{ 12 | int i = 0; 13 | }); 14 | connect(ui->start,QPushButton::released,this,[=]{ 15 | auto battlefield = new Battlefield; 16 | this->hide(); 17 | battlefield->show(); 18 | }); 19 | } 20 | 21 | MainWindow::~MainWindow() 22 | { 23 | delete ui; 24 | } 25 | -------------------------------------------------------------------------------- /BallBattle/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | namespace Ui { 7 | class MainWindow; 8 | } 9 | 10 | class MainWindow : public QMainWindow 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MainWindow(QWidget *parent = 0); 16 | ~MainWindow(); 17 | 18 | private slots: 19 | private: 20 | Ui::MainWindow *ui; 21 | }; 22 | 23 | #endif // MAINWINDOW_H 24 | -------------------------------------------------------------------------------- /BallBattle/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 160 21 | 20 22 | 80 23 | 15 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 0 35 | 0 36 | 400 37 | 17 38 | 39 | 40 | 41 | 42 | 43 | TopToolBarArea 44 | 45 | 46 | false 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /BallBattle/mydock.cpp: -------------------------------------------------------------------------------- 1 | #include "mydock.h" 2 | #include "maingamer.h" 3 | #include 4 | myDock::myDock(QWidget *parent) : QDockWidget(parent) 5 | { 6 | 7 | } 8 | 9 | myDock::muDock() 10 | { 11 | 12 | } 13 | 14 | myDock::~myDock() 15 | { 16 | 17 | } 18 | 19 | void myDock::initDock(double W,double H) 20 | { 21 | 22 | 23 | 24 | this->resize(200,400); 25 | this->move(1024-200,10); 26 | auto text1 = new QLabel; 27 | auto text2 = new QLabel; 28 | auto text3 = new QLabel; 29 | score = new QLabel; 30 | text1->setParent(this); 31 | text1->setText("玩家"); 32 | text1->move(10,-190); 33 | text2->setParent(this); 34 | text2->setText("分数"); 35 | text2->move(90,-190); 36 | text3->setParent(this); 37 | text3->setText("您"); 38 | text3->move(20,-160); 39 | auto qstr = QString::fromStdString(std::to_string(mainGamer::getInstance()->score)); 40 | score->setText(qstr); 41 | score->move(100,-160); 42 | score->setParent(this); 43 | score->show(); 44 | this->show(); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /BallBattle/mydock.h: -------------------------------------------------------------------------------- 1 | #ifndef MYDOCK_H 2 | #define MYDOCK_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | class myDock : public QDockWidget 11 | { 12 | Q_OBJECT 13 | public: 14 | explicit myDock(QWidget *parent = nullptr); 15 | muDock(); 16 | ~myDock(); 17 | void initDock(double W,double H); 18 | 19 | QLabel* score; 20 | 21 | signals: 22 | 23 | public slots: 24 | }; 25 | 26 | #endif // MYDOCK_H 27 | -------------------------------------------------------------------------------- /BallBattle/object.cpp: -------------------------------------------------------------------------------- 1 | #include "object.h" 2 | 3 | Object::Object() 4 | { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /BallBattle/object.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_H 2 | #define OBJECT_H 3 | #include 4 | 5 | class Object 6 | { 7 | public: 8 | void move(); 9 | Object(); 10 | double absX;//绝对坐标系位置 11 | double absY; 12 | double x;//相对坐标系位置 13 | double y; 14 | double picW;//贴图大小 15 | double picH;// 16 | QPixmap Pic; 17 | }; 18 | 19 | #endif // OBJECT_H 20 | -------------------------------------------------------------------------------- /BallBattle/res.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | res/floor/floor_01.png 4 | res/floor/floor_02.png 5 | res/floor/role.png 6 | res/ball/role_01.png 7 | res/food/food_01.png 8 | res/ball/role_02.png 9 | res/ball/role_03.png 10 | res/ball/role_04.png 11 | res/ball/role_05.png 12 | res/ball/role_06.png 13 | 14 | 15 | -------------------------------------------------------------------------------- /BallBattle/res/ai.cpp: -------------------------------------------------------------------------------- 1 | #include "ai.h" 2 | 3 | AI::AI() 4 | { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /BallBattle/res/ball/role_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangwangxianbeiok/QT_BallBattle/1a7fb16f6e343e7b5aaeef7b89361fcd64c5d58f/BallBattle/res/ball/role_01.png -------------------------------------------------------------------------------- /BallBattle/res/ball/role_02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangwangxianbeiok/QT_BallBattle/1a7fb16f6e343e7b5aaeef7b89361fcd64c5d58f/BallBattle/res/ball/role_02.png -------------------------------------------------------------------------------- /BallBattle/res/ball/role_03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangwangxianbeiok/QT_BallBattle/1a7fb16f6e343e7b5aaeef7b89361fcd64c5d58f/BallBattle/res/ball/role_03.png -------------------------------------------------------------------------------- /BallBattle/res/ball/role_04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangwangxianbeiok/QT_BallBattle/1a7fb16f6e343e7b5aaeef7b89361fcd64c5d58f/BallBattle/res/ball/role_04.png -------------------------------------------------------------------------------- /BallBattle/res/ball/role_05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangwangxianbeiok/QT_BallBattle/1a7fb16f6e343e7b5aaeef7b89361fcd64c5d58f/BallBattle/res/ball/role_05.png -------------------------------------------------------------------------------- /BallBattle/res/ball/role_06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangwangxianbeiok/QT_BallBattle/1a7fb16f6e343e7b5aaeef7b89361fcd64c5d58f/BallBattle/res/ball/role_06.png -------------------------------------------------------------------------------- /BallBattle/res/floor/floor_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangwangxianbeiok/QT_BallBattle/1a7fb16f6e343e7b5aaeef7b89361fcd64c5d58f/BallBattle/res/floor/floor_01.png -------------------------------------------------------------------------------- /BallBattle/res/floor/floor_02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangwangxianbeiok/QT_BallBattle/1a7fb16f6e343e7b5aaeef7b89361fcd64c5d58f/BallBattle/res/floor/floor_02.png -------------------------------------------------------------------------------- /BallBattle/res/floor/role.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangwangxianbeiok/QT_BallBattle/1a7fb16f6e343e7b5aaeef7b89361fcd64c5d58f/BallBattle/res/floor/role.png -------------------------------------------------------------------------------- /BallBattle/res/food/food_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangwangxianbeiok/QT_BallBattle/1a7fb16f6e343e7b5aaeef7b89361fcd64c5d58f/BallBattle/res/food/food_01.png -------------------------------------------------------------------------------- /BallBattle/test.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_H 2 | #define TEST_H 3 | class test{ 4 | 5 | }; 6 | 7 | #endif // TEST_H 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QT-BallBattle 2 | 使用C++QT库编写的球球大作战小游戏 3 | --------------------------------------------------------------------------------