PImage start;
PImage die;
PImage desert;
PImage frogdie;
int page = 1;
float backgroundX = 0;
float speed = 0.28;
Frog f1;
Cactus c1;
Cactus c2;
Cactus c3;
Cactus c4;
Bird b1;
Bird b2;
Bird b3;
Bird b4;
void setup () {
size(600, 400);
start = loadImage("start.png");
desert = loadImage("desert.png");
frogdie = loadImage("frog.png");
die = loadImage("die.png");
smooth();
noStroke();
f1 = new Frog("frog.png");
c1 = new Cactus(200, 337, .65, "cactus1.png"); //space images out
c2 = new Cactus(600, 270, .60, "cactus2.png");
c3 = new Cactus(1000, 290, .65, "cactus3.png");
c4 = new Cactus(1400, 315, .65, "cactus4.png");
b1 = new Bird(400, 20.5, .50, "bird1.png");
b2 = new Bird(800, 15.5, .50, "bird2.png");
b3 = new Bird(1200, 15.5, .50, "bird3.png");
b4 = new Bird(1600, 20.5, .50, "bird4.png");
}
void draw() {
if (page == 1) {
image (start, 0, 0);
backgroundX = 0;
speed = .28;
c1.speed = .50;
c2.speed = .50;
c3.speed = .50;
c4.speed = .50;
}
else if (page == 2)
{
backgroundX = backgroundX - speed;
backgroundX = constrain(backgroundX, width - desert.width, 0);
image(desert, backgroundX, 0);
f1.display();
f1.move();
c1.display();
c1.move();
c2.display();
c2.move();
c3.display();
c3.move();
c4.display();
c4.move();
b1.display();
b1.move();
b2.display();
b2.move();
b3.display();
b3.move();
b4.display();
b4.move();
//If Collision
if ((f1.x + f1.w) > c1.x && f1.x < (c1.x + c1.w) && (f1.y + f1.h) > c1.y && f1.y < c1.y + c1.h) {
page = 3;
}
if ((f1.x + f1.w) > c2.x && f1.x < (c2.x + c2.w) && (f1.y + f1.h) > c2.y && f1.y < c2.y + c2.h) {
page = 3;
}
if ((f1.x + f1.w) > c3.x && f1.x < (c3.x + c2.w) && (f1.y + f1.h) > c3.y && f1.y < c3.y + c3.h) {
page = 3;
}
if ((f1.x + f1.w) > c4.x && f1.x < (c4.x + c4.w) && (f1.y + f1.h) > c4.y && f1.y < c4.y + c4.h) {
page = 3;
}
}
else if (page == 3) {
image(die, 0, 0);
}
if ((f1.x + f1.w) > b2.x && f1.x < (b2.x + b2.w) && (f1.y + f1.h) > b2.y && f1.y < b2.y + b2.h) {
page = 3;
}
if ((f1.x + f1.w) > b3.x && f1.x < (b3.x + b3.w) && (f1.y + f1.h) > b3.y && f1.y < b3.y + b3.h) {
page = 3;
}
}
void keyPressed() {
if (key == ' ') {
if (page == 1) {
page = 2;
}
else if (page == 3) {
page = 1;
f1.restart();
c1.restart(200.0, 337.0);
c2.restart(600.0, 270.0);
c3.restart(1000.0, 290.0);
c4.restart(1400.0, 315.0);
b1.restart(400.0, 20.5);
b2.restart(800.0, 15.5);
b3.restart(1200.0, 15.5);
b4.restart(1600.0, 20.5);
}
}
}
class Bird { //Declare fields of class
float x;
float y;
float h;
float w;
float speed;
PImage thisBird;
Bird(float inx, float iny, float inspeed, String bird) { //constructor
x = inx;
y = iny;
speed = inspeed;
thisBird = loadImage(bird);
w = thisBird.width;
h = thisBird.height;
}
void display() {
image(thisBird, x, y);
}
void move() {
x = x - speed;
if (x < -w) {
x = width;
speed = speed + .03;
}
}
void restart(float inx, float iny) {
x = inx;
y = iny;
}
}
class Cactus { //Declare fields of class
float x;
float y;
float h;
float w;
float speed;
PImage thisCactus;
Cactus(float inx, float iny, float inspeed, String cactus) { //constructor
x = inx;
y = iny;
speed = inspeed;
thisCactus = loadImage(cactus);
w = thisCactus.width;
h = thisCactus.height;
}
void display() {
//rect(x, y, thisCactus.width, thisCactus.height);
image(thisCactus, x, y);
}
void move() {
x = x - speed;
if (x < -w) {
x = width;
speed = speed + .09;
}
}
void restart(float inx, float iny) {
x = inx;
y = iny;
}
}
class Frog {
float x = 50;
float y = 300;
float w;
float h;
float velocityX = 0;
float velocityY = 0.0;
float acceleration = 0.0;
PImage frog;
Frog(String frogin) {
frog = loadImage(frogin);
w = frog.width / 4 ;
h = frog.height / 4;
}
void restart() {
x = 50;
y = 300;
velocityX = 0;
velocityY = 0.0;
acceleration = 0.0;
}
void display() {
image(frog, x, y, w, h);
}
void move() {
if ((keyPressed == true) && (key == ' ') && (y > 100)) {
velocityY = -4.5;
acceleration = 1;
}
x = x + velocityX;
x = constrain(x, 0, 598);
y = y + velocityY;
y = constrain(y, 0, 300);
velocityY = velocityY + acceleration;
//println(velocityY + " " + acceleration);
}
}
Beth - Project 2
on Tuesday, Nov 18, 2008 – 10:16 pm
2 Comments
//the program seems to be running much slower on the web than it does in processing.
This has evolved well. I think the birds are visually too different from the other drawings. And if they are “villians”, they should look more menacing.