Sunday, February 22, 2015

Greenfoot game and Javadoc

This is the code for the game created for the class learning about the value of documentation.
Watch the video to understand and appreciate the learning process of this code.
https://www.youtube.com/watch?v=kOnvLnmbeno


public class myWorld extends World
{
public myWorld(){
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1,false);
getBackground().drawRect(300, 200, 100, 100);
getBackground().setColor(java.awt.Color.CYAN);
getBackground().fillRect(Greenfoot.getRandomNumber(550),Greenfoot.getRandomNumber(150),50,50);

prepare();
}

private void prepare(){
myDot mydot = new myDot();
addObject(mydot, 109, 96);
}
}

import greenfoot.*;

public class myDot extends Actor{
public void act() {
setLocation(getX()+5,getY()+5);
if(isAtEdge()){
gameOver lost=new gameOver();
getWorld().addObject(lost,getWorld().getWidth()/2,getWorld().getHeight()/2);
Greenfoot.stop();
}
if(Greenfoot.isKeyDown("up")){
setLocation(getX(),getY()-Greenfoot.getRandomNumber(25));
}
java.awt.Color isOnTop = getWorld().getBackground().getColorAt(getX(),getY());

if(isOnTop.equals(java.awt.Color.CYAN)){
//System.out.println("I'm over it!!!");
UgotIt won=new UgotIt();
getWorld().addObject(won,getWorld().getWidth()/2,getWorld().getHeight()/2);
Greenfoot.stop();
}
}
}

public class gameOver extends Actor{
public void act() {
// Add your action code here.
}
}

public class UgotIt extends Actor{
public void act() {
// Add your action code here.
}
}

No comments:

Post a Comment