I have multiple classes where the code is the exact same except the controls. The colors of each tank are green, red, blue, and yellow, but I only pasted the code of the green tank since the code is the same for all the classes. Is there any way to make these all into one class?
[Disallowed URL]
Tank-
-Turret
Health-
import greenfoot.*;
public class GreenC extends Tank
{
private double movement;
private double velocity;
private int turning;
private static int health;
public GreenC()
{
movement = 0;
velocity = 0.96;
turning = 3;
health = 2;
}
public void act()
{
if ( isTouching( Shell.class ) )
{
gotHit();
removeTouching( Shell.class );
}
if ( health > 0 )
{
movement *= velocity;
if ( Greenfoot.isKeyDown( "w" ) )
{
movement += 0.11;
}
if ( Greenfoot.isKeyDown( "s" ) )
{
movement -= 0.08;
}
if ( Greenfoot.isKeyDown( "d" ) )
{
turn( turning );
if ( isAtEdge() || isTouching( WallV.class ) || isTouching( WallH.class ) || isTouching( Tank.class ) )
{
turn( -turning );
}
}
if ( Greenfoot.isKeyDown( "a" ) )
{
turn( -turning );
if ( isAtEdge() || isTouching( WallV.class ) || isTouching( WallH.class ) || isTouching( Tank.class ) )
{
turn( turning );
}
}
if ( isAtEdge() || isTouching( WallV.class ) || isTouching( WallH.class ) || isTouching( Tank.class ) )
{
movement *= -1;
}
move( movement );
}
}
public void gotHit()
{
health--;
}
public int getHealth()
{
return health;
}
}import greenfoot.*;
public class GreenT extends Turret
{
private GreenC chasisG;
private int rotation;
private int reload;
public GreenT( GreenC chasisG )
{
this.chasisG = chasisG;
}
public void act()
{
setLocation( chasisG.getX(), chasisG.getY() );
setRotation( chasisG.getRotation() + rotation );
if ( chasisG.getHealth() > 0 )
{
if ( Greenfoot.isKeyDown( "q" ) )
{
rotation -= 2;
}
if ( Greenfoot.isKeyDown( "e" ) )
{
rotation += 2;
}
if ( reload > 0 )
{
reload--;
}
if ( reload <= 0 )
{
if ( Greenfoot.isKeyDown( "c" ) )
{
Shell shellG = new Shell();
shellG.setImage( "GreenS.png" );
shellG.setRotation( getRotation() );
getWorld().addObject( shellG, getX(), getY() );
shellG.move( 10 );
reload = 200;
}
}
}
}
} import greenfoot.*;
public class GreenH extends Health
{
private GreenC chasisG;
public GreenH( GreenC chasisG )
{
this.chasisG = chasisG;
}
public void act()
{
setLocation( chasisG.getX(), chasisG.getY() - 70 );
if ( chasisG.getHealth() == 1 )
{
setImage( "balloon2.png" );
}
if ( chasisG.getHealth() == 0 )
{
setImage( "balloon1.png" );
}
}
}

