Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [urgent] 2-d Side Scroller Game in CPP

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
[urgent] 2-d Side Scroller Game in CPP
varynoob
Member #14,058
February 2012

Hello everyone,

I am new to game programming, i am looking to code a 2-d side scroller which could do such things :
As the game begins, the ball starts rolling from left to right, and speeds up as the time goes on.
In its path, it faces obstacles like Triangles and Squares and combination of the two at random distances.
The player needs to jump the ball over the obstacles, suppose he hits the triangle he loses a life and stops if he hits a square and needs to jump over it, roll on the top and go ahead.
I need to code this very urgently, and i am confused in the proper C++ and ALLEGRO coding for
1. JUMP
2. Random Triangles and Squares.

Waiting for your replies.

Arvidsson
Member #4,603
May 2004

Why is this urgent?

There are many threads in this forum on how to implement jumping in platformers. Try to do a search and see if that helps you.

What do you mean by random triangles and squares? Do you know how to generate random numbers?

Also: showing some code always helps.

varynoob
Member #14,058
February 2012

I need to submit this in 4 days!

By random Triangles and Squares, i mean. when a ball rolls from right to left, triangles and squares and their combinations such as triangle-triangle, triangle-square etc are encountered as obstacles, and the player needs to jump over these to move ahead.

Yes i know the SRAND dunction, but how tu use it for these figures, is still cryptic.

I'll search again for JUMP, was looking for a simpler logic so as to understand it quickly and apply easily.

jmasterx
Member #11,410
October 2009

You can do jumping simply by having a vector to represent the velocity of a ball going in a direction. What you do is, when the player jumps, the Y value of the vector is increased. Each frame, the position of the ball increases by this vector. In addition, each frame the Y value of the vector is affected thus producing the parabola of jumping. To make sure the player doesn't keep jumping, you can figure out if he is on top of an object by finding the normal or some other hackish way. You only let the ball jump when it is on top of something.

For the other part, if your level is a plane, you could move by random increments along the plane and at each stop, if random number is 0, put triangle, else, put square or something.

Arvidsson
Member #4,603
May 2004

So this is homework?

As I have recently been implementing some jump mechanics myself, I took the time to produce a short tutorial covering one technique on how to implement jumping. It's based on some code you could have found if you had done a search :P I hope it's of use to you!

I guess you meant the rand() function. If you can generate random numbers within a range, you should then be able to generate the coordinates for your triangles and squares, and also what order they should come in.

It's easier to help if you show some code, and explain in more detail what you're having problem with.

varynoob
Member #14,058
February 2012

Thank you so much to both of you, will try and implement things and will post the code asap with the help needed.

PS : It is not a homework, i am an engineering graduate. So no more curriculum studies for now, but it is more of a demo kind of game, i need to submit. Your help may prove of a career paving path for me.

The code i have been stuck at :

PRESSING SPACE KEEPS THE BALL IN AIR!! REQUEST YOU TO CORRECT ME:-/

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_primitives.h> 3 4 5const int WIDTH = 1000; 6const int HEIGHT = 400; 7enum KEYS{LEFT, RIGHT, SPACE}; 8 9int main(void) 10{ 11 bool done = false; 12 bool redraw = true; 13 14 int pos_x = 30; 15 int pos_y = 370; 16 bool keys[3] = {false,false,false}; 17 18 int FPS = 60; 19 20 21 ALLEGRO_DISPLAY *display = NULL; 22 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 23 ALLEGRO_TIMER *timer; 24 //Initialization Functions 25 if(!al_init()) //initialize Allegro 26 return -1; 27 28 display = al_create_display(WIDTH, HEIGHT); //create our display object 29 30 if(!display) //test display object 31 return -1; 32 33 al_init_primitives_addon(); 34 al_install_keyboard(); 35 36 event_queue = al_create_event_queue(); 37 timer = al_create_timer(1.0/FPS); 38 39 40 al_register_event_source(event_queue, al_get_keyboard_event_source()); 41 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 42 43 al_start_timer(timer); 44 45 while(!done) 46 { 47 ALLEGRO_EVENT ev; 48 al_wait_for_event(event_queue, &ev); 49 50 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 51 { 52 switch(ev.keyboard.keycode) 53 { 54 /*case ALLEGRO_KEY_UP: 55 keys[UP] = true; 56 break; 57 case ALLEGRO_KEY_DOWN: 58 keys[DOWN] = true; 59 break;*/ 60 case ALLEGRO_KEY_LEFT: 61 keys[LEFT] = true; 62 break; 63 case ALLEGRO_KEY_RIGHT: 64 keys[RIGHT] = true; 65 break; 66 case ALLEGRO_KEY_SPACE: 67 keys[SPACE] = true; 68 break; 69 } 70 71 } 72 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 73 { 74 switch(ev.keyboard.keycode) 75 { 76 case ALLEGRO_KEY_LEFT: 77 keys[LEFT] = false; 78 break; 79 case ALLEGRO_KEY_RIGHT: 80 keys[RIGHT] = false; 81 break; 82 case ALLEGRO_KEY_ESCAPE: 83 done=true; 84 case ALLEGRO_KEY_SPACE: 85 keys[SPACE] = false; 86 break; 87 } 88 } 89 90 else if(ev.type == ALLEGRO_EVENT_TIMER) 91 { 92 pos_x -=keys[LEFT] * 7; 93 pos_x +=keys[RIGHT] * 7; 94 if(keys[SPACE] == false) 95 { 96 pos_y = 370; 97 } 98 if(pos_y>=330) 99 pos_y -=keys[SPACE] *40; 100 redraw = true; 101 } 102 103 104 if(redraw && al_is_event_queue_empty(event_queue)) 105 { 106 redraw= false; 107 al_draw_filled_circle(pos_x,pos_y,20,al_map_rgb(255,0,255)); 108 al_flip_display(); 109 al_clear_to_color(al_map_rgb(0,0,0)); 110 } 111 112 } 113 114 115 al_destroy_display(display); //destroy our display object 116 117 return 0; 118}

jmasterx
Member #11,410
October 2009

You really should do it with vectors. Your hack is hard to visualize. Also, it's not a good idea to multiply by a Boolean, because in theory a true Boolean only needs to be non zero, it does not have to be 1.

Dizzy Egg
Member #10,824
March 2009
avatar

for a start you don't break after:

case ALLEGRO_KEY_ESCAPE:
  done=true;

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Arvidsson
Member #4,603
May 2004

Try using velocities (i.e. vectors) as I've done in my tutorial and other people have suggested.

I'm not sure if you want to be able to control the jumping height or not.

If not, then it's simply this:

  • when the player pressed up, set dy to a negative value.

  • if player is not on ground, increase dy with a positive value (gravity)

  • update y using dy: y += dy

If you want controlled jumping height, see my tutorial I posted.

Steve Terry
Member #1,989
March 2002
avatar

wow... good luck man :P

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

varynoob
Member #14,058
February 2012

The ball bounces, but if the jump key is pressed, it bounces in the mid air.

All Triangles are generated in a small place, i.e all 4 triangles show up overlapping or inside each other, no random distance! :(:-/

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_primitives.h> 3#include "objects.h" 4 5const int WIDTH = 1000; 6const int HEIGHT = 400; 7const int NUM_TRIANGLES = 4; 8 9enum KEYS{LEFT, RIGHT, SPACE}; 10 11 12 13//prototypes 14 15void InitTriangle(Triangle shapeT[], int size); 16void DrawTriangle(Triangle shapeT[], int size); 17void UpdateTriangle(Triangle shapeT[], int size); 18 19 20int main(void) 21{ 22 bool done = false; 23 bool redraw = true; 24 bool jumping = false; 25 26 int pos_x = 30; 27 int pos_y = 380; 28 int speed =0; 29 30 float dx =1, dy = 1; 31 32 bool keys[3] = {false,false,false}; 33 34 int FPS = 60; 35 36 37 Triangle shapeT[NUM_TRIANGLES]; 38 39 40 ALLEGRO_DISPLAY *display = NULL; 41 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 42 ALLEGRO_TIMER *timer; 43 //Initialization Functions 44 45 46 47 if(!al_init()) //initialize Allegro 48 return -1; 49 50 display = al_create_display(WIDTH, HEIGHT); //create our display object 51 52 if(!display) //test display object 53 return -1; 54 55 al_init_primitives_addon(); 56 al_install_keyboard(); 57 58 59 srand(time(NULL)); 60 InitTriangle(shapeT, NUM_TRIANGLES); 61 62 event_queue = al_create_event_queue(); 63 timer = al_create_timer(1.0/FPS); 64 65 66 al_register_event_source(event_queue, al_get_keyboard_event_source()); 67 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 68 69 al_start_timer(timer); 70 71 while(!done) 72 { 73 ALLEGRO_EVENT ev; 74 al_wait_for_event(event_queue, &ev); 75 76 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 77 { 78 switch(ev.keyboard.keycode) 79 { 80 /*case ALLEGRO_KEY_UP: 81 keys[UP] = true; 82 break; 83 case ALLEGRO_KEY_DOWN: 84 keys[DOWN] = true; 85 break;*/ 86 case ALLEGRO_KEY_LEFT: 87 keys[LEFT] = true; 88 break; 89 case ALLEGRO_KEY_RIGHT: 90 keys[RIGHT] = true; 91 break; 92 case ALLEGRO_KEY_SPACE: 93 keys[SPACE] = true; 94 break; 95 } 96 97 } 98 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 99 { 100 switch(ev.keyboard.keycode) 101 { 102 case ALLEGRO_KEY_LEFT: 103 keys[LEFT] = false; 104 break; 105 case ALLEGRO_KEY_RIGHT: 106 keys[RIGHT] = false; 107 break; 108 case ALLEGRO_KEY_ESCAPE: 109 done=true; 110 break; 111 case ALLEGRO_KEY_SPACE: 112 keys[SPACE] = false; 113 break; 114 } 115 } 116 117 // pos_y -=keys[UP] * 10; 118 // pos_y +=keys[DOWN] * 10; 119 else if(ev.type == ALLEGRO_EVENT_TIMER) 120 { 121 pos_x -=keys[LEFT] * 7; 122 pos_x +=keys[RIGHT] * 7; 123 /*if(keys[SPACE] == false) 124 { 125 pos_y = 370; 126 } 127 if(pos_y>=330) 128 pos_y -=keys[SPACE] *40;*/ 129 130 if(keys[SPACE] && pos_y>=350) 131 { 132 dy= -10; 133 jumping = true; 134 } 135 136 if(pos_y < 380) 137 dy += 0.5; 138 139 if (keys[SPACE] && pos_y <=380 && dy > 0) 140 dy -= 0.1; // got to be LESS than 0.5 in this example 141 142 if(dy>5) 143 dy = 5; 144 145 speed +=0.5; 146 pos_x +=speed; 147 pos_y +=dy; 148 149 if(pos_y > 380) 150 pos_y = 380; 151 152 153 redraw = true; 154 } 155 156 157 if(redraw && al_is_event_queue_empty(event_queue)) 158 { 159 redraw= false; 160 al_draw_filled_circle(pos_x,pos_y,20,al_map_rgb(255,0,255)); 161 DrawTriangle(shapeT,NUM_TRIANGLES); 162 //al_draw_triangle(100,400,120,360,140,400,al_map_rgb(255,0,255),2); 163 //al_draw_rounded_rectangle(180,340,260,400,5,5,al_map_rgb(255,255,255),3); 164 al_flip_display(); 165 al_clear_to_color(al_map_rgb(0,0,0)); 166 167 } 168 169 } 170 171 //al_rest(5.0); 172 173 al_destroy_display(display); //destroy our display object 174 175 return 0; 176} 177 178void InitTriangle(Triangle shapeT[], int size) 179{ 180 for(int i=0; i<size; i++) 181 { 182 shapeT[i].ID = TRIANGLE; 183 shapeT[i].pos_x1 = 100 + rand() % 700; 184 shapeT[i].pos_y1 = 400; 185 shapeT[i].pos_x2 = 120 + rand() % 700; 186 shapeT[i].pos_y2 = 360; 187 shapeT[i].pos_x3 = 140 + rand() % 700; 188 shapeT[i].pos_y3 = 400; 189 } 190} 191 192void DrawTriangle(Triangle shapeT[], int size) 193{ 194 for(int i=0; i<size; i++) 195 { 196 197 al_draw_triangle(shapeT[i].pos_x1,shapeT[i].pos_y1,shapeT[i].pos_x2,shapeT[i].pos_y2, 198 shapeT[i].pos_x3,shapeT[i].pos_y3,al_map_rgb(255,0,255),2); 199 } 200} 201 202//void UpdateTriangle(Triangle &shapeT, int size) 203//{ 204//}

Arvidsson
Member #4,603
May 2004

if (pos_y >= 380) { // means the ball is on the ground right?
    if(keys[SPACE]) {
        if (jumping) {
            dy= -10;
            jumping = false;
        }
    }
    else
        jumping = true;
}
// a more proper name for the jumping variable would be canJump, or mayJumpAgain

Regarding the triangle problem, I would generate random numbers for just one xy-coordinate and create the other two vertices by using that coordinate. This way you can easily change the size of the triangles if you want to.

varynoob
Member #14,058
February 2012

Whoa, thanks a lot :) The bounce is working now! But i am getting the obstacles of the game(have been testing only on triangles for now) all wrong. Have attached an image.

Have modified, only x1 of the triangle and its way better now, but i get overlapping triangles!!

{"name":"605625","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/2\/9235031d8a61a4cb19d0ed47e6b819cb.jpg","w":1015,"h":122,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/2\/9235031d8a61a4cb19d0ed47e6b819cb"}605625

void InitTriangle(Triangle shapeT[], int size)
{
  for(int i=0; i<size; i++)
  {
  shapeT[i].ID = TRIANGLE;
  shapeT[i].pos_x1 = 100 + rand() % 600;
  shapeT[i].pos_y1 = 400;
  shapeT[i].pos_x2 = shapeT[i].pos_x1+20;
  shapeT[i].pos_y2 = 360;
  shapeT[i].pos_x3 = shapeT[i].pos_x1+40;
  shapeT[i].pos_y3 = 400;
  }
}

gnolam
Member #2,030
March 2002
avatar

varynoob said:

Have modified, only x1 of the triangle and its way better now, but i get overlapping triangles!!

Therefore, you should..?

You have all the information you need to solve that problem: a list of all the generated triangles and their coordinates.

[EDIT]
I really hope somebody doesn't just give you the solution to this too. You need to start thinking for yourself.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

varynoob
Member #14,058
February 2012

[UPDATED]
I have been trying. I have now managed to make things scroll and triangles and squares coming, but i am really in confusion with 2 things :

1. Squares and Triangles OVERLAP each other(as seen in the image).

{"name":"605627","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/f\/efd5069bfe3772701fee009ee6d06eca.jpg","w":806,"h":194,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/f\/efd5069bfe3772701fee009ee6d06eca"}605627

2. How can i make the screen scroll? I mean, for ex mario: you keep on going and things keep coming!!

jmasterx
Member #11,410
October 2009

You scroll by moving the world around the player. The player is always at the center of the screen except at the left and right extents of the level.

It's easier to think of it in terms of a camera. You can look into ALLEGRO_TRANSFORMations for this: http://www.liballeg.org/a5docs/refman/transformations.html

If you were able to get a degree in Engineering, which involved maths far more complex than simple transformations, then the maths required to simulate a camera should be a breeze.

The idea of transformations is that you make the Player's position 0,0 on the screen which will cause your character to be at the center of the screen which consequently scrolls your props too.

If you need an example on transformations in Allegro, there is ex_transform.c that you can have a look at. It comes with the Allegro source code.

In theory, these 3 functions should do it for you:

  ALLEGRO_TRANSFORM transform;
  al_identity_transform(&transform);
  al_translate_transform(&transform, x, y);
  al_use_transform(&transform);

It is up to you to figure out x and y.

Arvidsson
Member #4,603
May 2004

Problem: Triangles overlap
What we want to achieve: Make them not overlap
Solution: Is there a way to see if they overlap and then do something about it?

Neil Walker
Member #210
April 2000
avatar

So you're trying to remake the impossible game:

{"name":"images","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/7\/07445f55dd03592bdf31dede6f94f276.jpg","w":300,"h":168,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/7\/07445f55dd03592bdf31dede6f94f276"}images

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

varynoob
Member #14,058
February 2012

Neil Walker : Why impossible ? What i am trying to code, already exists as an smart phone app. The game in the picture isnt the exact model of it.

Arvidsson : As in, you want me to paste the code? I am confused with the statement

"Solution: Is there a way to see if they overlap and then do something about it?"

jmasterx : will try it, have been into the overlapping issue.

As i added, the Ball move functions, for left, right and jump, the JUMP Logic seems to be not working. Cant figure out why?

Here's the updated code :

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_primitives.h> 3#include "objects.h" 4 5const int WIDTH = 1000; 6const int HEIGHT = 400; 7const int NUM_TRIANGLES = 6; 8 9enum KEYS{LEFT, RIGHT, SPACE}; 10 11 12 13//prototypes 14 15void InitBall(Ball &shapeB); 16void DrawBall(Ball &shapeB); 17void MoveLeftBall(Ball &shapeB); 18void MoveRightBall(Ball &shapeB); 19void JumpBall(Ball &shapeB); 20 21 22void InitTriangle(Triangle shapeT[], int size); 23void DrawTriangle(Triangle shapeT[], int size); 24void StartTriangle(Triangle shapeT[], int size); 25void UpdateTriangle(Triangle shapeT[], int size); 26 27int main(void) 28{ 29 bool done = false; 30 bool redraw = true; 31// bool canJump = false; 32 33// float pos_x = 30; 34// float pos_y = 380; 35 float speed =0; 36 37// float dx =1, dy = 1; 38 39 bool keys[3] = {false,false,false}; 40 41 int FPS = 60; 42 43 44 Triangle shapeT[NUM_TRIANGLES]; 45 Ball shapeB; 46 47 ALLEGRO_DISPLAY *display = NULL; 48 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 49 ALLEGRO_TIMER *timer; 50 //Initialization Functions 51 52 53 54 if(!al_init()) //initialize Allegro 55 return -1; 56 57 display = al_create_display(WIDTH, HEIGHT); //create our display object 58 59 if(!display) //test display object 60 return -1; 61 62 al_init_primitives_addon(); 63 al_install_keyboard(); 64 65 66 srand(time(NULL)); 67 InitTriangle(shapeT, NUM_TRIANGLES); 68 InitBall(shapeB); 69 event_queue = al_create_event_queue(); 70 timer = al_create_timer(1.0/FPS); 71 72 73 al_register_event_source(event_queue, al_get_keyboard_event_source()); 74 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 75 76 al_start_timer(timer); 77 78 while(!done) 79 { 80 ALLEGRO_EVENT ev; 81 al_wait_for_event(event_queue, &ev); 82 83 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 84 { 85 switch(ev.keyboard.keycode) 86 { 87 case ALLEGRO_KEY_LEFT: 88 keys[LEFT] = true; 89 break; 90 case ALLEGRO_KEY_RIGHT: 91 keys[RIGHT] = true; 92 break; 93 case ALLEGRO_KEY_SPACE: 94 keys[SPACE] = true; 95 break; 96 } 97 98 } 99 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 100 { 101 switch(ev.keyboard.keycode) 102 { 103 case ALLEGRO_KEY_LEFT: 104 keys[LEFT] = false; 105 break; 106 case ALLEGRO_KEY_RIGHT: 107 keys[RIGHT] = false; 108 break; 109 case ALLEGRO_KEY_ESCAPE: 110 done=true; 111 break; 112 case ALLEGRO_KEY_SPACE: 113 keys[SPACE] = false; 114 break; 115 } 116 } 117 118 119 else if(ev.type == ALLEGRO_EVENT_TIMER) 120 { 121 redraw = true; 122 if(keys[LEFT]) 123 MoveLeftBall(shapeB); 124 if(keys[RIGHT]) 125 MoveRightBall(shapeB); 126 127 128 129 if (shapeB.pos_Y >= 380) 130 { // means the ball is on the ground 131 if(keys[SPACE]) 132 { 133 if (shapeB.canJump) 134 { 135 shapeB.dy= -10; 136 shapeB.canJump = false; 137 } 138 } 139 else 140 shapeB.canJump = true; 141 } 142 143 144 145 if(shapeB.pos_Y < 380) 146 shapeB.dy += 0.5; 147 148 149 //JumpBall(shapeB); 150 if (keys[SPACE] && shapeB.pos_Y <=380 && shapeB.dy > 0) 151 shapeB.dy -= 0.1; // got to be LESS than 0.5 152 153 if(shapeB.dy>5) 154 shapeB.dy = 5; 155 156 speed +=0.01; 157 //shapeB.pos_X +=speed*1/2; 158 shapeB.pos_Y +=shapeB.dy; 159 160 if(shapeB.pos_Y > 380) 161 shapeB.pos_Y = 380; 162 163 164 StartTriangle(shapeT, NUM_TRIANGLES); 165 UpdateTriangle(shapeT, NUM_TRIANGLES); 166 } 167 168 169 if(redraw && al_is_event_queue_empty(event_queue)) 170 { 171 redraw= false; 172 /*al_draw_filled_circle(pos_x,pos_y,20,al_map_rgb(255,0,255));*/ 173 //UpdateTriangle(shapeT,NUM_TRIANGLES); 174 DrawBall(shapeB); 175 DrawTriangle(shapeT,NUM_TRIANGLES); 176 177 //al_draw_rounded_rectangle(180,340,260,400,5,5,al_map_rgb(255,255,255),3); 178 al_flip_display(); 179 al_clear_to_color(al_map_rgb(0,0,0)); 180 181 182 } 183 } 184 185 //al_rest(5.0); 186 187 al_destroy_display(display); //destroy our display object 188 189 return 0; 190 } 191 192 193 194void InitBall(Ball &shapeB) 195{ 196 shapeB.ID=BALL; 197 shapeB.pos_X= 30; 198 shapeB.pos_Y= 380; 199 shapeB.lives = 3; 200 shapeB.speed = 7; 201 shapeB.score = 0; 202 shapeB.boundX = 57; 203 shapeB.boundY = 57; 204 shapeB.dx = 1; 205 shapeB.dy = 1; 206 207} 208void DrawBall(Ball &shapeB) 209{ 210 al_draw_filled_circle(shapeB.pos_X,shapeB.pos_Y,20,al_map_rgb(255,0,255)); 211} 212void MoveLeftBall(Ball &shapeB) 213{ 214 shapeB.pos_X -=shapeB.speed; 215 if(shapeB.pos_X<20) 216 shapeB.pos_X = 20; 217} 218void MoveRightBall(Ball &shapeB) 219{ 220 shapeB.pos_X += shapeB.speed; 221 if(shapeB.pos_X>600) 222 shapeB.pos_X = 600; 223} 224void JumpBall(Ball &shapeB) 225{ 226 if (shapeB.canJump) 227 { 228 shapeB.dy= -10; 229 shapeB.canJump = false; 230 } 231 232 else 233 { 234 shapeB.canJump = true; 235 } 236 237 if(shapeB.pos_Y < 380) 238 shapeB.dy += 0.5; 239 240} 241 242 243 244void InitTriangle(Triangle shapeT[], int size) 245{ 246 for(int i=0; i<size; i++) 247 { 248 shapeT[i].ID = TRIANGLE; 249 shapeT[i].live = false; 250 shapeT[i].speed = 2; 251 shapeT[i].random = 1 + rand() % 10; 252 253 } 254} 255 256void DrawTriangle(Triangle shapeT[], int size) 257{ 258 for(int i=0; i<size; i++) 259 { 260 if(shapeT[i].live) 261 { 262 263 //if(shapeT[i].pos_x3 == shapeT[i+1].pos_x1 || shapeT[i].pos_x3 +100<= shapeT[i+1].pos_x1) 264 //{ 265 /*&& shapeT[i].pos_x3 == shapeT[i].pos_rect_x1 || shapeT[i].pos_x3 + 400 <= shapeT[i].pos_rect_x1 266 || shapeT[i].pos_x1 >= shapeT[i].pos_rect_x2) */ 267 268 if(shapeT[i].random < 5) 269 al_draw_triangle(shapeT[i].pos_x1,shapeT[i].pos_y1,shapeT[i].pos_x2,shapeT[i].pos_y2, 270 shapeT[i].pos_x3,shapeT[i].pos_y3,al_map_rgb(255,0,255),2); 271 else 272 al_draw_rounded_rectangle(shapeT[i].pos_rect_x1,shapeT[i].pos_rect_y1, 273 shapeT[i].pos_rect_x2,shapeT[i].pos_rect_y2,5,5,al_map_rgb(255,255,255),3); 274 //} 275 } 276 } 277} 278 279void StartTriangle(Triangle shapeT[], int size) 280{ 281 for (int i = 0 ; i < size; i++) 282 { 283 if(!shapeT[i].live) 284 { 285 if(rand() % 500 == 0) 286 { 287 shapeT[i].live = true; 288 shapeT[i].pos_x1 = 780 + rand() % 20; 289 shapeT[i].pos_y1 = 400; 290 shapeT[i].pos_x2 = shapeT[i].pos_x1+10; 291 shapeT[i].pos_y2 = 370; 292 shapeT[i].pos_x3 = shapeT[i].pos_x1+20; 293 shapeT[i].pos_y3 = 400; 294 295 shapeT[i].pos_rect_x1 = shapeT[i].pos_x3 + 20; 296 shapeT[i].pos_rect_y1 = 320; 297 shapeT[i].pos_rect_x2 = shapeT[i].pos_rect_x1 + 80; 298 shapeT[i].pos_rect_y2 = 400; 299 300 break; 301 302 } 303 } 304 } 305} 306 307void UpdateTriangle(Triangle shapeT[], int size) 308{ 309 for(int i=0; i<size; i++) 310 { 311 if(shapeT[i].live) 312 { 313 shapeT[i].pos_x1 -= shapeT[i].speed; 314 shapeT[i].pos_x2 -= shapeT[i].speed; 315 shapeT[i].pos_x3 -= shapeT[i].speed; 316 shapeT[i].pos_rect_x1 -= shapeT[i].speed; 317 shapeT[i].pos_rect_x2 -= shapeT[i].speed; 318 319 320 if(shapeT[i].pos_x3 <0 || shapeT[i].pos_rect_x2 <0) 321 shapeT[i].live = false; 322 } 323 } 324}

Trent Gamblin
Member #261
April 2000
avatar

Trying to make a quick buck piggybacking off people here? :)

Arthur Kalliokoski
Second in Command
February 2005
avatar

I doubt he'd label it "urgent" if he was trying to develop an app, it's homework that he procrastinated on.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Trent Gamblin
Member #261
April 2000
avatar

That's why I said a "quick" buck. :P

gnolam
Member #2,030
March 2002
avatar

varynoob said:

Arvidsson : As in, you want me to paste the code? I am confused with the statement "Solution: Is there a way to see if they overlap and then do something about it?"

What Arvidsson meant is the same as I did: THINK. You can't expect other people to keep solving everything for you. Sooner or later, you're going to have to think for yourself. What he posted was a basic recipe to solving your problem.

I'll restate it in the form of three questions you should ask yourself:

  1. What is the problem?

  2. What are the conditions that cause the problem?

  3. Now that you know what the problem is and how it occurs, how do you fix it?

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

varynoob
Member #14,058
February 2012

No No No Sir.
Please dont get me wrong, and this isnt any home work, i need to code it by weekend in any case for then, i will miss an opportunity, that's it.
I have been just asking, since this is an allegro forum, and there are people like you who have hell lot of exp, while i am a total newbie.

If this is against, any rules, i am really sorry! ???

[EDIT]

I am trying to sir. But, for a first timer, it gets, little confusing at times.

Trent Gamblin
Member #261
April 2000
avatar

Ok, I'll take your word. Sounds like something for a job interview. Good luck.

 1   2   3 


Go to: