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

2020/3/1

Coding more smart

DaRafster DaRafster

2020/3/1

#
I want to get more familiar in generalizing my code more through methods, so I would be able to save more space and reduce the number of lines used. Here is an example of the prepare() method the world generates when saving the world:
/**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        int heightPokemonA = getHeight()/5;
        int heightPokemonB = getHeight()/3;

        Pikachu pikachu = new Pikachu();
        addObject(pikachu,getWidth()/8,heightPokemonA);

        Squirtle squirtle = new Squirtle();
        addObject(squirtle,getWidth()/2,heightPokemonB);

        Bulbasaur bulbasaur = new Bulbasaur();
        addObject(bulbasaur,getWidth()/ 2 + 350,heightPokemonA);

        StartGameButton startGameButton = new StartGameButton();
        addObject(startGameButton,getWidth()/2,700);

        Label label = new Label("Press on any Pokemon to Interact!");
        addObject(label,getWidth()/2 + 300,getHeight()/2 + 50);

        Charmander charmander = new Charmander();
        addObject(charmander,getWidth()/ 2 + 350,getHeight()/2);

        Turtwig turtwig = new Turtwig();
        addObject(turtwig,getWidth()/8,getHeight()/2);
        
        Label label2 = new Label("Press Start Game to Play a Round of Guess that Pokemon!");
        addObject(label2,getWidth()/2 + 150,635);;
    }
I feel as if that's too much coding and very repetitive. There must be a better way to code all this. Can someone give me an explanation of what I could do to "reduce" the amount of code, and give a brief example of what it would look like? I feel as if learning this skill will be able to save me more time, meaning more efficient and smarter coding.
danpost danpost

2020/3/1

#
You could combine the addObject lines with the assignment lines before them (in most cases. That is, use the assigned values in place of what the variable the value is assigned to in the addObject line. As the pairs of lines for your labels are already quite long, I might leave them alone (or just put the String values in a variable and use it when creating the actor in the addObject line). A non-label example would be to combine lines 10 and 11 into the following:
addObject(new Pikachu(), getWidth()/2, heightPokemonA);
DaRafster DaRafster

2020/3/1

#
danpost wrote...
You could combine the addObject lines with the assignment lines before them (in most cases. That is, use the assigned values in place of what the variable the value is assigned to in the addObject line. As the pairs of lines for your labels are already quite long, I might leave them alone (or just put the String values in a variable and use it when creating the actor in the addObject line). A non-label example would be to combine lines 10 and 11 into the following:
addObject(new Pikachu(), getWidth()/2, heightPokemonA);
Thanks, this saves me a couple of lines which makes things a little more organized and less congested. I also have one more thing I want to do to save more space. I've created a superclass that all my pokemon classes can access. I've made a scalingImageDown() method within the superclass so I wouldn't have to repetitively do it in each of my classes. I would also like to create a method within that superclass that plays the sound of "this" pokemon and changes the image when called. As of right now, all my pokemon almost have identical codes, but with different volumes, sound files, and image files. Here is my superclass code and the method I used to scale down the images:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class AllObjects here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AllObjects extends Actor
{
    int horizScaleDown;
    int vertScaleDown;
    /**
     * Act - do whatever the AllObjects 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.
    }    
    
    public void scalingImagesDown(int x, int y)
    {
       horizScaleDown = x;
       vertScaleDown = y;
       getImage().scale(getImage().getWidth()/horizScaleDown,getImage().getHeight()/horizScaleDown);
    }
}
I want the same function this method performs but with sound and images files. Here is my attempt of trying to do this within my superclass:
public void interaction(String activeImage, String image, int volume)
    {
        if(Greenfoot.mouseClicked(this))
        {
            setImage(activeImage);
            bulb.setVolume(volume);
            bulb.play();
            Greenfoot.delay(100);
            setImage(image);
            Greenfoot.delay(10);
        }
    }
I'm not quite sure what to put before the setVolume() and play() method. The "bulb" refers to the Bulbasaur sound file, but I want it to reference any GreenfootSound. So how would I complete this superclass method which would save me time and space? EDIT: I've managed to figure it out, here is my code:
public void interaction(String activeImage, String image, String soundFile, int volume)
    {
        if(Greenfoot.mouseClicked(this))
        {
            GreenfootSound sound = new GreenfootSound(soundFile);
            setImage(activeImage);
            sound.setVolume(volume);
            Greenfoot.playSound(soundFile);
            Greenfoot.delay(100);
            setImage(image);
            Greenfoot.delay(10);
        }
    }
However, once the image changes, the "active" image isn't scaled and the original image is also no longer scaled when it gets set back. What is wrong? EDIT AGAIN: I managed to figure it out, had to add in some extra values, here is what I mean:
public void interaction(String activeImage, String image, String soundFile, int x, int y, int xx, int yy)
    {
        if(Greenfoot.mouseClicked(this))
        {
            setImage(activeImage);
            scalingImagesDown(x,y);
            Greenfoot.playSound(soundFile);
            Greenfoot.delay(100);
            setImage(image);
            scalingImagesDown(xx,yy);
            Greenfoot.delay(10);
        }
    }
Anyways, it looks like I won't need help anymore for the meantime, but if any of you guys could suggest a better way to save more space in my code and see any unnecessary stuff, please feel free to mention it!
You need to login to post a reply.