Invaders game for the Gameduino

Dependencies:   Gameduino mbed

Committer:
TheChrisyd
Date:
Thu Dec 20 21:33:52 2012 +0000
Revision:
2:20a89dc286d5
Parent:
1:f44175dd69fd
Child:
4:e82f4a87df9e
Shields aren't destroyed when hit, life sprites aren't displaying properly.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TheChrisyd 2:20a89dc286d5 1 #ifndef JOYSTICK_INCLUDED
TheChrisyd 2:20a89dc286d5 2 #define JOYSTICK_INCLUDED
TheChrisyd 2:20a89dc286d5 3 /*------------------------------------------------------------
TheChrisyd 2:20a89dc286d5 4 Universal joystick driver for Gameduino
TheChrisyd 2:20a89dc286d5 5
TheChrisyd 2:20a89dc286d5 6 http://www.artlum.com/gameduino/gameduino.html#joystick
TheChrisyd 2:20a89dc286d5 7
TheChrisyd 2:20a89dc286d5 8 Important: Read the file "read_me_first.txt" *before*
TheChrisyd 2:20a89dc286d5 9 writing any code. Do it NOW!
TheChrisyd 2:20a89dc286d5 10
TheChrisyd 2:20a89dc286d5 11 Thanks go to 'Guy" for his contributions to this
TheChrisyd 2:20a89dc286d5 12 ------------------------------------------------------------*/
TheChrisyd 2:20a89dc286d5 13
TheChrisyd 2:20a89dc286d5 14 //#include "WProgram.h"
TheChrisyd 2:20a89dc286d5 15 #include "arduino.h"
TheChrisyd 2:20a89dc286d5 16 class Joystick {
TheChrisyd 2:20a89dc286d5 17 byte buttons; // State of buttons, packed into a byte
TheChrisyd 2:20a89dc286d5 18 byte prev; // State of the buttons on previous read
TheChrisyd 2:20a89dc286d5 19 byte dpad; // State of Up/Down/Left/Right buttons plus some control flags
TheChrisyd 2:20a89dc286d5 20 signed char stickX,stickY;// Analogue x/y position
TheChrisyd 2:20a89dc286d5 21 char xCal,yCal; // Calibration
TheChrisyd 2:20a89dc286d5 22 public:
TheChrisyd 2:20a89dc286d5 23 // The constructor sets up the Arduino pins for input
TheChrisyd 2:20a89dc286d5 24 // and calibrates the joystick using the current position
TheChrisyd 2:20a89dc286d5 25 Joystick();
TheChrisyd 2:20a89dc286d5 26
TheChrisyd 2:20a89dc286d5 27 // Read the current state
TheChrisyd 2:20a89dc286d5 28 void read();
TheChrisyd 2:20a89dc286d5 29
TheChrisyd 2:20a89dc286d5 30 // Return "true" if analog X/Y position is available
TheChrisyd 2:20a89dc286d5 31 bool hasAnalogStick();
TheChrisyd 2:20a89dc286d5 32
TheChrisyd 2:20a89dc286d5 33 // Digital joystick functions
TheChrisyd 2:20a89dc286d5 34 enum button_name {
TheChrisyd 2:20a89dc286d5 35 buttonA = 0x01,
TheChrisyd 2:20a89dc286d5 36 buttonB = 0x02,
TheChrisyd 2:20a89dc286d5 37 buttonC = 0x04,
TheChrisyd 2:20a89dc286d5 38 buttonX = 0x08,
TheChrisyd 2:20a89dc286d5 39 buttonY = 0x10,
TheChrisyd 2:20a89dc286d5 40 buttonZ = 0x20,
TheChrisyd 2:20a89dc286d5 41 buttonStart = 0x40,
TheChrisyd 2:20a89dc286d5 42 buttonSelect = 0x80
TheChrisyd 2:20a89dc286d5 43 };
TheChrisyd 2:20a89dc286d5 44 // Test a named button
TheChrisyd 2:20a89dc286d5 45 // nb. You can combine button names to test multiple buttons at the same time
TheChrisyd 2:20a89dc286d5 46 bool isPressed(byte n) { return (buttons&n)!=0; }
TheChrisyd 2:20a89dc286d5 47
TheChrisyd 2:20a89dc286d5 48 // This tells you if a button changed between the last two calls to "read()"
TheChrisyd 2:20a89dc286d5 49 bool changed(byte n) { return bool((buttons^prev)&n); }
TheChrisyd 2:20a89dc286d5 50
TheChrisyd 2:20a89dc286d5 51 // Joystick up/down/left/right (or analog stick)
TheChrisyd 2:20a89dc286d5 52 bool left() { return (dpad&0x01)!=0; }
TheChrisyd 2:20a89dc286d5 53 bool right() { return (dpad&0x02)!=0; }
TheChrisyd 2:20a89dc286d5 54 bool up() { return (dpad&0x04)!=0; }
TheChrisyd 2:20a89dc286d5 55 bool down() { return (dpad&0x08)!=0; }
TheChrisyd 2:20a89dc286d5 56
TheChrisyd 2:20a89dc286d5 57
TheChrisyd 2:20a89dc286d5 58 // Analog joystick functions
TheChrisyd 2:20a89dc286d5 59
TheChrisyd 2:20a89dc286d5 60 // Force the analog joystick to recalibrate itself
TheChrisyd 2:20a89dc286d5 61 void recalibrate();
TheChrisyd 2:20a89dc286d5 62
TheChrisyd 2:20a89dc286d5 63 // Current joystick position in range [-128..127]
TheChrisyd 2:20a89dc286d5 64 char analogX() { return stickX; }
TheChrisyd 2:20a89dc286d5 65 char analogY() { return stickY; }
TheChrisyd 2:20a89dc286d5 66
TheChrisyd 2:20a89dc286d5 67 // For debugging - show all state onscreen at (x,y)
TheChrisyd 2:20a89dc286d5 68 void dump(int x, int y);
TheChrisyd 2:20a89dc286d5 69 };
TheChrisyd 2:20a89dc286d5 70
TheChrisyd 2:20a89dc286d5 71
TheChrisyd 2:20a89dc286d5 72 // JOYSTICK_INCLUDED
TheChrisyd 2:20a89dc286d5 73 #endif