Adding tones to Cube Invaders

The "Cube4" is a 4x4x4 RGB LED cube with an Arduino Leonardo compatible onboard controller. [Product page]
Post Reply
ShaunCarMan
Posts: 1
Joined: Wed Feb 10, 2016 1:48 am
Contact:

Adding tones to Cube Invaders

Post by ShaunCarMan » Thu Feb 25, 2016 12:45 am

Hi, I'm trying to add a tone for the player every time it moves but I don't know where to put this code line and how to do it

Code: Select all

tone (piezo, 523, playerTime);
Any help would be appreciated and here is my code so far.

Code: Select all

#include "SPI.h"
#include "Cube.h"

Cube cube;

#include "Wire.h"
#include "WiiChuck.h"

WiiChuck wiichuck = WiiChuck();

unsigned int  joystickReadInterval  = 150;
unsigned long lastJoystickUpdate    = 0;
unsigned long lastEnemyMovement     = 0;
unsigned int  enemyMovementInterval = 600;

int playerX = 1;
int playerY = 0;
int playerZ = 1;

int enemyX;
int enemyY;
int enemyZ;

int speaker = A4;
int notes[] = {261, 293, 329, 349, 392, 440, 493, 523, 587, 659, 698, 783, 880};

int startTime = 10;
int playerTime = 50;
int enemyTime = 80;
int boomTime = 100;
int shootTime = 30;
int finishTime = 100;


byte lastButtonZState = 0;

void setup(void) {

  cube.begin(0, 115200);
  wiichuck.begin();
  wiichuck.update();
  enemyX = random(0, 4);
  enemyZ = random(0, 4);
  enemyY = 3;
  pinMode(A4, OUTPUT);
}

void loop(void) {
  delay(startTime);
  wiichuck.update();
  cube.all(BLACK);
  checkTrigger();
  updatePlayerPosition();
  updateEnemyPosition();
  cube.set(playerX, playerY, playerZ, BLUE);
  cube.set(enemyX, enemyY, enemyZ, GREEN);
}

void updatePlayerPosition()
{
  if((millis() - lastJoystickUpdate) > joystickReadInterval)
  {    
    int joyX;
    int joyY;

    joyX = wiichuck.readJoyX();
    joyY = wiichuck.readJoyY();

    if(joyX > 50)
      playerX++;

    if(joyX < -50)
      playerX--;

    if(joyY > 50)
      playerZ++;

    if(joyY < -50)
      playerZ--;

    if(playerX > 3)
      playerX = 0;

    if(playerX < 0)
      playerX = 3;

    if(playerZ > 3)
      playerZ = 0;

    if(playerZ < 0)
      playerZ = 3;

    lastJoystickUpdate = millis();
  }
}

void updateEnemyPosition()
{
  if((millis() - lastEnemyMovement) > enemyMovementInterval)
  {
    enemyY--;

    if(enemyY < 0)
    {
      enemyX = random(0, 4);
      enemyZ = random(0, 4);
      enemyY = 3;
    }

    lastEnemyMovement = millis();
    tone(speaker, 261, enemyTime);
  }
}

void checkTrigger()
{
  if((wiichuck.buttonZ) && (lastButtonZState == 0))
  {
    shoot();
  }
  if(wiichuck.buttonZ)
  {
    lastButtonZState = 1;
  } 
  else {
    lastButtonZState = 0;
  } 
}

void shoot()
{
  cube.set(playerX, 1, playerZ, YELLOW);
  tone(speaker, 698, shootTime);
  delay(shootTime);
  cube.set(playerX, 2, playerZ, ORANGE);
  tone(speaker, 783, shootTime);
  delay(shootTime);
  cube.set(playerX, 3, playerZ, RED);
  tone(speaker, 880, shootTime);
  delay(shootTime);

  if((enemyX == playerX) && (enemyZ == playerZ))
    enemyY = -1;

  delay(finishTime);

  if((enemyX == playerX) && (enemyZ == playerZ))
  {
    cube.all(YELLOW);
    tone(speaker, 392, boomTime);
    delay(boomTime);
    cube.all(ORANGE);
    tone(speaker, 440, boomTime);
    delay(boomTime);
    cube.all(RED);
    tone(speaker, 493, boomTime);
    delay(boomTime);
    cube.all(BLACK);
  }
}

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Adding tones to Cube Invaders

Post by andrew » Mon Apr 18, 2016 10:48 pm

If you want the tone after the player moves, try your tone function at the end of the void updatePlayerPosition() function.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests