简单吃球游戏

运用easyx实现的超简单作品

新建一个cpp文件输入即可

#include<graphics.h>
#include <tchar.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
//RGB()是设置三原色
#define WIDTH 1024
#define HEIGHT 576
#define MAPW (WIDTH*4)
#define MAPH (HEIGHT*4)
#define AINUM 200
#define FODNUM 2000
#define DISTANCE(x1,y1,x2,y2) (sqrt((float)(x1-x2)*(float)(x1-x2)+(float)(y1-y2)*(float)(y1-y2)))

struct food {
    bool eatistrue;
    int x, y;
    char style;
    COLORREF color;
}fod[FODNUM];
struct BALL {
    bool life;
    COLORREF color;
    int x, y;
    float r;
};
BALL mover = { 1,RGB(0,0,0),0,0,0 };
BALL ai[AINUM] = { 1,RGB(0,0,0),0,0,0 };
DWORD* pBUffer;//显存指针
int lx = -20, ly = MAPH + 20, rx = MAPW + 20, ry = -20;
int relx = -20, rely = MAPH + 20, rerx = MAPW + 20, rery = -20;
float asp = 1, Time = 0;int eaten = 0, aieaten = 0;

void start() {
    setbkcolor(WHITE);
    cleardevice();
    settextcolor(RED);
    setbkmode(TRANSPARENT);
    settextstyle(128, 0, _T("宋体"));
    outtextxy(100, 40, L"吃球游戏");
    settextstyle(32, 0, L"楷体");
    outtextxy(384, 500, L"按任意键开始");
    _getch();
}
//设置初始动态
void init() {
    srand((unsigned int)time(NULL));
    mover.color = RGB(rand() % 256, rand() % 256, rand() % 256);
    mover.life = 1;
    mover.x = int(WIDTH*0.5);//固定玩家视野为屏幕中心;
    mover.y = int(HEIGHT*0.5);
    mover.r = 20;

    for (int i = 0; i < AINUM; i++) {
        ai[i].color= RGB(rand() % 256, rand() % 256, rand() % 256);
        ai[i].life = 1;
        ai[i].x = rand() % (MAPW - int(ai[i].r + 0.5)) + int(ai[i].r + 0.5);
        ai[i].y = rand() % (MAPH - int(ai[i].r + 0.5)) + int(ai[i].r + 0.5);
        ai[i].r = float(rand() % 10 + 10);
    }
    for (int i = 0; i < FODNUM; i++) {
        fod[i].eatistrue = 1;
        fod[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
        fod[i].x = rand() % MAPW;
        fod[i].y = rand() % MAPH;
        fod[i].style = rand() % 10 + 1;
    }
    pBUffer = GetImageBuffer(NULL);
    setbkcolor(WHITE);
    cleardevice();
    settextcolor(LIGHTRED);
    setbkmode(TRANSPARENT);
    settextstyle(16, 0, _T("宋体"));
}
void AI() {
    for (int i = 0; i < AINUM; i++) {
        if (ai[i].r > mover.r) {
            if (DISTANCE(mover.x, mover.y, ai[i].x, ai[i].y) < (2 / 3.0*ai[i].r + mover.r)) {
                ai[i].r += (mover.r*mover.r) / ai[i].r;
                mover.life = 0;
                mover.r = 0;
            }
        }
        for (int j = 0; j < AINUM; j++) {
            if (ai[i].r > ai[j].r) {
                if (ai[j].life == 0)continue;
                if (DISTANCE(ai[i].x, ai[i].y, ai[j].x, ai[j].y) < (4 / 5.0*ai[i].r + ai[j].r)) {
                    ai[i].r += (ai[j].r*ai[j].r) / ai[i].r;
                    ai[j].life = 0;
                    aieaten++;
                }
            }
        }
        double min_DISTANCE = 100000;
        int min = -1;
        //AI接触
        for (int k = 0; k < AINUM; k++) {
            if (ai[i].r > ai[k].r&&ai[k].life == 1) {
                if (DISTANCE(ai[i].x, ai[k].y, ai[i].y, ai[k].y) < min_DISTANCE) {
                    min_DISTANCE = DISTANCE(ai[i].x, ai[k].x, ai[i].y, ai[k].y);
                    min = k;
                }
            }
        }
        if ((min != -1) && (rand() % 2 == 1)) { if (rand() % 2) { if (ai[i].x < ai[min].x)ai[i].x += 2; else ai[i].x--; } else if (ai[i].y < ai[min].y)ai[i].y += 2; else ai[i].y -= 2; }
        for (int n = 0; n < FODNUM; n++) {
            if (fod[n].eatistrue == 0)continue;
            if (DISTANCE(ai[i].x, ai[i].y, fod[n].x, fod[n].y) < ai[i].r)
            {
                ai[i].r += 4 / ai[i].r;
                fod[n].eatistrue = 0;
            }
        }
    }
}
void move(BALL* ball) {
    if (ball->r <= 0) ball->life = 0;
    if (ball->life == 0) {
        HWND hwnd = GetHWnd();
        MessageBox(hwnd, L"您已经死亡",0,MB_OK|MB_ICONEXCLAMATION);
        closegraph();
        exit(0);
    }
    if (ball->x > (MAPW - ball->r) || ball->x - ball->r < 0 || ball->y - ball->r<0 || ball->y>(MAPH - ball->r))
    {
        ball->r -= 0.1f;
    }
    //玩家操作
    for (int i = 0; i < AINUM; i++) {
        if(ball->r >= ai[i].r){
           if (0 == ai[i].life) continue;
           if (DISTANCE(ball->x, ball->y, ai[i].x, ai[i].y) < (4 / 5.0*(ball->r + ai[i].r))) {
               ai[i].life = 0;
               ball->r += (ai[i].r*ai[i].r / 2) / ball->r;
               eaten++;
          }
        }
    }
    for (int i = 0; i < FODNUM; i++) {
        if (fod[i].eatistrue == 0) continue;
            if (DISTANCE(float(ball->x), (float)ball->y, float(fod[i].x),float(fod[i].y)) < ball->r) {
                fod[i].eatistrue = 0;
                ball->r += 4/ ball->r;
            }
    }

    static int dx = 0, dy = 0;
    //获取位置偏移量
    if (GetAsyncKeyState(65) & 0x8000) { ball->x -= 3, dx += 3;}
    if (GetAsyncKeyState(87) & 0x8000) { ball->y -= 3, dy += 3;}
    if (GetAsyncKeyState(83) & 0x8000) { ball->y += 3, dy -= 3;}
    if (GetAsyncKeyState(68) & 0x8000) { ball->x += 3, dx -= 3;}
    setorigin(dx, dy);
}
void draw() {
    clearcliprgn();
    setlinestyle(PS_SOLID | PS_JOIN_BEVEL, 20);
    setlinecolor(RGB(0, 100, 0));
    line(relx, rely, relx, rery);
    line(relx, rely, rerx, rely);
    line(relx, rery, rerx, rery);
    line(rerx, rery, rerx, rely);
    setfillcolor(GREEN);

    if (mover.x - 0.5*WIDTH / asp < relx)  far floodfill(relx-11 , mover.y, RGB(0,100,0));
    if (mover.x + 0.5*WIDTH / asp > rerx) far floodfill(rerx +11, mover.y, RGB(0,100,0));
    if (mover.y - 0.5*HEIGHT / asp < rery) far floodfill(mover.x, rery+11, RGB(0,100,0));
    if (mover.y + 0.5*HEIGHT / asp > relx) far floodfill(mover.x, rely-11, RGB(0,100,0));

    setlinecolor(WHITE);
    setlinestyle(PS_NULL);
    for (int i = 0; i < FODNUM; i++) {
        if (fod[i].eatistrue == 0) continue;
        setfillcolor(fod[i].color);
        switch (fod[i].style) {
        case 1:solidellipse(fod[i].x, fod[i].y, fod[i].x + 2, fod[i].y + 4); break;
        case 2:solidellipse(fod[i].x, fod[i].y, fod[i].x + 4, fod[i].y + 2); break;
        case 3:solidrectangle(fod[i].x, fod[i].y, fod[i].x + 4, fod[i].y + 2); break;
        case 4:solidrectangle(fod[i].x, fod[i].y, fod[i].x + 2, fod[i].y + 4); break;
        case 5:solidroundrect(fod[i].x, fod[i].y, fod[i].x + 2, fod[i].y + 4,2,2); break;
        case 6:solidroundrect(fod[i].x, fod[i].y, fod[i].x + 4, fod[i].y + 2,2,2); break;
        case 7:solidroundrect(fod[i].x, fod[i].y, fod[i].x + 4, fod[i].y + 2,4,2); break;
        case 8:solidroundrect(fod[i].x, fod[i].y, fod[i].x + 4, fod[i].y + 2,2,4); break;
        case 9:solidroundrect(fod[i].x, fod[i].y, fod[i].x + 4, fod[i].y + 2,1,1); break;
        case 10:fillcircle(fod[i].x, fod[i].y, 4);
        }
    }
    for (int i = 0; i < AINUM; i++) {
        if (ai[i].life == 0) continue;
        setfillcolor(ai[i].color);
        fillcircle(ai[i].x, ai[i].y, (int)(ai[i].r + 0.5));//加0.5实现四舍五入
    }
    setfillcolor(mover.color);
    fillcircle(mover.x, mover.y, int(mover.r + 0.5));
    outtextxy(0, 0, eaten);
    //重构小地图
    IMAGE  map(150, 100);
    SetWorkingImage(&map);
    setbkcolor(RGB(120, 165, 209));
    cleardevice();
    for (int i = 0; i < AINUM; i++) {
        if (ai[i].life == 0)continue;
        setfillcolor(ai[i].color);
        fillcircle(ai[i].x * 150 / WIDTH / 4, ai[i].y * 100 / HEIGHT / 4, int(ai[i].r / 28 + 0.5));
    }
    setfillcolor(mover.color);
    fillcircle(mover.x * 150 / WIDTH / 4, mover.y * 100 / HEIGHT / 4, int(mover.r / 28 + 0.5));
    SetWorkingImage();//恢复制图
    putimage(mover.x + int(0.5*WIDTH) - 150, mover.y - int(0.5*HEIGHT),150,100,&map,0,0);
}
int main()
{
    initgraph(WIDTH, HEIGHT);
    start();
    init();
    BeginBatchDraw();
    while (1) {
        move(&mover);
        AI();
        draw();
        FlushBatchDraw();
        Sleep(10);
    }
    return 0;
}