当前位置: 代码迷 >> 综合 >> 大一制作C++飞机小游戏,记录一下学习到的东西,希望得到指正与教导
  详细解决方案

大一制作C++飞机小游戏,记录一下学习到的东西,希望得到指正与教导

热度:25   发布时间:2024-02-19 13:44:35.0

/*
字符,小键盘的键码值:上:72;下:80;左:75;右:77;
本质:和asc码一样,给个值。
*/
/*
int main() {
    char key = _getch();
    switch (key) {
    case 'W':
    case 'w':
    case 72:
        break;
    case 's':
    case 'S':
    case 80:
        break;
    case 'a':
    case 'A':
    case 75:
    }
}
#include<mmsystem.h>//多媒体设备接口
#pragma comment(lib,"winmm.lib")
int main() {
    initgraph(800, 640);
    IMAGE bkgrd;
    loadimage(&bkgrd, "背景图01.jpg",800,640);
    putimage(0, 0, &bkgrd);
    mciSendString("open Resonator.mp3 alias BGM",NULL,0,NULL);
    mciSendString(TEXT("play BGM repeat"),NULL,0,NULL);
    getchar();
    closegraph();
}*/
#include<graphics.h>
#include<conio.h>
#include<time.h>
#include<math.h>
#include<mmsystem.h>
#include<stdio.h>
#pragma comment(lib,"winmm.lib")
struct bullet {
public:
    int x;
    int y;
    bool exist;
};
struct enemy {
    int x;
    int y;
    bool alive;
}enemy[20];
struct medicine {
    int x;
    int y;
    bool alive;
}medicine[5];
int main() {
    initgraph(640, 800);
    IMAGE m;
    setbkcolor(WHITE);
    settextcolor(BLACK);
    cleardevice();//初始化
    loadimage(&m, "背景图01.jpg",800,640);
    putimage(0, 0, &m);
    mciSendString("open Resonator.mp3 alias BGM", 0, 0, 0);
    mciSendString("play BGM repeat", 0, 0, 0);//播放音乐
    setfillcolor(BLUE);
    int mode = 0;
    while (mode == 0) {
        setlinecolor(BLACK);
        rectangle(260, 380, 380, 420);
        outtextxy(270, 390, "开 始 游 戏");
        MOUSEMSG mouse;
        mouse = GetMouseMsg();
        if (mouse.x <= 380 && mouse.x >= 260 && mouse.y <= 420 && mouse.y >= 380) {
            rectangle(255, 375, 385, 425);
            switch (mouse.uMsg) {
            case WM_LBUTTONDOWN:mode++;
                break;
            }
        }
        else {
            clearrectangle(255, 375, 385, 425);
        }
    }
    int x, y, mx, my;
    mx = 320;
    my = 700;
    y = my;
    x = mx;
    int score = 0;
    fillcircle(mx, my, 30);
    MOUSEMSG mouse;
    /*while (1) {
    mouse = GetMouseMsg();
    x = mouse.x;
    y = mouse.y;
    clearcircle(mx, my, 50);
    putimage(0, 0, &m);
    fillcircle(x, y, 50);
    mx = x;
    my = y;
    }*/
    int speed = 5;
    int bullet_speed = 6;
    int enemy_speed = 3;
    bullet bullet[20];
    char str[20] = "";
    DWORD t1, t2, t3, t4, t5, t6;
    t1 = t2 = t3 = t4 = t5 = t6 = GetTickCount();
    for (int i = 0; i < 20; i++) bullet[i].exist = 0;//初始化子弹状态
    for (int i = 0; i < 20; i++) enemy[i].alive = 0;//初始化敌人状态
    for (int i = 0; i < 5; i++) medicine[i].alive = 0;//初始化药物状态
    cleardevice();
    setlinecolor(WHITE);
    int life = 3;
    int kaiguan = 1;
    while (kaiguan == 1) {
        //mode 1:开始游戏//
        while (mode == 1) {

            Sleep(10);

            /敌机部分/
            if (t4 - t3 > 200) {
                for (int i = 0; i < 20; i++) {
                    if (enemy[i].alive == 0) {
                        enemy[i].x = rand() % (640 - 20);
                        enemy[i].y = 0;
                        setfillcolor(RED);
                        fillcircle(enemy[i].x, enemy[i].y, 20);
                        enemy[i].alive = 1;
                        break;
                    }
                }t3 = t4;
                setfillcolor(BLUE);
            }
            t4 = GetTickCount();//敌机产生
            for (int i = 0; i < 20; i++) {
                if (enemy[i].alive) {
                    clearcircle(enemy[i].x, enemy[i].y, 20);
                    enemy[i].y += enemy_speed;
                    setfillcolor(RED);
                    fillcircle(enemy[i].x, enemy[i].y, 20);
                }
            }setfillcolor(BLUE);//敌机移动
            for (int i = 0; i < 20; i++) {
                if (enemy[i].alive) {
                    if (enemy[i].y >= 800) {
                        enemy[i].alive = 0;
                    }//敌机飞到最下方消失
                }
            }
            for (int i = 0; i < 20; i++) {
                if (enemy[i].alive) {
                    for (int j = 0; j < 20; j++) {
                        if (bullet[j].exist) {
                            if (sqrt((enemy[i].x - bullet[j].x) * (enemy[i].x - bullet[j].x) + (enemy[i].y - bullet[j].y) * (enemy[i].y - bullet[j].y)) < (20 + 10)) {
                                enemy[i].alive = 0;
                                bullet[j].exist = 0;
                                score += 10;
                                clearcircle(enemy[i].x, enemy[i].y, 20);
                                clearcircle(bullet[j].x, bullet[j].y, 10);
                            }
                        }
                    }
                }
            }//子弹击中敌机消失
药品部分/
                if (t6 - t5 > 30000) {
                    for (int i = 0; i < 5; i++) {
                        if (medicine[i].alive == 0) {
                            medicine[i].x = rand() % (640 - 20);
                            medicine[i].y = 0;
                            setfillcolor(GREEN);
                            fillcircle(medicine[i].x, medicine[i].y, 20);
                            medicine[i].alive = 1;
                            break;
                        }
                    }t5 = t6;
                    setfillcolor(BLUE);
                }
                t6 = GetTickCount();//药品产生
                for (int i = 0; i < 5; i++) {
                    if (medicine[i].alive) {
                        clearcircle(medicine[i].x, medicine[i].y, 20);
                        medicine[i].y += enemy_speed ;
                        setfillcolor(GREEN);
                        fillcircle(medicine[i].x, medicine[i].y, 20);
                    }
                }setfillcolor(BLUE);//药品移动
                for (int i = 0; i < 5; i++) {
                    if (medicine[i].alive) {
                        if (medicine[i].y >= 800) {
                            medicine[i].alive = 0;
                        }//药品飞到最下方消失
                    }
                }
                显示分数以及操作部分/
                settextcolor(BLACK);
                sprintf_s(str, "Score:%d", score);
                outtextxy(550, 20, str);//右上角分数
                if (GetAsyncKeyState(VK_UP)) {
                    y -= speed;
                }
                if (GetAsyncKeyState(VK_DOWN)) {
                    y += speed;
                }
                if (GetAsyncKeyState(VK_LEFT)) {
                    x -= speed;
                }
                if (GetAsyncKeyState(VK_RIGHT)) {
                    x += speed;
                }
                ///子弹部分///
                if (t2 - t1 > 300) {
                    for (int i = 0; i < 20; i++) {
                        if (bullet[i].exist == 0) {
                            bullet[i].x = x;
                            bullet[i].y = y;
                            fillcircle(bullet[i].x, bullet[i].y, 10);
                            bullet[i].exist = 1;
                            break;
                        }
                    }t1 = t2;
                }//间隔时间,发射子弹
                t2 = GetTickCount();
                for (int i = 0; i < 20; i++) {
                    if (bullet[i].exist) {
                        clearcircle(bullet[i].x, bullet[i].y, 10);
                        bullet[i].y -= bullet_speed;
                        fillcircle(bullet[i].x, bullet[i].y, 10);

                    }
                }//子弹移动
                for (int i = 0; i < 20; i++) {
                    if (bullet[i].y <= 0) {
                        bullet[i].exist = 0;
                        clearcircle(bullet[i].x, bullet[i].y, 10);
                    }
                }//子弹触底
人物移动///
                clearcircle(mx, my, 20);
                fillcircle(x, y, 20);
                mx = x;
                my = y;
//生命管理///
                for (int i = 0; i < 20; i++) {
                    int count = 1;
                    if (enemy[i].alive) {
                        if (sqrt((enemy[i].x - x) * (enemy[i].x - x) + (enemy[i].y - y) * (enemy[i].y - y)) <= (30 + 20)) {
                            life--;
                            for (int j = 0; j < 20; j++)enemy[j].alive = 0;
                            cleardevice();
                            count = 0;
                        }
                    }
                    if (count == 0) break;
                }//与敌机相撞减一生命
                for (int i = 0; i < 5; i++) {
                    if (medicine[i].alive) {
                        if (sqrt((medicine[i].x - x) * (medicine[i].x - x) + (medicine[i].y - y) * (medicine[i].y - y)) <= (30 + 20)) {
                            life++;
                            medicine[i].alive = 0;
                            clearcircle(medicine[i].x, medicine[i].y, 20);
                            if (life > 3) life = 3;
                        }
                    }
                }//吃到药回复1点生命,最多3点。
                if (life == 0) mode++;
                char str1[20] = "";
                sprintf_s(str1, "life:%d", life);
                outtextxy(550, 40, str1);
        }
        cleardevice();
        while (mode == 2) {
                outtextxy(310, 400, str);
                outtextxy(300, 420, "Congratulations!");
                outtextxy(290, 440, "PRESS 'R' to Play again");
                outtextxy(290, 460, "PRESS 'E' to exit");
                char choice = _getch();
                switch (choice) {
                case 'r':
                case 'R':
                    mode = 1;
                    score = 0;
                    life = 3;
                    break;
                case 'E':
                case 'e':
                    kaiguan = 0;
                    return 0;
                    break;
                }
        }
            cleardevice();
    }
        closegraph();
}

 

  相关解决方案