//Greg Batha - Project 2 "Play"
//DESMA 28 INTERACTIVITY Fall08
PImage tiredCloud;
PImage happyCloud;
PImage sadCloud;
PImage thunderCloud;
PImage cloudShape;
PImage cloudShadow;
//initializes sprites for facial expressions
PImage tiredFace;
PImage tiredFace_blink;
PImage happyFace;
PImage happyFace_blink;
PImage sadFace;
PImage sadFace_blink;
PImage thunderFace;
PImage thunderFace_blink;
PImage rainDropSprite;
PImage[] lightning = new PImage[5];
PImage bg;
PImage grass1;
PImage grass2;
PImage grass3;
PImage grass4;
PImage grass5;
PImage grass6;
int textVisible;
PFont font;
int thisFrame;
//initializes objects
Rain[] rainDrop;
Grass[] grassPatch;
Cloud clyde;
void setup(){
size(600, 400);
colorMode(HSB);
cloudShape = loadImage("cloudShape_test.png");
cloudShadow = loadImage("cloudShape_shadow.png");
lightning[0] = loadImage("lightning0.png");
lightning[1] = loadImage("lightning1.png");
lightning[2] = loadImage("lightning2.png");
lightning[3] = loadImage("lightning3.png");
lightning[4] = loadImage("lightning4.png");
tiredFace = loadImage("faceTired.png");
tiredFace_blink = loadImage("faceTired_blink.png");
happyFace = loadImage("faceHappy.png");
happyFace_blink = loadImage("faceHappy_blink.png");
sadFace = loadImage("faceSad.png");
sadFace_blink = loadImage("faceSad_blink.png");
thunderFace = loadImage("faceThunder.png");
thunderFace_blink = loadImage("faceThunder_blink.png");
bg = loadImage("hills2.png");
rainDropSprite = loadImage("rainDrop.png");
grass1 = loadImage("grass1v.png");
grass2 = loadImage("grass2v.png");
grass3 = loadImage("grass3v.png");
grass4 = loadImage("grass4v.png");
grass5 = loadImage("grass5v.png");
grass6 = loadImage("grass6v.png");
textVisible = 255;
font = loadFont("GrotesqueMT-Bold-16.vlw");
textFont(font, 16);
thisFrame = millis();
rainDrop = new Rain[50];
for(int i = 0; i < 50; i++){
rainDrop[i] = new Rain();
}
grassPatch = new Grass[6];
for(int i = 0; i < 6; i++){
grassPatch[i] = new Grass(i*100, null);
}
//THIS NEEDS TO BE FIXED
grassPatch[0].sprite = grass1;
grassPatch[1].sprite = grass2;
grassPatch[2].sprite = grass3;
grassPatch[3].sprite = grass4;
grassPatch[4].sprite = grass5;
grassPatch[5].sprite = grass6;
clyde = new Cloud();
}
void draw(){
//controls the sky color
if(clyde.full > 6){
background(color(map(209, 0, 360, 0, 255), map(75, 0, 100, 0, 255), clyde.tintLevel));
}
else {
background(color(map(209, 0, 360, 0, 255), map(75, 0, 100, 0, 255), 255));
}
image(bg, 0, 0);
//for loop checks to see if any rain objects have reached
//the designated y coordinate of the grass
for(int i = 0; i < 50; i++){
if(rainDrop[i].y + rainDrop[i].sprite.height > 350){
rainDrop[i].visible = false;
for(int j = 0; j < 6; j++){
if(rainDrop[i].x + 45 > grassPatch[j].x &&
rainDrop[i].x + (rainDrop[i].sprite.width/2) < grassPatch[j].x + 100){
grassPatch[j].watered = constrain(grassPatch[j].watered + 5, 50, 125);
}
}
}
}
//checks to see if grass patch is being struck by lightning
if(clyde.strike == true){
for(int i = 0; i < 6; i++){
if(grassPatch[i].x < clyde.x + 63 && grassPatch[i].x + 100 > clyde.x + 63){
grassPatch[i].watered = constrain(grassPatch[i].watered - 20, 50, 125);
}
}
}
//draws the grass
for(int i = 0; i < 6; i++){
grassPatch[i].display();
grassPatch[i].wilt();
}
//draws and moves the rain
for(int i = 0; i < 50; i++){
rainDrop[i].fall();
rainDrop[i].display();
}
//draws and moves clyde
clyde.move();
clyde.lightning();
clyde.display();
println(frameRate);
//writes text at the beginning
thisFrame = millis();
if(thisFrame < 10000){
if(thisFrame > 5000){
textVisible -= 2;
}
fill(255, textVisible);
text("- Click anywhere on the screen or press the space bar to rain! -", 45, 220);
}
}
class Grass{
//global varibles
PImage sprite;
float watered;
boolean overlap;
int x; //uper left corner of the grass object. each are 100px wide
Grass(int xPos, PImage grassSprite){
sprite = grassSprite;
x = xPos;
watered = 125;
}
void display(){
tint(map(watered, 0, 360, 0, 255), map(85, 0, 100, 0, 255), map(72, 0, 100, 0, 255));
image(sprite, 0, 0);
noTint();
}
void wilt(){
//WHY DO THESE CAUSE THE FRAMERATE TO DROP?
float r = 0.07;
watered = constrain(watered - r, 50, 125);
}
}
class Cloud{
//global variables
int x;
float y;
int l;
boolean strike;
int direction;
float full;
int lightningHeight;
float keyMillis;
float currentMillis;
float tintLevel;
float angle;
float speed;
float radius;
int n;
Cloud(){
x = 10;
y = 20;
l = 0;
direction = 1;
full = 5;
strike = false;
lightningHeight = 100;
keyMillis = 0;
currentMillis = 0;
tintLevel = 0;
angle = 0.0;
speed = 0.03;
radius = 15;
n = 0;
}
void move(){
x += 1*direction;
angle += speed;
float sinval = sin(angle);
float yoffset = sinval * radius;
y = 25 + yoffset;
full = constrain(full + 0.015, 0, 18);
if(x > 450 || x < 0){
direction *= -1;
}
}
void rain(){
if(n == 50){
n = 0;
}
for(int i = n; i < n + 5; i++){
rainDrop[i].x = this.x + int(random(30, 117));
rainDrop[i].y = this.y + int(random(28, 79));
rainDrop[i].falling = true;
rainDrop[i].visible = true;
}
n += 5;
}
void lightning(){
if(full > 10){
strike = true;
if(lightningHeight < 300){
image(lightning[l], x+35, y+40, 100, lightningHeight);
lightningHeight += 20;
}
else {
//if l is the last in the array, l = 0
if(l == 4){
l = 0;
}
//else l++
else {
l++;
}
lightningHeight = 100;
}
}
else {
strike = false;
}
}
void display(){
currentMillis = millis();
//controls the tint of the cloud shape
if(full > 6){
tintLevel = 255 - constrain(map(full, 6, 12, 0, 109), 0, 109);
tint(tintLevel);
image(cloudShape, x, y);
noTint();
}
else {
image(cloudShape, x, y);
}
//controls the tint of the cloud's shadow
if(full > 6){
tint(tintLevel - 30);
image(cloudShadow, x, y);
noTint();
}
else {
tint(map(214, 0, 360, 0, 255), map(27, 0, 100, 0, 255), 255);
image(cloudShadow, x, y);
noTint();
}
//if / else sequence for facial animation
if(currentMillis - keyMillis < 3000){
//have the if full == n check here before each image drawn
if(full < 4){
image(tiredFace, x, y);
}
else if (full > 4 && full < 8){
image(happyFace, x, y);
}
else if (full > 8 && full < 10){
image(sadFace, x, y);
}
else if (full > 10){
image(thunderFace, x, y);
}
}
else if(currentMillis - keyMillis < 3200){
if(full < 4){
image(tiredFace_blink, x, y);
}
else if (full > 4 && full < 8){
image(happyFace_blink, x, y);
}
else if (full > 8 && full < 10){
image(sadFace_blink, x, y);
}
else if (full > 10){
image(thunderFace_blink, x, y);
}
}
else if(currentMillis - keyMillis < 3300){
if(full < 4){
image(tiredFace, x, y);
}
else if (full > 4 && full < 8){
image(happyFace, x, y);
}
else if (full > 8 && full < 10){
image(sadFace, x, y);
}
else if (full > 10){
image(thunderFace, x, y);
}
}
else if(currentMillis - keyMillis < 3500){
if(full < 4){
image(tiredFace_blink, x, y);
}
else if (full > 4 && full < 8){
image(happyFace_blink, x, y);
}
else if (full > 8 && full < 10){
image(sadFace_blink, x, y);
}
else if (full > 10){
image(thunderFace_blink, x, y);
}
}
else {
if(full < 4){
image(tiredFace_blink, x, y);
}
else if (full > 4 && full < 8){
image(happyFace_blink, x, y);
}
else if (full > 8 && full < 10){
image(sadFace_blink, x, y);
}
else if (full > 10){
image(thunderFace_blink, x, y);
}
keyMillis = currentMillis;
}
}
}
class Rain{
int x;
float y;
boolean visible;
PImage sprite;
boolean falling;
Rain(){
x = 0;
y = -100;
visible = false;
sprite = rainDropSprite;
falling = false;
}
void fall(){
if(falling == true){
y += 1.75;
if(y > 350){
falling = false;
y = -100;
}
}
}
void display(){
if(visible == true){
image(sprite, x, y);
}
}
}
//user input makes clyde rain
void keyPressed(){
if(key == 32){
if(clyde.full > 2){
clyde.rain();
clyde.full -= 2;
}
}
}
//user input makes clyde rain
void mousePressed(){
if(clyde.full > 2){
clyde.rain();
clyde.full -= 2;
}
}
Gregory - Project 2
on Monday, Nov 17, 2008 – 6:01 am