This site requires JavaScript, please enable it in your browser!
Greenfoot back
rkr
rkr wrote ...

2016/12/16

Bomb code

rkr rkr

2016/12/16

#
my bomb is not removing or dealing damage to my enemies that are in range for some reason i dont know why? So this is my bomb code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Bomb here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bomb extends Bullet
{
    public int damage = -5;
    private int t = 100;
    // private SimpleTimer timer = new SimpleTimer();
    public void act() 
    {
        if (t == 0){
            explode();
            getWorld().removeObject(this);
        }
    }
    // removes object within the radius of the bomb
    public void explode (){
        //boolean placedBomb = getWorld().getObjects(Hero.class).get(0).placeBomb();
        // if (placedBomb == true){
        // timer.mark();
        // }
        // if (timer.millisElapsed() == 302)
        //{
        //  getWorld().removeObject(this);
        //}
    }

    public int damage(){
        List players = getObjectsInRange(200, Bomb.class);
        if(!players.isEmpty()){
            return damage;
        }
        else return 0;
    }

    // public int getDamage(){
    // List players = getObjectsInRange(200, Bomb.class);
    // boolean placedBomb = getWorld().getObjects(Hero.class).get(0).placeBomb();
    // if (placedBomb == true){
    // timer.mark();
    // }
    // if(timer.millisElapsed() > 300 && !players.isEmpty()){
    // getWorld().getObjectsInRange(200, SmartGrunt.class).isEmpty());
    // getWorld().removeObjects(getObjectsInRange(200, Grunt.class));
    // return damage;
    // }
    // else
    // {
    // getWorld().removeObject(this);
    // return 0;

    // }
    // }
}

