0

Code for an Arduino Game
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Code for an Arduino Game

by heath1980 on Sat Apr 26, 2014 8:21 am

I have created a game using an Arduino Mega 2560 and 6 x 12mm Diffused Thin Digital RGB LED Pixels (Strand of 25) - WS2801.
Game Title: The Blue Wall Defender
Description: Defend your blue wall against the invading Red and Green dots that are trying to destroy your wall.
The wall has 6 hit points and each enemy takes 1 off. The enemy's can not harm you only the wall. There are three buttons the middle one(Number 2) will start the game and shoot, the other two will move you up and down.
Screen Size: x-15 pixels X y-10 Pixels
screen setup: I have 3 screens of 50 Leds combined into one screen.
Screens.PNG
Screens.PNG (19.64 KiB) Viewed 441 times

Videos: https://www.youtube.com/watch?v=EdBJNYkrFQA https://www.youtube.com/watch?v=G0zqiOZv_rI
Code: Select all | TOGGLE FULL SIZE
// The Blue Wall Defender
// Created by Heath Young

#include "SPI.h"
#include "Adafruit_WS2801.h"

#define dataPin1   24    // Yellow wire on Adafruit Pixels
#define clockPin1  27    // Green wire on Adafruit Pixels
#define dataPin2   28    // Yellow wire on Adafruit Pixels
#define clockPin2  31    // Green wire on Adafruit Pixels
#define dataPin3   40    // Yellow wire on Adafruit Pixels
#define clockPin3  43    // Green wire on Adafruit Pixels

#define buttonPin1 51
#define buttonPin2 50
#define buttonPin3 52
boolean buttonPressed = false;

//                               Number of lights->X-Axis       Y-Axis
Adafruit_WS2801 strip1 = Adafruit_WS2801((uint16_t)5, (uint16_t)10, dataPin1, clockPin1);
Adafruit_WS2801 strip2 = Adafruit_WS2801((uint16_t)5, (uint16_t)10, dataPin2, clockPin2);
Adafruit_WS2801 strip3 = Adafruit_WS2801((uint16_t)5, (uint16_t)10, dataPin3, clockPin3);

boolean start = false;

uint8_t Difficultylevel = 5;

uint8_t playerX = 1;
uint8_t playerY = 5;

uint8_t playerWall[10] = {
  10,10,10,10,10,10,10,10,10,10};

boolean shot1 = false;
uint8_t shot1X = 0;
uint8_t shot1Y = 0;
boolean shot2 = false;
uint8_t shot2X = 0;
uint8_t shot2Y = 0;

boolean enemy[10] = {
  false, false, false, false, false, false, false, false, false, false};
boolean enemyA[10] = {
  false, false, false, false, false, false, false, false, false, false};
uint8_t enemyX[10] = {
  0,0,0,0,0,0,0,0,0,0};
uint8_t enemyY[10] = {
  0,0,0,0,0,0,0,0,0,0};

boolean explosions[3] = {
  false, false, false};
uint8_t explosionsX[3] = {
  0,0,0};
uint8_t explosionsY[3] = {
  0,0,0};
uint8_t explosionsCycle[3] = {
  0,0,0};

uint8_t score1 = 0;
uint8_t score10 = 0;
uint8_t score100 = 0;
uint8_t score1000 = 0;

uint8_t cycle10 = 0;

void setup() {
  strip1.begin();
  strip1.show();
  strip2.begin();
  strip2.show();
  strip3.begin();
  strip3.show();

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);

  // Serial.begin(9600);
}

void loop(){
  if(start == true)
  {
    if(digitalRead(buttonPin1) == HIGH || digitalRead(buttonPin2) == HIGH || digitalRead(buttonPin3) == HIGH)
    {
      //  Controls
      if(digitalRead(buttonPin1) == HIGH && buttonPressed == false)
      {
        buttonPressed = true;
        if(playerY <= 7)
        {
          playerY = playerY + 1;
        }
      }
      if(digitalRead(buttonPin2) == HIGH && buttonPressed == false)
      {
        buttonPressed = true;
        shoot();
      }
      if(digitalRead(buttonPin3) == HIGH && buttonPressed == false)
      {
        buttonPressed = true;
        if(playerY >= 2)
        {
          playerY = playerY - 1;
        }
      }

    }
    else
    {
      buttonPressed = false;
    }

    if(cycle10 == 0)
    {
      if(random(0,10) <= 4)
      {
        enemyCreate();
      }
    }

    // Cycle
    score();
    shotUpdate();
    if(cycle10 == 0)
    {
      enemyUpdate();
    }
     
    explosionsUpdate();
    wallUpdate();

    // Display
    shotDraw();
    enemyDraw();
    player();
    strip1.show();
    strip2.show();
    strip3.show();
    Clear();
   
    // Counter
    cycle();
  }
  else      // Start Game parameters
  {
    if(digitalRead(buttonPin2) == HIGH)
    {
      for(uint8_t i=0;i<10;i++)
      {
        playerWall[i] = 6;
        enemy[i] = false;
      }
      score1 = 0;
      score10 = 0;
      score100 = 0;
      score1000 = 0;
      start = true;
      buttonPressed = true;
      Difficultylevel = 3;
      shot1 = false;
      shot1X = 0;
      shot1Y = 0;
      shot2 = false;
      shot2X = 0;
      shot2Y = 0;
    }
    else
    {
      Clear();
      wallUpdate();
      explosionsUpdate();
      strip1.show();
      strip2.show();
      strip3.show();
      cycle();
    }
  }

}

void score()
{
  if(score1 >= 10)
  {
    score1 = 0;
    score10 = score10 + 1;
  }
  if(score10 >= 10)
  {
    score10 = 0;
    score100 = score10 + 1;
    if(Difficultylevel >= 10)
    {
      Difficultylevel = 10;
    }
    else
    {
    Difficultylevel = Difficultylevel + 1;
    }
  } 
  for(uint8_t i=0;i<score1;i++)
  {
    Draw( 14, i, 10,10,10);
  }
  for(uint8_t i=0;i<score10;i++)
  {
    Draw( 13, i, 10,10,10);
  }
}

//  Draws the player on screen
void player()
{
  Draw( playerX+1, playerY, 255,255,255 );
  Draw( playerX, playerY, 255,255,255 );
  Draw( playerX, playerY-1, 255,255,255 );
  Draw( playerX, playerY+1, 255,255,255 );
}

void wallUpdate()
{
  for(uint8_t i=0;i<10;i++)
  {

    if(playerWall[i] == 6)
    {
      Draw( 0, i, 0,100,255 ) ;
    }
    else if(playerWall[i] >= 5)
    {
      Draw( 0, i, 0,50,200 ) ;
    }
    else if(playerWall[i] >= 3)
    {
      Draw( 0, i, 0,0,150 ) ;
    }
    else if(playerWall[i] >= 2)
    {
      Draw( 0, i, 0,0,100 ) ;
    }
    else if(playerWall[i] >= 1)
    {
      Draw( 0, i, 0,0,50 ) ;
    }
    else if(playerWall[i] == 0)
    {
      if(cycle10 == 0)
      {
      explosionsCreate(1, i);
      }
      if(cycle10 == 2)
      {
      explosionsCreate(4, i);
      }
      if(cycle10 == 4)
      {
      explosionsCreate(7, i);
      }
      Draw( 0, i, 0,0,0 ) ;
      start = false;
    }
  }
}

void cycle()
{
  if(cycle10 >= 9)
  {
    cycle10 = 0;
  }
  else
  {
    cycle10 = cycle10 + 1;
  }
}


// Creates the shot.
void shoot()
{
  if(shot1 == false)
  {
    shot1 = true;
    shot1X = playerX;
    shot1Y = playerY;
  }
  else
    if(shot2 == false)
    {
      shot2 = true;
      shot2X = playerX;
      shot2Y = playerY;
    }
}

//  Draws the shot on screen.
void shotDraw()
{
  if(shot1 == true)
  {
    Draw( shot1X, shot1Y, 255,255,255 );
  }
  if(shot2 == true)
  {
    Draw( shot2X, shot2Y, 255,255,255 );
  }
}

//  Updates the position of the shot.
void shotUpdate()
{
  if(shot1 == true)
  {
    if(shot1X <= 13)
    {
      shot1X = shot1X + 1;
    }
    else
    {
      shot1 = false;
      shot1X = 0;
      shot1Y = 0;
    }
  }
  if(shot2 == true)
  {
    if(shot2X <= 13)
    {
      shot2X = shot2X + 1;
    }
    else
    {
      shot2 = false;
      shot2X = 0;
      shot2Y = 0;
    }
  }

  // Detect hit.
  for(uint8_t i=0;i<10;i++)
  {
    if(enemy[i] == true)
    {
      if(shot1X == enemyX[i] && shot1Y == enemyY[i])
      {
        explosionsCreate( enemyX[i], enemyY[i]);
        enemy[i] = false;
        enemyA[i] = false;
        enemyX[i] = 0;
        enemyY[i] = 0;
        shot1 = false;
        shot1X = 0;
        shot1Y = 0;
        score1 = score1 + 1;
      }
      if(shot2X == enemyX[i] && shot2Y == enemyY[i])
      {
        explosionsCreate( enemyX[i], enemyY[i]);
        enemy[i] = false;
        enemyA[i] = false;
        enemyX[i] = 0;
        enemyY[i] = 0;
        shot2 = false;
        shot2X = 0;
        shot2Y = 0;
        score1 = score1 + 1;
      }
    }
  }
}

void explosionsCreate( uint8_t X, uint8_t Y)
{
  for(uint8_t i=0;i<3;i++)
  {
    if(explosions[i] == false)
    {
      explosions[i] = true;
      explosionsX[i] = X;
      explosionsY[i] = Y;
      explosionsCycle[i] = 0;
      break;
    }
  }
}

void explosionsUpdate()
{
  for(uint8_t i=0;i<3;i++)
  {
    if(explosions[i] == true)
    {
      if(explosionsCycle[i] == 0)
      {
        Draw( explosionsX[i], explosionsY[i], 240,200,0 );
        explosionsCycle[i] = 1;
      }
      else if(explosionsCycle[i] == 1)
      {
        Draw( explosionsX[i]+1, explosionsY[i], 180,150,0 );
        Draw( explosionsX[i], explosionsY[i], 180,150,0 );
        Draw( explosionsX[i]-1, explosionsY[i], 180,150,0 );
        explosionsCycle[i] = 2;
      }
      else if(explosionsCycle[i] == 2)
      {
        Draw( explosionsX[i]+1, explosionsY[i], 100,80,0 );
        Draw( explosionsX[i]-1, explosionsY[i], 100,80,0 );
        Draw( explosionsX[i], explosionsY[i]+1, 100,80,00 );
        Draw( explosionsX[i], explosionsY[i]-1, 100,80,0 );
        explosionsCycle[i] = 0;
        explosions[i] = false;
      }
    }
  }
}

void enemyCreate()
{
  for(uint8_t i=0;i<Difficultylevel;i++)
  {
    if(enemy[i] == false)
    {
      if(random(0,11) <= 8)
      {
        enemy[i] = true;
        enemyA[i] = false;
        enemyX[i] = 14;
        enemyY[i] = random(1,9);
        break;
      }
      else
      {
        enemy[i] = true;
        enemyA[i] = true;
        enemyX[i] = 14;
        enemyY[i] = random(1,9);
        break; 
      }
    }
  }
}

void enemyUpdate()
{
  for(uint8_t i=0;i<10;i++)
  {
    if(enemy[i] == true)
    {
      if(enemyA[i] == false)
      {
        if(enemyX[i] >= 1)
        {
          enemyX[i] = enemyX[i] - 1;
        }
        else
        {
          enemy[i] = false;
          if(playerWall[i] == 0)
          {
          }
          else
          {
            playerWall[enemyY[i]] = playerWall[enemyY[i]] - 1;
          }
          enemyX[i] = 0;
          enemyY[i] = 0;
        }
      }
      else
      {
        if(enemyY[i] <= playerY +1 && enemyY[i] >= playerY - 1)
        {
           if(enemyY[i] >= playerY)
           {
             if(enemyY[i] >= 8)
             {
               enemyY[i] = enemyY[i]-1;
             }
             else
             {
               enemyY[i] = enemyY[i]+1;
             }
           }
           else
           {
             if(enemyY[i] <= 1)
             {
               enemyY[i] = enemyY[i]+1;
             }
             else
             {
               enemyY[i] = enemyY[i]-1;
             }
           }
        }
        else if(enemyX[i] >= 1)
        {
          enemyX[i] = enemyX[i] - 1;
        }
        else
        {
          enemy[i] = false;
          if(playerWall[enemyY[i]] == 0)
          {
          }
          else
          {
            playerWall[enemyY[i]] = playerWall[enemyY[i]] - 1;
          }
          enemyX[i] = 0;
          enemyY[i] = 0;
        }
      }
    }
  }
}

void enemyDraw()
{
  for(uint8_t i=0;i<10;i++)
  {
    if(enemy[i] == true)
    {
      if(enemyA[i] == false)
      {
        Draw( enemyX[i], enemyY[i], 255,0,0 );
      }
      else
      {
        Draw( enemyX[i], enemyY[i], 0,255,0 );
      } 
    }
  }
}
//  Draws a Single dot.
//  DrawLineX(x, y, Red, Green, Blue)
void Draw( uint8_t X, uint8_t Y,uint8_t R, uint8_t G, uint8_t B)
{
  if(X >= 10)
  {
    strip3.setPixelColor( X, Y, R,G,B );
  }
  else if(X >= 5)
  {
    strip2.setPixelColor( X, Y, R,G,B );
  }
  else
  {
    strip1.setPixelColor( X, Y, R,G,B );
  }
}

//  Draws a line on x axis.
//  DrawLineX(x, y, Lenght, Red, Green, Blue)
void DrawLineX( uint8_t X, uint8_t Y, uint8_t len,uint8_t R, uint8_t G, uint8_t B)
{
  for(uint8_t X1 = X;X1<(X + len);X1++)
  {
    if(X1 >= 10)
    {
      strip3.setPixelColor( X1, Y, R,G,B );
    }
    else if(X1 >= 5)
    {
      strip2.setPixelColor( X1, Y, R,G,B );
    }
    else
    {
      strip1.setPixelColor( X1, Y, R,G,B );
    }
  }
}

//  Draws a line on y axis.
//  DrawLineX(x, y, Lenght, Red, Green, Blue)
void DrawLineY( uint8_t X, uint8_t Y, uint8_t len,uint8_t R, uint8_t G, uint8_t B)
{
  for(uint8_t Y1 = Y;Y1<(Y + len);Y1++)
  {
    if(X >= 10)
    {
      strip3.setPixelColor( X, Y1, R,G,B );
    }
    else if(X >= 5)
    {
      strip2.setPixelColor( X, Y1, R,G,B );
    }
    else
    {
      strip1.setPixelColor( X, Y1, R,G,B );
    }
  }
}

//Clears the Display.
void Clear()
{
  for (uint8_t x=0; x<5; x++) {
    strip1.setPixelColor( x, 9, 0,0,0 );
    strip1.setPixelColor( x, 8, 0,0,0 );
    strip1.setPixelColor( x, 7, 0,0,0 );
    strip1.setPixelColor( x, 6, 0,0,0 );
    strip1.setPixelColor( x, 5, 0,0,0 );
    strip1.setPixelColor( x, 4, 0,0,0 );
    strip1.setPixelColor( x, 3, 0,0,0 );
    strip1.setPixelColor( x, 2, 0,0,0 );
    strip1.setPixelColor( x, 1, 0,0,0 );
    strip1.setPixelColor( x, 0, 0,0,0 );
    strip2.setPixelColor( x, 9, 0,0,0 );
    strip2.setPixelColor( x, 8, 0,0,0 );
    strip2.setPixelColor( x, 7, 0,0,0 );
    strip2.setPixelColor( x, 6, 0,0,0 );
    strip2.setPixelColor( x, 5, 0,0,0 );
    strip2.setPixelColor( x, 4, 0,0,0 );
    strip2.setPixelColor( x, 3, 0,0,0 );
    strip2.setPixelColor( x, 2, 0,0,0 );
    strip2.setPixelColor( x, 1, 0,0,0 );
    strip2.setPixelColor( x, 0, 0,0,0 );
    strip3.setPixelColor( x, 9, 0,0,0 );
    strip3.setPixelColor( x, 8, 0,0,0 );
    strip3.setPixelColor( x, 7, 0,0,0 );
    strip3.setPixelColor( x, 6, 0,0,0 );
    strip3.setPixelColor( x, 5, 0,0,0 );
    strip3.setPixelColor( x, 4, 0,0,0 );
    strip3.setPixelColor( x, 3, 0,0,0 );
    strip3.setPixelColor( x, 2, 0,0,0 );
    strip3.setPixelColor( x, 1, 0,0,0 );
    strip3.setPixelColor( x, 0, 0,0,0 );
  }
}




heath1980
 
Posts: 18
Joined: Wed Sep 25, 2013 7:18 pm

Re: Code for an Arduino Game

by adafruit_support_bill on Sat Apr 26, 2014 10:17 am

Cool! Thanks for posting.

adafruit_support_bill
 
Posts: 71810
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.