Last active
August 29, 2015 14:01
-
-
Save nataliefreed/8f407469bf65b08adbbb to your computer and use it in GitHub Desktop.
Stop Motion Creature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SquareCreature squareling; | |
float tblink, tbounce = 0; | |
int numSleepyImgs = 4; | |
PImage happyImg, blinkImg, angryImg, sleepyImg; | |
PImage[] sleepyImgs = new PImage[numSleepyImgs]; | |
void setup() | |
{ | |
imageMode(CENTER); | |
size(500, 500); | |
squareling = new SquareCreature(width/2, height/2, 150, 100, "happy"); | |
happyImg = loadImage("happy.png"); | |
blinkImg = loadImage("blinkhappy.png"); | |
angryImg = loadImage("angry.png"); | |
for (int i=0;i<sleepyImgs.length;i++) | |
{ | |
sleepyImgs[i] = loadImage("sleepy" + i + ".png"); | |
} | |
} | |
void draw() | |
{ | |
background(255); | |
squareling.update(); | |
squareling.display(); | |
} | |
class SquareCreature | |
{ | |
PVector center; | |
float w, h; | |
String mood; | |
int bounceOffset = 0; | |
float sleepyIndex; | |
float sleepyRate = 0.05; | |
float sleepyPerFrame = sleepyRate; | |
SquareCreature(int x, int y, float tempW, float tempH, String tempMood) | |
{ | |
center = new PVector(x, y); | |
mood = tempMood; | |
w = tempW; | |
h = tempH; | |
sleepyIndex = 0; | |
} | |
void update() | |
{ | |
if (mood == "happy") | |
{ | |
bounceHappily(); | |
} | |
} | |
void display() | |
{ | |
if (mood == "happy") | |
{ | |
if (shouldBlink()) | |
{ | |
image(blinkImg, center.x, center.y-bounceOffset); | |
} | |
else | |
{ | |
image(happyImg, center.x, center.y-bounceOffset); | |
} | |
} | |
else if (mood == "tired") | |
{ | |
image(sleepyImgs[int(sleepyIndex)], center.x, center.y); | |
sleepyIndex += sleepyPerFrame; | |
if (sleepyIndex > (numSleepyImgs-sleepyRate)) | |
{ | |
sleepyPerFrame = sleepyRate*-1.0; | |
} | |
else if (sleepyIndex < 0) | |
{ | |
sleepyPerFrame = sleepyRate; | |
} | |
} | |
else if (mood == "angry") | |
{ | |
image(angryImg, center.x+random(-2, 2), center.y+random(-1, 1)); | |
} | |
} | |
boolean shouldBlink() | |
{ | |
// return true; | |
boolean should = (noise(tblink) > 0.65); | |
tblink += 0.04; | |
return should; | |
} | |
void bounceHappily() | |
{ | |
boolean should = (noise(tbounce) > 0.3); | |
tbounce += 0.08; | |
if (should) | |
{ | |
bounceOffset = round(noise(tbounce)*30.0); | |
} | |
else | |
{ | |
bounceOffset = 0; | |
} | |
} | |
} | |
void mousePressed() | |
{ | |
if (squareling.mood == "happy") | |
{ | |
squareling.mood = "angry"; | |
} | |
else if (squareling.mood == "angry") | |
{ | |
squareling.mood = "tired"; | |
} | |
else if (squareling.mood == "tired") | |
{ | |
squareling.mood = "happy"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment