GBDK Joypad Tutorial

From GbdevWiki
Jump to: navigation, search

Introduction

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

Tools

You will need: GBDK, a text editor, Game Boy Tile Designer, and an emulator (BGB is recommended for its many debug features), and any level of knowledge of C.

Substitute your preferred tools if you want, but this tutorial is assuming you have those listed above.

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

Program1.gif

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

Program2.gif

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

Program3.gif

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

Exercises

  • Try changing the sprite when you press a key or switch directions.
  • Use J_START or J_SELECT to "pause" or open a menu.
  • Detect three buttons being pressed at once.[1]