首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用for循环正确地射击多个项目符号?

如何使用for循环正确地射击多个项目符号?
EN

Stack Overflow用户
提问于 2012-01-02 10:30:56
回答 1查看 5.2K关注 0票数 0

我正在使用SDL...Right在C++中开发一个shmup游戏的原型,现在我只是想在不使用类的情况下让基本的东西工作。现在,我有了它,所以它可以发射多颗子弹,但它的行为很奇怪,我相信这是因为计数器的方式,reset...The子弹会出现和消失,有些会在他们被射击的原始位置保持闪烁,有时会有延迟才能再次射击,即使screen...And上没有限制有时玩家角色会突然跳到右边,只能上下移动。我如何修复这个问题,使其能够顺利拍摄?我已经包含了所有相关的代码...

编辑请注意,我打算清理它,并将其全部移动到一个类,一旦我得到这个图形out...This只是一个简单的原型,所以我可以有游戏的基础编程。

edit2 ePos是敌人的位置,pPos是玩家的位置。

代码语言:javascript
复制
//global
SDL_Surface *bullet[10] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
bool shot[10];
int shotCount = 0;
SDL_Rect bPos[10];

//event handler
case SDLK_z: printf("SHOT!"); shot[shotCount] = true; ++shotCount; break; 

//main function
for(int i = 0; i <= 9; i++)
{
    shot[i] = false;  
    bullet[i] = IMG_Load("bullet.png");
}

//game loop
for(int i = 0; i <= shotCount; i++)
{    
    if(shot[i] == false)
    {
        bPos[i].x = pPos.x;
        bPos[i].y = pPos.y; 
    }
    if(shot[i] == true)
    { 
        bPos[i].y -= 8;
        SDL_BlitSurface(bullet[i], NULL, screen, &bPos[i]);

        if( (bPos[i].y + 16 == ePos.y + 16) || (bPos[i].x + 16 == ePos.x + 16) )
        {
            shot[i] = false;
        }
        else if(bPos[i].y == 0)
        {
            shot[i] = false;
        }
    }
}

if(shotCount >= 9) { shotCount = 0; }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-02 13:14:53

下面是我在评论中建议的内容。它只是写在我的头顶上,但它让你对我在说什么有一个大概的了解。

代码语言:javascript
复制
class GameObject
{
    public:
        int x;
        int y;
        int width;
        int height;
        int direction;
        int speed;

    GameObject()
    {
        x = 0;
        y = 0;
        width = 0;
        height = 0;
        direction = 0;
        speed = 0;
    }

    void update()
    {
        // Change the location of the object.
    }

    bool collidesWidth(GameObject *o)
    {
        // Test if the bullet collides with Enemy.
        // If it does, make it invisible and return true
    }
}

GameObject bullet[10];
GameObject enemy[5];

while(true)
{
    for(int x=0; x<=10;x++)
    {
        bullet[x].update();
        for(int y=0;y<=5;y++)
        {
            if(bullet[x].collidesWith(&enemy[y])
            {
                // Make explosion, etc, etc.
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8696973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档