源代码
main.cpp
1 2 3 4 | #include "game.h" int main() { Run(); } |
game.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <easyx.h> #include <conio.h> #include <time.h> #pragma comment(lib,"Winmm.lib") #pragma comment( lib, "MSIMG32.LIB") #define BulletMaxNum 20 #define EnemyMaxNum 5 struct Plane { //飞机 POINT pos = { 0,0 }; //位置 IMAGE img; //图片 }; struct Bullet { POINT pos = { 0,0 }; IMAGE img; int type = 0; int speed = 1; bool show = false; }; struct Enemy { IMAGE img; POINT pos = { 100,100 }; bool show = false; clock_t startClock=NULL, endClock=NULL; //时钟,用来减缓移动速度 }; void putAlphaImage(IMAGE* dstimg, int x, int y, IMAGE* srcimg, UINT transparentcolor); void initData(); void Draw(); void drawText(); void drawPlane(); void drawBullet(); void drawEnemy(); void Input(); void Logic(); void Run(); void enemyRandomMove(); void addEnemy(); bool moveDelay(clock_t c1, clock_t c2, int delay); bool isBumpBullet(Bullet* bullet, Enemy* enemy); bool isBumpEnemy(Enemy* enemy1, Enemy* enemy2); bool isBumpPlane(Enemy* enemy1, Plane* enemy2); void playBoomSound(); |
game.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | #include "game.h" clock_t startClock, processClock; int directLvl = 1; Plane plane; Bullet bullets[BulletMaxNum]; Enemy enemys[EnemyMaxNum]; IMAGE bg; bool gameQuit = false; int bulletNum, enemyNum; int planeSpeed; double enemySpeed; int enemyMoveDealy; void initData() { planeSpeed = 1; enemySpeed = 1; enemyMoveDealy = 5; for (int i = 0; i < BulletMaxNum; i++) { loadimage(&bullets[i].img, _T("data/bullet.png")); } for (int i = 0; i < EnemyMaxNum; i++) { loadimage(&enemys[i].img, _T("data/enemy.png")); } bulletNum = 0; enemyNum = 0; gameQuit = false; plane.pos.x = 500; plane.pos.y = 600; loadimage(&plane.img, _T("data/plane.png")); loadimage(&bg, _T("data/bg.png")); startClock = clock(); } void Run() { initData(); initgraph(1000, 800); while (!gameQuit) { Draw(); Input(); Logic(); } closegraph(); } void Draw() { BeginBatchDraw(); putimage(0, 0, &bg); drawText(); drawBullet(); drawEnemy(); drawPlane(); EndBatchDraw(); } void drawText() { //TCHAR s1[] = _T("飞机大战"); //TCHAR s2[] = _T("移动控制飞机,左键开火"); //outtextxy(800, 100, s1); //outtextxy(800, 150, s2); } void drawPlane() { //putimage(plane.pos.x, plane.pos.y, &plane.img); putAlphaImage(NULL, plane.pos.x, plane.pos.y, &plane.img, 0xffffff); } void drawBullet() { for (int i = 0; i < BulletMaxNum; i++) { if (bullets[i].show) { putAlphaImage(NULL, bullets[i].pos.x, bullets[i].pos.y, &bullets[i].img, 0xffffff); } } } void drawEnemy() { for (int i = 0; i < EnemyMaxNum; i++) { if (enemys[i].show) { putAlphaImage(NULL, enemys[i].pos.x, enemys[i].pos.y, &enemys[i].img, 0xffffff); } } } void Input() { MOUSEMSG mouse; while (MouseHit()) { mouse = GetMouseMsg(); switch (mouse.uMsg) { case WM_MOUSEMOVE: plane.pos.x = mouse.x - plane.img.getwidth() / 2; plane.pos.y = mouse.y - plane.img.getheight() / 2; break; case WM_LBUTTONUP: for (int i = 0; i < BulletMaxNum; i++) { if (bullets[i].show == false) { bullets[i].show = true; bullets[i].pos.x = plane.pos.x + plane.img.getwidth() / 2 - bullets[i].img.getwidth() / 2; bullets[i].pos.y = plane.pos.y - plane.img.getheight() / 2 - bullets[i].img.getheight() / 2; break; } } break; } } } void Logic() { for (int i = 0; i < BulletMaxNum; i++) { if (bullets[i].show) { bullets[i].pos.y -= 0.1; if (bullets[i].pos.y <= 0) { bullets[i].pos = { 0,0 }; bullets[i].show = false; } } } for (int j = 0; j < EnemyMaxNum; j++) { if (enemys[j].show) { if (isBumpPlane(&enemys[j], &plane)) { //gameQuit = true; } for (int i = 0; i < BulletMaxNum; i++) { if (bullets[i].show) { if (isBumpBullet(&bullets[i], &enemys[j])) playBoomSound(); } } for (int k = 0; k < EnemyMaxNum; k++) { if (k == j) { break; } if (enemys[k].show) { if (isBumpEnemy(&enemys[j], &enemys[k])) playBoomSound(); } } } } addEnemy(); enemyRandomMove(); } void putAlphaImage(IMAGE* dstimg, int x, int y, IMAGE* srcimg, UINT transparentcolor) { //绘制透明图 HDC dstDC = GetImageHDC(dstimg); HDC srcDC = GetImageHDC(srcimg); int w = srcimg->getwidth(); int h = srcimg->getheight(); // 使用 Windows GDI 函数实现透明位图 TransparentBlt(dstDC, x, y, w, h, srcDC, 0, 0, w, h, transparentcolor); } void enemyRandomMove() { for (int i = 0; i < EnemyMaxNum; i++) { if (enemys[i].show) { enemys[i].endClock = clock(); if (moveDelay(enemys[i].startClock, enemys[i].endClock, enemyMoveDealy)) { enemys[i].startClock = clock(); POINT difPos = { enemys[i].pos.x - plane.pos.x,enemys[i].pos.y - plane.pos.y }; difPos.x > 0 ? enemys[i].pos.x -= enemySpeed : enemys[i].pos.x += enemySpeed; difPos.y > 0 ? enemys[i].pos.y -= enemySpeed : enemys[i].pos.y += enemySpeed; } } } } void addEnemy() { for (int i = 0; i < EnemyMaxNum; i++) { processClock = clock(); //结束增加敌人计时 if (!enemys[i].show) { if ((processClock - startClock) / CLK_TCK >= 2) { //两秒增加一个敌人 srand((unsigned)time(NULL)); POINT pos = { 0,0 }; enemys[i].pos = pos; enemys[i].show = true; startClock = clock(); //增加敌人计时 enemys[i].startClock = clock(); //敌人计时 enemys[i].endClock = clock(); } } } } bool moveDelay(clock_t c1, clock_t c2, int delay) { if (c2 - c1 >= delay) { return true; } return false; } bool isBumpBullet(Bullet* bullet, Enemy* enemy) { if (bullet->pos.y < enemy->pos.y + enemy->img.getheight() && enemy->pos.y < bullet->pos.y + bullet->img.getheight() && enemy->pos.x < bullet->pos.x + bullet->img.getwidth() && bullet->pos.x < enemy->pos.x + enemy->img.getwidth() ) { bullet->show = false; bullet->pos = { -100,-100 }; enemy->show = false; enemy->pos = { -100,-100 }; return true; } return false; } bool isBumpEnemy(Enemy* enemy1, Enemy* enemy2) { if (enemy1->pos.y < enemy2->pos.y + enemy2->img.getheight() && enemy2->pos.y < enemy1->pos.y + enemy1->img.getheight() && enemy2->pos.x < enemy1->pos.x + enemy1->img.getwidth() && enemy1->pos.x < enemy2->pos.x + enemy2->img.getwidth()) { enemy1->show = false; enemy1->pos = { -100,-100 }; enemy2->show = false; enemy2->pos = { -100,-100 }; return true; } return false; } bool isBumpPlane(Enemy* enemy1, Plane* enemy2) { if (enemy1->pos.y < enemy2->pos.y + enemy2->img.getheight() && enemy2->pos.y < enemy1->pos.y + enemy1->img.getheight() && enemy2->pos.x < enemy1->pos.x + enemy1->img.getwidth() && enemy1->pos.x < enemy2->pos.x + enemy2->img.getwidth()) { return true; } return false; } void playBoomSound() { mciSendString(_T("close boomMusic"), NULL, 0, NULL); mciSendString(_T("open data/boom.mp3 alias boomMusic"), NULL, 0, NULL); mciSendString(_T("play boomMusic"), NULL, 0, NULL); } |
游戏素材
一共就四张,背景,飞机,子弹,敌人