String[] littleCars = {"camioncito.png","camioncito2.png","carrito.png","carrito2.png","carrito3.png"};
int minspace = 40;
int maxspace = 120;
int minvel = 1;
int maxvel = 5;
int carsSize = 40;
Sapo littleSapo;
int i,j,k;
Car[] mycars1;
Car[] mycars2;
Car[] mycars3;
int[] newVel = new int[3];
int[] newX = new int[3];
void setup() {
size(400,200);
mycars1 = new Car[6];
mycars2 = new Car[6];
mycars3 = new Car[6];
cars(mycars1, 75, true);
cars(mycars2, 105, false);
cars(mycars3, 135, true);
littleSapo = new Sapo(width/2, 165, 20, "sapito.png");
}
void draw() {
j = 60;
background(100);
littleSapo.loadImg();
house();
while(j<height-40){
i = 0;
while(i<width){
stroke(255,255,255);
line(i, j,i+5,j);
i= i+10;
}
j=j+30;
}
frameRate(30);
int l = 0;
while(l<mycars1.length){
mycars1[l].move();
mycars2[l].back();
mycars3[l].move();
if(littleSapo.hasty(mycars1[l])||littleSapo.hasty(mycars2[l])||littleSapo.hasty(mycars3[l])){
littleSapo.setImage("sapitomuerto.png");
littleSapo.loadImg();
}
if(mycars1[l].outScreen){
if(l==0){
if (mycars1[l].directionRight){
newVel[0] = int (random(minvel,maxvel));
newX[0] = 0;
}else{
newVel[0] = int (random(-maxvel,-minvel));
newX[0] = width;
}
}
change(mycars1[l], 0);
}
if(mycars2[l].outScreen){
if(l==0){
if (mycars2[l].directionRight){
newVel[1] = int (random(minvel,maxvel));
newX[1] = 0;
}else{
newVel[1] = int (random(-maxvel,-minvel));
newX[1] = width;
}
}
change(mycars2[l], 1);
}
if(mycars3[l].outScreen){
if(l==0){
if (mycars3[l].directionRight){
newVel[2] = int (random(minvel,maxvel));
newX[2] = 0;
}else{
newVel[2] = int (random(-maxvel,-minvel));
newX[2] = width;
}
}
change(mycars3[l], 2);
}
l++;
}
}
void cars(Car[] autos, int y, boolean directionRight){
k=0;
int vel = 0;
int space1 = 0;
int space2 = 0;
int nextX = 0;
if (directionRight){
vel = int (random(minvel,maxvel));
space1 = maxspace*-1;
space2 = minspace*-1;
nextX = 0;
}else{
vel = int (random(-maxvel,-minvel));
space1 = minspace;
space2 = maxspace;
nextX = width;
}
int ima = int(random(0,littleCars.length));
while(k<autos.length){
autos[k]=new Car(nextX,y,vel,carsSize,littleCars[ima],directionRight);
nextX = nextX + int(random (space1, space2));
ima = int(random(0,littleCars.length));
k++;
}
}
void house(){
PImage pi = loadImage("casita.png");
imageMode(CENTER);
image(pi,22,25);
}
void change(Car car, int place){
if (car.directionRight){
newX[place] = newX[place] + int(random (maxspace*-1, minspace*-1));
}else{
newX[place] = newX[place] + int(random (minspace, maxspace));
}
car.changed(newX[place],newVel[place],littleCars[int(random(0,littleCars.length))]);
car.outScreen = false;
}
void keyPressed( ){
if(!littleSapo.died){
if(key=='w'||key=='W'){
if(littleSapo.getPosy() > 0){
littleSapo.setPosy(littleSapo.getPosy() - 30);
}
}else if(key=='s'||key=='S'){
if(littleSapo.getPosy() < height){
littleSapo.setPosy(littleSapo.getPosy() + 30);
}
}else if(key=='a'||key=='A'){
if(littleSapo.getPosx() > 0){
littleSapo.setPosx(littleSapo.getPosx() - 30);
}
}else if(key=='d'||key=='D'){
if(littleSapo.getPosx() < width){
littleSapo.setPosx(littleSapo.getPosx() + 30);
}
}
}
}
void mousePressed(){
setup();
}
class Car
{
private int posx;
private int posy;
private int velocity;
private int carSize;
private String image;
boolean directionRight;
boolean outScreen=false;
Car(int px, int py, int ve, int ta, String im, boolean adel) {
posx = px;
posy = py;
velocity = ve;
carSize = ta;
image = im;
directionRight = adel;
}
void setPosx(int x){posx=x;}
void setPosy(int y){posy=y;}
int getPosx(){return posx;}
int getPosy(){return posy;}
int getSize(){return carSize;}
void move () {
PImage b = loadImage(image);
imageMode(CENTER);
image(b,posx,posy,carSize,carSize/2);
posx = posx + velocity;
if (posx > width+carSize) {
posx = -carSize;
outScreen = true;
}
}
void back (){
PImage b = loadImage(image);
imageMode(CENTER);
pushMatrix();
scale(-1,1);
image(b,-posx,posy,carSize,carSize/2);
popMatrix();
posx = posx + velocity;
if (posx < 0-carSize) {
posx = width+carSize;
outScreen = true;
}
}
void changed(int x, int v, String i){
posx = x;
velocity = v;
image = i;
}
}
class Sapo{
private int posx;
private int posy;
private int carSize;
private String image;
boolean died = false;
Sapo(int px, int py, int ta, String im) {
posx = px;
posy = py;
carSize = ta;
image = im;
}
void loadImg(){
PImage b = loadImage(image);
imageMode(CENTER);
image(b,posx,posy,carSize,carSize/2);
}
void setPosx(int x){posx=x;}
void setPosy(int y){posy=y;}
int getPosx(){return posx;}
int getPosy(){return posy;}
void setImage(String im){image = im;}
boolean hasty(Car auto){
int CmaxX = auto.getPosx()+(auto.getSize()/2);
int CmaxY = auto.getPosy()+(auto.getSize()/2);
int CminX = auto.getPosx()-(auto.getSize()/2);
int CminY = auto.getPosy()-(auto.getSize()/2);
int FmaxX = posx+(carSize/2);
int FmaxY = posy+(carSize/2);
int FminX = posx-(carSize/2);
int FminY = posy-(carSize/2);
if(FmaxX<CmaxX && FmaxX >CminX){
if(FmaxY<CmaxY && FmaxY >CminY){
died = true;
}
}
if(FminX < CmaxX && FminX >CminX){
if(FminY < CmaxY && FminY > CminY){
died = true;
}
}
return died;
}
}
Páginas: 1 2
Jajaja yo me acuerdo de ese juego, te quedo muy bien, gracias por compartir.
Y por la fisica ponlo a saltar 😉
jajaja no seas malo, el salto se hace solo con animaciones, no necesito un engine de fisica para hacer eso, es como cazar atun con arpon XD