打飞蛋(c/c++)

打飞蛋!!!!!!!!!!!!  哈哈哈哈哈哈哈哈!!!!!!!!!!!!!!

运行环境:vs2019

效果图:

打飞蛋(c/c++)

 

 代码:

#include <graphics.h>
#include <stdio.h>
#include <tchar.h>

#define LENGTH 20
#define MAX_PLAN 1

struct Bullet
{
    int x;
    int y;
    int r;
    int vy;
    int flag;
};

struct Plan
{
    int left;
    int top;
    int right;
    int bottom;
    int vy;
    int flag;
};

void createPlan(Plan plans[]);

void movePlan(Plan plans[]);

void moveBat(Bullet bts[]);

void createBat(Bullet bts[], int x, int y, int r);

void initBullet(Bullet bts[]);

void initPlan(Plan plans[]);

void isHitPlan(Plan plans[], Bullet bts[], int& score);

void showScore(const int& score);

void char2TCHAR(const char* _char, TCHAR* tchar);

void initWin(Bullet  bts[], Plan  plans[], int left, int top, int right, int bottom);

void moveBattery(int& left, int top, int& right, int bottom, int vx, Bullet  bts[]);

int isGameOver(Plan  plans[1], int top, int left, int right);

void isStart(int& start);

int main()
{
    int left = 10, top = 570, right = 40, bottom = 600;
    int vx = 30, score = 0, start = 0;
    Bullet bts[LENGTH];
    Plan plans[MAX_PLAN];
    initWin(bts, plans, left, top, right, bottom);
    outtextxy(150, 300, _T("点击鼠标左键开始"));
    BeginBatchDraw();
    while (1)
    {
        FlushBatchDraw();
        if (start == 0)
        {
            initBullet(bts);
            initPlan(plans);
            isStart(start);
            score = 0;
            cleardevice();
            continue;
        }
        if (MouseHit())
            moveBattery(left, top, right, bottom, vx, bts);
        Sleep(1);
        isHitPlan(plans, bts, score);
        moveBat(bts);
        movePlan(plans);
        setfillcolor(RED);
        solidrectangle(left, top, right, bottom);
        createPlan(plans);
        showScore(score);
        start = isGameOver(plans, top, left, right);
    }
    EndBatchDraw();
    getchar();
    return 0;
}

void isStart(int& start)
{
    while (1)
    {
        if (MouseHit())
        {
            MOUSEMSG msg = GetMouseMsg();
            if (msg.uMsg == WM_LBUTTONDOWN)
            {
                clearrectangle(0, 100, 400, 350);
                start = 1;
            }
        }
        if (start == 1) break;
    }
}

int isGameOver(Plan  plans[1], int top, int left, int right)
{
    int i;
    int start = 1;
    for (i = 0; i < MAX_PLAN; i++)
        if ((plans[i].bottom >= top && plans[i].left <= left && plans[i].right >= left) ||
            (plans[i].bottom >= top && plans[i].left <= right && plans[i].right >= right))
        {
            start = 0;
            outtextxy(150, 270, _T("游戏结束"));
            outtextxy(100, 300, _T("点击鼠标左键重新开始"));
        }
    return start;
}

// 移动炮台
void moveBattery(int& left, int top, int& right, int bottom, int vx, Bullet  bts[])
{
    MOUSEMSG msg = GetMouseMsg();
    clearrectangle(left, top, right, bottom);
    left = msg.x;
    right = msg.x + vx;
    if (right >= 400) 
    {
        left = 370;
        right = 400;
    }
    if (left < 0)
    {
        left = 0;
        right = 30;
    }
    if (msg.uMsg == WM_LBUTTONDOWN)
        createBat(bts, left + 15, top - 10, 8);
}

// 初始化窗口
void initWin(Bullet  bts[], Plan  plans[], int left, int top, int right, int bottom)
{
    initBullet(bts);
    initPlan(plans);
    initgraph(400, 600);
    setbkcolor(BLUE);
    cleardevice();
    setfillcolor(RED);
    solidrectangle(left, top, right, bottom);
}

// 初始化子弹
void initBullet(Bullet bts[])
{
    int i;
    for (i = 0; i < LENGTH; i++)
    {
        bts[i].x = 0;
        bts[i].y = 0;
        bts[i].r = 8;
        bts[i].vy = 1;
        bts[i].flag = 0;
    }
}

// 新建一个子弹
void createBat(Bullet bts[], int x, int y, int r)
{
    int i;
    for (i = 0; i < LENGTH; i++)
    {
        if (bts[i].flag == 0)
        {
            bts[i].x = x;
            bts[i].y = y;
            bts[i].flag = 1;
            setfillcolor(GREEN);
            solidcircle(x, y, r);
            break;
        }
    }
}

// 新建一个敌机
void createPlan(Plan plans[])
{
    int i, count = 0, maxNum = rand() % (MAX_PLAN + 1);
    for (i = 0; i < MAX_PLAN && count < maxNum; i++)
    {
        if (plans[i].flag == 0) 
        {
            plans[i].left = rand() % 370;
            plans[i].right = plans[i].left + 30;
            setfillcolor(YELLOW);
            solidellipse(plans[i].left,  plans[i].top, plans[i].right, plans[i].bottom);
            plans[i].flag = 1;
            count++;
            break;
        }
    }
}

// 移动敌机
void movePlan(Plan plans[])
{
    int i;
    for (i = 0; i < MAX_PLAN; i++)
    {
        if (plans[i].flag == 1)
        {
            setfillcolor(YELLOW);
            clearellipse(plans[i].left, plans[i].top, plans[i].right, plans[i].bottom);
            plans[i].top += plans[i].vy;
            plans[i].bottom += plans[i].vy;
            solidellipse(plans[i].left, plans[i].top, plans[i].right, plans[i].bottom);
            if (plans[i].top >= 600)
            {
                plans[i].flag = 0;
                plans[i].top = 0;
                plans[i].bottom = 20;
                clearellipse(plans[i].left, plans[i].top, plans[i].right, plans[i].bottom);
            }
        }
    }
}

// 移动炮弹
void moveBat(Bullet bts[])
{
    int i;
    for (i = 0; i < LENGTH; i++)
    {
        if (bts[i].flag == 1)
        {
            clearcircle(bts[i].x, bts[i].y, bts[i].r);
            bts[i].y -= bts[i].vy;
            setfillcolor(GREEN);
            solidcircle(bts[i].x, bts[i].y, bts[i].r);
            if (bts[i].y <= 0)
            {
                clearcircle(bts[i].x, bts[i].y, bts[i].r);
                bts[i].flag = 0;
            }
        }
    }
}

// 初始化敌机数据
void initPlan(Plan plans[])
{
    int i;
    for (i = 0; i < MAX_PLAN; i++)
    {
        plans[i].bottom = 20;
        plans[i].flag = 0;
        plans[i].left = 0;
        plans[i].top = 1;
        plans[i].right = 0;
        plans[i].vy = 1;
    }
}

// 命中敌机
void isHitPlan(Plan plans[], Bullet bts[], int& score)
{
    int i, j;
    for (i = 0; i < MAX_PLAN; i++)
    {
        for (j = 0; j < LENGTH; j++)
        {
            if (plans[i].bottom >= bts[j].y && ((bts[j].x >= plans[i].left && bts[j].x <= plans[i].right) ||
                ((bts[j].x - bts[j].r) >= plans[i].left && (bts[j].x + bts[j].r) <= plans[i].right) ||
                ((bts[j].x - bts[j].r) >= plans[i].left && bts[j].x <= plans[i].right)))
            {
                score++;
                clearcircle(bts[j].x, bts[j].y, bts[j].r);
                clearellipse(plans[i].left, plans[i].top, plans[i].right, plans[i].bottom);
                plans[i].bottom = 20;
                plans[i].flag = 0;
                plans[i].left = 0;
                plans[i].top = 1;
                plans[i].right = 0;
                plans[i].vy = 1;
                bts[j].x = 0;
                bts[j].y = 0;
                bts[j].r = 8;
                bts[j].vy = 1;
                bts[j].flag = 0;
                break;
            }
        }
    }
}

void showScore(const int& score) 
{
    char s[10];
    TCHAR ss[10];
    sprintf(s, "成绩:%d", score);
    char2TCHAR(s, ss);
    outtextxy(15, 30, ss);
}

// char转TCHAR
void char2TCHAR(const char* _char, TCHAR* tchar)
{
    int iLength;
    iLength = MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, NULL, 0);
    MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, tchar, iLength);
}

 

原文链接: https://www.cnblogs.com/li1234567980/p/13057649.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    打飞蛋(c/c++)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/353306

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午7:59
下一篇 2023年3月2日 上午8:00

相关推荐