程序冻结,找不到解决方案。 SDL 和 C++

发布于 2024-12-22 11:52:07 字数 14310 浏览 0 评论 0原文

我检查了代码中的所有位置,但找不到程序每次加载时都会冻结的原因。在我添加一个新的 AI 标头“schoolboy.h”后,问题开始发生。我检查过以确保尝试位块传输图像不是问题,但事实并非如此。因此,经过一些测试,我发现问题出在“schoolboy.h”中的“void schoolboy_action()”。这是整个项目的代码。我花了一个小时查看该部分,但找不到解决方案。我知道这篇文章真的很长,而且我打算填补其中的一些漏洞,但请耐心等待。

Main.cpp

#include "schoolboy.h"
#include "include_file.h"

int main(int argc,char* argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    variables();

    while (quit == 1)
    {
        while (MARS.alive == 1)
        {
            SDL_WM_SetCaption("Mobile Anihilation Robot System", NULL);
            applysurface(0,0,background,screen);
            MARS_action();
            MARS_bullet_action(1);
            gunshot_explosion_action(1);
            if (create_schoolboy_variable == 1)
            {create_schoolboy(); create_schoolboy_variable = 0;}
            else if (create_schoolboy_variable < 1)
            {create_schoolboy_variable = rand() % 1;};
            schoolboy_action(0,0,0);
            applysurface(MARS.x-25, MARS.y-25, MARS_image, screen);
            SDL_Flip(screen);
            SDL_Delay(1);
        };
    while (MARS.alive == 0)
    {
            SDL_WM_SetCaption("Mobil Anihilation Robot System", NULL);
    };
    };
    SDL_FreeSurface(screen);
    SDL_Quit();
    return 0;
};

include_file.h

#ifndef INCLUDE_FILE_H_INCLUDED
#define INCLUDE_FILE_H_INCLUDED

#include <map>
#include <cstdlib>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

/*structs*/

struct instance_struct
{
    int gunshot_explosion;
    int schoolboy_instance;
};
struct MARS_struct
{
int x;
int y;
int alive;
int bullet;
};
struct MARS_bullet_struct
{
int x;
int y;
int direction;
int exist;
int id;
bool operator<(const MARS_bullet_struct & n)const{return this->id<n.id;}
};
struct schoolboy_struct
{
    int x;
    int y;
    int direction;
    int id;
    int exist;
    int walk_delay;
    int shoot_delay;
    int walked;
    SDL_Rect clip[3];
    bool operator<(const schoolboy_struct&n)const{return this->id<n.id;}
};
struct gunshot_explosion_struct
{
    int x;
    int y;
    int id;
    int life;
    int exist;
    bool operator<(const gunshot_explosion_struct&n)const{return this->id<n.id;}
};

/*declaring structs*/
MARS_struct MARS;
instance_struct instance_body;
std::map<int, MARS_bullet_struct>MARS_bullet;
std::map<int, schoolboy_struct>schoolboy;
std::map<int, gunshot_explosion_struct>gunshot_explosion;

/*applysurface*/
void applysurface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect* clip = NULL)
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface(source,clip,destination,&offset);
};

/*declaring global variables*/
int quit;
int create_schoolboy_variable;
SDL_Event event;
SDL_Rect clip[3];
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *gunshot_explosion_image = NULL;
SDL_Surface *MARS_image = NULL;
SDL_Surface *MARS_bullet_image = NULL;
SDL_Surface *schoolboy_image = NULL;

/*giving variables values*/
void variables()
{
    quit = 1;
    MARS.alive = 1;
    MARS.x = 256;
    MARS.y = 256;
    create_schoolboy_variable = 0;

    clip[0].x = 0;
    clip[0].y = 0;
    clip[0].w = 50;
    clip[0].h = 50;
    clip[1].x = 50;
    clip[1].y = 50;
    clip[1].w = 100;
    clip[1].h = 100;
    clip[2].x = 100;
    clip[2].y = 100;
    clip[2].w = 150;
    clip[2].h = 150;
    clip[3].x = 150;
    clip[3].y = 150;
    clip[3].w = 200;
    clip[3].h = 200;

    screen = SDL_SetVideoMode(512,512,32,SDL_SWSURFACE);
    background = IMG_Load("images/background.png");
    gunshot_explosion_image = IMG_Load("images/gunshot_explosion.png");
    MARS_image = IMG_Load("images/MARS.png");
    MARS_bullet_image = IMG_Load("images/MARS_bullet.png");
    schoolboy_image = IMG_Load("images/schoolboy.png");
};

void gunshot_explosion_action(int instance)
{
    while (instance <= instance_body.gunshot_explosion)
    {
        if (gunshot_explosion[instance].exist == 0)
        {
            gunshot_explosion[instance].life = gunshot_explosion[instance].life + 1;
            if (gunshot_explosion[instance].life > 7)
            {gunshot_explosion[instance].exist = 1;};
            applysurface(gunshot_explosion[instance].x-6,gunshot_explosion[instance].y-6,gunshot_explosion_image,screen);
        };
        instance = instance + 1;
    };
};

#endif // INCLUDE_FILE_H_INCLUDED

MARS.h

#ifndef MARS_H_INCLUDED
#define MARS_H_INCLUDED

#include "include_file.h"

/*character functions*/
void MARS_action()
{
    if (SDL_PollEvent(&event))
    {
        if (event.type == SDL_QUIT)
        {
            MARS.alive = 2;
            quit = 0;
        };

        if (event.type == SDL_KEYDOWN)
        {
            switch(event.key.keysym.sym)
            {
                case SDLK_a:
                    if(MARS.x > 0){MARS.x = MARS.x - 16;}; break;
                case SDLK_d:
                    if(MARS.x < 512){MARS.x = MARS.x + 16;}; break;
                case SDLK_w:
                    if(MARS.y > 0){MARS.y = MARS.y - 16;}; break;
                case SDLK_s:
                    if(MARS.y < 512){MARS.y = MARS.y + 16;}; break;
                case SDLK_LEFT:
                    MARS.bullet = MARS.bullet + 1;
                    MARS_bullet[MARS.bullet].direction = 2;
                    MARS_bullet[MARS.bullet].x = MARS.x-25;
                    MARS_bullet[MARS.bullet].y = MARS.y;
                    instance_body.gunshot_explosion = instance_body.gunshot_explosion+1;
                    gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x-25;
                    gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y;
                    break;
                case SDLK_RIGHT:
                    MARS.bullet = MARS.bullet + 1;
                    MARS_bullet[MARS.bullet].direction = 0;
                    MARS_bullet[MARS.bullet].x = MARS.x+25;
                    MARS_bullet[MARS.bullet].y = MARS.y;
                    instance_body.gunshot_explosion = instance_body.gunshot_explosion+1;
                    gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x+25;
                    gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y;
                    break;
                case SDLK_UP:
                    MARS.bullet = MARS.bullet + 1;
                    MARS_bullet[MARS.bullet].direction = 1;
                    MARS_bullet[MARS.bullet].x = MARS.x;
                    MARS_bullet[MARS.bullet].y = MARS.y-25;
                    instance_body.gunshot_explosion = instance_body.gunshot_explosion+1;
                    gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x;
                    gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y-25;
                    break;
                case SDLK_DOWN:
                    MARS.bullet = MARS.bullet + 1;
                    MARS_bullet[MARS.bullet].direction = 3;
                    MARS_bullet[MARS.bullet].x = MARS.x;
                    MARS_bullet[MARS.bullet].y = MARS.y+25;
                    instance_body.gunshot_explosion = instance_body.gunshot_explosion+1;
                    gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x;
                    gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y+25;
                    break;
                case SDLK_ESCAPE: quit = 0; MARS.alive = 2; break;
            };
        };
    };
};

void MARS_bullet_action(int instance)
{
    while (instance <= MARS.bullet)
    {
        if (MARS_bullet[instance].exist == 0)
        {
            if (MARS_bullet[instance].direction == 0)
            {MARS_bullet[instance].x = MARS_bullet[instance].x + 5;};
            if (MARS_bullet[instance].direction == 1)
            {MARS_bullet[instance].y = MARS_bullet[instance].y - 5;};
            if (MARS_bullet[instance].direction == 2)
            {MARS_bullet[instance].x = MARS_bullet[instance].x - 5;};
            if (MARS_bullet[instance].direction == 3)
            {MARS_bullet[instance].y = MARS_bullet[instance].y + 5;};
            if (MARS_bullet[instance].x < 0 or MARS_bullet[instance].x > 512 or MARS_bullet[instance].y < 0 or MARS_bullet[instance].y > 512)
            {MARS_bullet[instance].exist = 1;};
    applysurface(MARS_bullet[instance].x-5, MARS_bullet[instance].y-5, MARS_bullet_image, screen);
        };
        instance = instance + 1;
    };
};

#endif // MARS_H_INCLUDED

schoolboy.h

#ifndef SCHOOLBOY_H_INCLUDED
#define SCHOOLBOY_H_INCLUDED

#include "include_file.h"

void create_schoolboy(int positionx = 0, int positiony = 0)
{
    instance_body.schoolboy_instance = instance_body.schoolboy_instance + 1;
    positionx = rand() % 1;
    positiony = rand() % 1;
    if (positionx == 0 and positiony == 0)
    {
        schoolboy[instance_body.schoolboy_instance].x = 0;
        schoolboy[instance_body.schoolboy_instance].y = 0;
    };
    if (positionx == 1 and positiony == 0)
    {
        schoolboy[instance_body.schoolboy_instance].x = 512;
        schoolboy[instance_body.schoolboy_instance].y = 0;
    };
    if (positionx == 0 and positiony == 1)
    {
        schoolboy[instance_body.schoolboy_instance].x = 0;
        schoolboy[instance_body.schoolboy_instance].y = 512;
    };
    if (positionx == 1 and positiony == 1)
    {
        schoolboy[instance_body.schoolboy_instance].x = 512;
        schoolboy[instance_body.schoolboy_instance].y = 512;
    };
};

void schoolboy_action(int instance, int bullet, int first_direction)
{
    while (instance <= instance_body.schoolboy_instance)
    {
        first_direction = rand() % 1;
        if (schoolboy[instance].exist == 0)
        {
            while (bullet <= MARS.bullet)
            {
                if (schoolboy[instance].x-12 >= MARS_bullet[bullet].x and MARS_bullet[bullet].x <= schoolboy[instance].x+12 and schoolboy[instance].y-12 >= MARS_bullet[bullet].y and MARS_bullet[bullet].y <= schoolboy[instance].y+12)
                {
                    schoolboy[instance].exist = 1;
                };
                bullet = bullet + 1;
            };
            if (schoolboy[instance].walk_delay == 0)
            {
                if (first_direction == 0)
                {
                    schoolboy[instance].walked = 0;
                    if (MARS.x > schoolboy[instance].x)
                    {
                        schoolboy[instance].x = schoolboy[instance].x + 16;
                        applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[0]);
                        schoolboy[instance].walked = 1;
                    }
                    else if (MARS.x < schoolboy[instance].x)
                    {
                        schoolboy[instance].x = schoolboy[instance].x - 16;
                        applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[2]);
                        schoolboy[instance].walked = 1;
                    };
                    if (schoolboy[instance].walked = 0)
                    {
                        if (MARS.y > schoolboy[instance].y)
                        {
                            schoolboy[instance].y = schoolboy[instance].y+16;
                            applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[3]);
                            schoolboy[instance].walked = 1;
                        }
                        else if (MARS.y < schoolboy[instance].y)
                        {
                            schoolboy[instance].y = schoolboy[instance].y+16;
                            applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[1]);
                            schoolboy[instance].walked = 1;
                        };
                    };

                };
                if (first_direction == 1)
                {
                    schoolboy[instance].walked = 0;
                    if (MARS.y > schoolboy[instance].y)
                    {
                        schoolboy[instance].y = schoolboy[instance].y+16;
                        applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[3]);
                        schoolboy[instance].walked = 1;
                    }
                    else if (MARS.y < schoolboy[instance].y)
                    {
                        schoolboy[instance].y = schoolboy[instance].y+16;
                        applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[1]);
                        schoolboy[instance].walked = 1;
                    };
                    if (schoolboy[instance].walked = 0)
                    {
                        if (MARS.x > schoolboy[instance].x)
                        {
                            schoolboy[instance].x = schoolboy[instance].x + 16;
                            applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[0]);
                            schoolboy[instance].walked = 1;
                        }
                        else if (MARS.x < schoolboy[instance].x)
                        {
                            schoolboy[instance].x = schoolboy[instance].x - 16;
                            applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[2]);
                            schoolboy[instance].walked = 1;
                        };
                    };
                };
                schoolboy[instance].walk_delay = schoolboy[instance].walk_delay + 1;
            }
            else {schoolboy[instance].walk_delay = schoolboy[instance].walk_delay + 1;};
            if (schoolboy[instance].walk_delay == 10){schoolboy[instance].walk_delay = 0;};
        };
    };
    instance = instance + 1;
};

#endif // SCHOOLBOY_H_INCLUDED

我所有的文件都是相互关联的,尤其是 schoolboy.h,这就是我把它们放下来的原因。如果您找到答案,您能解释一下为什么会这样吗?任何帮助表示赞赏!

I've checked everywhere in my code and I could not find the reason why my program freezes every time I load it. The problem started happening after I included a new AI header, "schoolboy.h". I checked to make sure that attempting to blit an image wasn't the problem, and it wasn't. So, after some testing, I figured that the problem lied in "void schoolboy_action()" within "schoolboy.h". Here is the entire project's code. I spent an hour looking at that section and could not find a solution. I know that the is really long, and that there are some holes in it that I plan on filling, but please bear with me.

Main.cpp

#include "schoolboy.h"
#include "include_file.h"

int main(int argc,char* argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    variables();

    while (quit == 1)
    {
        while (MARS.alive == 1)
        {
            SDL_WM_SetCaption("Mobile Anihilation Robot System", NULL);
            applysurface(0,0,background,screen);
            MARS_action();
            MARS_bullet_action(1);
            gunshot_explosion_action(1);
            if (create_schoolboy_variable == 1)
            {create_schoolboy(); create_schoolboy_variable = 0;}
            else if (create_schoolboy_variable < 1)
            {create_schoolboy_variable = rand() % 1;};
            schoolboy_action(0,0,0);
            applysurface(MARS.x-25, MARS.y-25, MARS_image, screen);
            SDL_Flip(screen);
            SDL_Delay(1);
        };
    while (MARS.alive == 0)
    {
            SDL_WM_SetCaption("Mobil Anihilation Robot System", NULL);
    };
    };
    SDL_FreeSurface(screen);
    SDL_Quit();
    return 0;
};

include_file.h

#ifndef INCLUDE_FILE_H_INCLUDED
#define INCLUDE_FILE_H_INCLUDED

#include <map>
#include <cstdlib>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

/*structs*/

struct instance_struct
{
    int gunshot_explosion;
    int schoolboy_instance;
};
struct MARS_struct
{
int x;
int y;
int alive;
int bullet;
};
struct MARS_bullet_struct
{
int x;
int y;
int direction;
int exist;
int id;
bool operator<(const MARS_bullet_struct & n)const{return this->id<n.id;}
};
struct schoolboy_struct
{
    int x;
    int y;
    int direction;
    int id;
    int exist;
    int walk_delay;
    int shoot_delay;
    int walked;
    SDL_Rect clip[3];
    bool operator<(const schoolboy_struct&n)const{return this->id<n.id;}
};
struct gunshot_explosion_struct
{
    int x;
    int y;
    int id;
    int life;
    int exist;
    bool operator<(const gunshot_explosion_struct&n)const{return this->id<n.id;}
};

/*declaring structs*/
MARS_struct MARS;
instance_struct instance_body;
std::map<int, MARS_bullet_struct>MARS_bullet;
std::map<int, schoolboy_struct>schoolboy;
std::map<int, gunshot_explosion_struct>gunshot_explosion;

/*applysurface*/
void applysurface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect* clip = NULL)
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface(source,clip,destination,&offset);
};

/*declaring global variables*/
int quit;
int create_schoolboy_variable;
SDL_Event event;
SDL_Rect clip[3];
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *gunshot_explosion_image = NULL;
SDL_Surface *MARS_image = NULL;
SDL_Surface *MARS_bullet_image = NULL;
SDL_Surface *schoolboy_image = NULL;

/*giving variables values*/
void variables()
{
    quit = 1;
    MARS.alive = 1;
    MARS.x = 256;
    MARS.y = 256;
    create_schoolboy_variable = 0;

    clip[0].x = 0;
    clip[0].y = 0;
    clip[0].w = 50;
    clip[0].h = 50;
    clip[1].x = 50;
    clip[1].y = 50;
    clip[1].w = 100;
    clip[1].h = 100;
    clip[2].x = 100;
    clip[2].y = 100;
    clip[2].w = 150;
    clip[2].h = 150;
    clip[3].x = 150;
    clip[3].y = 150;
    clip[3].w = 200;
    clip[3].h = 200;

    screen = SDL_SetVideoMode(512,512,32,SDL_SWSURFACE);
    background = IMG_Load("images/background.png");
    gunshot_explosion_image = IMG_Load("images/gunshot_explosion.png");
    MARS_image = IMG_Load("images/MARS.png");
    MARS_bullet_image = IMG_Load("images/MARS_bullet.png");
    schoolboy_image = IMG_Load("images/schoolboy.png");
};

void gunshot_explosion_action(int instance)
{
    while (instance <= instance_body.gunshot_explosion)
    {
        if (gunshot_explosion[instance].exist == 0)
        {
            gunshot_explosion[instance].life = gunshot_explosion[instance].life + 1;
            if (gunshot_explosion[instance].life > 7)
            {gunshot_explosion[instance].exist = 1;};
            applysurface(gunshot_explosion[instance].x-6,gunshot_explosion[instance].y-6,gunshot_explosion_image,screen);
        };
        instance = instance + 1;
    };
};

#endif // INCLUDE_FILE_H_INCLUDED

MARS.h

#ifndef MARS_H_INCLUDED
#define MARS_H_INCLUDED

#include "include_file.h"

