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

Yes exactly, thankyou sir. :)

Its my dream to be a game programmer, and this opportunity means a lot to me, got just 3 days left!
Sadly though, here in, we do not get much resources or motivation for Game Programming, so gets stiff.

Trent Gamblin
Member #261
April 2000
avatar

Well try a little harder, and you'll get it done and get answers. Ask specific questions relating to your code, which you should post if it has changed.

For example "my triangles overlap, how do I fix it" is a pretty general question. You should try fixing it. You might be able to do it yourself (don't sell yourself short!) If you can't figure it out, show us the code where you tried to figure it out and someone can probably tell you how to fix your code.

People here just don't like giving out a 100% ready made solution, they want you to try and figure it out yourself first.

Johan Halmén
Member #1,550
September 2001

Ok, for the triangles: check the variables defining the positions of the triangles before you draw them.

Anyway, if game programming skills are crucial for the career opening, something says me you should wait for the opening 2 or 3 years and meanwhile improve your skills.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Dizzy Egg
Member #10,824
March 2009
avatar

If I put something 10ft wide 7ft away from something else that is 10ft wide do you think they will touch....?

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

Mark Oates
Member #1,146
March 2001
avatar

The London Symphony Orchestra has an opening in the Oboe section! :o I just got an Oboe! Quick, what do the buttons to!? :o

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Dizzy Egg
Member #10,824
March 2009
avatar

I'm not sure, but I'm thinking they change the pitch of the sound. Try putting the keys in an array called pitch[numNotes] or something, and then do something else blah blah blah yawn. Comon varynoob give us all hope with a custom solution.... :o

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

varynoob
Member #14,058
February 2012

Guys, look at this, everything seems to be ohkay, except the Ball hangs in the air, when Jump key is pressed. Before i made Ball Struct & functions for Ball Movement, ball was jumping fine. But now, there is this weird error with the same code.
It has something to do with the Draw function of the Ball, but i am not able to figure it out.
Looking to put in collision detection and spritesheets, so need functions for ball movement, such as MoveRightBall/Left/Jump.

This is the code i have changed

#SelectExpand
1 2//HAVE CHANGED THIS PART, THINGS WORKED, WHEN STRUCT WAS NOT CREATED/USED. NOW THE //BALL HANGS IN THE AIR AND DOES NOT COME DOWN 3 4if(keys[LEFT]) 5 MoveLeftBall(shapeB); 6 if(keys[RIGHT]) 7 MoveRightBall(shapeB); 8 9 10 11 if (shapeB.isOnGround) 12 { // means the ball is on the ground 13 if(keys[SPACE]) 14 { 15 if (shapeB.canJump) 16 { 17 shapeB.dy= -10; 18 shapeB.canJump = false; 19 } 20 } 21 else 22 shapeB.canJump = true; 23 } 24 25 26 27 if(!shapeB.isOnGround) 28 shapeB.dy += 0.5; 29 30 if (keys[SPACE] && shapeB.isOnGround && shapeB.dy > 0) 31 shapeB.dy -= 0.1; // got to be LESS than 0.5 in this example 32 33 if(shapeB.dy>5) 34 shapeB.dy = 5; 35 36 speed +=0.01; 37 //pos_x +=speed*1/2; 38 shapeB.pos_Y += shapeB.dy; 39 40 if(shapeB.pos_Y > 380) 41 { 42 shapeB.pos_Y = 380; 43 shapeB.dy = 0; 44 } 45 46 47 48 49 50 51 StartTriangle(shapeT, NUM_TRIANGLES); 52 UpdateTriangle(shapeT, NUM_TRIANGLES); 53 } 54 55 56 if(redraw && al_is_event_queue_empty(event_queue)) 57 { 58 redraw= false; 59 //al_draw_filled_circle(pos_x,pos_y,20,al_map_rgb(255,0,255)); THIS WAS THE WORKING BALL WITHOUT BALL STRUCT AND FUNCTIONS 60 DrawBall(shapeB); 61 DrawTriangle(shapeT,NUM_TRIANGLES); 62 63//HAVE EDITED OUT THE DESTROY PART AND END OF main() 64 65//THE FUNCTION BEGINS 66 67void InitBall(Ball &shapeB) 68{ 69 shapeB.ID=BALL; 70 shapeB.pos_X= 30; 71 shapeB.pos_Y= 380; 72 shapeB.lives = 3; 73 shapeB.speed = 7; 74 shapeB.score = 0; 75 shapeB.boundX = 57; 76 shapeB.boundY = 57; 77 shapeB.dx = 1; 78 shapeB.dy = 1; 79 shapeB.canJump = false; 80 shapeB.isOnGround = true; 81 82} 83void DrawBall(Ball &shapeB) 84{ 85 if(shapeB.pos_Y < 380) 86 shapeB.isOnGround = false; 87 else 88 shapeB.isOnGround = true; 89 al_draw_filled_circle(shapeB.pos_X,shapeB.pos_Y,20,al_map_rgb(255,0,255)); 90} 91void MoveLeftBall(Ball &shapeB) 92{ 93 shapeB.pos_X -=shapeB.speed; 94 if(shapeB.pos_X<20) 95 shapeB.pos_X = 20; 96} 97void MoveRightBall(Ball &shapeB) 98{ 99 shapeB.pos_X += shapeB.speed; 100 if(shapeB.pos_X>600) 101 shapeB.pos_X = 600; 102} 103//void JumpBall(Ball &shapeB) NEED TO FIGURE THIS OUT AS WELL 104//{ 105// if (shapeB.canJump) 106// { 107// shapeB.dy= -10; 108// shapeB.canJump = false; 109// } 110// 111// else 112// { 113// shapeB.canJump = true; 114// } 115// 116// if(shapeB.pos_Y < 380) 117// shapeB.dy += 0.5; 118// 119//}

Neil Walker
Member #210
April 2000
avatar

varynoob said:

Neil Walker : Why impossible ?

Because that's the name of the game :P

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

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

Arvidsson
Member #4,603
May 2004
avatar

The ball hangs in the air you say. Then you should ask yourself, what is the condition for the ball to fall. Could it be gravity? What is the condition for gravity to be applied? Is it ever true?

varynoob
Member #14,058
February 2012

Arvidsson : Sir, after trying several times, got the mistake, corrected for the JUMP. Have been working on collisions.

[EDIT]
CIRCLE_TRIANGLE
Is this okay ?

if((shapeB.pos_X + shapeB.r > shapeT[i].pos_x1) && (shapeB.pos_X - shapeB.r < shapeT[i].pos_x3) 
        && (shapeB.pos_Y + shapeB.r > shapeT[i].pos_y2 && shapeB.pos_Y - shapeB.r < shapeT[i].pos_y3))

[edit]

HOW DO I CHECK IN BALL(bounding rectangle)<->RECTANGLE, IF THE BALL HITS LEFT SIDE OF RECTANGLE, THE SCREEN PAUSES, ELSE IF BALL IS ON TOP EDGE OF SQUARE, IT ROLLS DOWN AND SCREEN KEEPS SCROLLING AND GAME CONTINUES!

Suggestions plz. Using this (not working):

#SelectExpand
1if((shapeT[i].pos_x1 - shapeT[i].bound_triangle_x < shapeB.pos_X + shapeB.boundx) 2 && (shapeT[i].pos_x1 + shapeT[i].bound_triangle_x > shapeB.pos_X - shapeB.boundx) 3 &&(shapeT[i].pos_y1 - shapeT[i].bound_triangle_x < shapeB.pos_Y + shapeB.boundy) 4 &&(shapeT[i].pos_y1 + shapeT[i].bound_triangle_x > shapeB.pos_Y - shapeB.boundy)) 5 { 6 //shapeT[i].collidingT = true; 7 shapeB.lives--; 8 //shapeB.pos_Y = shapeT[i].pos_y2 - shapeB.r; 9 //shapeT[i].speed = 0; 10 shapeT[i].live = false; 11 12 StartExplosions(explosion,shapeB.pos_X, shapeB.pos_Y); 13 14 } 15 16 else if((shapeT[i].pos_rect_x1 < shapeB.pos_X + shapeB.boundx) 17 &&(shapeT[i].pos_rect_x2 > shapeB.pos_X - shapeB.boundx) 18 &&(shapeT[i].pos_rect_y1 < shapeB.pos_Y + shapeB.boundy) 19 &&(shapeT[i].pos_rect_y2 > shapeB.pos_Y - shapeB.boundy)) 20 21 22 { 23 //shapeB.pos_X = shapeT[i].pos_rect_x1 - shapeB.r; 24 //shapeB.pos_Y = shapeT[i].pos_rect_y1 - shapeB.r; 25 //shapeT[i].collidingS = true; 26 27 28 //shapeT[i].speed = 0; 29 shapeT[i].live = false; 30 31 }

Mark Oates
Member #1,146
March 2001
avatar

Oh God, he's shouting now. :-[

We'll never make the London Symphony. :'(

*sniff... :'( screw it! :'( screw it all!
{"name":"605641","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/d\/6de24c7282296dab186ca05f9e5d0e22.jpg","w":268,"h":331,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/d\/6de24c7282296dab186ca05f9e5d0e22"}605641

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

varynoob
Member #14,058
February 2012

What are you talking about? Got no idea!! Spamming ?? :P :-/

Anyone with Circle-Triangle & Circle-Rectangle collision detection simultaneous checking ? ?:-[

jmasterx
Member #11,410
October 2009

With your education, you should have no trouble figuring out a formula for each type of detection; even if it's inefficient, it will do.

Once you come up with some formulas, we will gladly guide you on how to turn that formula into C++ code.

If you can figure out if a point is in a circle, if a point is in a triangle, and if a point is in a rectangle, you should be able to figure out a way to do it.

You might find it easier to treat a circle as a rectangle because if the circle's radius is less than the box's height, you'll have to check if the circle collides with its edge.

Also, he is not spamming, this is the atmosphere at a.cc .

Steve Terry
Member #1,989
March 2002
avatar

Mark, that guy in the background may want to see about getting his uni-brow waxed :D

___________________________________
[ 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

LennyLen
Member #5,313
December 2004
avatar

varynoob, without trying to sound demeaning, it's very obvious that you are out of your depth here. If you do manage to get the help you need from people here in order to get the job, then what? It won't take long before your employer realizes that you don't have the necessary experience.

Dizzy Egg
Member #10,824
March 2009
avatar

He's absolutely right I'm afraid, that's why I stopped giving you hints. Sorry, but maybe it's time to get realistic. :-[

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

varynoob
Member #14,058
February 2012

I have learnt from my mistakes sir, as everybody had told here, to try and try, and come up with things, i did the same.

Used Bounding box for circle, and bounding box of triangle to check collisions between the two, while the rectangle itself, has sides, so it is easy for that.

But what i am facing is, the collision comes out to be ok at times and weird at other times. So asked you of the flaw.

Here is the code.

#SelectExpand
1void CollideTriangle(Triangle shapeT[], int csize, Ball &shapeB, Explosion &explosion) //FUNCTION TO CHECK BOTH THE COLLSIONS 2{ 3 for(int i = 0; i < csize; i++) 4 { 5 if(shapeT[i].live) 6 { 7 8 9 if(((shapeT[i].pos_x1 - shapeT[i].bound_triangle_x < shapeB.pos_X + shapeB.boundx) 10 && (shapeT[i].pos_x1 + shapeT[i].bound_triangle_x > shapeB.pos_X - shapeB.boundx) 11 &&(shapeT[i].pos_y1 - shapeT[i].bound_triangle_x < shapeB.pos_Y + shapeB.boundy) 12 &&(shapeT[i].pos_y1 + shapeT[i].bound_triangle_x > shapeB.pos_Y - shapeB.boundy)) || 13 14//UPPER PART CHECKS FOR CIRCLE-TRIANGLE WHILE THE PART BELOW CHECKS FOR CIRCLE=RECTAGLE 15 16 ((shapeT[i].pos_rect_x1 < shapeB.pos_X + shapeB.boundx) 17 &&(shapeT[i].pos_rect_x2 > shapeB.pos_X - shapeB.boundx) 18 &&(shapeT[i].pos_rect_y1 < shapeB.pos_Y + shapeB.boundy) 19 &&(shapeT[i].pos_rect_y2 > shapeB.pos_Y - shapeB.boundy))) 20 21 { 22 //shapeT[i].collidingT = true; 23 //shapeB.lives--; 24 //shapeB.pos_Y = shapeT[i].pos_y2 - shapeB.r; 25 //shapeT[i].speed = 0; 26 shapeT[i].live = false; 27 28 StartExplosions(explosion,shapeB.pos_X, shapeB.pos_Y); 29 30 } 31 32 } 33 } 34}

PS : SO I HAVE BEEN TRYING, AND HAVE COME UP WITH THE COLLISION TECHNIQUES AS WELL.

IT IS ALL ABOUT MORE UP THAN BOTTOM, MORE BOTTOM THAN UP, MORE LEFT THAN RIGHT & MORE RIGHT THAN LEFT.

Dizzy Egg
Member #10,824
March 2009
avatar

Why would you add/subtract bound_triangle_x from posy ??

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

varynoob
Member #14,058
February 2012

Dizzy Egg : Typo sir! It was meant to be 'bound_triangle_y'.

I have been trying out different things, but havnt really landed into any good one, as in i want to achieve if Circle hits Left of square, everything freezes except the ball, which if made to bounce, should make things back normal and scrolling.

What i did is, checked the collision with the sides of square i.e Right of circle more right than square's left & Left of circle more left than Square's right.

If the condition is true, Leftcollision is true.

If Leftcollision is true, All the update functions are skipped, thus screen is freezed.

But this stuff doesnt do well for me. Code below :

#SelectExpand
1for(int i=0; i < ssize ; i++) 2 { 3 if((shapeS[i].pos_rect_x1 -5 < shapeB.pos_X + shapeB.boundx) 4 &&(shapeS[i].pos_rect_x1+ 40 > shapeB.pos_X - shapeB.boundx) 5/* &&(shapeS[i].pos_rect_y1 < shapeB.pos_Y + shapeB.boundy) 6 &&(shapeS[i].pos_rect_y1+40 > shapeB.pos_Y - shapeB.boundy)*/) 7 { 8 //StartExplosions(explosion,shapeB.pos_X, shapeB.pos_Y); 9 10 shapeB.LeftCollision = true; 11 } 12 else if((shapeS[i].pos_rect_x1 -5 < shapeB.pos_X + shapeB.boundx) 13 &&(shapeS[i].pos_rect_x1+ 40 > shapeB.pos_X - shapeB.boundx) 14 &&(shapeS[i].pos_rect_y1 < shapeB.pos_Y + shapeB.boundy) 15 &&(shapeS[i].pos_rect_y1+40 > shapeB.pos_Y - shapeB.boundy)) 16 { 17 //shapeB.pos_Y = shapeS[i].pos_rect_y1 - shapeB.boundy ; 18 shapeS[i].collidingS = true; 19 shapeB.LeftCollision = false; 20 } 21 22 else 23 shapeB.LeftCollision = false; 24 } 25}

Dizzy Egg
Member #10,824
March 2009
avatar

if((shapeS[i].pos_rect_x1 -5 < shapeB.pos_X + shapeB.boundx)

...think about it; if shapeS[i].pos_rect_x1 = 20, and shapeB.pos_X = 50, this is ALWAYS going to be true! THINK ABOUT THE LOGIC!!!!!!!! :P

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

varynoob
Member #14,058
February 2012

if((shapeS[i].pos_rect_x1 -5 < shapeB.pos_X + shapeB.boundx)
&&(shapeS[i].pos_rect_x1+ 40 > shapeB.pos_X - shapeB.boundx)

But then, this condition also has an && thus if 1st is always true, the 2nd will only be true, if the left of the ball is less than the right of the square!!

boundx = 23.

So for shapeS[i].pos_rect_x1 = 20, and shapeB.pos_X = 50

it will be : (15<73) && (60 >27> | TRUE

For shapeS[i].pos_rect_x1 = 20, and shapeB.pos_X = 90

it will be : (15<113) && (60 > 67 ) | FALSE

So, it should be correct?? What's wrong? ???

Dizzy Egg
Member #10,824
March 2009
avatar

I don't know you tell me! What is wrong! Explain whats wrong when you run the code...

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

varynoob
Member #14,058
February 2012

Oh yes, sorry, i mean it collides with some squares while it doesnt with others! All random, it is something to do with my Update and Draw functions of square!

How do i check, that only if there is not square already on (x,y) position, next square should be drawn else, neither drawn nor updated(coz sometimes, invisible squares are coming up, i.e which are not drawn, but updated!)

THE SQUARE FUNCTIONS ARE AS :

#SelectExpand
1void InitSquare(Square shapeS[], int size) 2{ 3 for(int i = 0; i < size ; i++) 4 { 5 shapeS[i].ID = SQUARE; 6 shapeS[i].live = false; 7 shapeS[i].pos_rect_x1 = 960; 8 shapeS[i].pos_rect_y1 = 360; 9 shapeS[i].speed = 2; 10 shapeS[i].collidingS = false; 11 } 12} 13void DrawSquare(Square shapeS[], int size) 14{ 15 for(int i = 0; i<size; i++) 16 { 17 if(shapeS[i].live) 18 { 19 //if(random < 10)// && shapeT[i].pos_rect_x1 > shapeT[i-1].pos_rect_x2) 20 al_draw_rounded_rectangle(shapeS[i].pos_rect_x1,shapeS[i].pos_rect_y1, 21 shapeS[i].pos_rect_x1+40,shapeS[i].pos_rect_y1+40,6,6,al_map_rgb(255,255,255),2); 22 } 23 } 24} 25void StartSquare(Square shapeS[], int size) 26{ 27 for (int i = 0 ; i < size; i++) 28 { 29 if(!shapeS[i].live) 30 { 31 if(rand() % 100 == 0) 32 { 33 shapeS[i].live = true; 34 shapeS[i].pos_rect_x1 = 960; 35 shapeS[i].pos_rect_y1 = 360; 36 //shapeT[i].pos_rect_x2 = shapeS[i].pos_rect_x1 + 40; 37 //shapeT[i].pos_rect_y2 = shapeS[i].pos_rect_y1 + 40; 38 39 break; 40 41 } 42 } 43 } 44} 45void UpdateSquare(Square shapeS[], int size, Ball &shapeB) 46{ 47 for(int i=0; i<size; i++) 48 { 49 if((shapeS[i].live)) 50 { 51 shapeS[i].pos_rect_x1 -= shapeS[i].speed; 52 //shapeT[i].pos_rect_x2 -= shapeS[i].speed; 53 54 55 if(shapeS[i].pos_rect_x1+40 <= 0) 56 { 57 shapeS[i].live = false; 58 shapeB.score++; 59 } 60 } 61 } 62} 63 64void CollideSquare(Square shapeS[], int ssize, Ball &shapeB, Explosion &explosion) 65{ 66 for(int i=0; i < ssize ; i++) 67 { 68 if((shapeS[i].pos_rect_x1 -5 < shapeB.pos_X + shapeB.boundx) 69 &&(shapeS[i].pos_rect_x1+ 40 > shapeB.pos_X - shapeB.boundx) 70 { 71 shapeB.LeftCollision = true; //CHECKING FOR COLLIOION WITH LEFT SIDE OF SQUARE 72 } 73 else if((shapeS[i].pos_rect_x1 -5 < shapeB.pos_X + shapeB.boundx) 74 &&(shapeS[i].pos_rect_x1+ 40 > shapeB.pos_X - shapeB.boundx) 75 &&(shapeS[i].pos_rect_y1 < shapeB.pos_Y + shapeB.boundy) 76 &&(shapeS[i].pos_rect_y1+40 > shapeB.pos_Y - shapeB.boundy)) 77 { 78 shapeB.pos_Y = shapeS[i].pos_rect_y1 - shapeB.boundy ; //THIS SHOULD MAKE BALL LAND OVER THE SQUARE WHEN ALL CONDITIONS ARE MET, BUT DOESNT 79 shapeS[i].collidingS = true; 80 shapeB.LeftCollision = false; 81 } 82 83 else 84 shapeB.LeftCollision = false; 85 } 86}

23yrold3yrold
Member #1,134
March 2001
avatar

LennyLen said:

varynoob, without trying to sound demeaning, it's very obvious that you are out of your depth here. If you do manage to get the help you need from people here in order to get the job, then what? It won't take long before your employer realizes that you don't have the necessary experience.

Gonna have to echo this. You'll finish the game, get the job, and be fired in two weeks. The point of this assignment is to see if you can do the job. If you could, you wouldn't be back in this thread every ten minutes.

I work as a video game programmer. When I applied for the job, they told me to make a Breakout clone, and I was budgeted two days. Difference was I could actually do it. And good thing, because I've had ample opportunity to laugh at some of the things other people send in.

Here, have a video break:

So You Want To Be A Developer Part One
So You Want To Be A Developer Part Two

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Mark Oates
Member #1,146
March 2001
avatar

Awesome videos, 23. :)

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

 1   2   3 


Go to: