简体   繁体   English

如何删除在C中循环中被调用的char图像?

[英]How to delete a char image being called as a function in a loop in C?

I'm creating a game. 我在创造一个游戏。 I have a function, which draws a character image at random locations using a loop. 我有一个函数,它使用循环在随机位置绘制一个字符图像。 I have collision logic which all works perfectly fine. 我有碰撞逻辑,一切都很好。 The issue I'm having is that I don't know how to get the character image to delete from the game upon collision. 我遇到的问题是我不知道如何在碰撞时从游戏中删除角色图像。 The image is one of many of the same kind within the game display, so I specifically want to be able to delete the character image at the specific location, and decrease the stored total stored number if images of that type within the game, to return it on a scoreboard. 图像是游戏显示中的许多相同类型之一,因此我特别希望能够删除特定位置的角色图像,并减少存储的总存储数量(如果游戏中该类型的图像),则返回它在记分牌上。

I've tried deleting the desired iteration of the called function in the same way you would if it was an array item, but that was just giving errors. 我尝试以与数组项目相同的方式删除所需的被调用函数迭代,但这只是给出了错误。

void drawEnemy(){
   drawEnemyImage(x[i], y[i], enemyImage);
}

void drawAllEnemies(){
   for(int i = 0; i < numEnemies; i++){
   drawEnemy(i);
   }
}
// (i also have collision logic here but it's not needed for the problem)

void returnCollision(){
    for (int i = 0; i < numEnemies; i++){
        if(collision() == true)
            return;
}

From this code, I can only presume you want to delete the enemy character image. 从这段代码中,我只能假设您要删除敌人角色图像。 Ok then. 好吧。 We would usually do something like this: 我们通常会这样做:

void deleteEnemy(int i) {
    --numEnemies;
    if (i == numEnemies) return;
    memmove(x + i, x + i + 1, (numEnemies - i) * sizeof(x[0]));
    memmove(y + i, y + i + 1, (numEnemies - i) * sizeof(y[0]));
}

Now the enemy in the middle of the arrays is gone. 现在阵列中间的敌人已经消失了。 I'm assuming x and y are arrays of integers or something like that. 我假设xy是整数数组或类似的东西。 If you have memory to free first, you would place the appropriate free() call before the if . 如果你有内存要先释放,你可以在if之前放置适当的free()调用。

Please use better variables names for globals. 请为全局变量使用更好的变量名称。 enemyx and enemyy would be huge improvement already. enemyxenemyy已经是巨大的进步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM