• Advertisement
Sign in to follow this  

[ASAP]2-d Side Scroller [Allegro-CPP]

This topic is 2196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Looking to code a 2-d side scroller, where in a ball rolls from left to right, and obstacles such as triangles and squares and combinations of the two are faced.
Looking for :

1. In the code below, the jumping mechanism seems to be in some problem, as i made functions for Ball movement(previously it was working well).
2. The triangles and squares(randomly generated) seems to overlap sometimes.
[attachment=7389:ball-tri2.JPG]
3. How shall i make scroll sideways, such as Mario, (you keep going towards right and things keep coming), i.e if i need to implement the camera type simulation, how shall i go ahead with it ?

PS : I am new to game programming. Your help will be duly appreciated.




#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include "objects.h"

const int WIDTH = 1000;
const int HEIGHT = 400;
const int NUM_TRIANGLES = 6;

enum KEYS{LEFT, RIGHT, SPACE};



//prototypes

void InitBall(Ball &shapeB);
void DrawBall(Ball &shapeB);
void MoveLeftBall(Ball &shapeB);
void MoveRightBall(Ball &shapeB);
void JumpBall(Ball &shapeB);


void InitTriangle(Triangle shapeT[], int size);
void DrawTriangle(Triangle shapeT[], int size);
void StartTriangle(Triangle shapeT[], int size);
void UpdateTriangle(Triangle shapeT[], int size);

int main(void)
{
bool done = false;
bool redraw = true;
// bool canJump = false;

// float pos_x = 30;
// float pos_y = 380;
float speed =0;

// float dx =1, dy = 1;

bool keys[3] = {false,false,false};

int FPS = 60;


Triangle shapeT[NUM_TRIANGLES];
Ball shapeB;

ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer;
//Initialization Functions



if(!al_init()) //initialize Allegro
return -1;

display = al_create_display(WIDTH, HEIGHT); //create our display object

if(!display) //test display object
return -1;

al_init_primitives_addon();
al_install_keyboard();


srand(time(NULL));
InitTriangle(shapeT, NUM_TRIANGLES);
InitBall(shapeB);
event_queue = al_create_event_queue();
timer = al_create_timer(1.0/FPS);


al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_timer_event_source(timer));

al_start_timer(timer);

while(!done)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);

if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_LEFT:
keys

= true;
break;
case ALLEGRO_KEY_RIGHT:
keys

= true;
break;
case ALLEGRO_KEY_SPACE:
keys[SPACE] = true;
break;
}

}
else if(ev.type == ALLEGRO_EVENT_KEY_UP)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_LEFT:
keys

= false;
break;
case ALLEGRO_KEY_RIGHT:
keys

= false;
break;
case ALLEGRO_KEY_ESCAPE:
done=true;
break;
case ALLEGRO_KEY_SPACE:
keys[SPACE] = false;
break;
}
}


else if(ev.type == ALLEGRO_EVENT_TIMER)
{
redraw = true;
if(keys

)
MoveLeftBall(shapeB);
if(keys

)
MoveRightBall(shapeB);



if (shapeB.pos_Y >= 380)
{ // means the ball is on the ground
if(keys[SPACE])
{
//JumpBall(shapeB);
if (shapeB.canJump)
{
shapeB.dy= -10;
shapeB.canJump = false;
}
}
else
shapeB.canJump = true;
}



if(shapeB.pos_Y < 380)
shapeB.dy += 0.5;


//JumpBall(shapeB);
if (keys[SPACE] && shapeB.pos_Y <=380 && shapeB.dy > 0)
shapeB.dy -= 0.1; // got to be LESS than 0.5 in this example

if(shapeB.dy>5)
shapeB.dy = 5;

speed +=0.01;
//shapeB.pos_X +=speed*1/2;
shapeB.pos_Y +=shapeB.dy;

if(shapeB.pos_Y > 380)
shapeB.pos_Y = 380;


StartTriangle(shapeT, NUM_TRIANGLES);
UpdateTriangle(shapeT, NUM_TRIANGLES);
}


