GBDK Joypad Tutorial

From GbdevWiki
Revision as of 10:21, 15 June 2016 by Gbjosh (Talk | contribs) (This tutorial aims to detail the use of the joypad with GBDK.)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Introduction

This tutorial aims to detail the use of the joypad with GBDK.

Tools

You will need GBDK, and any knowledge of C.

The joypad() function

GBDK's gb.h features a joypad() function that returns the status of the joypad.

The joypad() function can return the status of the following inputs:

J_START
J_SELECT
J_B
J_A
J_DOWN
J_UP
J_LEFT
J_RIGHT

Program 1: Return joypad() status

Lets write a simple program to return the status of joypad() when a button is pressed:

#include <stdio.h> // include this file for the printf() function
#include <gb/gb.h> // include this file for Game Boy functions

void main(void){
	
	while(1) {

	switch(joypad()) {
		
		case J_RIGHT : // If joypad() is equal to RIGHT
			printf("Right!\n");
			delay(100);
			break;
		case J_LEFT : // If joypad() is equal to LEFT
                        printf("Left!\n");
                        delay(100);
			break;
		case J_UP : // If joypad() is equal to UP
			printf("Up!\n");
			delay(100);
			break;
		case J_DOWN : // If joypad() is equal to DOWN
			printf("Down!\n");
			delay(100);
			break;
		case J_START : // If joypad() is equal to START
			printf("Start!\n");
			delay(100);
			break;
		case J_SELECT : // If joypad() is equal to SELECT
			printf("Select!\n");
			delay(100);
			break;
		case J_A : // If joypad() is equal to A
			printf("A!\n");
			delay(100);
			break;
		case J_B : // If joypad() is equal to B
			printf("B!\n");
			delay(100);
			break;			
		default :
			break;
			}
		}
	}

Program 2: Using waitpad() and waitpadup()

There are also two more joypad functions that you can utilize:

waitpad()  // This function waits for a button to be pressed.
waitpadup() // This function waits for all buttons to be released.

Lets use both the waitpad() function and the waitpadup() function in a simple program:

#include <stdio.h> // include this file for the printf() function
#include <gb/gb.h> // include this file for Game Boy functions

void main(void){
	
	while(1) {

	printf("Please press A\n");
	waitpad(J_A); // waitpad() is waiting for the A button to be pressed.
	printf("You pressed A! Cool!\n\n");
	
	printf("Hold down the LEFT button\n");
	waitpad(J_LEFT); // waitpad() is waiting for the LEFT button to be pressed.
	printf("You're holding down LEFT!\n");
	waitpadup(); // waitpadup() is waiting for all buttons to be depressed but you have to hold down LEFT to get here so it is
                    // waiting on LEFT to be depressed.
	printf("You've released LEFT\n\n\n");
	
		}
	}

Program 3: Moving a sprite on the display

Now lets use the joypad() function to move a sprite on the screen. If you are inexperienced with GBDK sprites, you will need to view the GBDK Sprite Tutorial.

#include <gb/gb.h> // include this file for Game Boy functions

//Created with GBTD, exported to .c with options from: 0, to: 0, label: smile
unsigned char smile[] =
{
  0x3C,0x3C,0x42,0x42,0x81,0x81,0xA5,0xA5,
  0x81,0x81,0x81,0xA5,0x42,0x5A,0x3C,0x3C
};

void main(){
	
	int x = 55; // Our beginning x coord
	int y = 75; // Our beginning y coord
	
	SPRITES_8x8;
	set_sprite_data(0, 0, smile);
	set_sprite_tile(0, 0);
	move_sprite(0, x, y); // Move sprite to our predefined x and y coords

	SHOW_SPRITES;

	while(1){
		if(joypad()==J_RIGHT) // If RIGHT is pressed
		{
			x++;
			move_sprite(0,x,y); // move sprite 0 to x and y coords
			delay(10);
		}
		
		if(joypad()==J_LEFT)  // If LEFT is pressed
		{
			x--;
			move_sprite(0,x,y); // move sprite 0 to x and y coords
			delay(10);
		}
		
		if(joypad()==J_UP)  // If UP is pressed
		{ 
			y--;
			move_sprite(0,x,y); // move sprite 0 to x and y coords
			delay(10);
		}
		
		if(joypad()==J_DOWN)  // If DOWN is pressed
		{ 
			y++;
			move_sprite(0,x,y); // move sprite 0 to x and y coords
			delay(10);
		}
	}