Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6983515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:25:44+00:00 2026-05-27T18:25:44+00:00

I’ve checked everywhere in my code and I could not find the reason why

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:25:45+00:00Added an answer on May 27, 2026 at 6:25 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.