Hero code (press space to place):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Hero here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Hero extends Actor
{
    GreenfootSound shootSFX = new GreenfootSound("Tear.mp3");
    GreenfootSound backgroundMusic = new GreenfootSound("dipterasonata.mp3");  
    int moveRight = 5, moveLeft = 5, moveUp = 5, moveDown = 5;
    int numKey = 0;
    double health;
    private int wait = 0;
    private boolean placeBomb = false;
    String v = "null";
    boolean movingRight = false, movingLeft = false, movingUp = false, movingDown = false;
    double shootSpeed = 600; //can shoot every 600 miliseconds
    int damage = -1;
    GreenfootImage image1 = new GreenfootImage("hero1.png");
    GreenfootImage image2 = new GreenfootImage("hero2.png");
    GreenfootImage image3 = new GreenfootImage("hero3.png");
    GreenfootImage image4 = new GreenfootImage("hero4.png");
    GreenfootImage image5 = new GreenfootImage("hero5.png");
    GreenfootImage image6 = new GreenfootImage("hero6.png");
    GreenfootImage image7 = new GreenfootImage("hero7.png");
    GreenfootImage image8 = new GreenfootImage("hero8.png");
    GreenfootImage image9 = new GreenfootImage("hero9.png");
    private SimpleTimer timer = new SimpleTimer();

    /**
     * Act - do whatever the Hero wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() {
        removeDoor();
        headOnCollision();
        sideCollision();
        movement();
        pickDmgUp();
        shoot();
        pickKey();
        checkDeath();
        //backgroundMusic.playLoop();
        backgroundMusic.setVolume(50);
        bomb();
        checkBombStatus();

    }

    //basic movement
    public void movement() {
        //had to make these exceptions since otherwise the hero would get stuck at the walls.
        //basically if it comes into contact with a wall from a specific direction (let's say to the right), the movement to the right is set to 0
        //but you can still move left
        if (moveRight == 0) {
            moveLeft = 5;
        } else if (moveLeft == 0) {
            moveRight = 5;
        } else {
            moveLeft = moveRight;
        }

        //same here but vertically
        if (moveDown == 0) {
            moveUp = 5;
        } else if (moveUp == 0) {
            moveDown = 5;
        } else {
            moveDown = moveUp;
        }

        //basic movement
        if (Greenfoot.isKeyDown("w")) {
            setLocation(getX(), getY() - moveUp);
            animateUp();
            movingUp = true;
            movingDown = false;
            movingLeft = false;
            movingRight = false;
        } else if (Greenfoot.isKeyDown("s")) {
            setLocation(getX(), getY() + moveDown);
            animateDown();
            movingUp = false;
            movingDown = true;
            movingLeft = false;
            movingRight = false;
        } else if (Greenfoot.isKeyDown("d")) {
            setLocation(getX() + moveRight, getY());
            movingRight = true;
            movingLeft = false; 
            movingUp = false;
            movingDown = false;
            animateRight();
        } else if (Greenfoot.isKeyDown("a")) {
            setLocation(getX() - moveLeft, getY());
            movingLeft = true;
            movingRight = false;
            movingUp = false;
            movingDown = false;
            animateLeft();
        } else {
            setImage(image1);
            movingLeft = false;
            movingRight = false;
            movingUp = false;
            movingDown = false;
        }
    }

    public boolean movingDirection(int i) {
        if (i == 1) {
            return movingRight;
        } else if (i == 2){
            return movingLeft;
        } else if (i == 3) {
            return movingUp;
        } else if (i == 4) {
            return movingDown;
        } else {
            return false;
        }
    }

    //checks if the hero is colliding with specific parts of the wall and sets the speed of the hero accordingly (speed at 0 if colliding)
    public void sideCollision() {
        Obstacle wCheck = (Obstacle)getOneObjectAtOffset(getImage().getWidth()/2, 0, Obstacle.class);
        Obstacle wCheck2 = (Obstacle)getOneObjectAtOffset(getImage().getWidth()/2, -getImage().getHeight()/2, Obstacle.class);
        Obstacle wCheck3 = (Obstacle)getOneObjectAtOffset(getImage().getWidth()/2, getImage().getHeight()/4, Obstacle.class);
        Obstacle wCheck4 = (Obstacle)getOneObjectAtOffset(getImage().getWidth()/2, -getImage().getHeight()/4, Obstacle.class);

        Obstacle wCheck5 = (Obstacle)getOneObjectAtOffset(-getImage().getWidth()/2, 0, Obstacle.class);
        Obstacle wCheck6 = (Obstacle)getOneObjectAtOffset(-getImage().getWidth()/2, -getImage().getHeight()/2, Obstacle.class);
        Obstacle wCheck7 = (Obstacle)getOneObjectAtOffset(-getImage().getWidth()/2, getImage().getHeight()/4, Obstacle.class);
        Obstacle wCheck8 = (Obstacle)getOneObjectAtOffset(-getImage().getWidth()/2, -getImage().getHeight()/4, Obstacle.class);

        if (wCheck != null || wCheck2 != null || wCheck3 != null || wCheck4 != null) {
            moveRight = 0;
        } else {
            moveRight = 5;
        }

        if (wCheck5 != null || wCheck6 != null || wCheck7 != null || wCheck8 != null) {
            moveLeft = 0;
        } else {
            moveLeft = 5;
        }

    }

    //for use in bullet curve
    public int moveRightValue() {
        return moveRight;
    }
    //same here but with top down collisions
    public void headOnCollision() {
        Obstacle wCheck3 = (Obstacle)getOneObjectAtOffset(0, getImage().getHeight()/2, Obstacle.class);
        Obstacle wCheck4 = (Obstacle)getOneObjectAtOffset(0, -getImage().getHeight()/2, Obstacle.class);
        if (wCheck3 != null) {
            moveDown = 0;
        } else {
            moveDown = 5;
        }

        if (wCheck4 != null) {
            moveUp = 0;
        } else {
            moveUp = 5;
        }
    }

    public boolean check() {
        Actor d = getOneIntersectingObject(EnemyBullet.class);
        if (d!=null) {
            return true;
        }
        return false;
    }

    //various animations. Checks which key is pressed and what picture should be switched to
    private void animateDown() {
        if (getImage() == image2) {
            setImage(image3);
        } else {
            setImage(image2);
        }
    }

    private void animateLeft() {
        if (getImage() == image4) {
            setImage(image5);
        } else {
            setImage(image4);
        }
    }

    private void animateRight() {
        if (getImage() == image6) {
            setImage(image7);
        } else {
            setImage(image6);
        }
    }

    private void animateUp() {
        if (getImage() == image8) {
            setImage(image9);
        } else {
            setImage(image8);
        }
    }

    private void checkBombStatus(){
        if (wait >0){
            wait --;
        }
        if (wait == 0){
            placeBomb = false;
        }

    }
    // // takes the counter and decreases one every time the user uses the bomb
    public void bomb(){
        Bomb b = new Bomb();
        if(Greenfoot.isKeyDown("space") && !placeBomb){
            getWorld().addObject(b,getX(), getY());
            placeBomb = true;
            placeBomb();
        }
        else placeBomb = false;
    }

    public boolean placeBomb() {
        return placeBomb;
    }

    //Directional shooting with arrow keys spawns an instance of 'hero bullet' and rotates it according to which arrow key is pressed. Actual shooting happens in that class
    public void shoot() {
        HeroBullet h = new HeroBullet();
        if (timer.millisElapsed() > shootSpeed){
            if(Greenfoot.isKeyDown("right")){
                shootSFX.play();
                v = "right";
                getWorld().addObject(h, getX(), getY());
                timer.mark();
            }
            else if (Greenfoot.isKeyDown("left")){
                shootSFX.play();
                v = "left";
                getWorld().addObject(h, getX(), getY());
                h.setRotation(180);
                timer.mark();
            }
            else if(Greenfoot.isKeyDown("up")){
                shootSFX.play();
                v = "up";
                getWorld().addObject(h, getX(), getY());
                h.setRotation(270);
                timer.mark();
            }
            else if(Greenfoot.isKeyDown("down")){
                shootSFX.play();
                v = "down";
                getWorld().addObject(h, getX(), getY());
                h.setRotation(90);
                timer.mark();
            }
        }
    }

    public String returnV() {
        return v;
    }

    // if hero touches enemy's bullets. Health will be lost
    public boolean checkDeath() {
        Actor d = getOneIntersectingObject(Enemy.class);
        Actor f = getOneIntersectingObject(EnemyBullet.class);
        if (d!=null || f!=null ) {
            return true;
            //Greenfoot.stop();
        }
        else return false;
    }   

    // checks if hero comes in contact with the healthPowerUp
    public boolean checkHealthPowerUp(){
        Actor d = getOneIntersectingObject(HealthPowerUp.class);
        if(d!=null){
            return true;
        }
        else {
            return false;
        }
    }   

    //picks up key if in contact
    public void pickKey() {
        Actor d = getOneIntersectingObject(Key.class);
        if (d != null) {         
            numKey++;
        }
    }

    //sees if the actor is colliding with the door and if he has the required amount of keys, and returns true if both are true
    public boolean removeDoor(){
        Actor g = getOneIntersectingObject(LockedDoor.class);
        if (g != null && numKey > 0) { 
            return true;
        }
        return false;
    }

    //sets the the damage to -5 if hero picks up the powerup
    public void pickDmgUp(){
        Actor d = getOneIntersectingObject(DamageUp.class);
        if (d != null) {         
            damage = -5;
        }
    }

    //returns damage, used in enemies when substracting their health
    public int getDamage(){
        return damage;
    }

    //necessary to make removeDoor() work, forgive me
    public void reduceKey() {
        numKey--;
    }
}
here is my enemy code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Grunt here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Grunt extends Enemy
{
    private int health = 1;
    GreenfootSound shootSFX = new GreenfootSound("enemyshoot.mp3");
    /**
     * Act - do whatever the Grunt wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act(){
        moveRandomly();
        randomShoot();
        checkDeath();
    }    

    //moves the grunt randomly
    //http://www.greenfoot.org/topics/3205/15
    public void moveRandomly(){
        //countinously moving in the direction its facing at speed 5
        move(5); 
        //greenfoot continously pulls a random number. If the random number is less than 10 turns the grunt a random angle between 45 and -45 
        if (Greenfoot.getRandomNumber(100) < 10){ 
            turn(Greenfoot.getRandomNumber(90 + 1) - 45);
        }       

        //if colliding with any obstacle, turns a complete 180 degrees
        if (isColliding()){ 
            turn(180);
        }
    }

    //checks if the grunt is colliding with any object of class Obstacle and returns true if it is.
    public boolean isColliding() {
        Actor w = getOneIntersectingObject(Obstacle.class);
        return w != null;
    }

    //shoots a bullet randomly
    public void randomShoot() {
        EnemyBullet e = new EnemyBullet();
        //greennfoot picks a random number between 0 and 499 each act. If this number is less than 5, then a bullet is spawned. 
        //Actual shooting happens in bullet class
        if (Greenfoot.getRandomNumber(500) < 10){
            shootSFX.play();
            getWorld().addObject(e, getX(), getY());
        }   
    }

    public void checkDeath() {
        Actor d = getOneIntersectingObject(HeroBullet.class);
        if (d!=null) {
            int healthLostBullet = getWorld().getObjects(Hero.class).get(0).getDamage();
            int healthLostBomb = getWorld().getObjects(Bomb.class).get(0).damage();
            health(healthLostBullet);
            healthBomb(healthLostBomb);
            getWorld().removeObject(getOneIntersectingObject(HeroBullet.class));
            if (getHealth() == 0) {
                getWorld().removeObject(this);
            }
        }
    }

    public void health(int healthLostBullet ){
        health = health + healthLostBullet;
    }

    public void healthBomb(int healthLostBomb){
        health = health + healthLostBomb;
    }

    public int getHealth(){
        return health;
    }
}
Nosson1459 Nosson1459

2016/12/16

#
Your Bomb act is all in an 'if' statement:
if (t == 0){
and there is nothing to decrease the value of 't'. I didn't see an 'if' in Hero for the Bomb.
rkr rkr

2016/12/16

#
i fixed that but still does not deal damage it removes the bomb tho
Nosson1459 Nosson1459

2016/12/16

#
rkr wrote...
i fixed that but still does not deal damage it removes the bomb tho
In the posted code it's not fixed, and that's the code that I can look for problems. The if in the Bomb (that you fixed) now runs through the explode() code (if it's not commented anymore) all that has in it is removeObject() (after .302 of a sceond, which by then might have left the method already back to the act but it doesn't make a difference because of the next line of code), so it either does the removeObject in explode() or in the act (but I don't know how the "if" works with the 't', I don't know where you increase and decrease it).
You need to login to post a reply.