Created
April 14, 2015 20:41
-
-
Save ivanseidel/94dece685b5dac0cbba2 to your computer and use it in GitHub Desktop.
Processing Game: Avoid the Robot!
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
class Robo{ | |
Vetor position; | |
Vetor direction; | |
Robo(double x, double y){ | |
position = new Vetor(x, y); | |
direction = fromTheta(Math.PI/4); | |
} | |
public void mover(double passos){ | |
position = position.soma(direction.multiplicar(passos)); | |
} | |
public void girar(double angulo){ | |
direction = direction.rotate(Math.toRadians(angulo)); | |
} | |
public void draw(){ | |
fill(0, 255, 0); | |
ellipse((float)position.x, (float)position.y, 20, 20); | |
double pX = position.x; | |
double pY = position.y; | |
double dX = direction.x; | |
double dY = direction.y; | |
line( (float)pX, (float)pY, | |
(float)(pX + dX*20), (float)(pY + dY*20)); | |
} | |
} | |
Robo robos[] = new Robo[20]; | |
void setup(){ | |
size(700, 300); | |
for(int i = 0; i < 20; i++){ | |
robos[i] = new Robo(i*30, 150); | |
} | |
} | |
int vidas = 10; | |
float pontos = 0; | |
void draw(){ | |
background(255); | |
if(vidas <= 0){ | |
textSize(42); | |
fill(255, 40, 30); | |
textAlign(CENTER); | |
text("VOCE PERDEU! HAHAHA", width/2, height/2); | |
text("Pontos: "+Math.round(pontos), width/2, height/2 + 40); | |
textSize(14); | |
textAlign(LEFT); | |
text("Clique na tela para reiniciar", 20, height - 40); | |
return; | |
} | |
// Atualiza Robo | |
Vetor dir = robos[0].position.soma(-mouseX, -mouseY); | |
robos[0].direction = fromTheta(Math.atan2(dir.y, dir.x)).multiplicar(-1); | |
robos[0].mover(pontos/20.0); | |
robos[0].draw(); | |
// Checa se PEGOU o Mouse... | |
if(Math.sqrt(dir.x * dir.x + dir.y*dir.y) < 10){ | |
print("PEGOU!"); | |
vidas--; | |
// Reinicia em lugar aleatorio da tela | |
Vetor novaP = fromTheta(Math.random() * Math.PI * 2).multiplicar(300); | |
novaP.soma(width/2, height/2); | |
robos[0].position = novaP; | |
} | |
// Renderiza UI | |
textAlign(LEFT); | |
textSize(22); | |
fill(0, 102, 153); | |
text("Vidas: "+vidas, 10, 40); | |
text("Pontos: "+Math.round(pontos), 10, 70); | |
// Adiciona pontos! | |
pontos += 0.1; | |
} | |
public void mouseClicked(){ | |
if(vidas <= 0){ | |
vidas = 10; | |
pontos = 0; | |
} | |
} | |
public class Vetor { | |
public double x = 0; | |
public double y = 0; | |
Vetor(double x, double y){ | |
this.x = x; | |
this.y = y; | |
} | |
Vetor(){} | |
public Vetor soma(double x2, double y2){ | |
return new Vetor(x + x2, y + y2); | |
} | |
public Vetor soma(Vetor v){ | |
return new Vetor(x + v.x, y + v.y); | |
} | |
public Vetor multiplicar(double e){ | |
return new Vetor(x * e, y * e); | |
} | |
public Vetor dividir(double e){ | |
if(e == 0) | |
return new Vetor(); | |
return new Vetor(x / e, y / e); | |
} | |
// Creates a new vector from a Theta value (Radians is default) | |
public Vetor rotate(double theta){ | |
double currentTheta = Math.atan2(this.y, this.x); | |
double currentNorm = Math.sqrt(x*x + y*y); | |
// Creates vector with new Angle | |
Vetor finalVector = fromTheta(currentTheta + theta); | |
// Un-normalize vector | |
return finalVector.multiplicar(currentNorm); | |
} | |
public void imprimir(){ | |
System.out.printf("[%.2f,%.2f]",x,y); | |
} | |
} | |
Vetor fromTheta(double theta){ | |
double x = Math.cos(theta); | |
double y = Math.sin(theta); | |
return new Vetor(x, y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment