Sign in to follow this  
vary

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

Recommended Posts

vary    100
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.




[code]#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[LEFT] = true;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = 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[LEFT] = false;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = 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[LEFT])
MoveLeftBall(shapeB);
if(keys[RIGHT])
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[i].ID = TRIANGLE;
shapeT[i].live = false;
shapeT[i].speed = 2;
shapeT[i].random = 1 + rand() % 10;

}
}

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

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

if(shapeT[i].random < 5)
al_draw_triangle(shapeT[i].pos_x1,shapeT[i].pos_y1,shapeT[i].pos_x2,shapeT[i].pos_y2,
shapeT[i].pos_x3,shapeT[i].pos_y3,al_map_rgb(255,0,255),2);
else
al_draw_rounded_rectangle(shapeT[i].pos_rect_x1,shapeT[i].pos_rect_y1,
shapeT[i].pos_rect_x2,shapeT[i].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[i].live)
{
if(rand() % 500 == 0)
{
shapeT[i].live = true;
shapeT[i].pos_x1 = 780 + rand() % 20;
shapeT[i].pos_y1 = 400;
shapeT[i].pos_x2 = shapeT[i].pos_x1+10;
shapeT[i].pos_y2 = 370;
shapeT[i].pos_x3 = shapeT[i].pos_x1+20;
shapeT[i].pos_y3 = 400;

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

break;

}
}
}
}

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


if(shapeT[i].pos_x3 <0 || shapeT[i].pos_rect_x2 <0)
shapeT[i].live = false;
}
}
}[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this