// Define sketch size parameters.
final int WIDTH = 200;
final int HEIGHT = WIDTH;
final int MIDDLE = WIDTH / 2;
// Define symbolic color constants.
final color BLACK = color(0);
final color RED = color(255, 0, 0);
final color GREEN = color(0, 255, 0);
final color BLUE = color(0, 0, 255);
final color YELLOW = color(255,247,3);
// Define car dimensions.
final int CAR_WIDTH = 30;
final int CAR_HEIGHT = 10;
int frogx = 165;
int frogy = 170;
PImage frogimg;
// Declare the cars.
Car car1;
Car car2;
Car car3;
// Setup the initial conditions.
// Create the cars.
void setup()
{
size(WIDTH, HEIGHT);
rectMode(CENTER);
car1 = new Car(RED, 0, 100, CAR_WIDTH, CAR_HEIGHT, 2);
car2 = new Car(GREEN, 50, 50, CAR_WIDTH, CAR_HEIGHT, 1);
car3 = new Car(BLUE, 150, 150, CAR_WIDTH, CAR_HEIGHT, 2);
frogimg = loadImage("frog.gif");
}
// Draw the road, and the cars.
void draw()
{
background(BLACK);
strokeWeight(4);
stroke(255,247,3);
line(200,20,0,20);
line(200,180,0,180);
//////////////////////
//fill(0);
for(int i = 14; i<width; i+=14) {
noStroke();
fill(190);
rect (i,height/2.6,7,4);
}
///////////////////////
for(int i = 14; i<width; i+=14) {
noStroke();
fill(190);
rect (i,height/1.6,7,4);
}
//////////////////////
car1.draw();
car1.move();
car2.draw();
car2.move();
car3.draw();
car3.move();
/////////////////////////
image(frogimg, frogx, frogy, 32,32);
if (frogx < 2){
frogx+=2;
}
if (frogx > 160){
frogx-=2;
}
if(frogy > 170){
frogy-=2;
}
if ((frogy - 16) > this.y && (frogy - 16) < this.y && (frogx + 16) > this.x && (frogx + 16) < this.y);
}
void keyPressed() {
if (keyCode == 39) {
frogx = frogx + 20; //right arrow
}
if (keyCode == 37) {
frogx = frogx - 20; //left arrow
}
if (keyCode == 38) {
frogy = frogy - 20; //up arrow
}
if (keyCode == 40) {
frogy = frogy + 20; //down arrow
}
}
/////////////////////////////////////////////
class Car
{
color col;
int x, y, w, h;
int speed;
/**
* Constructor for cars.
*
* @param c The color of the car.
* @param x The x location of the center of the car.
* @param y The y location of the center of the car.
* @param w The width of the car.
* @param h The height of the car.
* @param s The speed of the car in pixels moved per frame.
*/
Car(color c, int x, int y, int w, int h, int s)
{
this.col = c;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.speed = s;
}
// Draw the car on the sketch.
void draw()
{
noStroke();
fill(this.col);
rect(this.x, this.y, this.w, this.h);
}
// Move the car.
// Car location wraps when it reaches the right edge.
void move()
{
this.x += this.speed;
if (this.x > width) {
this.x = 0;
}
}
}
