#include<stdio.h> // include this file for the printf() function #include<gb/gb.h> // include this file for Game Boy functions
voidmain(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: 使用 waitpad() 和 waitpadup()
您还可以使用另外两个游戏手柄的函数:
1 2
waitpad() // This function waits for a button to be pressed. waitpadup() // This function waits for all buttons to be released.
#include<stdio.h> // include this file for the printf() function #include<gb/gb.h> // include this file for Game Boy functions
voidmain(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"); } }
#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 unsignedchar smile[] = { 0x3C,0x3C,0x42,0x42,0x81,0x81,0xA5,0xA5, 0x81,0x81,0x81,0xA5,0x42,0x5A,0x3C,0x3C };
voidmain(){ 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); } }