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

2015/2/26

Code always needs good comments

ValeroDeniro ValeroDeniro

2015/2/26

#
Hi coders, I sent my friend a game which I made fro a school project, he then sent me it back with so many improvements without any comments. Cann someone very kindly help me understand what is going on in each line or most it. Thank you so much. (The code is below)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class DodgeWorld here.
 * 
 * author T Y
 
 */
public class DodgeWorld extends World
{
    /**
     * Constructor for objects of class DodgeWorld.
     * 
     */
    public DodgeWorld()
    {    
        super(400, 800, 1); // Changes size of canvas 
        addCharacter();
        
        scoreCounter = new Counter("Score: ");//
        addObject(scoreCounter, getWidth() / 3 - 15, getHeight() / 8 - 15);
        
        lifeCounter = new Counter("Lives: ");//
        addObject(lifeCounter, getWidth() / 2, getHeight() / 8 - 15);
        
        getLifeCounter().add(3);
            
    }
    
    public Counter scoreCounter;//
    public Counter lifeCounter;//
    public int score = 0;//
    private int zone = 100;//
    private int timer = 0;//
    
    public boolean gameOver = false;
    
    public void act()
    {
        addDangers();
    }
    
    public void addCharacter()
    {
        addObject(new Man(), 200, 680); //positionning of the bluebird
    }
    
    public void addDangers()
    {
        if (Greenfoot.getRandomNumber(1000) < zone)
        {
            addObject(new Rock(), Greenfoot.getRandomNumber(400), -10);//
            if (timer >= 100 && zone < 200)
            {
                zone++;
            }
            
            if (timer == 400 || timer == 800 || timer == 1000)//
            {
                zone += 25;
            }
            
            timer++;//
        }
    }
    
    public Counter getScoreCounter()
    {
        return scoreCounter;//
    }
    
    public Counter getLifeCounter()
    {
        return lifeCounter;//
    }
}
erdelf erdelf

2015/2/26

#
a little try to comment it
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
 
/**
 * Write a description of class DodgeWorld here.
 *
 * author T Y
  
 */
public class DodgeWorld extends World
{
    /**
     * Constructor for objects of class DodgeWorld.
     *
     */
    public DodgeWorld()
    {   
        super(400, 800, 1); // Changes size of canvas
        addCharacter(); // calls the method addCharacter
         
        scoreCounter = new Counter("Score: ");// creates a new Counter with the prefix "Score: "
        addObject(scoreCounter, getWidth() / 3 - 15, getHeight() / 8 - 15); // adds the Counter to the world
         
        lifeCounter = new Counter("Lives: ");// creates a new Counter with the prefix "Lives: "
        addObject(lifeCounter, getWidth() / 2, getHeight() / 8 - 15); // adds the Counter to the world
         
        getLifeCounter().add(3);
             
    }
     
    public Counter scoreCounter;// a counter for the score
    public Counter lifeCounter;// a counter for lifes
    public int score = 0;// the score
    private int zone = 100;
    private int timer = 0;
     
    public boolean gameOver = false; // is the game over ?
     
    public void act()
    {
        addDangers(); // calls the method addDangers
    }
     
    public void addCharacter()
    {
        addObject(new Man(), 200, 680); //positioning of the bluebird
    }
     
    public void addDangers()
    {
        if (Greenfoot.getRandomNumber(1000) < zone) // with a chance of 10% the following is executed
        {
            addObject(new Rock(), Greenfoot.getRandomNumber(400), -10);// adds a rock at a random x coordinate above the world
            if (timer >= 100 && zone < 200)
            {
                zone++;
            }
             
            if (timer == 400 || timer == 800 || timer == 1000)
            {
                zone += 25;
            }
             
            timer++;
        }
    }
     
    public Counter getScoreCounter()
    {
        return scoreCounter;//
    }
     
    public Counter getLifeCounter()
    {
        return lifeCounter;//
    }
}
ValeroDeniro ValeroDeniro

2015/2/26

#
Wow amazing. Thank you!
You need to login to post a reply.