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

2021/1/18

Can somebody proofread this? I'm not sure it's perfect...

ChafikAZ ChafikAZ

2021/1/18

#
private int fireDelay = 0; //Delays multiple firing private int currentLocationX = 0; private int currentLocationY = 0; public int scoreMultiplier = 1; private int imageOffsetX = 0; private int imageOffsetY = 0; public int publicOffsetX; public int publicOffsetY; /** * Act - do whatever the Person wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. movePerson(); eatFood(); fire(); increaseScoreMultiplier(); declareImageOffsets; } public declareImageOffsets() { imageOffsetX = publicOffsetX; imageOffsetY = publicOffsetY; } public void assignImageOffsetsHard() { publicOffsetX = 50; publicOffsetY = 50; } public void assignImageOffsetEasy() { publicOffsetX = 300; publicOffsetY = 300; } public void fire() { if (Greenfoot.isKeyDown("space")) { if (fireDelay == 0) {//Ignore any space bar presses if the fire delay counter is pressed fireDelay = 50; //Set the fire delay counter Urban urban = (Urban)getWorld(); if (urban != null) {//Add the banana projectile to the world at the position of the Person object urban.addObject(new Projectile(), getX()+ imageOffsetX, getY()+ imageOffsetY); } } } if (fireDelay > 0) {//Decrement the fire counter if it is set fireDelay = fireDelay - 1; } } public void increaseScoreMultiplier() { if (isTouching(ScoreMultiplier.class)) { removeTouching(ScoreMultiplier.class); scoreMultiplier = scoreMultiplier + 1; } } public void eatFood() { if (isTouching(Plum.class)) { removeTouching(Plum.class); Greenfoot.playSound("Eating.wav"); respawn(); } } public void respawn() { //Cast the SuperClass World to the SubClass Urban Urban urban = (Urban)getWorld(); if (urban != null) { urban.addObject(new Plum(), Greenfoot.getRandomNumber(urban.getWidth()), Greenfoot.getRandomNumber(urban.getHeight())); urban.Score(1 * scoreMultiplier); } } public void movePerson() { currentLocationX = getX(); currentLocationY = getY(); if (Greenfoot.isKeyDown("left")) { move(-5); } else if (Greenfoot.isKeyDown("right")) { move(5); } else if (Greenfoot.isKeyDown("up")) { setLocation( currentLocationX , (currentLocationY - 5)); } else if (Greenfoot.isKeyDown("down")) { setLocation( currentLocationX, (currentLocationY + 5)); } } }
danpost danpost

2021/1/18

#
The fire method can be simplified to be:
public void fire()
{
    if (Greenfoot.isKeyDown("space"))
    {
        if (fireDelay == 0)
        {
            getWorld().addObject(new Projectile(), getX()+ imageOffsetX, getY()+ imageOffsetY);
            fireDelay = 50;
        }           
    }
    else fireDelay--;
}
I presume the fields and methods concerning offsets will be made use of later.
ChafikAZ ChafikAZ

2021/1/19

#
Thanks, I appreciate it.
danpost danpost

2021/1/19

#
ChafikAZ wrote...
Thanks, I appreciate it.
Notice a bit of a problem with what I game. Revising to:
public void fire()
{
    if (fireDelay > 0) fireDelay--;
    else
    {
        if (Greenfoot.isKeyDown("space"))
        {
            getWorld().addObject(new Projectile(), getX()+imageOffsetX, getY()+imageOffsetY());
            fireDelay = 50;
        }
    }
}
ChafikAZ ChafikAZ

2021/1/19

#
As my teacher says, "Work smarter, not harder."
You need to login to post a reply.