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

2020/6/22

Making the Score = to number of projectile

soumya.__.khanna soumya.__.khanna

2020/6/22

#
Hello! This is the final part of my project. Here, I want to make the number of flames (projectile) mario can throw be equal to the score of the world, and as mario shoots the projectile, the score also decreases! I tried a couple things, but nothing really worked. If you all could help, it would be appreciated Score class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    private String label;
    private int score;
    //Font and text variables:
    private int fontSize;
    private Color fontColour;
    private Color backgroundColour;
    private String scoreText;
    public Score(int score)
    {
        this.score = score;
        //What the score will sppear as
        fontSize = 32;
        fontColour = Color.ORANGE;
        backgroundColour = new Color(0,0,0,0);
        this.label = "Score: " + score;
        updateImage();
    }
    public void update(int change)
    {
        score += change;
        setLabel("Score: " + score);
    }
    public void setLabel(String text)
    {
        this.label = text;
        updateImage();
        scoreText = "Score: " + score;
    }
    public void updateImage()
    {
        GreenfootImage myImg = new GreenfootImage(label, fontSize, fontColour, backgroundColour); //Properties of the scoreboard
        setImage(myImg); //Set the image as it updates
    }    
}
FireBall:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class FireBall here.
* 
* @author (your name) 
* @version (a version number or a date)
*/
public class FireBall extends Actor
{
    private static GreenfootImage[] FLAME = {new GreenfootImage("Flame0.gif"),
                                             new GreenfootImage("Flame1.gif")};
    private GreenfootImage[] animation;
    private int speed, damage;
    private int frame;
    private int actCounter;
    int skipRate = 20;
    public void act() 
    {
        marioAnimator();
        updateAmimation();
        actCounter++;
        move(speed);
        checkBoundaries();
    }
    public FireBall() 
    {
        animation = FLAME; //Animation when staying the way it is
    }
    public FireBall( int s, int d) 
    {
        speed = s;
        damage = d;
    }
    public void updateAmimation()
    {
        if(animate())
        {
            if(frame >= animation.length)//Bring frame to beginning of array to not crash
            {
                frame = 0;
            }
            setImage(animation[frame]);
            frame++;
        }
    }
    public boolean animate()
    {
        return actCounter % skipRate == 0;
    }
    public void marioAnimator()
    {
        animation = FLAME; //The animation sequence to play
        this.skipRate = skipRate ; //How fast the animation will be in each case
        actCounter = skipRate;
        //frame = 0;
    }
    public void checkBoundaries()
    {
        GreenfootImage img = getImage();
        int width = img.getWidth();
        int height = img.getHeight();
    
        World livesIn = getWorld();
        int left = getX() - width/2;
        int right = getX() + width/2;
        int top = getY() - height/2;
        int bottom = getY() + height/2;
        if( left <= 0)
        {
            livesIn.removeObject(this);
        }
        else if( right >= livesIn.getWidth())
        {
            livesIn.removeObject(this);
        }
        else if(top <= 0)
        {
            livesIn.removeObject(this);
        }
    
        else if(bottom >= livesIn.getHeight())
        {
            livesIn.removeObject(this);
        }
    }
}
Hero:
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
{
    
    public static GreenfootImage[]IDLE = 
                                            {new GreenfootImage("Idle0.png"),
                                             new GreenfootImage("Idle1.png"),
                                             new GreenfootImage("Idle2.png"),
                                             new GreenfootImage("Idle3.png"),
                                             new GreenfootImage("Idle4.png"),
                                             new GreenfootImage("Idle5.png"),
                                             new GreenfootImage("Idle6.png"),
                                             new GreenfootImage("Idle7.png")};
    
    public static GreenfootImage[] GROUNDLEFT =
                                            {new GreenfootImage("Left1.png"),
                                             new GreenfootImage("Left2.png"),
                                             new GreenfootImage("Left3.png")};
    public static GreenfootImage[] GROUNDRIGHT =
                                            {new GreenfootImage("Right1.png"),
                                             new GreenfootImage("Right2.png"),
                                             new GreenfootImage("Right3.png")};
    
    
    public boolean triggerReleased; //Once weapon has been released
    public int attackCycle; //For the hero to attack
    public static int LifeLine;
    public int speed; //Refers to the heros' speed, and health
    
    public int frame;
    public int actCounter;
    public int skipRate;
    public int floor = 440;
    
    public GreenfootImage[] animation;
    //ints
    //doubles to allow decimals
    public double gravity, gravityIncrement;
    //Booleans
    public boolean groundLevel;
    public boolean changeAnimation;
    public boolean movingRight;
    public boolean ifIdle;
    public boolean movingLeft; 
    public boolean damageTaken = false; //Hero has not taken any damage
    public int damageTakenTimer = 0; 
    public void act() 
    {
        updateAmimation();
        actCounter++;
        attack();
        triggerReleased = true; //Trigger is released        
        marioMover();
        gravity();
        checkCeiling();
        floatingMario();
    } 
    public Hero()
    {
        this.LifeLine = 5;
        frame = 0; //initial frame
        skipRate = 75; //speed of animaation
        actCounter = 0; //initial act
        speed = 3; //How fast it moves
        gravityIncrement = .05; //Velocity 
        gravity = 1;
    }
    public void updateAmimation()
    {
        if(animate())
        {
            setImage(animation[frame]);
            frame ++;
            if( frame >= animation.length)
            {
                frame = 0;
            }
        }
    }
    public boolean animate()
    {
        return actCounter % skipRate == 0;
    }    
    public void fireWeapons(FireBall fireThis)
    {
        fireThis.setRotation (getRotation());
        getWorld().addObject (fireThis,getX(),getY());
        fireThis.move(getImage().getWidth()/2);
    }
    public void attack()
    {
        if(attackCycle <= 0) //To avoid rapid shooting
        {
            if(Greenfoot.isKeyDown("M"))
            {
                fireWeapons(new FireBall(3, 3)); //Shoot the weapons depending on whether or not key is pressed
            }
            else
            {
                triggerReleased = true; //Otherwise, trigger is released.
            }
            attackCycle = 10; // n being the cooldown in act cycles.    
        }
        else if(attackCycle > 0)
        {
            attackCycle--; // decrement by 1 per act cycle.
        }        
    }    
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        setLocation(getX(),newY);
        groundLevel = true;
    }  
    public void checkCeiling() 
    {
        try
        {
            Actor ceiling = getOneObjectAtOffset(0, -getCustomHeight(), Block.class);//Create information of platforms at offset
            if(ceiling != null)//Check for platform(double negative)
            {
                groundLevel = false;
                goToCeiling(ceiling);
            }
        }
        catch(IllegalStateException ex)
        {
            //yay
        }
    }
    public void goToCeiling(Actor ceiling) //Check for what is on top of Mario's head
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int ceilingBottom = ceiling.getY() + ceilingHeight/2;
        setLocation(getX(), ceilingBottom+getCustomHeight()); //Set this location
        if(getY() == ceilingBottom+getCustomHeight())
        {
            setLocation(getX(), getY() +(int) gravity); //Stay at the same x value, but fall with gravity
            gravity = gravity + 70*gravityIncrement; //Adding velocity
        }
    }   
    public void floatingMario()
    {
        if (getY() >= floor) //Don't go past the floor
        {
            setLocation(getX(), floor);
            groundLevel = true;
            //gravity = 0
        }
    }
    public void gravity()
    {
        setLocation(getX(), getY() +(int) gravity); //Stay at the same x value, but fall with gravity
        gravity = gravity + gravityIncrement; //Adding velocity
        if(groundLevel)
        {
            gravity = 0;
        }
    }
    public void marioMover()
    {
        try
        {
            if (Greenfoot.isKeyDown("left")) //Go in the left direction(accoring to speed), if left key is down
                {
                    setLocation(getX() - speed, getY());
                    movingLeft = true;
                }
            if (Greenfoot.isKeyDown("right")) //Move right
            {
                setLocation(getX() + speed, getY());
                movingRight = true;
            }
            gravity(); //Fall from the top, and after jumps
        }
        
        catch(IllegalStateException ex)
        {
        }
    }
    
    //Getters and Setters    
    public int getSpeed()
    {
        return speed; //Get information from speed
    }
    public void setSpeed(int s)
    {
        speed = s; 
    }
    public int getHeight() 
    {
        return getImage().getHeight();
    }    
    public int getCustomHeight() 
    {
        return getImage().getHeight()/4;
    }
    public int getWidth()
    {
        return getImage().getWidth();
    }    
    public int getCustomWidth()
    {
        return getImage().getWidth()/4;
    }  
    public static int getLives()
    {
        return LifeLine;
    }
}
World: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * The parent class of worlds * * @soumya.__.khanna (Soumya Khanna) * @I'llLookItUp (Jun 17th, 2020) */ public class Worlds extends World { public int width = getWidth(); //Width of the world --Accessed in children classes public static Hero mario = new Hero(); //Declaring the Swimming Mario Class //Number of hearts based on the life of Mario public Life life0; public Life life1; public Life life2; public Life life3; public Life life4; public Score score; //Adding the score board public void act() { checkLife(); } public Worlds() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(2200, 576, 1, false); } public void lifeCounter(int x, int y) { //Declaring all lives life0 = new Life(); life1 = new Life(); life2 = new Life(); life3 = new Life(); life4 = new Life(); //Adding lifes evenly spaced around addObject(life0, x, y); addObject(life1, x + 30, y); addObject(life2, x + 60, y); addObject(life3, x + 90, y); addObject(life4, x + 120, y); } public void checkLife() { int lives = Hero.getLives(); if (lives == 4) { removeObject(life4); } if (lives == 3) { removeObject(life3); } if (lives == 2) { removeObject(life2); } if (lives == 1) { removeObject(life1); } if (lives == 0) { removeObject(life0); } } public Score getScore() { return score; //Information from scoreboard } } And Coin Block -- by bumping into which Mario increases score.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * This is the class for all coing blocks that Mario can hit
 * 
 * @soumya.__.khanna (Soumya Khanna) 
 * @3.16.8 IThink? (June 20th)
 */
public class CoinBlock extends Block
{
    public final int value = 1;
    public void act() 
    {
        super.act(); //Adapt the parent act method
    }      
    public CoinBlock() //Initial image 
    {
        setImage(coin); 
    }    
    public void changeImage() //New image, when mario hits block
    {
        setImage(coinHit);
        Worlds myWorld = (Worlds)getWorld();
        Score scoreboard = myWorld.getScore();
        scoreboard.update(value);
    }
}
Thank you!
danpost danpost

2020/6/25

#
The value of the score field in the Score class will first need to be made accessible to other classes. Either make the score field public or add a public getter method that returns it's value.
You need to login to post a reply.