/*character functions*/
void MARS_action()
{
    if (SDL_PollEvent(&event))
    {
        if (event.type == SDL_QUIT)
        {
            MARS.alive = 2;
            quit = 0;
        };

        if (event.type == SDL_KEYDOWN)
        {
            switch(event.key.keysym.sym)
            {
                case SDLK_a:
                    if(MARS.x > 0){MARS.x = MARS.x - 16;}; break;
                case SDLK_d:
                    if(MARS.x < 512){MARS.x = MARS.x + 16;}; break;
                case SDLK_w:
                    if(MARS.y > 0){MARS.y = MARS.y - 16;}; break;
                case SDLK_s:
                    if(MARS.y < 512){MARS.y = MARS.y + 16;}; break;
                case SDLK_LEFT:
                    MARS.bullet = MARS.bullet + 1;
                    MARS_bullet[MARS.bullet].direction = 2;
                    MARS_bullet[MARS.bullet].x = MARS.x-25;
                    MARS_bullet[MARS.bullet].y = MARS.y;
                    instance_body.gunshot_explosion = instance_body.gunshot_explosion+1;
                    gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x-25;
                    gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y;
                    break;
                case SDLK_RIGHT:
                    MARS.bullet = MARS.bullet + 1;
                    MARS_bullet[MARS.bullet].direction = 0;
                    MARS_bullet[MARS.bullet].x = MARS.x+25;
                    MARS_bullet[MARS.bullet].y = MARS.y;
                    instance_body.gunshot_explosion = instance_body.gunshot_explosion+1;
                    gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x+25;
                    gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y;
                    break;
                case SDLK_UP:
                    MARS.bullet = MARS.bullet + 1;
                    MARS_bullet[MARS.bullet].direction = 1;
                    MARS_bullet[MARS.bullet].x = MARS.x;
                    MARS_bullet[MARS.bullet].y = MARS.y-25;
                    instance_body.gunshot_explosion = instance_body.gunshot_explosion+1;
                    gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x;
                    gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y-25;
                    break;
                case SDLK_DOWN:
                    MARS.bullet = MARS.bullet + 1;
                    MARS_bullet[MARS.bullet].direction = 3;
                    MARS_bullet[MARS.bullet].x = MARS.x;
                    MARS_bullet[MARS.bullet].y = MARS.y+25;
                    instance_body.gunshot_explosion = instance_body.gunshot_explosion+1;
                    gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x;
                    gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y+25;
                    break;
                case SDLK_ESCAPE: quit = 0; MARS.alive = 2; break;
            };
        };
    };
};

void MARS_bullet_action(int instance)
{
    while (instance <= MARS.bullet)
    {
        if (MARS_bullet[instance].exist == 0)
        {
            if (MARS_bullet[instance].direction == 0)
            {MARS_bullet[instance].x = MARS_bullet[instance].x + 5;};
            if (MARS_bullet[instance].direction == 1)
            {MARS_bullet[instance].y = MARS_bullet[instance].y - 5;};
            if (MARS_bullet[instance].direction == 2)
            {MARS_bullet[instance].x = MARS_bullet[instance].x - 5;};
            if (MARS_bullet[instance].direction == 3)
            {MARS_bullet[instance].y = MARS_bullet[instance].y + 5;};
            if (MARS_bullet[instance].x < 0 or MARS_bullet[instance].x > 512 or MARS_bullet[instance].y < 0 or MARS_bullet[instance].y > 512)
            {MARS_bullet[instance].exist = 1;};
    applysurface(MARS_bullet[instance].x-5, MARS_bullet[instance].y-5, MARS_bullet_image, screen);
        };
        instance = instance + 1;
    };
};

#endif // MARS_H_INCLUDED

schoolboy.h

#ifndef SCHOOLBOY_H_INCLUDED
#define SCHOOLBOY_H_INCLUDED

#include "include_file.h"

void create_schoolboy(int positionx = 0, int positiony = 0)
{
    instance_body.schoolboy_instance = instance_body.schoolboy_instance + 1;
    positionx = rand() % 1;
    positiony = rand() % 1;
    if (positionx == 0 and positiony == 0)
    {
        schoolboy[instance_body.schoolboy_instance].x = 0;
        schoolboy[instance_body.schoolboy_instance].y = 0;
    };
    if (positionx == 1 and positiony == 0)
    {
        schoolboy[instance_body.schoolboy_instance].x = 512;
        schoolboy[instance_body.schoolboy_instance].y = 0;
    };
    if (positionx == 0 and positiony == 1)
    {
        schoolboy[instance_body.schoolboy_instance].x = 0;
        schoolboy[instance_body.schoolboy_instance].y = 512;
    };
    if (positionx == 1 and positiony == 1)
    {
        schoolboy[instance_body.schoolboy_instance].x = 512;
        schoolboy[instance_body.schoolboy_instance].y = 512;
    };
};

