Bouncing Ball
This program loads a sprite resource and bounces it arround the Cybiko display. Direct keyboard is also used
to allow the user to exit the program by pressing the space bar. The sprite is a 16x16 pic file that is put
in the 'res' folder and then gets included in the actual app.
You can download the app + source using the link below.
Download now
If you have any comments about this tutorial, please use the DevCybiko /
PlanetCybiko forums or personal messaging
and let me know!
What you will need
The Cybiko SDK
The program
Note: Don't type in the line numbers!
- //
- // Bouncing Ball by ssjx (7/8/6)
- //
-
- #include <cybiko.h>
-
- struct module_t main_module;
- struct DirectKeyboard* ptr_direct_keyboard;
- struct Bitmap bmp;
-
- long main(int argc, char *argv[],bool start)
- {
- int x,y,xdir=1,ydir=1,h,w,ext=0;
-
- init_module(&main_module);
-
- ptr_direct_keyboard = DirectKeyboard_get_instance();
-
- x=20+random(120);
- y=20+random(60);
-
- TRACE("Loading sprite");
- Bitmap_ctor_Ex1( &bmp,"ball.pic" );
-
- h=Bitmap_get_h(&bmp);
- w=Bitmap_get_w(&bmp);
- TRACE("Sprite is %d x %d in size",w,h);
-
- TRACE("Starting..");
-
- while(ext==0)
- {
- TGraph_fill_screen( main_module.m_gfx, CLR_WHITE );
- Graphics_draw_bitmap( main_module.m_gfx, &bmp, x, y, BM_NORMAL );
- DisplayGraphics_show(main_module.m_gfx );
-
- x+=xdir;
- y+=ydir;
-
- if ( (x+w)==160){xdir=-xdir;}
- if ( (y+h)==100){ydir=-ydir;}
- if (x==0){xdir=-xdir;}
- if (y==0){ydir=-ydir;}
-
- if ( DirectKeyboard_is_key_pressed( ptr_direct_keyboard, KEY_SPACE ) ){ext=1;}
- }
-
- TRACE("Finished");
-
- DirectKeyboard_dtor( ptr_direct_keyboard, FREE_MEMORY );
- Bitmap_dtor( &bmp, LEAVE_MEMORY );
- return 0L;
- }
How it works
| Line(s)
|
|
| 5
| The standard cybiko include and main module required by most cybiko programs.
|
| 7-9
| The main module struct is almost always required, and as we are using bitmaps (sprites) and the keyboard, they
also need structs to hold data in.
|
| 13
| Our variables, these will hold the position, direction and size of our sprite ball.
|
| 15
| Initialises the main_module struct.
|
| 17
| Allows us to use the DirectKeyboard functions to read the keyboard.
|
| 19-20
| Give the sprite a random start postion.
|
| 22-27
| Load our sprite which was included in our app into the bitmap stuct and then get its dimensions.
|
| 31
| The start of our sprite bounce loop.
|
| 33-35
| These lines handle the sprite movement. This is done by clearing the screen, drawing the ball at its new postion
and then displaying everything. If you wanted to slow it down to lessen the motion blurring, a sleep() could be inserted
after the _show() function.
|
| 37-38
| Change the position of the sprite ball. Remember, the sprite does not actually move until it the loop gets to
lines 33-35.
|
| 40-43
| These lines change the direction of the sprite if it reaches the edge of the screen. The sprites origin point is the
top left so we need to add its width to check if it has hit something on the right. The height needs to be added to check
the bottom of the sprite. The direct is changed by changing the direction values sign, e.g. 1 (right) becomes -1 (left).
|
| 45
| Check to see if the space key has been press, if so set our exit variable so we can leave the loop. This loop could
just as easily checked for an exit at the end using 'do{}while(ext==0);'.
|
| 50-53
| Tidy up by deleting our bitmap and keyboard objects, then exit our program.
|
|
|