The code is supposed to display the victory text after shooting down three enemy tanks. When a tank is shot down, the variable enemyCount whose value is 3 is deducted by one. This variable is in the world class. If the value of enemyCount is equal to 0, it will add the object VictoryScreen(). Below are the codes for each class:
code of the world "test":
code of the tanks:
Code of the player's shot class:
Help is much appreciated!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class test here. * * @author (your name) * @version (a version number or a date) */ public class test extends World { private PlayerTank thePlayerTank; //Creates an accessible PlayerTank actor. private TitleScreen theTitle; private InstructionsScreen theRules; public VictoryScreen victory = new VictoryScreen(); /** * Constructor for objects of class test. * */ int enemyCount = 3; public test() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); thePlayerTank = new PlayerTank(); addObject(thePlayerTank, 500, 340); addObject(new AI1(), 100, 340); addObject(new AI2(), 500, 60); addObject(new AI3(), 100, 60); if (enemyCount == 0){ addObject(new VictoryScreen(), getWidth()/2, getHeight()/2); } } public void deduct(){ enemyCount = enemyCount - 1; } }
import greenfoot.*; public class AI1 extends Actor { public void act() { shoot(); moveAndTurn(); if (isTouching(Shot.class)){ test test = new test(); test.deduct(); } }//Declares all the methods to be used within the actor AI1. private int shotTimer = 0; //Creates a variable that will later on store the time it takes to reload the tank. public void shoot() { if (shotTimer > 0) { shotTimer = shotTimer - 1; }//After a shot is fired, the if-statement runs as a timer for when you can shoot again. else if (Greenfoot.getRandomNumber (1000)<100) { getWorld().addObject(new ShotAI1(getRotation()), getX(), getY()); shotTimer = 80; }//Randomly generates a shot and starts the timer. } public void moveAndTurn() { if (Greenfoot.getRandomNumber(1000)<100){ move (5); }//Randomly moves the tank. else { turn(5); }//If the tank is not moving forward, it is turning in place. } }
import greenfoot.*; public class Shot extends Actor { private PlayerTank myShip; private int direction, speed; //Declares variables to be used within the code. public Shot (int dir) { direction = dir; speed = 5; //Instanciates the variables direction and speed. } public Shot(PlayerTank myShip) { this.myShip = myShip; }//Transfers the value of local variable myShip to global variable myShip. public void act() { shooting(); } public void shooting (){ if (isAtEdge() != true) { setRotation (direction); move (speed); setLocation(getX(), getY()); int enemyCount = 3; //Sets the location of the shot to the same location as the tank and moves the shot forward in the same direction that the tank is facing. Actor AI1 = getOneIntersectingObject(AI1.class); Actor AI2 = getOneIntersectingObject(AI2.class); Actor AI3 = getOneIntersectingObject(AI3.class); //Checks whether the shot is intersecting with the AI Tanks. if (AI1 != null) { getWorld().removeObject(AI1); getWorld().removeObject(this); }//If the shot intersects the location of the first AI tank, then the shot and the AI tank disappear. if (AI2!= null) { getWorld().removeObject(AI2); getWorld().removeObject(this); }//If the shot intersects the location of the second AI tank, then the shot and the AI tank disappear. if (AI3 != null) { getWorld().removeObject(AI3); getWorld().removeObject(this); }//If the shot intersects the location of the third AI tank, then the shot and the AI tank disappear. /*if (enemyCount == 0){ World world = getWorld(); world.removeObjects(world.getObjects(null)); world.addObject(new VictoryScreen(this), world.getWidth()/2, world.getHeight()/2); }*/ }//If the location of the shot is not (x,0), then the if-statement will run. else{ getWorld().removeObject(this); } //If the location of the shot is at the edge of the world then the shot will disappear. } }