C语言放烟花

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

#define WIN_WIDTH 1000//窗口大小
#define WIN_HEIGHT 600

#define KEY_DOWN(KEY) ((GetAsyncKeyState(KEY))& 0x8000 ? 1 : 0)//鼠标键盘消息
HWND m;
POINT p;

bool num[100] = { false };
int pot[100] = { 0 };
POINT suib1[100] = { 0 };
POINT suib2[100] = { 0 };
COLORREF color[100];

int play_one(int currentx, int initx, int inity, int num)    //获取y实时坐标
{
    int currenty;
    if (num == 0) currenty = (((currentx - initx) * (currentx - initx) - 100 * (currentx - initx)) / 40 + inity);                        //-向右+向左
    else if (num == 1) currenty = (((currentx - initx) * (currentx - initx) + 100 * (currentx - initx)) / 40 + inity);
    else if (num == 2) currenty = (((currentx - initx) * (currentx - initx) + 30 * (currentx - initx)) / 40 + inity);
    else if (num == 3) currenty = (((currentx - initx) * (currentx - initx) - 30 * (currentx - initx)) / 40 + inity);
    return currenty;
}

void getnum(bool* num, COLORREF* color, POINT* suib1, POINT* suib2)
{
    for (int i = 0; i < 100; i++) if (num[i] == false)
    {
        num[i] = true;
        color[i] = RGB(rand() % 256, rand() % 256, rand() % 256);
        suib1[i].x = p.x;
        suib2[i].x = p.x;
        suib2[i].y = p.y;
        break;
    }
}

void myputpixel(bool* num, POINT* suib1, POINT* suib2, COLORREF* color, int s, int direction)
{
    for (int i = 0; i < 100; i++)
    {
        if (num[i] == true)
        {
            putpixel(suib1[i].x, play_one(suib1[i].x, suib2[i].x, suib2[i].y, s), color[i]);
            putpixel(suib1[i].x + 1, play_one(suib1[i].x + 1, suib2[i].x, suib2[i].y, s), color[i]);
            putpixel(suib1[i].x + 2, play_one(suib1[i].x + 2, suib2[i].x, suib2[i].y, s), color[i]);
            putpixel(suib1[i].x + 3, play_one(suib1[i].x + 3, suib2[i].x, suib2[i].y, s), color[i]);
            putpixel(suib1[i].x + 4, play_one(suib1[i].x + 4, suib2[i].x, suib2[i].y, s), color[i]);
            putpixel(suib1[i].x + 5, play_one(suib1[i].x + 5, suib2[i].x, suib2[i].y, s), color[i]);
            putpixel(suib1[i].x + 6, play_one(suib1[i].x + 6, suib2[i].x, suib2[i].y, s), color[i]);
            putpixel(suib1[i].x + 7, play_one(suib1[i].x + 7, suib2[i].x, suib2[i].y, s), color[i]);
            putpixel(suib1[i].x + 8, play_one(suib1[i].x + 8, suib2[i].x, suib2[i].y, s), color[i]);
            putpixel(suib1[i].x + 9, play_one(suib1[i].x + 9, suib2[i].x, suib2[i].y, s), color[i]);
            if (direction == 0) suib1[i].x -= 3;
            else suib1[i].x += 3;
            if (play_one(suib1[i].x, suib2[i].x, suib2[i].y, s) > WIN_HEIGHT) num[i] = false;
        }
    }
}

void play_1()
{
    while (!(KEY_DOWN('2') || KEY_DOWN('3')))
    {
        cleardevice();
        m = GetForegroundWindow();
        GetCursorPos(&p);
        ScreenToClient(m, &p);
        if (KEY_DOWN(MOUSE_MOVED) && p.x > 0 && p.x < WIN_WIDTH && p.y>0 && p.y < WIN_HEIGHT)
        {
            getnum(num, color, suib1, suib2);
        }
        myputpixel(num, suib1, suib2, color, 0, 1);
        Sleep(10);
        FlushBatchDraw();
    }
}

int direction_2[100] = { 0 };
bool isdirection_2[100] = { false };

int play_two(int currentx, int initx, int inity, int i)
{
    int currenty;
    if (direction_2[i] == 0) currenty = (((currentx - initx) * (currentx - initx) + (rand() % 100 + 50) * (currentx - initx)) / (rand() % 40 + 20) + inity);
    else currenty = (((currentx - initx) * (currentx - initx) - (rand() % 100 + 50) * (currentx - initx)) / (rand() % 40 + 20) + inity);
    return currenty;
}

void play_2()
{
    while (!(KEY_DOWN('1') || KEY_DOWN('3')))
    {
        cleardevice();
        m = GetForegroundWindow();
        GetCursorPos(&p);
        ScreenToClient(m, &p);

        if (KEY_DOWN(MOUSE_MOVED) && p.x > 0 && p.x < WIN_WIDTH && p.y>0 && p.y < WIN_HEIGHT)
        {
            for (int i = 0; i < 100; i++) if (num[i] == false)
            {
                num[i] = true;
                color[i] = RGB(rand() % 256, rand() % 256, rand() % 256);
                suib1[i].x = p.x;
                suib2[i].x = p.x;
                suib2[i].y = p.y;
                break;
            }
        }

        for (int i = 0; i < 100; i++)
        {
            if (num[i] == true)
            {
                if (isdirection_2[i] == false)
                {
                    direction_2[i] = rand() % 2;
                    isdirection_2[i] = true;
                }
                putpixel(suib1[i].x, play_two(suib1[i].x, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 1, play_two(suib1[i].x + 1, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 2, play_two(suib1[i].x + 2, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 3, play_two(suib1[i].x + 3, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 4, play_two(suib1[i].x + 4, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 5, play_two(suib1[i].x + 5, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 6, play_two(suib1[i].x + 6, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 7, play_two(suib1[i].x + 7, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 8, play_two(suib1[i].x + 8, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 9, play_two(suib1[i].x + 9, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 10, play_two(suib1[i].x + 10, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 11, play_two(suib1[i].x + 11, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 12, play_two(suib1[i].x + 12, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 13, play_two(suib1[i].x + 13, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 14, play_two(suib1[i].x + 14, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 15, play_two(suib1[i].x + 15, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 16, play_two(suib1[i].x + 16, suib2[i].x, suib2[i].y, i), color[i]);
                putpixel(suib1[i].x + 17, play_two(suib1[i].x + 17, suib2[i].x, suib2[i].y, i), color[i]);
                if (direction_2[i] == 0) suib1[i].x -= 3;
                else suib1[i].x += 3;
                if (play_two(suib1[i].x, suib2[i].x, suib2[i].y, i) > WIN_HEIGHT)
                {
                    num[i] = false;
                    isdirection_2[i] = false;
                }
            }
        }


        Sleep(10);
        FlushBatchDraw();
    }
}


bool num3_1[100] = { 0 };
COLORREF color3_1[100] = { 0 };
POINT point3_1_1[100] = { 0 };
POINT point3_1_2[100] = { 0 };
bool num3_2[100] = { 0 };
COLORREF color3_2[100] = { 0 };
POINT point3_2_1[100] = { 0 };
POINT point3_2_2[100] = { 0 };
bool num3_3[100] = { 0 };
COLORREF color3_3[100] = { 0 };
POINT point3_3_1[100] = { 0 };
POINT point3_3_2[100] = { 0 };
bool num3_4[100] = { 0 };

void play_3()
{
    while (!(KEY_DOWN('2') || KEY_DOWN('1')))
    {
        cleardevice();
        m = GetForegroundWindow();
        GetCursorPos(&p);
        ScreenToClient(m, &p);
        if (KEY_DOWN(MOUSE_MOVED) && p.x > 0 && p.x < WIN_WIDTH && p.y>0 && p.y < WIN_HEIGHT)
        {
            getnum(num, color, suib1, suib2);
            getnum(num3_1, color3_1, point3_1_1, point3_1_2);
            getnum(num3_2, color3_2, point3_2_1, point3_2_2);
            getnum(num3_3, color3_3, point3_3_1, point3_3_2);
        }
        myputpixel(num, suib1, suib2, color, 0, 1);
        myputpixel(num3_1, point3_1_1, point3_1_2, color3_1, 1, 0);
        myputpixel(num3_2, point3_2_1, point3_2_2, color3_2, 2, 0);
        myputpixel(num3_3, point3_3_1, point3_3_2, color3_3, 3, 1);

        Sleep(10);
        FlushBatchDraw();
    }
}

int main()
{
    srand(time(NULL));
    printf("今天你开心了吗?玩玩烟花吧!\n");
    printf("按下回车键开始玩吧!\n");
    printf("(输入1,2,3可更改玩法)");
    while (!KEY_DOWN(13))
    {
        m = GetForegroundWindow();
        GetCursorPos(&p);
        ScreenToClient(m, &p);
    }
    initgraph(WIN_WIDTH, WIN_HEIGHT);
    setbkcolor(BLACK);
    cleardevice();
    BeginBatchDraw();
    int play_size = 1;
    while (1)
    {
        cleardevice();
        m = GetForegroundWindow();
        GetCursorPos(&p);
        ScreenToClient(m, &p);
        if (KEY_DOWN('1')) play_size = 1;
        else if (KEY_DOWN('2')) play_size = 2;
        else if (KEY_DOWN('3')) play_size = 3;
        switch (play_size)
        {
        case 1: play_1(); break;
        case 2: play_2(); break;
        case 3: play_3(); break;
        default:
            break;
        }
        Sleep(10);
        FlushBatchDraw();
    }
    EndBatchDraw();
}

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值