2
struct animate {
int drawable;
int animationFrames;
char *frames[TOTAL_FRAMES+1];
int charWidth;
int charHeight;
int xCoord;
int yCoord;
int xMove;
int yMove;
int currentFrame;
};
struct animate reaper[] =
{
{
    1,
    3,
    {
        "PIX\\Reaper\\reaper_right_r.gif",
        "PIX\\Reaper\\reaper_right_still.gif",
        "PIX\\Reaper\\reaper_right_l.gif",
        "PIX\\Reaper\\reaper_right_still.gif",
    },
    50,
    50,
    enemyx,
    enemyy,
    10,
    0,
    0,
},
{
    0,
    3,
    {
        "PIX\\Reaper\\reaper_left_r.gif",
        "PIX\\Reaper\\reaper_left_still.gif",
        "PIX\\Reaper\\reaper_left_l.gif",
        "PIX\\Reaper\\reaper_left_still.gif",
    },
    50,
    50,
    enemyx,
    enemyy,
    -10,
    0,
    0,
},
{
    0,
    3,
    {
        "PIX\\Reaper\\reaper_up_r.gif",
        "PIX\\Reaper\\reaper_up_still.gif",
        "PIX\\Reaper\\reaper_up_l.gif",
        "PIX\\Reaper\\reaper_up_still.gif",
    },
    50,
    50,
    enemyx,
    enemyy,
    0,
    -10,
    0,
},
{
    0,
    3,
    {
        "PIX\\Reaper\\reaper_down_r.gif",
        "PIX\\Reaper\\reaper_down_still.gif",
        "PIX\\Reaper\\reaper_down_l.gif",
        "PIX\\Reaper\\reaper_down_still.gif",
    },
    50,
    50,
    enemyx,
    enemyy,
    0,
    10,
    0,
},
};

    void reaper_animation()
{

srand(time(NULL));
sprintf(charHealthBuff, "Health: %d", charHealth);
outtextxy(30, 30, charHealthBuff);
//randomly determines starting coordinates
enemyx = rand() % 100 + 600;
enemyy = rand() % 100 + 600;

for (int i = 0; i < 4; i++){
    if( ! reaper[i].drawable)
        continue;
    //defines values of collisionBox struct
    reaperHit.boundx = reaper[i].xCoord;
    reaperHit.boundy = reaper[i].yCoord;
    reaperHit.boundheight = reaper[i].charHeight;
    reaperHit.boundwidth = reaper[i].charWidth;
    //Puts image of reaper on screen based on inputs from struct
readimagefile(reaper[i].frames[reaper[i].currentFrame],
    reaper[i].xCoord,
    reaper[i].yCoord,
    reaper[i].xCoord + reaper[i].charWidth,
    reaper[i].yCoord + reaper[i].charHeight );

reaper[i].currentFrame++; //increments the "frame" by one, thus only animating one image at a time
if(reaper[i].currentFrame == reaper[i].animationFrames){
    reaper[i].currentFrame = 0;} //When frame 3 is reached, it resorts back to first frame and restarts
    //character[i].drawable = 0;}

//increments reaper position based on xMove or yMove value
reaper[i].xCoord += reaper[i].xMove;
reaper[i].yCoord += reaper[i].yMove;
//harms character if this collision is true
if(collisionTest(reaperHit, charHit) == true){
    printf("reaper char COLLISION\n");
    charHealth = charHealth - 5;}
//harms reaper if this collision is true
if(collisionTest(thunderHit, reaperHit) == true){
    reaperHealth = reaperHealth - 10;}
}

}

void playReaper(){
int olddirection = 0, newdirection = 0; //starting values
int reaperDirection;
int moveLoop;
int activate = 0;
reaperHealth = 30;

do {
    srand(time(NULL));
    moveLoop = 10;
    reaperDirection = rand() % 4;
    //This loop keeps reaper going in same direction for more than one iteration
    for(int k = 0; k < moveLoop; k++){
    if(reaperDirection == 0)
        newdirection = 0;
    if(reaperDirection == 1)
        newdirection = 1;
    if(reaperDirection == 2)
        newdirection = 2;
    if(reaperDirection == 3)
        newdirection = 3;


if(olddirection != newdirection){
    reaper[newdirection].xCoord = reaper[olddirection].xCoord; //keeps xCoord and yCoord the same when changing directions
    reaper[newdirection].yCoord = reaper[olddirection].yCoord; //allowing sprite to stay where they were

    reaper[olddirection].drawable = 0; //the previous direction is no longer drawable
    reaper[newdirection].drawable = 1; //new direction becomes drawable and animated
    reaper[newdirection].currentFrame = 0; //resets frame to 0 so complete animation can begin
    if(newdirection == 0 || newdirection == 1 || newdirection == 2 || newdirection == 3){
    olddirection = newdirection;} //turns newdirection into olddirection for next run through of do-while() loop
}
checkReaperSectors(); //checks for sector collision and allows sector refresh

reaper_animation(); //animates the reaper

Sleep(40); //Slows reaper movement down 
    check_reaper(); //checks for collisions with wall or obstacles
    }
}
while(reaperHealth > 0);
if(reaperHealth <= 0){
    level++;}
}

好的,这很长……哈哈,但这是我处理敌人动画的全部代码,收割者。有 4 个定义的结构体,它们给出了要调用的 reaper 值的每个方向,playReaper() 函数确定要绘制哪个结构体,然后动画 reaper 将其实际放在屏幕上。

问题是......我一次只能让一个收割者运行......我试图弄清楚如何使用这个来上课,但我担心如果我将收割者改为班级,我将不得不改变角色,老板也要上课吗?(它们以相同的风格处理,使用动画结构)。

或者如何创建一个结构数组的数组。就像 struct animate reaper[] 的 100 个数组。这将创建 100 个准备就绪的单独收割者,然后我可以在每个级别调用一定数量的收割者,然后它们将随机出现并在屏幕上随机移动。

它只是......我不知道如何真正地上课......所以我绝对不知道我将如何去做这个。我尝试在网上进行研究,但仅此而已……这些示例似乎都不像我想做的那样,因此很难将它们应用到我的代码中(尤其是在从未上过课时……)。

我也不确定如何创建一个 reaper[] 数组。:(

谁能指导我完成我必须做的事情?或者至少给我一个好的起点?

将不胜感激!

ps 这是简单的自上而下的 2d 游戏,我在 graphics.h 中:/

4

0 回答 0