void schoolboy_action(int instance, int bullet, int first_direction)
{
    while (instance <= instance_body.schoolboy_instance)
    {
        first_direction = rand() % 1;
        if (schoolboy[instance].exist == 0)
        {
            while (bullet <= MARS.bullet)
            {
                if (schoolboy[instance].x-12 >= MARS_bullet[bullet].x and MARS_bullet[bullet].x <= schoolboy[instance].x+12 and schoolboy[instance].y-12 >= MARS_bullet[bullet].y and MARS_bullet[bullet].y <= schoolboy[instance].y+12)
                {
                    schoolboy[instance].exist = 1;
                };
                bullet = bullet + 1;
            };
            if (schoolboy[instance].walk_delay == 0)
            {
                if (first_direction == 0)
                {
                    schoolboy[instance].walked = 0;
                    if (MARS.x > schoolboy[instance].x)
                    {
                        schoolboy[instance].x = schoolboy[instance].x + 16;
                        applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[0]);
                        schoolboy[instance].walked = 1;
                    }
                    else if (MARS.x < schoolboy[instance].x)
                    {
                        schoolboy[instance].x = schoolboy[instance].x - 16;
                        applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[2]);
                        schoolboy[instance].walked = 1;
                    };
                    if (schoolboy[instance].walked = 0)
                    {
                        if (MARS.y > schoolboy[instance].y)
                        {
                            schoolboy[instance].y = schoolboy[instance].y+16;
                            applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[3]);
                            schoolboy[instance].walked = 1;
                        }
                        else if (MARS.y < schoolboy[instance].y)
                        {
                            schoolboy[instance].y = schoolboy[instance].y+16;
                            applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[1]);
                            schoolboy[instance].walked = 1;
                        };
                    };

                };
                if (first_direction == 1)
                {
                    schoolboy[instance].walked = 0;
                    if (MARS.y > schoolboy[instance].y)
                    {
                        schoolboy[instance].y = schoolboy[instance].y+16;
                        applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[3]);
                        schoolboy[instance].walked = 1;
                    }
                    else if (MARS.y < schoolboy[instance].y)
                    {
                        schoolboy[instance].y = schoolboy[instance].y+16;
                        applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[1]);
                        schoolboy[instance].walked = 1;
                    };
                    if (schoolboy[instance].walked = 0)
                    {
                        if (MARS.x > schoolboy[instance].x)
                        {
                            schoolboy[instance].x = schoolboy[instance].x + 16;
                            applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[0]);
                            schoolboy[instance].walked = 1;
                        }
                        else if (MARS.x < schoolboy[instance].x)
                        {
                            schoolboy[instance].x = schoolboy[instance].x - 16;
                            applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[2]);
                            schoolboy[instance].walked = 1;
                        };
                    };
                };
                schoolboy[instance].walk_delay = schoolboy[instance].walk_delay + 1;
            }
            else {schoolboy[instance].walk_delay = schoolboy[instance].walk_delay + 1;};
            if (schoolboy[instance].walk_delay == 10){schoolboy[instance].walk_delay = 0;};
        };
    };
    instance = instance + 1;
};

#endif // SCHOOLBOY_H_INCLUDED

All of my files are involved with each other, especially schoolboy.h, which is why I put them down. If you find the answer, can you explain why that's so. Any help appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2024-12-29 11:52:07

schoolboy.h 末尾的 instance = instance + 1 行看起来像是在 while 循环之外,该循环依赖于它的递增以最终结束。

Your instance = instance + 1 line at the end of schoolboy.h looks like it's outside the while loop that relies on it being incremented to eventually end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文