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

2016/12/3

explosion code

rkr rkr

2016/12/3

#
I have my bomb code done and destroys the enemy when ever the timer is 0. Problems in code: the bomb explodes before the enemy dies and i want to see if my main character is in the range of the bomb and if so i change my heart image to a dark heart image here is the code:
danpost danpost

2016/12/3

#
rkr wrote...
here is the code:
Where is the code?
rkr rkr

2016/12/3

#
// 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
{
    int timer = 100;
    public void act() 
    {
        timer--;
        if (timer == 0){
            explode();
            getWorld().removeObject(this);
        }
    }
    // removes object within the radius of the bomb
    public void explode (){
        getWorld().removeObjects(getObjectsInRange(200, SmartGrunt.class));
        getWorld().removeObjects(getObjectsInRange(200, Grunt.class));
    }
}

rkr rkr

2016/12/3

#
// MAIN CHARACTER CODE 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 final int waitTime = 101; 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(); World a = getWorld(); MyWorld m = (MyWorld)a; BombCounter counter = m.getBombCounter(); BombCounter bc = (BombCounter) getWorld().getObjects(BombCounter.class).get(0); if (bc.howManyBombs() > 0){ if(Greenfoot.isKeyDown("space") && !placeBomb){ getWorld().addObject(b,getX(), getY()); counter.loseBomb(); placeBomb = true; wait += waitTime; } } } //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--; } }
rkr rkr

2016/12/3

#
// HEALTH CODE

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class HeartOneHealthBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HeartOneHealthBar extends Actor
{
    Boolean hasReduced = false;

    /**
     * Act - do whatever the HeartOneHealthBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public HeartOneHealthBar(){

    }

    public void act() 
    {
        loseHealth();
        addHealth();
    } 

    public void addHealth(){
        Hero h = (Hero) getWorld().getObjects(Hero.class).get(0);
        if (h.checkHealthPowerUp() == true){
            setImage("hearthealth2.png");
        }

    }
    // if the hero collides with enemy or bullet it will change image to dark heart.
    public void loseHealth(){
        Hero h = (Hero) getWorld().getObjects(Hero.class).get(0);       
        if (h.checkDeath() == true){
            setImage("dark2.png");
            hasReduced = true;
        }
    }
    
    //need in EnemyBullet
    public boolean hasReduced() {
        return hasReduced;
    }
}
rkr rkr

2016/12/3

#
// MAIN CHARACTER CODE

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 final int waitTime = 101;
    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();
        World a = getWorld();
        MyWorld m = (MyWorld)a;
        BombCounter counter = m.getBombCounter();
        BombCounter bc = (BombCounter) getWorld().getObjects(BombCounter.class).get(0);
        if (bc.howManyBombs() > 0){
            if(Greenfoot.isKeyDown("space") && !placeBomb){
                getWorld().addObject(b,getX(), getY());
                counter.loseBomb();
                placeBomb = true;
                wait += waitTime;
            }
        }

    }
    //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--;
    }
}
Nosson1459 Nosson1459

2016/12/4

#
rkr wrote...
Problems in code: the bomb explodes before the enemy dies
What's the problem, do you mean the enemy is not dying at all, or is there an explosion THEN the enemy disappears?
rkr rkr

2016/12/4

#
Nevermind i solved the problem
Nosson1459 Nosson1459

2016/12/4

#
YAY! I'm trying out the "Yay" tag (good time and place to try it).
You need to login to post a reply.