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

//
// Bouncing Ball by ssjx (07/08/2006)
//

#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 xdir=1,ydir=1,h,w,ext=0;

	init_module(&main_module);

	// Get a pointer to the keyboard
	ptr_direct_keyboard = DirectKeyboard_get_instance();

	// Give the sprite a random start position.
	int x=20+random(120);
	int y=20+random(60);

	// Load our sprite which was included in our app into the bitmap struct and then get its dimensions.
	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){
		// 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.

		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 );

		// Update the position of the sprite ball.

		x+=xdir;
		y+=ydir;

		// 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).

		if ( (x+w)==160){xdir=-xdir;}
		if ( (y+h)==100){ydir=-ydir;}
		if (x==0){xdir=-xdir;}
		if (y==0){ydir=-ydir;}

		// 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);'.

		if ( DirectKeyboard_is_key_pressed( ptr_direct_keyboard, KEY_SPACE ) ){ext=1;}
	}

	TRACE("Finished");

	// Clean up
	DirectKeyboard_dtor( ptr_direct_keyboard, FREE_MEMORY );
	Bitmap_dtor( &bmp, LEAVE_MEMORY );
	return 0L;
}

Updated 20/07/2026