SSJX.CO.UK
Content

Bouncing Ball

This program loads a sprite resource and bounces it around 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

What you will need

The Cybiko SDK

The program

Note: Don't type in the line numbers!

  1. //
  2. // Bouncing Ball by ssjx (07/08/2006)
  3. //
  4. #include <cybiko.h>
  5. struct module_t main_module;
  6. struct DirectKeyboard* ptr_direct_keyboard;
  7. struct Bitmap bmp;
  8. long main(int argc, char *argv[],bool start)
  9. {
  10. int x,y,xdir=1,ydir=1,h,w,ext=0;
  11. init_module(&main_module);
  12. ptr_direct_keyboard = DirectKeyboard_get_instance();
  13. x=20+random(120);
  14. y=20+random(60);
  15. TRACE("Loading sprite");
  16. Bitmap_ctor_Ex1( &bmp,"ball.pic" );
  17. h=Bitmap_get_h(&bmp);
  18. w=Bitmap_get_w(&bmp);
  19. TRACE("Sprite is %d x %d in size",w,h);
  20. TRACE("Starting..");
  21. while(ext==0)
  22. {
  23. TGraph_fill_screen( main_module.m_gfx, CLR_WHITE );
  24. Graphics_draw_bitmap( main_module.m_gfx, &bmp, x, y, BM_NORMAL );
  25. DisplayGraphics_show(main_module.m_gfx );
  26. x+=xdir;
  27. y+=ydir;
  28. if ( (x+w)==160){xdir=-xdir;}
  29. if ( (y+h)==100){ydir=-ydir;}
  30. if (x==0){xdir=-xdir;}
  31. if (y==0){ydir=-ydir;}
  32. if ( DirectKeyboard_is_key_pressed( ptr_direct_keyboard, KEY_SPACE ) ){ext=1;}
  33. }
  34. TRACE("Finished");
  35. DirectKeyboard_dtor( ptr_direct_keyboard, FREE_MEMORY );
  36. Bitmap_dtor( &bmp, LEAVE_MEMORY );
  37. return 0L;
  38. }

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 position.
22-27 Load our sprite which was included in our app into the bitmap struct 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 position 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.

Updated 20/04/2024