ArcadeSlam software template

The ArcadeSlam set of programs make use of a simple Sprite based library which controls images on the screen.  They also use a Screen object and a collection of buttons.  The code below contains a template that you might use to start a new game.  It draws a simple sprite on the screen that you can move around using the buttons.  You need an MSP430 Launchpad to write new games.  This can be obtained from Radionics or other electronics suppliers.

To write your own program, download the template program from here.  Open it in Energia and modify to suit your needs.  To download the program to the MSP430G2553 in the ArcadeSlam breadboard, wire a launchpad PCB as shown below.   WARNING: be sure to remove the battery connections before you do this.  Be very careful wiring the launchpad to the breadboard (zoom in really tight on this image).

img_20161116_211235165

 

The main code is listed below

// ArcadeSlam : Retro arcade gaming on an MCU.
// This program is designed to run on the TIMSP430G2553 and was developed in the Energia Integrated Development Environment
// For construction details and update please visit http://roboslam.com/arcadeslam
// This program is relased under the GNU Public License Version 2 a copy of which should be included with
// this collection of software
//
// New game template.
// Modify the PlayGame function to implement your game.
//
/*
The Button interface
SetupButtons() : Configures the port pins for use as button inputs
LeftPressed() : returns 1 if the left button was pressed.  Otherwise returns 0.
RightPressed() : returns 1 if the right button was pressed.  Otherwise returns 0.
FirePressed() : returns 1 if the fire button was pressed.  Otherwise returns 0.

The Screen interface
putPixel( x, y, colour);
putImage( x,  y, width, height,  Image);
fillRectangle( x, y, width, height, Colour);
putText(Text,  length,  x,  y, ForeColour, BackColour);
putNumber(Number, x, y,  ForeColour, BackColour);

The Sprite interface
Sprite(Image, x, y, width, height) : This constructor builds the Sprite object.  Supply an image, its dimensions and location
show() : show the sprite
hide() : hide the sprite
redraw() : redraw the sprite (useful if has been obscured by another sprite)
visible() : returns 1 if the sprite is visible
move( x, y) : move the sprite to the new position
getX() : returns sprite X ordinate
getY() : returns sprite Y ordinate
getWidth() : returns width of sprite
getHeight() : returns height of sprite
touching( x, y ) : Does the point (x,y) touch the perimiter of the sprite?
within( x, y ) : Is the point (x,y) within the sprite

Useful macros:
SCREEN_WIDTH, SCREEN_HEIGHT : the current screen dimension
RGBtoWord(r,g,b) : combine the r,g and b bytes to produce a colour number in a format the display recognizes

*/

#include <SPI.h>
#include "Sprite.h"
#include "Display.h"
#include "Images.h"

extern Display Screen;

void setup()
{
  SetupButtons();
  Screen.begin();
  randomSeed(analogRead(A0)); // initialize the random number generator (might get some noise on A0)
  pinMode(P1_0,OUTPUT); // Used for debugging
}

uint8_t GameStarted=0;
void loop()
{
  GameStarted = 0;
  Screen.fillRectangle(0,0,240,320,0);
  Screen.putText("Arcade",6,70,50, RGBToWord(0x1f,0x1f,0xff), 0);
  Screen.putText("Slam!",5,125,50, RGBToWord(0xff,0xff,0x0), 0);
  Screen.putText("Press fire to start", 19, 5, 100, RGBToWord(0x1f,0xff,0x1f), 0);
  while (GameStarted == 0)
  {
    if (FirePressed())
    {
      GameStarted = 1;
      PlayGame();
    }
  }
}
// Definition of the Frog image
#define FROG RGBToWord(0x1f,0xff,0x1f)
#define EYES RGBToWord(0x3f,0x3f,0xff)
//  An frog image is shown below
/*
    #.#......#.#
    .#...##...#.
    .#.EE##EE.#.
    ..########..
    ..########..
    ..########..
    ..########..
    ...##..##...
    ...##..##...
    ..##....##..
    .#.#....#.#.
*/

const uint16_t FrogImage[] = { \
    FROG,BGND,FROG,BGND,BGND,BGND,BGND,BGND,BGND,FROG,BGND,FROG, \
    BGND,FROG,BGND,BGND,BGND,FROG,FROG,BGND,BGND,BGND,FROG,BGND, \
    BGND,FROG,BGND,EYES,EYES,FROG,FROG,EYES,EYES,BGND,FROG,BGND, \
    BGND,BGND,FROG,FROG,FROG,FROG,FROG,FROG,FROG,FROG,BGND,BGND, \
    BGND,BGND,FROG,FROG,FROG,FROG,FROG,FROG,FROG,FROG,BGND,BGND, \
    BGND,BGND,FROG,FROG,FROG,FROG,FROG,FROG,FROG,FROG,BGND,BGND, \
    BGND,BGND,FROG,FROG,FROG,FROG,FROG,FROG,FROG,FROG,BGND,BGND, \
    BGND,BGND,BGND,FROG,FROG,BGND,BGND,FROG,FROG,BGND,BGND,BGND, \
    BGND,BGND,BGND,FROG,FROG,BGND,BGND,FROG,FROG,BGND,BGND,BGND, \
    BGND,BGND,FROG,FROG,BGND,BGND,BGND,BGND,FROG,FROG,BGND,BGND, \
    BGND,FROG,BGND,FROG,BGND,BGND,BGND,BGND,FROG,BGND,FROG,BGND  \
};
void PlayGame()
{
  // This is not a game really.  It just shows you how to put a moveable sprite on the screen.
  // In this case, the sprite is a little green frog with blue eyes.
  int GameOver = 0;
  Sprite Frog(FrogImage,SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ,12,11); // Create the frog sprite and put it in the middle of the display
  Screen.fillRectangle(0,0,240,320,0); // Blank the screen before starting
  Frog.show();  // Show the frog
  while (!GameOver)  // The main game loop
  {
    if (LeftPressed())
    {
      // If the left button is pressed, move the frog one pixel to the left.
      Frog.move(Frog.getX()-1,Frog.getY());
    }
    if (RightPressed())
    {
      // If the right button is pressed, move the frog one pixel to the right
      Frog.move(Frog.getX()+1,Frog.getY());
    }
  }

}
void SetupButtons()
{
    // Set up the buttons
  pinMode(P2_4,INPUT);
  pinMode(P2_6,INPUT);
  pinMode(P2_7,INPUT);
  // enable pull-up resistors
  P2REN |= (BIT4 + BIT6 + BIT7);
  P2OUT |= (BIT4 + BIT6 + BIT7);
}
int LeftPressed()
{
  if ( (P2IN & BIT4) == 0)
    return 1;
  else
    return 0;
}
int RightPressed()
{
  if ( (P2IN & BIT6) == 0)
    return 1;
  else
    return 0;
}
int FirePressed()
{
  static int PreviousState = 0;
  if ((P2IN & BIT7) == 0)
  {
    if (PreviousState == 0)
    {
      PreviousState = 1;
      return 1;
    }
    else
      return 0;

  }
  else
  {
    PreviousState = 0;
    return 0;
  }
}

Advertisement