Using the keyboard, fading, oscillating
Here's a more complex example that pulls together the keyboard input and the oscillator code.
PImage ship1;
PImage ship2;
PImage ship3;
PImage ship4;
PImage water1;
PImage water2;
int alpha1=0;
int alpha2=0;
int alpha3=0;
int alpha4=0;
int speed1=0;
int speed2=0;
int speed3=0;
int speed4=0;
int water1alpha=0;
int water2alpha=50;
int water1speed=1;
int water2speed=-1;
void setup()
{
size(1024,480);
ship1=loadImage("ship1.png");
ship2=loadImage("ship2.png");
ship3=loadImage("ship3.png");
ship4=loadImage("ship4.png");
water1=loadImage("water1.jpg");
water2=loadImage("water2.jpg");
}
void draw()
{
// fade in if the different keys are pressed
if (keyPressed)
{
if (key=='a')
{
speed1=10;
}
else if (key=='f')
{
speed2=10;
}
else if (key=='h')
{
speed3=10;
}
else if (key=='k')
{
speed4=10;
}
}
// update the alpha values and check for thresholds
alpha1 += speed1;
if (alpha1 > 255)
{
alpha1=255;
speed1=-1;
}
alpha2 += speed2;
if (alpha2 > 255)
{
alpha2=255;
speed2=-1;
}
alpha3 += speed3;
if (alpha3 > 255)
{
alpha3=255;
speed3=-1;
}
alpha4 += speed4;
if (alpha4 > 255)
{
alpha4=255;
speed4=-1;
}
water1alpha += water1speed;
if (water1alpha<0)
{
water1speed=1;
}
if (water1alpha>50)
{
water1speed=-1;
}
water2alpha+=water2speed;
if (water2alpha<0)
{
water2speed=1;
}
if (water2alpha>53)
{
water2speed=-1;
}
background(0);
// draw the water layers
tint(255,255,255,water1alpha);
image(water1,0,0);
tint(255,255,255,water2alpha);
image(water2,0,0);
// draw the ships. tint (r,g,b,a) adjusts color of an image before drawing it.
tint(255,255,255,alpha1);
image(ship1,0,0);
tint(255,255,255,alpha2);
image(ship2,250,0);
tint(255,255,255,alpha3);
image(ship3,500,0);
tint(255,255,255,alpha4);
image(ship4,750,0);
}





