//////////////////////////////////////////////// class Universe { private ArrayList fallingStuff; private int maxElements; private float chance; private int speed; private Player player; // 2 private int stuffCollected; // 3 private int stuffMissed; // 3 public Universe() { this.fallingStuff = new ArrayList(); this.maxElements = 10; this.chance = 80; this.speed = 1; this.stuffCollected = 0; // 3 this.stuffMissed = 0; // 3 this.player = new Player(); // 2 } public void provideFallingThings() { float probability = random(1, 10000); if(probability <= this.chance && this.fallingStuff.size() < this.maxElements) { int x = (int)random(0, width); int y = 0; int width = 50; FallingThing fallingThing = new FallingThing(x, y, width, this.speed); // Corregir por fuera de ventana this.fallingStuff.add(fallingThing); } } public void letThingsFall() { for(int i=0; i public void checkForStuffCollected() { for(int i=0; i public void draw() { for(int i=0; i textAlign(CENTER, CENTER); fill(223, 223, 223); textSize(60); text("Collected " + this.stuffCollected + "; Missed " + this.stuffMissed, 50, 100, width - 100, 100); textSize(45); text("http://jorgeivanmeza.com/", 50, 200, width - 100, 100); // } // public void handleKeyboardEvent() { if (key == CODED) { if (keyCode == LEFT) { player.goLeft(); } if (keyCode == RIGHT) { player.goRight(); } } } // } //////////////////////////////////////////////// abstract class MovingThing { protected int x; protected int y; protected int _width; protected int _height; protected int step; // public int getX() { return this.x; } public int getY() { return this.y; } public int getWidth() { return this._width; } public int getHeight() { return this._height; } public boolean checkCollision(MovingThing anotherThing) { int x1 = anotherThing.getX(); int x2 = anotherThing.getX() + anotherThing.getWidth(); int y1 = anotherThing.getY(); int y2 = anotherThing.getY() + anotherThing.getHeight(); if(x2 < this.getX()) return false; if(x1 > this.getX() + this.getWidth()) return false; if(y2 < this.getY()) return false; if(y1 > this.getY() + this.getHeight()) return false; return true; } // } //////////////////////////////////////////////// // class Player extends MovingThing { public Player() { this(100, 20, 10); } public Player(int _width, int _height, int step) { this.x = width / 2; this.y = height - _height; this._width = _width; this._height = _height; this.step = step; } public void draw() { stroke(3); rectMode(CORNER); fill(100, 195, 215); // 3 rect(this.x - this._width/2, this.y - this._height/2, this._width, this._height); } public void goLeft() { this.x = constrain(this.x - this.step, 0, width); } public void goRight() { this.x = constrain(this.x + this.step, 0, width); } } // //////////////////////////////////////////////// class FallingThing extends MovingThing { protected int speed; protected int next; public FallingThing(int x, int y, int width, int speed) { this.x = x; this.y = y; this._width = width; this._height = width; this.speed = speed; this.next = millis(); this.step = 1; } public void draw() { stroke(1); rectMode(CORNER); fill(250, 80, 70); // 3 rect(this.x - this._width/2, this.y - this._height/2, this._width, this._height); } public void fall() { if(millis() >= this.next) { this.y = this.y + this.step; int t = (int)(this.step / (this.speed / 30.0)); this.next = this.next + t; } } public boolean isOnGround() { if(this.y >= height) return true; return false; } } //////////////////////////////////////////////// color backgroundColor = color(255, 255, 255); Universe universe; //////////////////////////////////////////////// void setup() { size(800, 600); background(backgroundColor); frameRate(30); universe = new Universe(); } void draw() { background(backgroundColor); universe.provideFallingThings(); universe.letThingsFall(); universe.draw(); universe.checkForStuffCollected(); // 3 universe.checkForFallenStuff(); } // void keyPressed() { universe.handleKeyboardEvent(); } // ////////////////////////////////////////////////