Hello I'm making a simple break out game using SDL 2 for Switch using C++ and Homebrew sdk. I was able to implement Ball colliding with screen boundaries.But I'm unable to do ball bat collision. Here is my code.You can ignore some switch initialisation code.
My idea was if ball.x == bat.x and ball.y == bat.y then make ball_vel_y = -ball_vel_y
But my idea didn't work out. Is there is any other way to do it.
#include<iostream>
#include<SDL2/SDL.h>
#include<switch.h>
// some switch buttons
#define JOY_A 0
#define JOY_B 1
#define JOY_X 2
#define JOY_Y 3
#define JOY_PLUS 10
#define JOY_MINUS 11
#define JOY_LEFT 12
#define JOY_UP 13
#define JOY_RIGHT 14
#define JOY_DOWN 15
#define KEY_LSTICK_UP 17
#define KEY_LSTICK_DOWN 19
#define KEY_LSTICK_LEFT 16
#define KEY_LSTICK_RIGHT 18
bool Quit = false;
SDL_Event event;
int ball_x = 20;
int ball_y = 50;
int ball_vel_x = 1;
int ball_vel_y = 1;
int bkw = 1920;
int bkh = 1080;
int bkwmin = 0;
int bkhmin = 0;
int bat_x = bkw /2 ;
int bat_y = bkh - 300;;
void BallCollision()
{
if(ball_x <= bkwmin || ball_x >= bkw - 60)
{
ball_vel_x = -ball_vel_x;
}
if(ball_y <= bkhmin || ball_y >= bkh - 300)
{
ball_vel_y = -ball_vel_y;
}
//ball bat collision here
}
void MoveBall()
{
ball_x += ball_vel_x * 5;
ball_y += ball_vel_y * 5;
}
void EventHandler()
{
if(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
Quit = true;
}
else if(event.type == SDL_JOYBUTTONDOWN)
{
if(event.jbutton.button == KEY_LSTICK_LEFT && bat_x > 0)
{
bat_x -= 2*50;
}
else if(event.jbutton.button == KEY_LSTICK_RIGHT && bat_x < bkw - 100)
{
bat_x += 2*50;
}
}
}
}
int main()
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
SDL_Window* Window = SDL_CreateWindow("HelloWorld",0,0,1920, 1080,SDL_WINDOW_OPENGL);
SDL_Renderer* Renderer = SDL_CreateRenderer(Window,0,SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);
romfsInit();
for (int i = 0; i < 2; i++) {
if (SDL_JoystickOpen(i) == NULL) {
SDL_Log("SDL_JoystickOpen: %s\n", SDL_GetError());
SDL_Quit();
return -1;
}
}
SDL_Rect bkrect;
bkrect.x = 0;
bkrect.y = 0;
bkrect.h = 1080;
bkrect.w = 1920;
SDL_Surface* ball = SDL_LoadBMP("romfs:/resources/ball.bmp");
SDL_Texture* balltexture = SDL_CreateTextureFromSurface(Renderer,ball);
SDL_Surface* BackgroundImage = SDL_LoadBMP("romfs:/resources/bk.bmp");
SDL_Texture* BackgroundTexture = SDL_CreateTextureFromSurface(Renderer,BackgroundImage);
SDL_Surface* bat = SDL_LoadBMP("romfs:/resources/bat.bmp");
SDL_Texture* battexture = SDL_CreateTextureFromSurface(Renderer,bat);
while(!Quit)
{
EventHandler();
MoveBall();
BallCollision();
SDL_Rect ballrect;
ballrect.x = ball_x;
ballrect.y = ball_y;
ballrect.h = 60;
ballrect.w = 60;
SDL_Rect batrect;
batrect.x = bat_x;
batrect.y = bat_y;
batrect.w = 100;
batrect.h = 50;
SDL_RenderCopy(Renderer,BackgroundTexture,NULL,&bkrect);
SDL_RenderCopy(Renderer,balltexture,NULL,&ballrect);
SDL_RenderCopy(Renderer,battexture,NULL,&batrect);
SDL_RenderPresent(Renderer);
SDL_RenderClear(Renderer);
}
SDL_DestroyRenderer(Renderer);
romfsExit();
SDL_DestroyWindow(Window);
SDL_Quit();
return 0;
}
Related
I started writing a little game but something with the image is not working. They're working fine at the beginning but after some moments there become black and then they disappear.
Don't be worried about the long code the relevant things with the images happen mostly in the following methods: build_mode_draw() and play_mode_draw().
I tested the program with a blue cube instead of a image and it worked fine
Probably I don't really understand how the image gets loaded
#include <SDL2/SDL.h>
#define WindowWidth 1500
#define WindowHight 800
#define ArrayGrosseBuildMode 100
#define StartFensterBlockmenge 10 // in Plockgröße bezüglich der kleineren Achse
bool end = false;
bool programm_part_run = true;
unsigned int play_mode_speed = 0;
unsigned int counter;
unsigned int counter_2;
unsigned int counter_3;
unsigned int blocksize;
unsigned int blocks_fit_in_X;
unsigned int blocks_fit_in_Y;
unsigned int play_mode_blockamount;
//unsigned int blockamount = 0;
bool build_mode_block_pos[ArrayGrosseBuildMode][ArrayGrosseBuildMode];
unsigned int play_mode_block_pos_X[WindowWidth]; // Fächer beschreiben
unsigned int play_mode_block_pos_Y[WindowHight]; // ||
//mouse variables
unsigned short int pressed_mouse_button = 0; // 0 = no , 1 = left , mouse Button pressed
unsigned int MouseX;
unsigned int MouseY;
//keyboard variables
//set window
SDL_Window* window = NULL;
//set renderer
SDL_Renderer* renderer = NULL;
//set event
SDL_Event event;
void input()
{
SDL_PollEvent(&event);
// reset variables
pressed_mouse_button = 0; // set to no mouse button pressed
switch(event.type)
{
case SDL_QUIT:
end = true;
programm_part_run = false;
break;
case SDL_MOUSEMOTION:
MouseX = event.motion.x;
MouseY = event.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
switch(event.button.button)
{
case SDL_BUTTON_LEFT:
pressed_mouse_button = 1;
break;
}
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_SPACE:
programm_part_run = false;
break;
}
}
}
void put_build_mode_grid_in_renderer()
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
for(counter = 0; counter <= blocks_fit_in_Y; counter = counter + 1)
{
SDL_RenderDrawLine(renderer,0,counter * blocksize,blocks_fit_in_X*blocksize,counter * blocksize);
}
for(counter = 0; counter <= blocks_fit_in_X; counter = counter + 1)
{
SDL_RenderDrawLine(renderer,counter * blocksize,0,counter * blocksize,blocks_fit_in_Y*blocksize);
}
}
void build_mode_draw()
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
SDL_RenderClear(renderer);
put_build_mode_grid_in_renderer();
SDL_Surface * image = SDL_LoadBMP("stealcube.bmp");
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer,image);
SDL_FreeSurface(image);
for(counter = 0; counter <= blocks_fit_in_X; counter = counter + 1)
{
for(counter_2 = 0; counter_2 <= blocks_fit_in_Y; counter_2 = counter_2 + 1)
{
if(build_mode_block_pos[counter][counter_2] == true)
{
SDL_Rect dstrect = { counter * blocksize, counter_2 * blocksize, blocksize, blocksize};
SDL_RenderCopy(renderer, texture, NULL, &dstrect);
}
}
}
SDL_RenderPresent(renderer);
}
void build_mode()
{
while(programm_part_run)
{
input();
if(pressed_mouse_button == 1)
{
build_mode_block_pos[MouseX/blocksize][MouseY/blocksize] = true;
}
build_mode_draw();
}
}
void play_mode_draw()
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
SDL_RenderClear(renderer);
SDL_Surface * image = SDL_LoadBMP("stealcube.bmp");
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer,image);
SDL_FreeSurface(image);
for(counter = 0; counter < play_mode_blockamount; counter = counter + 1)
{
SDL_Rect dstrect = { play_mode_block_pos_X[counter], play_mode_block_pos_Y[counter], blocksize, blocksize};
SDL_RenderCopy(renderer, texture, NULL, &dstrect);
}
SDL_RenderPresent(renderer);
}
void play_mode()
{
counter_3 = 0;
for(counter = 0; counter <= blocks_fit_in_X; counter = counter + 1)
{
for(counter_2 = 0; counter_2 <= blocks_fit_in_Y; counter_2 = counter_2 + 1)
{
if(build_mode_block_pos[counter][counter_2] == true)
{
play_mode_block_pos_X[counter_3] = counter*blocksize;
play_mode_block_pos_Y[counter_3] = counter_2*blocksize;
counter_3 = counter_3 + 1;
}
}
}
play_mode_blockamount = counter_3;
while(programm_part_run)
{
for(counter = 0; counter < play_mode_speed; counter = counter + 1)
{
input();
SDL_Delay(1);
}
for(counter = 0; counter <= play_mode_blockamount; counter = counter + 1)
{
if(play_mode_block_pos_Y[counter] < blocks_fit_in_Y * blocksize - blocksize)
{
play_mode_block_pos_Y[counter] = play_mode_block_pos_Y[counter] + 1;
}
}
play_mode_draw();
}
}
int main (int argc, char** argv)
{
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow
(
"Test Fenster :)", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WindowWidth,
WindowHight,
SDL_WINDOW_SHOWN
);
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
//setup
if(WindowWidth < WindowHight)
{
blocksize = WindowWidth/StartFensterBlockmenge;
}
else
{
blocksize = WindowHight/StartFensterBlockmenge;
}
blocks_fit_in_X = WindowWidth/blocksize;
blocks_fit_in_Y = WindowHight/blocksize;
while(!end)
{
programm_part_run = true;
build_mode();
programm_part_run = true;
play_mode();
}
}
I am making a Map Editor using Allegro 5, and it works with tiles. However, the bitmaps in it are extremely buggy, and I don't really know why.
Here's my code:
#include <allegro5\allegro.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_primitives.h>
#include <allegro5\allegro_native_dialog.h>
#include <stdio.h>
#include <vector>
//ENUMS DEFINITION SECTION
enum TYPE{LEFT,RIGHT,UP,DOWN};
//CLASS DEFINITION SECTION
class bmplist
{
public:
ALLEGRO_BITMAP *bmp;
TYPE type;
};
class T_center
{
public:
//place
int p;
int y;
//place bound y up, x left, y down, x right
int pbyu;
int pbxl;
int pbyd;
int pbxr;
ALLEGRO_BITMAP *img;
bmplist a;
};
class bmpdata
{
public:
bmplist bmp;
};
class bmphold
{
public:
ALLEGRO_BITMAP *bmp;
};
const int tileamount = 196;
T_center tile[tileamount];
int height = 700;
int width = 700;
ALLEGRO_DISPLAY *display;
ALLEGRO_EVENT ev;
ALLEGRO_EVENT_QUEUE *queue;
ALLEGRO_BITMAP *curr;
ALLEGRO_BITMAP *tilelr;
ALLEGRO_BITMAP *tileud;
ALLEGRO_BITMAP *tileltd;
ALLEGRO_BITMAP *tileltu;
ALLEGRO_BITMAP *tilertd;
ALLEGRO_BITMAP *tilertu;
ALLEGRO_BITMAP *pathup;
ALLEGRO_BITMAP *pathdown;
ALLEGRO_BITMAP *pathleft;
ALLEGRO_BITMAP *pathright;
ALLEGRO_FONT *actual;
bool isClicked;
bmplist bmps[2];
bool MAPMOUNTING = true;
bool MAPCOORDINATING = false;
bmplist arrows[4];
bmplist bmpindex[20];
bmpdata arrowindex[4];
char tilenow[15];
//FUNCTION DECLARATION SECTION
void func_start_system();
void func_start_tiles(int tilesize, T_center *tile, int tilearray, int xsize);
void func_start_variables();
void func_detect_mouse_move(T_center *tile, int tilearray, int bmpsize);
void mapper_init();
//THE TOOL ITSELF
int main()
{
al_init();
al_init_font_addon();
al_init_image_addon();
al_init_primitives_addon();
mapper_init();
}
//FUNCTION DEFINITION SECTION
void func_start_system()
{
al_install_keyboard();
al_install_mouse();
}
void func_start_tiles(int tilesize, T_center *tile, int tilearray, int xsize)
{
int xcount = 0;
int tile_x = tilesize;
int tile_y = tilesize;
int y_count = 1;
for (int i = 0; i < tilearray; i++)
{
if (xcount == 14)
{
xcount = 0;
tile_y = tile[i-1].pbyd + tilesize;
tile_x = tilesize;
}
tile[i].p = tile_x, tile[i].y = tile_y, tile[i].pbxl = tile_x - tilesize, tile[i].pbxr = tile_x + tilesize, tile[i].pbyu = tile_y - tilesize, tile[i].pbyd = tile_y + tilesize;
tile_x = tile[i].pbxr + tilesize;
printf("%i, %i, left: %i\n", tile[i].p, tile[i].y, tile[i].pbxl);
xcount++;
}
}
void func_start_variables()
{
display = al_create_display(width, height);
tilelr = al_load_bitmap("tilelr.png");
tileud = al_load_bitmap("tileud.png");
tileltu = al_load_bitmap("tireltu.png"), tileltd = al_load_bitmap("tireltd.png"), tilertu = al_load_bitmap("tirertu.png"), tilertd = al_load_bitmap("tirertd.png");
pathup = al_load_bitmap("up.png"), pathdown = al_load_bitmap("down.png"), pathleft = al_load_bitmap("left.png"), pathright = al_load_bitmap("right.png");
arrows[1].bmp = al_load_bitmap("up.png"), arrows[1].type = UP, arrows[2].bmp = al_load_bitmap("down.png"), arrows[2].type = DOWN;
arrows[3].bmp = al_load_bitmap("left.png"), arrows[3].type = LEFT, arrows[4].bmp = al_load_bitmap("right.png"), arrows[4].type = RIGHT;
bmpindex[1].bmp = tilelr, bmpindex[2].bmp = tileud, bmpindex[3].bmp = tileltu, bmpindex[4].bmp = tileltd, bmpindex[5].bmp = tilertu, bmpindex[6].bmp = tilertd;
bmpindex[7].bmp = pathup, bmpindex[7].type = UP, bmpindex[8].bmp = pathdown, bmpindex[8].type = DOWN;
bmpindex[9].bmp = pathleft, bmpindex[9].type = LEFT, bmpindex[10].bmp = pathright, bmpindex[10].type = RIGHT;
curr = bmpindex[1].bmp;
actual = al_load_font("app850.ttf", 12, 0);
queue = al_create_event_queue();
al_register_event_source(queue, al_get_mouse_event_source());
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_display_event_source(display));
}
void func_detect_mouse_move(T_center *tile, int tilearray, int bmpsize)
{
int index = 1;
int a = true;
while (a)
{
printf("%i", index);
al_clear_to_color(al_map_rgb(0, 0, 0));
for (int i = 0; i < tilearray; i++)
{
if (tile[i].img != NULL)
al_draw_bitmap(tile[i].img, tile[i].pbxl, tile[i].pbyu, 0);
if (tile[i].a.bmp != NULL)
al_draw_bitmap(tile[i].a.bmp, tile[i].pbxl, tile[i].pbyu, 0);
}
al_wait_for_event(queue, &ev);
{
if (ALLEGRO_EVENT_MOUSE_AXES)
{
for (int i = 0; i < tilearray; i++)
if (ev.mouse.x >= tile[i].pbxl && ev.mouse.x < tile[i].pbxr && ev.mouse.y >= tile[i].pbyu && ev.mouse.y < tile[i].pbyd)
{
al_draw_tinted_bitmap(bmpindex[index].bmp, al_map_rgb(200, 200, 200), tile[i].pbxl, tile[i].pbyu, 0);
al_flip_display();
}
}
if (ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
{
if (ev.mouse.button & 1)
for (int i = 0; i < tilearray; i++)
{
if (ev.mouse.x >= tile[i].pbxl && ev.mouse.x < tile[i].pbxr && ev.mouse.y >= tile[i].pbyu && ev.mouse.y < tile[i].pbyd)
{
if (bmpindex[index].type == NULL)
{
tile[i].img = curr;
al_flip_display();
}
if (bmpindex[index].type != NULL)
{
tile[i].a.bmp = bmpindex[index].bmp;
tile[i].a.type = bmpindex[index].type;
}
}
}
if (ev.mouse.button & 2)
for (int i = 0; i < tilearray; i++)
{
if (ev.mouse.x >= tile[i].pbxl && ev.mouse.x < tile[i].pbxr && ev.mouse.y >= tile[i].pbyu && ev.mouse.y < tile[i].pbyd)
{
tile[i].img = NULL;
al_flip_display();
}
}
if (ALLEGRO_EVENT_KEY_DOWN)
{
if (ev.keyboard.keycode == ALLEGRO_KEY_1)
index = 1;
if (ev.keyboard.keycode == ALLEGRO_KEY_2)
index = 2;
if (ev.keyboard.keycode == ALLEGRO_KEY_3)
index = 3;
if (ev.keyboard.keycode == ALLEGRO_KEY_4)
index = 4;
if (ev.keyboard.keycode == ALLEGRO_KEY_5)
index = 5;
if (ev.keyboard.keycode == ALLEGRO_KEY_6)
index = 6;
if (ev.keyboard.keycode == ALLEGRO_KEY_UP)
index = 7;
if (ev.keyboard.keycode == ALLEGRO_KEY_DOWN)
index = 8;
if (ev.keyboard.keycode == ALLEGRO_KEY_LEFT)
index = 9;
if (ev.keyboard.keycode == ALLEGRO_KEY_RIGHT)
index = 10;
al_flush_event_queue(queue);
}
curr = bmpindex[index].bmp;
}
}
}
}
void mapper_init()
{
func_start_system();
func_start_variables();
func_start_tiles(25, tile, tileamount, tileamount);
al_flip_display();
func_detect_mouse_move(tile, tileamount, 10);
}
The problem with this map editor is that sometimes, just by moving the mouse, the index, which is an integer that selects a bitmap in the bmpindex array, bugs out and a completely random bitmap appears in the mouse's position. For example, I am placing some bitmaps on the screen with a number 2 index, but then all of a sudden the index number becomes 4. Also, whenever I press a key in the keyboard, the actual selected bitmap is drawn in a completely random part of the screen. Why is this bug happening? How can I fix it?
I'd say it's because you have a small typo there. Those conditions, i.e.:
if (ALLEGRO_EVENT_MOUSE_AXES)
if (ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
if (ALLEGRO_EVENT_KEY_DOWN)
Will always evaluate as TRUE and therefore the statements will always run. This way you'll end up in situations you described that does not seem make sense (at least on the first sight) but that actually are quite logical
Like reading ev.mouse.x even though mouse did not move at all (but that did not matter because your condition if (ALLEGRO_EVENT_MOUSE_AXES) is always true) and contents of ev do not match mouse union part because it could have been the keyboard that caused the event, so the resulting coordinates are nonsensical (because there were none in that part of the memory)
Solution is actually quite simple - you just need to check type of ev for that value in condition, i.e. replace:
if (ALLEGRO_EVENT_MOUSE_AXES)
With:
if (ev.type == ALLEGRO_EVENT_MOUSE_AXES)
And so on.
I am attempting to make a simple scrolling shooter game with SDL2. I have a moving player on a screen, and I am trying to make the player shoot a bullet using an array (so they can shoot multiple bullets) however, when I press the space bar, nothing happens, and instead the bullet image sort of flashes in the top left corner.
Heres the same code in codepad: http://codepad.org/rOhE1AqY
#include <SDL.h>
#include <stdio.h> //use for things like printf, same as cout
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
//screend dimensions& sprtie dimensions
const int SCREEN_HEIGHT = 600;
const int SCREEN_WIDTH = 400;
const int SPRITE_WIDTH = 60;
const int SPRITE_HEIGHT = 80;
const int MAX_BULLETS = 50;
SDL_Window* Window = NULL;//the window rendering to
SDL_Surface* ScreenSurface = NULL;//surface contained by window
SDL_Surface* Background = NULL;
SDL_Surface* Player = NULL;
SDL_Surface* Enemy = NULL;
SDL_Surface* Bullet = NULL;
SDL_Surface* newBullet = NULL;
SDL_Rect posPlayer, posEnemy, posBullet, posnewBullet;
const Uint8* keystate = SDL_GetKeyboardState(NULL);
SDL_Event event;
class thePlayer
{
public:
thePlayer();
void player_movement();
void show_player();
private:
};
class theBullet
{
public:
theBullet();
bool isActive;
int x_position;
int y_position;
void bullet_movement();
void add_new_bullet();
void show_bullet();
private:
};
theBullet arrayofBullets[MAX_BULLETS];
class theEnemy
{
public:
theEnemy();
void enemy_movement();
void show_enemy();
private:
};
thePlayer::thePlayer()
{
posPlayer.x = 170;
posPlayer.y = SCREEN_HEIGHT;
posPlayer.w = 20;
posPlayer.h = 30;
}
void thePlayer::player_movement()
{
if(keystate[SDL_SCANCODE_LEFT])
{
posPlayer.x -= 2;
}
if(keystate[SDL_SCANCODE_RIGHT])
{
posPlayer.x += 2;
}
if(keystate[SDL_SCANCODE_UP])
{
posPlayer.y -= 2;
}
if(keystate[SDL_SCANCODE_DOWN])
{
posPlayer.y += 2;
}
if ((posPlayer.x + SPRITE_WIDTH) > SCREEN_WIDTH)
{
posPlayer.x = (SCREEN_WIDTH - SPRITE_WIDTH);
}
if ((posPlayer.y + SPRITE_HEIGHT) > SCREEN_HEIGHT)
{
posPlayer.y = (SCREEN_HEIGHT - SPRITE_HEIGHT);
}
}
void thePlayer::show_player()
{
SDL_BlitSurface(Player, NULL, ScreenSurface, &posPlayer);
SDL_SetColorKey(Player, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255));
}
theBullet::theBullet()
{
/*posBullet.x;
posBullet.y;
posBullet.w = 10;
posBullet.h = 15;*/
}
void theBullet::bullet_movement()
{
/*if(keystate[SDL_SCANCODE_SPACE])
{
posBullet.x = posPlayer.x + 25;
posBullet.y = posPlayer.y + 10;
}
posBullet.y -= 2;
if(posBullet.y < 0)
{
posBullet.y = -50;
}*/
}
void theBullet::show_bullet()
{
//SDL_BlitSurface(Bullet, NULL, ScreenSurface, &posBullet);
//SDL_SetColorKey(Bullet, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255));//removes white background
}
theEnemy::theEnemy()
{
srand (time(NULL));
posEnemy.x = rand() % 300 + 50;
posEnemy.y =0;
posEnemy.w = 35;
posEnemy.h = 60;
}
void theEnemy::enemy_movement()
{
posEnemy.y += 1;
if(posEnemy.y > SCREEN_HEIGHT)
{
posEnemy.y = SCREEN_HEIGHT +50;
}
}
void theEnemy::show_enemy()
{
SDL_BlitSurface(Enemy, NULL, ScreenSurface, &posEnemy);
SDL_SetColorKey(Enemy, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255));
}
bool initialise()
{
bool success = true;
if (SDL_Init(SDL_INIT_EVERYTHING) !=0)
{
cout<<"SDL_Init Error."<<SDL_GetError()<<endl;
success = false;
}
else
{
//create the window for game
Window = SDL_CreateWindow("Scrolling Shooter Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (Window == NULL)
{
cout<<"Window Error"<<SDL_GetError()<<endl;
success = false;
}
else
{
//get window surface
ScreenSurface = SDL_GetWindowSurface(Window);
}
}
return success;
}
bool LoadingMedia()
{
bool success = true;
Background = SDL_LoadBMP("background.bmp");
if (Background == NULL)
{
cout<<"Error in loading background."<<SDL_GetError()<<endl;
success = false;
}
Player = SDL_LoadBMP("spaceship.bmp");
if (Player == NULL)
{
cout<<"Error in loading player."<<SDL_GetError()<<endl;
success = false;
}
Enemy = SDL_LoadBMP("enemy.bmp");
if (Enemy == NULL)
{
cout<<"Error in loading enemy."<<SDL_GetError()<<endl;
success = false;
}
Bullet = SDL_LoadBMP("bullet.bmp");
if (Bullet == NULL)
{
cout<<"Error in loading bullet."<<SDL_GetError()<<endl;
success = false;
}
return success;
}
void closedown()
{
SDL_FreeSurface(Background);
Background = NULL;
SDL_FreeSurface(Player);
Player = NULL;
SDL_FreeSurface(Enemy);
Enemy = NULL;
SDL_DestroyWindow(Window);
Window = NULL;
SDL_Quit();
}
int main(int argc, char** argv)
{
bool quit = false;
thePlayer myPlayer;
theEnemy myEnemy;
theBullet myBullet;
if (!initialise())
{
cout<<"Failed to initialise"<<SDL_GetError()<<endl;
}
else
{
if (!LoadingMedia())
{
cout<<"Error loading media"<<SDL_GetError()<<endl;
}
}
//makes all bullets false
for (int i=0; i<MAX_BULLETS; i++)
{
arrayofBullets[i].isActive = false;
}
//GAME LOOP
while (quit == false)
{
SDL_BlitSurface(Background, NULL, ScreenSurface, NULL);
myPlayer.show_player();
myPlayer.player_movement();
while (SDL_PollEvent(&event))
{
if( event.type == SDL_QUIT )
{
quit = true;
break;
}
if(keystate[SDL_SCANCODE_SPACE])
{
for (int i=0; i<MAX_BULLETS; i++)
{
if (arrayofBullets[i].isActive == false)
{
arrayofBullets[i].x_position = posPlayer.x + 25;
arrayofBullets[i].y_position = posPlayer.y + 10;
arrayofBullets[i].isActive = true;
break;
}
}
}
//update game objects
for (int i=0; i<MAX_BULLETS; i++)
{
if (arrayofBullets[i].isActive == true)
{
arrayofBullets[i].y_position -= 2;
if (arrayofBullets[i].y_position < 0)
{
arrayofBullets[i].isActive = false;
}
}
}
for (int i=0; i<MAX_BULLETS; i++)
{
if (arrayofBullets[i].isActive == true)
{
SDL_BlitSurface(Bullet, NULL, ScreenSurface, NULL);
}
}
}
//myPlayer.show_player();
//myBullet.show_bullet();
//myEnemy.show_enemy();
//myPlayer.player_movement();
//myBullet.bullet_movement();
//myEnemy.enemy_movement();
SDL_UpdateWindowSurface(Window); //updates screen
}
closedown();
return 0;
}
SDL_BlitSurface(Bullet, NULL, ScreenSurface, NULL);
You haven't specified destination rect, so it will blit on left-top corner.
It should be
SDL_Rect dstrect;
dstrect.x = arrayofBullets[i].x_position;
dstrect.y = arrayofBullets[i].y_position;
dstrect.w = Bullet->w;
dstrect.h = Bullet->h;
SDL_BlitSurface(Bullet, NULL, ScreenSurface, &dstrect);
I am making a TBS game, at least im trying to.
So i've started off by following a tutorial on lazyfoo.net (lesson 29) on how to make a tiled environment and making my own changes along the way.
Every time i try to compile it it gives me two main errors:
No. 1: 255 error: expected primary-expression before ',' token
No. 2: 267 error: expected ';' before 'myUnit'
Here is the source code, i am fairly certain the that the problem is not with the images or the map.
CODE:
#include "SDL/SDL_image.h"
#include "SDL/SDL.h"
#include <string>
#include <fstream>
const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;
const int FPS = 30;
const int UNIT_WIDTH = 50;
const int UNIT_HEIGHT = 50;
const int LEVEL_WIDTH = 600;
const int LEVEL_HEIGHT = 600;
const int TILE_WIDTH = 60;
const int TILE_HEIGHT = 60;
const int TOTAL_TILES = 100;
const int TILE_SPRITES = 3;
const int TILE_GRASS = 0;
const int TILE_WATER = 1;
const int TILE_MOUNTAIN = 2;
SDL_Surface *screen = NULL;
SDL_Surface *Unit = NULL;
SDL_Surface *tileMap = NULL;
SDL_Rect clips[ TILE_SPRITES ];
SDL_Event occur;
class Tile
{
private:
SDL_Rect box;
int type;
public:
Tile(int x, int y, int tileType);
void show();
int get_type();
SDL_Rect get_box();
};
class Unit
{
private:
SDL_Rect Box;
bool movement;
public:
Unit();
void handle_input();
void move( Tile *tiles[]);
void show();
};
SDL_Surface *load_image(std::string filename)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load(filename.c_str());
if(loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
if(optimizedImage != NULL)
{
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0,0xff,0xff));
}
}
return optimizedImage;
}
void apply_surface(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);
}
bool init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
if(screen == NULL)
{
return false;
}
SDL_WM_SetCaption("Strategy Game", NULL);
return true;
}
bool load_files()
{
Unit = load_image("infantry_red.png");
if(Unit == NULL)
{
return false;
}
tileMap = load_image("tilemap.png");
if( tileMap == NULL)
{
return false;
}
return 0;
}
void clean_up(Tile *tiles[])
{
SDL_FreeSurface(Unit);
SDL_FreeSurface(tileMap);
for(int t = 0;t < TOTAL_TILES; t++)
{
delete tiles[ t ];
}
SDL_Quit();
}
void clip_tiles()
{
clips[TILE_GRASS].x = 0;
clips[TILE_GRASS].y = 0;
clips[TILE_GRASS].w = TILE_WIDTH;
clips[TILE_GRASS].h = TILE_HEIGHT;
clips[TILE_WATER].x = 60;
clips[TILE_WATER].y = 0;
clips[TILE_WATER].w = TILE_WIDTH;
clips[TILE_WATER].h = TILE_HEIGHT;
clips[TILE_MOUNTAIN].x = 120;
clips[TILE_MOUNTAIN].y = 0;
clips[TILE_MOUNTAIN].w = TILE_WIDTH;
clips[TILE_MOUNTAIN].h = TILE_HEIGHT;
}
bool set_tiles( Tile *tiles[])
{
int x = 0, y = 0;
std::ifstream map("strategy_game.map");
if(map == NULL)
{
return false;
}
for(int t = 0; y < TOTAL_TILES; t++)
{
int tileType = -1;
map >> tileType;
if(map.fail() == true)
{
map.close();
return false;
}
if( (tileType >= 0) && (tileType < TILE_SPRITES))
{
tiles[t] = new Tile(x,y,tileType);
}
else
{
map.close();
return false;
}
x += TILE_WIDTH;
if(x >= LEVEL_WIDTH)
{
x = 0;
y += TILE_HEIGHT;
}
}
map.close();
return true;
}
Tile::Tile(int x, int y, int tileType)
{
box.x = x;
box.y = y;
box.w = TILE_WIDTH;
box.h = TILE_HEIGHT;
type = tileType;
}
void Tile::show()
{
apply_surface(box.x, box.y, tileMap, screen, &clips[type]);
}
int Tile::get_type()
{
return type;
}
SDL_Rect Tile::get_box()
{
return box;
}
Unit::Unit()
{
Box.x = 0;
Box.y = 0;
Box.w = UNIT_WIDTH;
Box.h = UNIT_HEIGHT;
}
SDL_Rect rect;
int mouseX,mouseY;
void Unit::handle_input()
{
if(occur.type == SDL_MOUSEBUTTONDOWN)
{
mouseX = occur.button.x;
mouseY = occur.button.y;
}
}
void Unit::move(Tile *tiles[])
{
Box.x += mouseX;
if( Box.x < 0 || Box.x + UNIT_WIDTH > LEVEL_WIDTH )
{
Box.x -= mouseX;
}
Box.y -= mouseY;
if( Box.y < 0 || Box.y + UNIT_HEIGHT > LEVEL_HEIGHT)
{
Box.y -= mouseY;
}
}
void Unit::show()
{
int BoxX;
int BoxY;
Box.x = BoxX;
Box.y = BoxY;
SDL_Rect unitOffset;
unitOffset.x = BoxX;
unitOffset.y = BoxY;
SDL_BlitSurface(Unit,NULL,screen,unitOffset);
}
Uint32 start;
int main(int argc, char* args[])
{
bool quit = false;
Unit myUnit;
Tile *tiles[TOTAL_TILES];
if(init() == false)
{
return 1;
}
if(load_files() == false)
{
return 1;
}
clip_tiles();
if( set_tiles(tiles) == false)
{
return 1;
}
while(quit == false)
{
start = SDL_GetTicks();
while(SDL_PollEvent(&occur));
{
myUnit.handle_input();
switch(occur.type)
{
case SDL_QUIT:
quit = false;
break;
}
}
myUnit.move(tiles);
for(int t = 0;t<TOTAL_TILES;t++)
{
tiles[t]->show();
}
myUnit.show();
//render
SDL_FillRect(screen,&screen->clip_rect,0);
SDL_Flip(screen);
if(1000/FPS > SDL_GetTicks() - start ){
SDL_Delay(1000/FPS - (SDL_GetTicks()-start));
}
}
clean_up( tiles );
return 0;
}
Remove semi-colon from while(SDL_PollEvent(&occur))
Change: SDL_Surface *Unit = NULL; to `SDL_Surface *unitSurf = NULL;'
Change:
Unit = load_image("infantry_red.png");
if(Unit == NULL)
{
return false;
}
to
unitSurf = load_image("infantry_red.png");
if(unitSurf == NULL)
{
return false;
}
Change: SDL_BlitSurface(Unit,NULL,screen,unitOffset); to SDL_BlitSurface(unitSurf,NULL,screen,unitOffset);
Remove the semicolon after while(SDL_PollEvent(&occur))
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!
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.