赞
踩
声明:本文大部分内容参考up主进行编写(C语言飞机大战)
并对他没有讲解到的进行延续,以及本人编写时遇到的诸多问题。
1.准备飞机大战所需素材,并预装esayx第三方图形库。
2.游戏窗口创建及游戏角色的的创建
3.游戏角色的移动
4.对游戏角色进行移动边界限制
5.玩家飞机子弹的的发射与移动
6.大量敌机的绘制与移动
7.定时器的设置
8.背景音乐及得分面板的输出
所需函数
编程过程中遇到的一些棘手问题:
附源代码(底层)
- #include<stdio.h>
- #include<conio.h>
- #include<Windows.h>
- //多媒体接口
- #include<mmsystem.h>
- //导入静态库
- #pragma comment(lib,"winmm.lib")
- #include<time.h>
- //图形库,预装第三方库 easyX
- #include<graphics.h>
- enum My
- {
- WIDTH = 480, //窗口高度
- HEIGHT = 700,
- BULLLET_NUM = 15,//子弹数量
- ENEMY_NUM = 10, //敌机数量
- BIG, //敌机类型
- SMALL
-
- };
- //飞机结构
- struct Plane
- {
- int x;
- int y;
- int live; //是否存活
- int width;
- int height;
- int hp;
- int type; //敌机类型
- }player , bull[BULLLET_NUM] , enemy[ENEMY_NUM];
- //保存背景图片
- IMAGE bk;
- //保存玩家图片
- IMAGE img_palyer[2];
- //保存子弹图片
- IMAGE img_bull[2];
- //保存敌机图片
- IMAGE img_enemy[2][2];
- //得分板
- int score = 0;
- //控制音乐的全局变量
- short static
- shortCommandRunCounterr = 0;
- //音乐文件管理
- void openMusic()
- {
- //背景音效
- mciSendString(LPCSTR("open ./music/game_music.mp3 alias BGM"), 0, 0, 0);
- //子弹音效
- mciSendString(LPCSTR("open ./music/bullet.mp3 alias blow"), 0, 0, 0);
- //撞击音效
- mciSendString(LPCSTR("open ./music/enemy3_down.mp3 alias collide"), 0, 0, 0);
- //击中音效
- mciSendString(LPCSTR("open ./music/get_bomb.mp3 alias hit"), 0, 0, 0);
- }
- //加载游戏背景图片和音乐
- void loadpicture()
- {
- //加载背景图
- loadimage(&bk , _T("./images/background.png"));
- //加载玩家
- loadimage(&img_palyer[0], _T("./images/me1.jpg"));
- loadimage(&img_palyer[1], _T("./images/me2.jpg"));
- //加载子弹
- loadimage(&img_bull[0], _T("./images/bullet1.jpg"));
- loadimage(&img_bull[1], _T("./images/bullet2.jpg"));
- //加载敌机
- loadimage(&img_enemy[0][0], _T("./images/enemy1.jpg"));
- loadimage(&img_enemy[0][1], _T("./images/enemy2.jpg"));
- loadimage(&img_enemy[1][0], _T("./images/enemy3_n1.jpg"));
- loadimage(&img_enemy[1][1], _T("./images/enemy3_n2.jpg"));
- //加载音乐
- openMusic();
- if (shortCommandRunCounterr == 0)
- {
- shortCommandRunCounterr++;
- mciSendString(LPCSTR("play BGM repeat"), 0, 0, 0); //循环播放
- }
- }
- //敌机类型初始化
- void enemyHp(int i)
- {
- if (rand() % 10 == 0)
- {
- enemy[i].type = BIG;
- enemy[i].hp = 3;
- enemy[i].width = 169;
- enemy[i].height = 258;
- }
- else
- {
- enemy[i].type = SMALL;
- enemy[i].hp = 1;
- enemy[i].width = 57;
- enemy[i].height = 43;
- }
- }
- void gameInit()
- {
- player.x = WIDTH / 2;
- player.y = HEIGHT - 120;
- player.live = true;
- //初始化子弹
- for (int i = 0 ; i < BULLLET_NUM ; i++)
- {
- bull[i].x = 0;
- bull[i].y = 0;
- bull[i].live = false;
- }
- //初始化敌机
- for (int i = 0; i < ENEMY_NUM; i++)
- {
- enemy[i].live = false;
- enemyHp(i);
- }
- }
- //游戏绘制函数
- void gameDraw()
- {
- loadpicture();
- //贴背景
- putimage(0, 0, &bk);
- //贴玩家
- putimage(player.x , player.y, &img_palyer[0] , NOTSRCERASE);
- putimage(player.x , player.y, &img_palyer[1] , SRCINVERT);
- //贴子弹
- for (int i = 0; i < BULLLET_NUM; i++)
- {
- if (bull[i].live)
- {
- putimage(bull[i].x, bull[i].y, &img_bull[0], NOTSRCERASE);
- putimage(bull[i].x, bull[i].y, &img_bull[1], SRCINVERT);
-
- }
- }
- //贴敌机
- for (int i = 0; i < ENEMY_NUM; i++)
- {
- if (enemy[i].live)
- {
- if (enemy[i].type == BIG)
- {
- putimage(enemy[i].x, enemy[i].y, &img_enemy[1][0], NOTSRCERASE);
- putimage(enemy[i].x, enemy[i].y, &img_enemy[1][1], SRCINVERT);
- }
- else
- {
- putimage(enemy[i].x, enemy[i].y, &img_enemy[0][0], NOTSRCERASE);
- putimage(enemy[i].x, enemy[i].y, &img_enemy[0][1], SRCINVERT);
- }
- }
- }
- //绘制得分板
- outtextxy(WIDTH * 0.5, HEIGHT * 0.9, _T("得分:"));
- TCHAR s[5];
- _stprintf_s(s, _T("%d"), score);
- outtextxy(WIDTH * 0.6, HEIGHT * 0.9, s);
- }
- //创建子弹
- void createBullet()
- {
- for (int i = 0; i < BULLLET_NUM; i++)
- {
- if (!bull[i].live)
- {
- bull[i].x = player.x+50;
- bull[i].y = player.y;
- bull[i].live = true;
- break;
- }
- }
- }
- //子弹移动
- void bulletMove(int speed)
- {
- for (int i = 0; i < BULLLET_NUM; i++)
- {
- if (bull[i].live)
- {
- bull[i].y -= speed;
- if (bull[i].y < 0)
- {
- bull[i].live = false;
- }
- }
- }
- }
- //使用定时器让敌机均匀下落
- bool timer(int ms, int id)
- {
- static DWORD t[10];
- if (clock() - t[id] > ms)
- {
- t[id] = clock();
- return true;
- }
- return false;
- }
- //角色移动 && 子弹移动
- void playerMove(int speed)
- {
- //使用windows函数 GetAsyncKeyState
- if (GetAsyncKeyState(VK_UP) || GetAsyncKeyState('W'))
- {
- if(player.y > 0 )
- player.y -= speed;
- }
- if (GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState('S'))
- {
- if (player.y+60 < HEIGHT)
- player.y += speed;
- }
- if (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('A'))
- {
- if (player.x+50 > 0)
- player.x -= speed;
- }
- if (GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('D'))
- {
- if (player.x+50 < WIDTH)
- player.x += speed;
- }
- //使用封装函数定时器解决子弹移动
- if (GetAsyncKeyState(VK_SPACE) && timer(200 , 1))
- {
- //调用函数创建一个子弹
- createBullet();
- //播放子弹音效
- mciSendString(LPCSTR("close blow"), 0, 0, 0);
- openMusic();
- mciSendString(LPCSTR("play blow"), 0, 0, 0);
- }
-
- }
- //产生敌机
- void createEnemy()
- {
- for (int i = 0; i < ENEMY_NUM; i++)
- {
- if (!enemy[i].live)
- {
- enemy[i].live = true;
- enemy[i].x = rand() % (WIDTH / 2);
- enemy[i].y = 0;
- enemyHp(i);
- break;
- }
- }
- }
- //敌机的移动
- void enemyMove(int speed)
- {
- for (int i = 0; i < ENEMY_NUM; i++)
- {
- if (enemy[i].live)
- {
- enemy[i].y += speed;
- if (enemy[i].y > HEIGHT)
- {
- enemy[i].live = false;
- }
- }
- }
- }
- //打飞机检测
- void playPlance()
- {
- for (int i = 0; i < ENEMY_NUM; i++)
- {
- if (!enemy[i].live)
- continue;
- for (int k = 0; k < ENEMY_NUM; k++)
- {
- if (!bull[k].live)
- continue;
- //子弹碰撞检测
- if (bull[k].x > enemy[i].x && bull[k].x < enemy[i].x + enemy[i].width
- && bull[k].y>enemy[i].y && bull[k].y < enemy[i].y + enemy[i].height)
- {
- score++;
- //播放击中音效
- mciSendString(LPCSTR("close hit"), 0, 0, 0);
- openMusic();
- mciSendString(LPCSTR("play hit"), 0, 0, 0);
- bull[k].live = false;
- enemy[i].hp--;
- }
- }
- //飞机碰撞检测
- if (player.x > enemy[i].x && player.x < enemy[i].x + enemy[i].width
- && player.y>enemy[i].y && player.y < enemy[i].y + enemy[i].height)
- {
- player.live = false;
- //播放碰撞音效
- mciSendString(LPCSTR("close collide"), 0, 0, 0);
- openMusic();
- mciSendString(LPCSTR("play collide"), 0, 0, 0);
- }
- if (enemy[i].hp <= 0 || !player.live)
- {
- enemy[i].live = false;
- }
- }
- }
-
- //主函数
- int main()
- {
- //创建一个窗口
- initgraph(WIDTH , HEIGHT , SHOWCONSOLE);
- gameInit();
- //双缓冲绘图
- BeginBatchDraw();
- while (1)
- {
- gameDraw();
- playerMove(2);
- bulletMove(4);
- //使用定时器让敌机均匀下落
- if (timer(200 , 0))
- {
- createEnemy();
- }
- if (timer(5, 2))
- {
- enemyMove(1);
- }
- playPlance();
- FlushBatchDraw();
- }
- EndBatchDraw();
- return 0;
- }

可根据需要进行修改。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。