pppphhhhyyyy 2024-01-19 23:46 采纳率: 80.6%
浏览 9
已结题

火柴人小游戏碰到的问题

想请问一下为什么我这个没有砖块出现?效果是下面这样,感谢各位

img

#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 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)
{
    setfillcolor(LIGHTBLUE);
    solidrectangle(pb->x, pb->y, pb->x + pb->width, pb->y + pb->high);
}
void Movebricks(struct Bricks* pb)
{
    pb->x -= 25;
}
struct Node
{
    struct Bricks* pb;
    struct Node* next;
};
struct Node* list = NULL;
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);
        }
    }
}
IMAGE MOVE[8];
IMAGE ROLL[8];
IMAGE Jump;
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()
{
    for (int i = 0; i < 8; i++)
    {
        cleardevice();
        putimage(50, 300, ROLL + i);
        solidrectangle(0, 360, 640, 400);
        printflist(list);
        Sleep(50);
    }
}
void Jumpgraph()
{
    int y = 260;
    for (int i = 0; i < 5; i++)
    {
        cleardevice();
        putimage(50, y, &Jump);
        solidrectangle(0, 360, 640, 400);
        y -= 30;
        printflist(list);
        Sleep(50);
    }
    for (int i = 0; i < 5; i++)
    {
        cleardevice();
        putimage(50, y, &Jump);
        solidrectangle(0, 360, 640, 400);
        y += 30;
        printflist(list);
        Sleep(50);
    }
}
int getkey()
{
    if (GetAsyncKeyState('S'))
    {
        Rollgraph();
    }
    else if (GetAsyncKeyState(' '))
    {
        Jumpgraph();
    }
    return 1;
}
void Movegraph()
{
    int i = 0;
    while(getkey())
    {
        cleardevice();
        putimage(50, 260, MOVE+i%8);
        solidrectangle(0, 360, 640, 400);
        i++;
        printflist(list);
        Sleep(50);
    }
}


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;
}


int main()
{
    list = creatlist();
    srand((unsigned int)time(NULL));
    initgraph(640,400);
    setbkcolor(WHITE);
    cleardevice();
    begingraph();
    Mousemsgplace();
    Loadpicture();
    while (1)
    {
        Movegraph();
        if (Time(1000, 0))
        {
            insertNode(list, creatbrick(640, 310 - rand() % 200, 50, 50));
        }
    }
    closegraph();
    return 0;
}

  • 写回答

2条回答 默认 最新

  • pppphhhhyyyy 2024-01-20 12:09
    关注

    已经解决了,是因为main里面的if语句要放进Movegraph里面

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月28日
  • 已采纳回答 1月20日
  • 创建了问题 1月19日