Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / complete noob

Author
Message
gregorio1994
10
Years of Service
User Offline
Joined: 17th Feb 2014
Location:
Posted: 17th Feb 2014 14:40
My task is to create a program that will display 30 moving balls on screen of random colour, size and speed. They should bounce and move in opposite direction once they hit the edge. This is just a beginning of my adventure with dark gdk and I have no idea how to start it off

Greg
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 18th Feb 2014 05:01
I'm new to dark gdk myself, but I'm a longtime darkbasic user, so I can at least give you some pointers on where to start, though you'll need to use the help files to get the appropriate commands.

To display the balls, I would recommend using sprites, as they are the easiest way to display 2D images that move around the screen (you can use Paste Image for better performance, but you have to manually redraw the screen each iteration). You will probably want the balls to be saved as a BMP file, then loaded in (dbLoadImage() would be a good way to do that). Load the image and create the sprites before the beginning of the main loop. Position them wherever you want, just not at the same spot (because that will be boring).

For each ball, you will need to store their velocity along each axis (x and y).

Each loop, position the sprite for the ball at it's current location + its velocity. I believe this would be the appropriate syntax:

dbPositionSprite(SpriteNum,dbSpriteX(SpriteNum)+velocityX,dbSpriteY(SpriteNum)+velocityY);

Again, double check in the help files to make sure I got the syntax correct.

So now you have a bunch of balls all moving, but they won't bounce around, so they will just go off the screen. Checking for collision is pretty simple. Use this kind of construct:


Breaking down what happens here:
LeftBound, RightBound,TopBound, and BottomBound are variables that represent where you want the "walls" to be. In general, TopBound=LeftBound=0, RightBound=Screen Width, and Bottom Bound=Screen Height.

spriteVelocityY is just a placeholder for however you store your velocities. For ease, I would recommend an array of doubles for each axis, like so:


but if you are more familiar with C++, you could use an array of std::pair<double,double>, std::vector 's, or even a class for each ball that stores the sprite number, and each velocity value. The choice is yours.

The if's check various points against the boundaries. Sprites are positioned by their top-left point. Effectively, the left-most point will be the sprite's position, so to see if the ball has hit the left side, you just check to see if the left most point (x position value) is less than or equal to your left bound. With the right bound, you have to take the position and add its width to get the right-most point of the ball.

Then, to get it to "bounce", you multiply the appropriate velocity component by -1. So if it hits the right side, that means it's x component was >0, thus now it is "flipped" and becomes <0, making the ball now start traveling to the left.

This logic is repeated for the y axis.

Hope this helps!

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
gregorio1994
10
Years of Service
User Offline
Joined: 17th Feb 2014
Location:
Posted: 18th Feb 2014 16:09
Dude you are awesome, that should get me started

Greg

Login to post a reply

Server time is: 2024-03-29 11:42:20
Your offset time is: 2024-03-29 11:42:20