So I'm working on this program and I have most of it done, but I can't figure out one of the most important things I want to have in it. In the program there are 10 ships in the air which look fine, but I want them to move when I press their corresponding number. So ship0 i press 0 and that ship should be the ship with active controls that i control with the arrow keys. That needs to work with all the ships 0-9. As of now they just sit in place.
Also I have the ships where I want them to start when the program launches but I was wondering if I could turn on gravity easily to make them float to the bottom of the screen until i want to move them. Thanks in advance.
here is what i have so far. I know my syntax is sloppy, just warning you
Code:
Ship[] ships;
int x;
void setup()
{
smooth();
size(500,500);
ships = new Ship[10];
ships[0] = new Ship(300,300,1);
ships[1] = new Ship(150,150,1);
ships[2] = new Ship(450,200,1);
ships[3] = new Ship(400,400,1);
ships[4] = new Ship(500,450,1);
ships[5] = new Ship(225,200,1);
ships[6] = new Ship(450,325,1);
ships[7] = new Ship(100,250,1);
ships[8] = new Ship(300,150,1);
ships[9] = new Ship(175,400,1);
x = 0;
if (keyPressed == true)
{
if (key == '0')
x = 0;
else if (key == '1')
x = 1;
else if (key == '2')
x = 2;
else if (key == '3')
x = 3;
else if (key == '4')
x = 4;
else if (key == '5')
x = 5;
else if (key == '6')
x = 6;
else if (key == '7')
x = 7;
else if (key == '8')
x = 8;
else if (key == '9')
x = 9;
if(key == CODED)
{
if (keyCode == UP)
{
for(int i = 0; i < 10; i++)
ships[x].moveUP();
}
else if (keyCode == DOWN)
{
for(int i = 0; i < 10; i++)
ships[x].moveDOWN();
}
else if (keyCode == LEFT)
{
for(int i = 0; i < 10; i++)
ships[x].moveLEFT();
}
else if (keyCode == RIGHT)
{
for(int i = 0; i < 10; i++)
ships[x].moveRIGHT();
}
}
}
}
void draw()
{
background(125);
for (int i = 0; i < 10; i++)
{
ships[i].drawShip();
}
}
class Ship
{
int locationX;
int locationY;
float scalar;
Ship(int x, int y, float scl)
{
moveShip(x,y);
scalar = scl;
}
void moveShip(int x, int y)
{
locationX = x;
locationY = y;
}
void drawShip()
{
pushMatrix();
strokeWeight(2);
stroke(0);
scale(scalar,scalar);
translate(locationX,locationY);
fill(192,192,192);
ellipse(-50,-50,25,25);
fill(0,255,0);
ellipse(-50,-40,75,20);
fill(0,0,0);
ellipse(-50,-33,35,10);
strokeWeight(2);
stroke(255,0,0);
popMatrix();
}
void moveUP()
{
locationY++;
}
void moveDOWN()
{
locationY--;
}
void moveLEFT()
{
locationX--;
}
void moveRIGHT()
{
locationX++;
}
}