火柴人闯关游戏

学自B站UPC语言编程_Plus,加入了我自己的一些创新虽然就一点点哈哈,结构体这里也还没有完全看懂,也请各位大佬指点一下

#define _CRT_SECURE_NO_WARNINGS
#include <graphics.h>
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <assert.h>
struct Bricks
{
    int x;
    int y;
    int width;
    int high;
};
struct Node
{
    struct Bricks* pb;
    struct Node* next;
};
HWND hwnd = NULL;
struct Node* list = NULL;
struct Bricks* creatbrick(int x, int y, int width, int high)
{
    struct Bricks* pb = (struct Bricks*)malloc(sizeof(struct Bricks));
    assert(pb);
    pb->x = x;
    pb->y = y;
    pb->width = width;
    pb->high = high;
    return pb;
}
void Drawbricks(struct Bricks* pb)
{
    solidrectangle(pb->x, pb->y, pb->x + pb->width, pb->y + pb->high);
}
void Movebricks(struct Bricks* pb)
{
    pb->x -= 25;
}


struct Node* creatlist()
{
    struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
    assert(headNode);
    headNode->pb = NULL;
    headNode->next = NULL;
    return headNode;
}
struct Node* creatNode(struct Bricks* pb)
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    assert(newNode);
    newNode->pb = pb;
    newNode->next = NULL;
    return newNode;
}
void insertNode(struct Node* headNode, struct Bricks* pb)
{
    struct Node* newNode = creatNode(pb);
    newNode->next = headNode->next;
    headNode->next = newNode;
}

void printflist(struct Node* headNode)
{
    struct Node* pMove = headNode->next;
    while (pMove != NULL)
    {
        Drawbricks(pMove->pb);
        Movebricks(pMove->pb);
        pMove = pMove->next;
    }
}
void basebar()
{
    setfillcolor(LIGHTBLUE);
    solidrectangle(0, 360, 640, 400);
}
void begingraph()
{
    setfillcolor(LIGHTBLUE);
    solidrectangle(240, 100, 380, 150);
    solidrectangle(240, 160, 380, 210);
    basebar();
    char begin[] = "开始游戏";
    char over[] = "结束游戏";
    char tips[] = "提示:空格跳跃";
    settextcolor(RED);
    settextstyle(30, 0, "楷体");
    setbkmode(TRANSPARENT);
    outtextxy(250, 115, begin);
    outtextxy(250, 175, over);
    outtextxy(240, 370, tips);

}
void Mousemsgplace()
{
    while (1)
    {
        MOUSEMSG m;
        m = GetMouseMsg();
        if (m.x >= 240 && m.y >= 100 && m.x <= 380 && m.y <= 150)
        {
            setlinecolor(RED);
            setlinestyle(PS_JOIN_ROUND, 5);
            rectangle(235, 95, 385, 155);
            if (m.uMsg == WM_LBUTTONDOWN)
            {
                break;
            }
        }
        else if (m.x >= 240 && m.y >= 160 && m.x <= 380 && m.y <= 210)
        {
            setlinecolor(RED);
            setlinestyle(PS_JOIN_ROUND, 5);
            rectangle(235, 155, 385, 215);
            if (m.uMsg == WM_LBUTTONDOWN)
            {
                exit(0);
            }
        }
        else
        {
            setlinecolor(WHITE);
            setlinestyle(PS_JOIN_ROUND, 5);
            rectangle(235, 95, 385, 155);
            rectangle(235, 155, 385, 215);
        }
    }
}
int Time(time_t num, int id)
{
    static time_t start[10];
    time_t end = clock();
    if (end - start[id] > num)
    {
        start[id] = end;
        return 1;
    }
    return 0;
}
IMAGE MOVE[8];
IMAGE ROLL[8];
IMAGE Jump;

int hitbricks(int x, int y, int width, int high, struct Node* list)
{
    struct Node* pMove = list->next;
    while (pMove != NULL)
    {
        if (pMove->pb->x >= (x - pMove->pb->width) && pMove->pb->x <= (x + width))
        {
            if (pMove->pb->y >= (y - pMove->pb->high) && pMove->pb->y <= (y + high))
            {
                return 1;
            }
        }
        pMove = pMove->next;
    }
    return 0;
}
void Loadpicture()
{
    loadimage(&Jump, "jump.jpg", 70, 100);
    for (int i = 1; i <= 8; i++)
    {
        char picture[20] = " ";
        sprintf(picture, "move8_%d.jpg", i);
        loadimage(MOVE + i - 1, picture, 70, 100);
        sprintf(picture, "gun8_%d.jpg", i);
        loadimage(ROLL + i - 1, picture, 70, 100);
    }
}
void Rollgraph(int num)
{
    int i = 0;
    BeginBatchDraw();
    while(i<num)
    {
        cleardevice();
        putimage(50, 300, ROLL + i);
        solidrectangle(0, 360, 640, 400);
        printflist(list);
        i++;
        if (hitbricks(50, 300, 70, 100, list))
        {
            MessageBox(hwnd, "Gameover", "Gameover", MB_OK);
            exit(0);
        }
        Sleep(50);
        FlushBatchDraw();
    }
    EndBatchDraw();
}
void Jumpgraph()
{
    int y = 260;
    BeginBatchDraw();
    for (int i = 0; i < 5; i++)
    {
        cleardevice();
        putimage(50, y, &Jump);
        solidrectangle(0, 360, 640, 400);
        if (hitbricks(50, y, 70, 100, list))
        {
            MessageBox(hwnd, "Gameover", "Gameover", MB_OK);
            exit(0);
        }
        y -= 30;
        printflist(list);
        Sleep(50);
        FlushBatchDraw();
    }
    for (int i = 0; i < 5; i++)
    {
        cleardevice();
        putimage(50, y, &Jump);
        solidrectangle(0, 360, 640, 400);
        if (hitbricks(50, y, 70, 100, list))
        {
            MessageBox(hwnd, "Gameover", "Gameover", MB_OK);
            exit(0);
        }
        y += 30;
        printflist(list);
        Sleep(50);
        FlushBatchDraw();
    }
    EndBatchDraw();
}
int getkey()
{
    if (GetAsyncKeyState('S'))
    {
        Rollgraph(8);
    }
    else if (GetAsyncKeyState(' '))
    {
        Jumpgraph();
    }
    return 1;
}
void Movegraph()
{
    int i = 0;
    while(getkey())
    {
        BeginBatchDraw();
        cleardevice();
        putimage(50, 260, MOVE + i%8 );
        solidrectangle(0, 360, 640, 400);
        i++; 
        if (Time(1000, 0))
        {
            insertNode(list, creatbrick(640, 310 - rand() % 200, 50, 50));
        }
        if (hitbricks(50, 260, 70, 100, list))
        {
            MessageBox(hwnd,"Gameover","Gameover",MB_OK);
            exit(0);
        }
        printflist(list);
        Sleep(50);
        FlushBatchDraw();
        EndBatchDraw();
    }

}
int main()
{
    list = creatlist();
    srand((unsigned int)time(NULL));
    hwnd=initgraph(640, 400);
    setbkcolor(WHITE);
    cleardevice();
    begingraph();
    Mousemsgplace();
    Loadpicture();
    while (1)
    {
        Movegraph();
    }
    closegraph();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值