if(redraw && al_is_event_queue_empty(event_queue))
{
redraw= false;
/*al_draw_filled_circle(pos_x,pos_y,20,al_map_rgb(255,0,255));*/
//UpdateTriangle(shapeT,NUM_TRIANGLES);
DrawBall(shapeB);
DrawTriangle(shapeT,NUM_TRIANGLES);

//al_draw_rounded_rectangle(180,340,260,400,5,5,al_map_rgb(255,255,255),3);
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));


}
}

//al_rest(5.0);

al_destroy_display(display); //destroy our display object

return 0;
}



void InitBall(Ball &shapeB)
{
shapeB.ID=BALL;
shapeB.pos_X= 30;
shapeB.pos_Y= 380;
shapeB.lives = 3;
shapeB.speed = 7;
shapeB.score = 0;
shapeB.boundX = 57;
shapeB.boundY = 57;
shapeB.dx = 1;
shapeB.dy = 1;

}
void DrawBall(Ball &shapeB)
{
al_draw_filled_circle(shapeB.pos_X,shapeB.pos_Y,20,al_map_rgb(255,0,255));
}
void MoveLeftBall(Ball &shapeB)
{
shapeB.pos_X -=shapeB.speed;
if(shapeB.pos_X<20)
shapeB.pos_X = 20;
}
void MoveRightBall(Ball &shapeB)
{
shapeB.pos_X += shapeB.speed;
if(shapeB.pos_X>600)
shapeB.pos_X = 600;
}
void JumpBall(Ball &shapeB)
{
if (shapeB.canJump)
{
shapeB.dy= -10;
shapeB.canJump = false;
}

else
{
shapeB.canJump = true;
}

if(shapeB.pos_Y < 380)
shapeB.dy += 0.5;

}



void InitTriangle(Triangle shapeT[], int size)
{
for(int i=0; i<size; i++)
{
shapeT.ID = TRIANGLE;
shapeT.live = false;
shapeT.speed = 2;
shapeT.random = 1 + rand() % 10;

}
}

void DrawTriangle(Triangle shapeT[], int size)
{
for(int i=0; i<size; i++)
{
if(shapeT.live)
{

//if(shapeT.pos_x3 == shapeT[i+1].pos_x1 || shapeT.pos_x3 +100<= shapeT[i+1].pos_x1)
//{
/*&& shapeT.pos_x3 == shapeT.pos_rect_x1 || shapeT.pos_x3 + 400 <= shapeT.pos_rect_x1
|| shapeT.pos_x1 >= shapeT.pos_rect_x2) */

if(shapeT.random < 5)
al_draw_triangle(shapeT.pos_x1,shapeT.pos_y1,shapeT.pos_x2,shapeT.pos_y2,
shapeT.pos_x3,shapeT.pos_y3,al_map_rgb(255,0,255),2);
else
al_draw_rounded_rectangle(shapeT.pos_rect_x1,shapeT.pos_rect_y1,
shapeT.pos_rect_x2,shapeT.pos_rect_y2,5,5,al_map_rgb(255,255,255),3);
//}
}
}
}

void StartTriangle(Triangle shapeT[], int size)
{
for (int i = 0 ; i < size; i++)
{
if(!shapeT.live)
{
if(rand() % 500 == 0)
{
shapeT.live = true;
shapeT.pos_x1 = 780 + rand() % 20;
shapeT.pos_y1 = 400;
shapeT.pos_x2 = shapeT.pos_x1+10;
shapeT.pos_y2 = 370;
shapeT.pos_x3 = shapeT.pos_x1+20;
shapeT.pos_y3 = 400;

shapeT.pos_rect_x1 = shapeT.pos_x3 + 20;
shapeT.pos_rect_y1 = 320;
shapeT.pos_rect_x2 = shapeT.pos_rect_x1 + 80;
shapeT.pos_rect_y2 = 400;

break;

}
}
}
}

void UpdateTriangle(Triangle shapeT[], int size)
{
for(int i=0; i<size; i++)
{
if(shapeT.live)
{
shapeT.pos_x1 -= shapeT.speed;
shapeT.pos_x2 -= shapeT.speed;
shapeT.pos_x3 -= shapeT.speed;
shapeT.pos_rect_x1 -= shapeT.speed;
shapeT.pos_rect_x2 -= shapeT.speed;


if(shapeT.pos_x3 <0 || shapeT.pos_rect_x2 <0)
shapeT.live = false;
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement