Right now I only have one image. I tried duplicating image(shirt, x, y,40,40); with a second image but it only layered the images. I also tried image(shirt, random(x), random(y),40,40); but then that randomly generated them without falling or interacting with the basket. I'm not sure what else to do.
Basket basket;
Timer timer;
Laundry[] laundry;
boolean gameOver = false;
final static int EAST = 2;
final static int WEST = 8;
int result;
int score = 0;
int level = 1;
int lives = 3;
int levelCounter = 0;
int totalLaundry = 0;
int x = 400;
PFont f;
PImage bg,bin,shirt,sock,scarf;
void setup() {
size(800,600);
smooth();
noCursor();
basket = new Basket(132); //control basket height
laundry = new Laundry[15]; //amount of laundry
timer = new Timer(1000); //spread of objects over time
timer.start();
rectMode(CENTER);
imageMode(CENTER);
f = createFont("Arial",12,true);
bg = loadImage("background.gif");
bin = loadImage("basket.gif");
sock = loadImage("sock.gif");
shirt = loadImage("shirt.gif");
scarf = loadImage("scarf.gif");
}
void draw() {
background(bg);
switch(result) {
case EAST: x = x + 5; break;
case WEST: x = x - 5; break;
}
if (gameOver) {
textFont(f,48);
textAlign(CENTER);
fill(95,67,14);
text("GAME OVER",width/2,height/2);
text("score: " + score,width/2,height/2 + 50);
}
else {
basket.setLocation(x,590);
basket.display();
x = constrain(x,66,733);
//timer
if (timer.isFinished()) {
// Initialize laundry
if (totalLaundry < laundry.length) {
laundry[totalLaundry] = new Laundry();
totalLaundry++;
}
timer.start();
}
//display laundry
for (int i = 0; i < totalLaundry; i++ ) {
if (!laundry[i].finished) {
laundry[i].move();
laundry[i].display();
if (laundry[i].reachedBottom()) {
levelCounter++;
laundry[i].finished();
lives--;
if (lives <= 0) {
gameOver = true;
}
}
if (basket.intersect(laundry[i])) {
laundry[i].finished();
levelCounter++;
score++;
}
}
}
if (levelCounter >= laundry.length) {
level++;
levelCounter = 0;
timer.setTime(constrain(300-level*25,0,300));
totalLaundry = 0;
}
//HUD
textFont(f,14);
fill(95,67,14);
text(" " + lives,25,20); //lives
rect(20,40,10,lives*20);
text(" " + level,770,20); //level
text(" " + score,770,40); //score
}
}
void keyPressed(){
switch(key) {
case('d'):case('D'):result |=EAST;break;
case('a'):case('A'):result |=WEST;break;
}
}
void keyReleased(){
switch(key) {
case('d'):case('D'):result ^=EAST;break;
case('a'):case('A'):result ^=WEST;break;
}
}
class Laundry {
float x, y, h, speed;
//is object still being used
boolean finished = false;
Laundry() {
h = 40;
x = random(width);
y = 0;
speed = random(0.5, 0.7);
}
void move() {
y += speed*(level*.5);
}
boolean reachedBottom() {
if (y > height + h) {
return true;
}
else {
return false;
}
}
void display() {
fill(175);
noStroke();
rect(x, y, h, h);
image(shirt, x, y,40,40);
x = constrain(x, 20, 780);
}
void finished() {
finished = true;
}
}
class Timer {
int savedTime; // When Timer started
int totalTime; // How long Timer should last
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
void setTime(int t) {
totalTime = t;
}
void start() {
savedTime = millis();
}
// The function isFinished() returns true if 5,000 ms have passed.
boolean isFinished() {
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}