飞机大战

此作业的要求:[https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11577]

一、功能和截图

GUI版本:

 

 
玩家操作说明:通过键盘的“w”向上移动,“s”向下移动,“a”向左移动,“d”向右移动,“ ”(空格)来发射子弹。击中敌机得一分,五分敌机速度提高一档,共4档。飞机共有5血量,敌机撞击一次减少一滴血,5滴血消耗完后游戏结束。
二、代码

#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#pragma comment(lib,"Winmm.lib")
#define Width  400
#define High 700
IMAGE img_bk;
IMAGE im_planeNormal_1, im_planeNormal_2;
IMAGE im_bullet1, im_bullet2;
IMAGE im_enemyPlane1, im_enemyPlane2;
IMAGE img_planeExplode1, img_planeExplode2;
int position_x; //飞机的初始位置 
int position_y; //飞机的初始位置
int bullet_x, bullet_y; //子弹的位置 
int em_x, em_y;         //敌机位置 
int speed;               //敌机下落速度
int sum=0;                //得分
int isBoo=0;            //飞机是否爆炸
int HP = 5;                //血量
char tmp;


void starup() {                    //初始化数据 
    
    sum = 0;
    speed = 10;
    position_x = Width / 2;
    position_y = High / 2;
    bullet_x =0;
    bullet_y =-50;
    em_y = 0;
    em_x = 0;
    initgraph(Width, High);

    loadimage(&im_enemyPlane1, L"D:\\c\\enemyPlane1.jpg");
    loadimage(&im_enemyPlane2, L"D:\\c\\enemyPlane2.jpg");
    loadimage(&im_bullet1, L"D:\\c\\bullet2.jpg");
    loadimage(&im_bullet2, L"D:\\c\\bullet1.jpg");
    loadimage(&img_bk,L"D:\\c\\background.jpg");
    loadimage(&im_planeNormal_1, L"D:\\c\\planeNormal_1.jpg");
    loadimage(&im_planeNormal_2, L"D:\\c\\planeNormal_2.jpg");
    loadimage(&img_planeExplode1, L"D:\\c\\planeExplode_1.jpg");
    loadimage(&img_planeExplode2, L"D:\\c\\planeExplode_2.jpg");


    BeginBatchDraw();
}

void show() {                    //显示页面
    putimage(0,0,&img_bk);
    if (isBoo == 0) {
        putimage(bullet_x, bullet_y, &im_bullet1, SRCINVERT);
        putimage(bullet_x, bullet_y, &im_bullet2, NOTSRCERASE);
        putimage(position_x - 50, position_y - 60, &im_planeNormal_1, NOTSRCERASE);
        putimage(position_x - 50, position_y - 60, &im_planeNormal_2, SRCINVERT);
        putimage(em_x, em_y, &im_enemyPlane1, NOTSRCERASE);
        putimage(em_x, em_y, &im_enemyPlane2, SRCINVERT);
    }
    else {
        putimage(position_x - 50, position_y - 60, &img_planeExplode1, NOTSRCERASE); // 显示爆炸飞机    
        putimage(position_x - 50, position_y - 60, &img_planeExplode2, SRCINVERT);
} outtextxy(Width*0.001 , High*0.95 , L"击中敌机得一分,集五分敌机提速共4档,您共有5个血量!"); char s[5]; sprintf_s(s, "%d", sum); //outtextxy(Width*0.3, High * 0.95,s);  FlushBatchDraw(); } void updataWhithOutInput() { //与用户输入无关的数据更新 //子弹上升的速度 if (bullet_y > -50) { bullet_y -= 2; } //控制敌机的下落速度  static int em_speed = 0; if (em_speed < speed) { em_speed++; } if (em_speed == speed) { if (em_y > High) { em_x = rand() % Width; em_y = -1; } else { em_y++; em_speed = 0; } } //当子弹击中敌机 if (abs(em_y - bullet_y) + abs(em_x - bullet_x) < 80) { sum++; if (sum % 5 == 0) { if (speed > 6) { speed -= 5; } else if (speed != 0) { speed -= 2; } else { speed = 1; } } em_y = -10; em_x = rand() % Width-10; bullet_x = 0; bullet_y = -50; } //当敌机与我机相撞 if (abs(em_y - position_y) + abs(em_x - bullet_x) < 200) { //isBoo = 1; if (HP <= 0) { isBoo = 1; //outtextxy(Width , High, L"游戏结束!点击b键继续,任意键退出。"); /* tmp = _getch(); if (tmp == 'b') { isBoo = 0; HP = 5; }*/ } else { HP--; //printf("剩余血量:%d,点击任意键重新开始。", HP); em_y = -10; em_x = rand() % Width-10; isBoo = 0; } } } void updataWhithInput() { //用户输入的更新 char input; if (_kbhit()) { //检测键盘输入  input = _getch(); if (input == 's') { if (position_y < High - 1) { position_y += 5; } } if (input == 'w') { if (position_y > 0) { position_y-=5; } } if (input == 'd') { if (position_x < Width - 1) { position_x += 5; } } if (input == 'a') { if (position_x > 0) { position_x-=5; } } if (input == ' ') { bullet_x = position_x - 1; bullet_y = position_y; mciSendString(L"close fgmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭 mciSendString(L"open D:\\c\\f_gun.mp3 alias fgmusic", NULL, 0, NULL); // 打开音乐 mciSendString(L"play fgmusic", NULL, 0, NULL); // 仅播放一次  } } } int main(int argc, char* argv[]) { starup(); //数据的初始化  while (1) { show(); //显示页面 updataWhithOutInput(); //与用户输入无关的数据更新  updataWhithInput(); } closegraph(); return 0; }

三、PSP:

学习

观看童晶老师视频函数封装飞机游戏

11.29 14:35

11.2914:49

0

14min

编程

模仿童晶老师程序,然后添加得分,飞机遇到图形边界,子弹打中敌机。

11.2915:04

11.2917:32

0

148min

编程

飞机小游戏添加敌机撞到飞机,暂停、继续,HP血量

11.29 19:40

1.29 20:51

0

71min

学习

观看童晶老师视频P37学习使用EasyX

11.30 16:03

11.30 16:35

 0

 32min

编程

熟悉使用Easy X,初步对飞机大战GUI进行熟悉。

11.30 16:50

11.30 17:55

8min

57min

编程

对飞机大战进行GUI设计

11.30 21:00

11.30 22:32

0min

92min

 

posted @ 2020-11-30 22:46  松鼠and龙猫  阅读(105)  评论(0编辑  收藏  举报