There's the code of the car class:
Image
As you can see, in some road junctions we don't turn to left or right but to ahead.
import greenfoot.*;
import java.util.List;
/**
* Write a description of class Carro here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Carro extends Actor
{
//Declaração de variáveis
private int dirX, dirY; // sentido do carro para X e Y
public char Key;//
private int direction;
public boolean carRemoved = false;
private char Car;
GreenfootImage img = new GreenfootImage("explosao.png");
public static Actor controlledCar;
public int nextDirection = 0;
public int pontuação = 30;
/**
* Act - do whatever the Carro wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move();
if(Greenfoot.mouseClicked(this))
{
controlledCar = this;
}
if(controlledCar == this)
{
int dir = 0;
if (Greenfoot.isKeyDown("a"))
{
dir++;
}
if (Greenfoot.isKeyDown("d"))
{
dir--;
}
if (dir != 0)
{
nextDirection = dir;
}
}
mostraScore();
colisaocurvas();
//selectCar();
//checkKey();
turnToHouse();
foraEstrada();
colisaoCasa();
mostraScore();
//listaCarrosPresentes = World.getObjects(Carro.class);
colisaoentroncamentos();
}
/**
Método para velociade automática no carro
*/
public void move()
{
move(-1);
}
/**
Determina intersecção entre os carros e as curvas
*/
public void colisaocurvas()
{
if(this.isTouching(Curva.class) && this.getX() == 185 && this.getY() == 399)
{
turn(-90);
}
else if(this.isTouching(Curva.class) && this.getX() == 41 && this.getY() == 542)
{
turn(90);
}
else
{
carRemoved = true;
}
}
/**
Verifica a tecla pressionada e guarda numa variávél;
*/
/**
Determina para onde o carro deve virar consoante a última tecla pressionada
*/
public void colisaoentroncamentos()
{
if(this.isTouching(Entroncamento.class) && this.getX() == 185 && this.getY() == 542)
{
turn(90*nextDirection);
}
else
{
carRemoved = true;
}
if(this.isTouching(Entroncamento.class) && this.getX() == 41 && this.getY() == 255)
{
turn(90*nextDirection);
}
else
{
carRemoved = true;
}
if(this.isTouching(Entroncamento.class) && this.getX() == 256 && this.getY() == 255)
{
turn(90*nextDirection);
}
else
{
carRemoved = true;
}
if(this.isTouching(Entroncamento.class) && this.getX() == 544 && this.getY() == 542)
{
turn(90*nextDirection);
}
else
{
carRemoved = true;
}
if(this.isTouching(Entroncamento.class) && this.getX() == 544 && this.getY() == 326)
{
turn(90*nextDirection);
}
else
{
carRemoved = true;
}
if(this.isTouching(Entroncamento.class) && this.getX() == 544 && this.getY() == 183)
{
turn(90*nextDirection);
}
else
{
carRemoved = true;
}
}
/**
Método que faz os carros virarem para a sua casa
*/
public void turnToHouse()
{
if(this.isTouching(Rua_sem_Saida.class) && this.getX() == 820 && this.getY() == 326)
{
turn(-90);
}
else if(this.isTouching(Rua_sem_Saida.class) && this.getX() == 741 && this.getY() == 542)
{
turn(-90);
}
else if(this.isTouching(Rua_sem_Saida.class) && this.getX() == 675 && this.getY() == 183)
{
turn(-90);
}
else if(this.isTouching(Rua_sem_Saida.class) && this.getX() == 473 && this.getY() == 183)
{
turn(90);
}
else
{
carRemoved = true;
}
}
/**
Remove o veículo que chegou à casa
*/
public void colisaoCasa()
{
if(this.isTouching(Casa.class) && this.getX() == 820 && this.getY() == 241)
{
getWorld().removeObject(this);
}
else if(this.isTouching(Casa.class) && this.getX() == 741 && this.getY() == 455)
{
getWorld().removeObject(this);
}
else if(this.isTouching(Casa.class) && this.getX() == 41 && this.getY() == 87)
{
getWorld().removeObject(this);
}
else if(this.isTouching(Casa.class) && this.getX() == 256 && this.getY() == 93)
{
getWorld().removeObject(this);
}
else if(this.isTouching(Casa.class) && this.getX() == 473 && this.getY() == 90)
{
getWorld().removeObject(this);
}
else if(this.isTouching(Casa.class) && this.getX() == 675 && this.getY() == 99)
{
getWorld().removeObject(this);
}
else
{
carRemoved = true;
}
}
/**
Mostra a pontuação do utilizador quando este coloca o veículo na casa correta
*/
public void mostraScore()
{
List<Pontuação> listaTexto = getWorld().getObjects(Pontuação.class);
Pontuação textoScore = listaTexto.get(0);
textoScore.escreve("" + pontuação);
}
}


