Wednesday, January 22, 2014

Today we are adding "shooting" to our game. This is our "ShootingManager" class from today:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ShootingManager here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ShootingManager extends Actor
{
    /**
     * Act - do whatever the ShootingManager wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        killBadGuys();
    }    
    
    public void remove()
    {
       Actor walls = getOneIntersectingObject(Platform.class);
       if(getX() <=1 || getX() >= getWorld().getWidth() -1)
       {
           getWorld().removeObject(this);
        }
        else if(walls != null)
        {
            getWorld().removeObject(this);
        }
    }
    
    public boolean amIshot(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        return actor !=null;
    }
    
    public void kill(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        if(actor != null)
        {
            getWorld().removeObject(actor);
        }
    }
    
    public void killBadGuys()
    {
        if(amIshot(BadGuys.class))
        {
            kill(BadGuys.class);
            getWorld().removeObject(this);
        }
        else
        {
            remove();
        }
    }
}
And here is the current Player code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 16;
    private int speed = 4;
    
    private int direction = 1; // 1 = right and -1 = left
    private int shootingCounter = 20; // Delay shooting
    
    private GreenfootImage run1r = new GreenfootImage("run1r.png");
    private GreenfootImage run2r = new GreenfootImage("run2r.png");
    private GreenfootImage run3r = new GreenfootImage("run3r.png");
    private GreenfootImage run4r = new GreenfootImage("run4r.png");
    private GreenfootImage run1l = new GreenfootImage("run1l.png");
    private GreenfootImage run2l = new GreenfootImage("run2l.png");
    private GreenfootImage run3l = new GreenfootImage("run3l.png");
    private GreenfootImage run4l = new GreenfootImage("run4l.png");
    private int frame = 1;
    private int animationCounter = 0;
    
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkFall();
        checkKey();
        platformAbove();
        checkRightWalls();
        checkLeftWalls();
        shooting(); 
        shootingCounter --;
        animationCounter++;
    }   
    
    public void checkKey()
    {
        if(Greenfoot.isKeyDown("up") && jumping == false)
        {
            jump();
        }
        if(Greenfoot.isKeyDown("right"))
        {
            direction = 1;
            moveRight();
        }
        if(Greenfoot.isKeyDown("left"))
        {
            direction = -1;
            moveLeft();
        }
    }
    
    public boolean shooting()
    {
        if(Greenfoot.isKeyDown("space") && shootingCounter <= 0 && direction ==1)
        {
            getWorld().addObject(new ShootRight(), getX(), getY());
            shootingCounter = 20;
            return true;
        }
        if(Greenfoot.isKeyDown("space") && shootingCounter <= 0 && direction ==-1)
        {
            getWorld().addObject(new ShootLeft(), getX(), getY());
            shootingCounter = 20;
            return true;
        }
        return false;
    }
    
    public void moveRight()
    {
        setLocation(getX()+speed, getY());
        if(animationCounter % 4 == 0)
        {
        animateRight();
        }
    }
    
    public void animateRight()
    {
        if(frame == 1)
        {
            setImage(run1r);
        }
        else if(frame == 2)
        {
            setImage(run2r);
        }
        else if(frame == 3)
        {
            setImage(run3r);
        }
        else if(frame == 4)
        {
            setImage(run4r);
            frame = 1;
            return;
        }
        frame++;
    }
    
    public void moveLeft()
    {
        setLocation(getX()-speed, getY());
        if(animationCounter %4 == 0)
        {
        animateLeft();
    }
    }
    
    public void animateLeft()
    {
        if(frame == 1)
        {
            setImage(run1l);
        }
        else if(frame == 2)
        {
            setImage(run2l);
        }
        else if(frame == 3)
        {
            setImage(run3l);
        }
        else if(frame == 4)
        {
            setImage(run4l);
            frame = 1;
            return;
        }
        frame++;
    }
    
    public boolean platformAbove()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/-2);
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Platform.class);
        if(ceiling != null)
        {
            vSpeed = 1;
            bopHead(ceiling);
            return true;
        }
        else
    {
        return false;
    }
    }
    
    public boolean checkRightWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/2);
        Actor rightWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
        if(rightWall == null)
        {
            return false;
        }
        else
        {
            stopByRightWall(rightWall);
            return true;
        }
    }
    
    public void stopByRightWall(Actor rightWall)
    {
        int wallWidth = rightWall.getImage().getWidth();
        int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2;
        setLocation(newX - 5, getY());
        
    }
    
        public boolean checkLeftWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/-2);
        Actor leftWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
        if(leftWall == null)
        {
            return false;
        }
        else
        {
            stopByLeftWall(leftWall);
            return true;
        }
    }
    
    public void stopByLeftWall(Actor leftWall)
    {
        int wallWidth = leftWall.getImage().getWidth();
        int newX = leftWall.getX() + (wallWidth + getImage().getWidth())/2;
        setLocation(newX + 5, getY());
        
    }
  
    public void bopHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
    }
    
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        if(vSpeed <=9)
        {
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
    }
    
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/2) + 5;
        Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);
        if(ground == null)
        {
            jumping = true;
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
    }
    
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
        jumping = false;
    }
    
    
    public void checkFall()
    {
        if(onGround())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
    
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
    }
}

Friday, January 17, 2014

Monsters and Platform Blocks

Today we worked on adding a new "Monster" class (see code below) and added sub-classes to the Platform class.  We also explored 32x32 pixel blocks for possible use as Platform objects.  Here's an "Google Search" for some examples:

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Monster here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Monster extends Actor
{
    int frame = 0;
    private int animationCount;
    private int vSpeed = 0;
    private int acceleration = 2;
    
    private int spriteHeight = getImage().getHeight();
    private int spriteWidth = getImage().getWidth();
    private int lookForGroundDistance = (int)spriteHeight/2;
    private int lookForEdge = (int)spriteWidth/2;
    
    private int speed = 1;
    
    GreenfootImage monster1l = new GreenfootImage("monster1l.png");
    GreenfootImage monster2l = new GreenfootImage("monster2l.png");
    GreenfootImage monster3l = new GreenfootImage("monster3l.png");
    GreenfootImage monster4l = new GreenfootImage("monster4l.png");
    
    GreenfootImage monster1r = new GreenfootImage("monster1r.png");
    GreenfootImage monster2r = new GreenfootImage("monster2r.png");
    GreenfootImage monster3r = new GreenfootImage("monster3r.png");
    GreenfootImage monster4r = new GreenfootImage("monster4r.png");
    
    /**
     * Act - do whatever the Monster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkFall();
        move();
        
        if(speed<0)
        {
            if(animationCount % 4 == 0)
            animateLeft();
        }
        else
        {
            if(animationCount % 4 == 0)
            animateRight();
        }
        animationCount ++;
    }    
    
    public void move()
    {
        Actor ground = getOneObjectAtOffset(lookForEdge, lookForGroundDistance, Platform.class);
        
        if(ground == null)
        {
            speed *= -1; // Reverses direction
            lookForEdge *= -1; // Looks for a negative number
        }
        else
        {
            move(speed);
        }
    }
    
    public void animateLeft()
    {
        if(frame == 0)
        {
            setImage(monster1l);
        }
        if(frame == 1)
        {
            setImage(monster2l);
        }
        if(frame == 2)
        {
            setImage(monster3l);
        }
        if(frame == 3)
        {
            setImage(monster4l);
            frame = 0;
        }
        frame++;
    }
    
        public void animateRight()
    {
        if(frame == 0)
        {
            setImage(monster1r);
        }
        if(frame == 1)
        {
            setImage(monster2r);
        }
        if(frame == 2)
        {
            setImage(monster3r);
        }
        if(frame == 3)
        {
            setImage(monster4r);
            frame = 0;
        }
        frame++;
    }
    
        public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        if(vSpeed <=9)
        {
            vSpeed = vSpeed + acceleration;
        }
        
    }
    
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/2) + 5;
        Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);
        if(ground == null)
        {
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
    }
    
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
    }
    
    
    public void checkFall()
    {
        if(onGround())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
    
}

Platformer: Platform Blocks and Monsters

To begin with, we're going to add some new subclasses to our Platform class.  I created one called Wall, one called Platform2, and one called Block -- but you can choose your own.  I then assigned them different images.  What this does is it will allow us to add different world objects that have the same properties as a Platform object -- meaning you can jump on them but not walk through them.
Next, we're going to add a "Monster" class.  Obviously you can call yours whatever you would like, but it will be up to you to follow the tutorial.  Here's a beginning code for our Monster class -- which should look similar to the other classes we have created:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Monster here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Monster extends Actor
{
    int frame = 0;
    private int animationCount;
    
    GreenfootImage monster1l = new GreenfootImage("monster1l.png");
    GreenfootImage monster2l = new GreenfootImage("monster2l.png");
    GreenfootImage monster3l = new GreenfootImage("monster3l.png");
    GreenfootImage monster4l = new GreenfootImage("monster4l.png");
    
    GreenfootImage monster1r = new GreenfootImage("monster1r.png");
    GreenfootImage monster2r = new GreenfootImage("monster2r.png");
    GreenfootImage monster3r = new GreenfootImage("monster3r.png");
    GreenfootImage monster4r = new GreenfootImage("monster4r.png");
    
    /**
     * Act - do whatever the Monster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        animationCount ++;
    }    
    
    public void animateLeft()
    {
        if(frame == 0)
        {
            setImage(monster1l);
        }
        if(frame == 1)
        {
            setImage(monster2l);
        }
        if(frame == 2)
        {
            setImage(monster3l);
        }
        if(frame == 3)
        {
            setImage(monster4l);
            frame = 0;
        }
        frame++;
    }
    
        public void animateRight()
    {
        if(frame == 0)
        {
            setImage(monster1r);
        }
        if(frame == 1)
        {
            setImage(monster2r);
        }
        if(frame == 2)
        {
            setImage(monster3r);
        }
        if(frame == 3)
        {
            setImage(monster4r);
            frame = 0;
        }
        frame++;
    }
}
Now we will begin editing the code by copying chunks of our Player class to recycle.

Thursday, January 16, 2014

Platformer: Ceilings and Walls

Today we added the additional functionality of ceilings and walls -- basically making the platform blocks impassable.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 16;
    private int speed = 4;
    
    private GreenfootImage run1r = new GreenfootImage("run1r.png");
    private GreenfootImage run2r = new GreenfootImage("run2r.png");
    private GreenfootImage run3r = new GreenfootImage("run3r.png");
    private GreenfootImage run4r = new GreenfootImage("run4r.png");
    private GreenfootImage run1l = new GreenfootImage("run1l.png");
    private GreenfootImage run2l = new GreenfootImage("run2l.png");
    private GreenfootImage run3l = new GreenfootImage("run3l.png");
    private GreenfootImage run4l = new GreenfootImage("run4l.png");
    private int frame = 1;
    private int animationCounter = 0;
    
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkFall();
        checkKey();
        platformAbove();
        checkRightWalls();
        checkLeftWalls();
        animationCounter++;
    }   
    
    public void checkKey()
    {
        if(Greenfoot.isKeyDown("space") && jumping == false)
        {
            jump();
        }
        if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
        if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        }
    }
    
    public void moveRight()
    {
        setLocation(getX()+speed, getY());
        if(animationCounter % 4 == 0)
        {
        animateRight();
        }
    }
    
    public void animateRight()
    {
        if(frame == 1)
        {
            setImage(run1r);
        }
        else if(frame == 2)
        {
            setImage(run2r);
        }
        else if(frame == 3)
        {
            setImage(run3r);
        }
        else if(frame == 4)
        {
            setImage(run4r);
            frame = 1;
            return;
        }
        frame++;
    }
    
    public void moveLeft()
    {
        setLocation(getX()-speed, getY());
        if(animationCounter %4 == 0)
        {
        animateLeft();
    }
    }
    
    public void animateLeft()
    {
        if(frame == 1)
        {
            setImage(run1l);
        }
        else if(frame == 2)
        {
            setImage(run2l);
        }
        else if(frame == 3)
        {
            setImage(run3l);
        }
        else if(frame == 4)
        {
            setImage(run4l);
            frame = 1;
            return;
        }
        frame++;
    }
    
    public boolean platformAbove()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/-2);
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Platform.class);
        if(ceiling != null)
        {
            vSpeed = 1;
            bopHead(ceiling);
            return true;
        }
        else
    {
        return false;
    }
    }
    
    public boolean checkRightWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/2);
        Actor rightWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
        if(rightWall == null)
        {
            return false;
        }
        else
        {
            stopByRightWall(rightWall);
            return true;
        }
    }
    
    public void stopByRightWall(Actor rightWall)
    {
        int wallWidth = rightWall.getImage().getWidth();
        int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2;
        setLocation(newX - 5, getY());
        
    }
    
        public boolean checkLeftWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/-2);
        Actor leftWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
        if(leftWall == null)
        {
            return false;
        }
        else
        {
            stopByLeftWall(leftWall);
            return true;
        }
    }
    
    public void stopByLeftWall(Actor leftWall)
    {
        int wallWidth = leftWall.getImage().getWidth();
        int newX = leftWall.getX() + (wallWidth + getImage().getWidth())/2;
        setLocation(newX + 5, getY());
        
    }
  
    public void bopHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
    }
    
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        if(vSpeed <=9)
        {
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
    }
    
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/2) + 5;
        Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);
        if(ground == null)
        {
            jumping = true;
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
    }
    
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
        jumping = false;
    }
    
    
    public void checkFall()
    {
        if(onGround())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
    
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
    }
}

Tuesday, January 14, 2014

Platformer: Work on Graphics

Today I would like you to focus on graphics for your platformer game. 
  • Player Sprites: 4 left and 4 right facing.
  • Enemy Sprites: 4 left and 4 right facing.
  • Wall / Platform: This will be the floors & walls.
  • Player Bullet: This could be a bullet, snowball, laser, etc.
  • Enemy Bullet: Again, whatever the enemy is shooting/throwing at you.
  • Pieces to Collect: Coins, food, etc. to add to score.
  • Title Page: Probably 640x480.
  • Game Over Page: Again, probably 640x480.
We will be working on walls, enemies/bad guys, adding shooting, adding objects to pick up, and implementing the title & game over screens.

Monday, January 13, 2014

Platformer: Animated Player Sprite

Today we added our player sprites and animated them:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 16;
    private int speed = 4;
    
    private GreenfootImage run1r = new GreenfootImage("run1r.png");
    private GreenfootImage run2r = new GreenfootImage("run2r.png");
    private GreenfootImage run3r = new GreenfootImage("run3r.png");
    private GreenfootImage run4r = new GreenfootImage("run4r.png");
    private GreenfootImage run1l = new GreenfootImage("run1l.png");
    private GreenfootImage run2l = new GreenfootImage("run2l.png");
    private GreenfootImage run3l = new GreenfootImage("run3l.png");
    private GreenfootImage run4l = new GreenfootImage("run4l.png");
    private int frame = 1;
    private int animationCounter = 0;
    
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkFall();
        checkKey();
        animationCounter++;
    }   
    
    public void checkKey()
    {
        if(Greenfoot.isKeyDown("space") && jumping == false)
        {
            jump();
        }
        if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
        if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        }
    }
    
    public void moveRight()
    {
        setLocation(getX()+speed, getY());
        if(animationCounter % 4 == 0)
        {
        animateRight();
        }
    }
    
    public void animateRight()
    {
        if(frame == 1)
        {
            setImage(run1r);
        }
        else if(frame == 2)
        {
            setImage(run2r);
        }
        else if(frame == 3)
        {
            setImage(run3r);
        }
        else if(frame == 4)
        {
            setImage(run4r);
            frame = 1;
            return;
        }
        frame++;
    }
    
    public void moveLeft()
    {
        setLocation(getX()-speed, getY());
        if(animationCounter %4 == 0)
        {
        animateLeft();
    }
    }
    
    public void animateLeft()
    {
        if(frame == 1)
        {
            setImage(run1l);
        }
        else if(frame == 2)
        {
            setImage(run2l);
        }
        else if(frame == 3)
        {
            setImage(run3l);
        }
        else if(frame == 4)
        {
            setImage(run4l);
            frame = 1;
            return;
        }
        frame++;
    }
    
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        if(vSpeed <=9)
        {
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
    }
    
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/2) + 5;
        Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);
        if(ground == null)
        {
            jumping = true;
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
    }
    
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
        jumping = false;
    }
    
    public void checkFall()
    {
        if(onGround())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
    
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
    }
}

Friday, January 10, 2014

Platformer: Adding Movement

Now that we have our player "falling" and landing on platforms and jumping, it's time to add some left & right motion.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 16;
    private int speed = 4;
    
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkFall();
        checkKey();
    }   
    
    public void checkKey()
    {
        if(Greenfoot.isKeyDown("space") && jumping == false)
        {
            jump();
        }
        if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
        if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        }
    }
    
    public void moveRight()
    {
        setLocation(getX()+speed, getY());
    }
    
    public void moveLeft()
    {
        setLocation(getX()-speed, getY());
    }
    
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        if(vSpeed <=9)
        {
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
    }
    
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int lookForGround = (int)(spriteHeight/2) + 5;
        Actor ground = getOneObjectAtOffset(0, lookForGround, Platform.class);
        if(ground == null)
        {
            jumping = true;
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
    }
    
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
        jumping = false;
    }
    
    public void checkFall()
    {
        if(onGround())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
    
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
    }
}

Thursday, January 9, 2014

Platformer: Jumping

Today we will add jumping with some faux physics effects.
code here
And the MyWorld code:

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Player player = new Player();
        addObject(player, 441, 32);
        Platform platform01 = new Platform();
        addObject(platform01, 10, 580);
        Platform platform02 = new Platform();
        addObject(platform02, 40, 580);
        Platform platform03 = new Platform();
        addObject(platform03, 70, 580);
        Platform platform04 = new Platform();
        addObject(platform04, 100, 580);
        Platform platform05 = new Platform();
        addObject(platform05, 130, 580);
        Platform platform06 = new Platform();
        addObject(platform06, 160, 580);
        Platform platform07 = new Platform();
        addObject(platform07, 190, 580);
        Platform platform08 = new Platform();
        addObject(platform08, 220, 580);
        Platform platform09 = new Platform();
        addObject(platform09, 250, 580);
        Platform platform10 = new Platform();
        addObject(platform10, 280, 580);
        Platform platform11 = new Platform();
        addObject(platform11, 300, 580);
        Platform platform12 = new Platform();
        addObject(platform12, 330, 580);
        Platform platform13 = new Platform();
        addObject(platform13, 360, 580);
        Platform platform14 = new Platform();
        addObject(platform14, 390, 580);
        Platform platform15 = new Platform();
        addObject(platform15, 420, 580);
        Platform platform16 = new Platform();
        addObject(platform16, 450, 580);
        Platform platform17 = new Platform();
        addObject(platform17, 480, 580);
        Platform platform18 = new Platform();
        addObject(platform18, 510, 580);
        Platform platform19 = new Platform();
        addObject(platform19, 540, 580);
        Platform platform20 = new Platform();
        addObject(platform20, 570, 580);
        Platform platform21 = new Platform();
        addObject(platform21, 600, 580);
        Platform platform22 = new Platform();
        addObject(platform22, 630, 580);
        Platform platform23 = new Platform();
        addObject(platform23, 660, 580);
        Platform platform24 = new Platform();
        addObject(platform24, 690, 580);
        Platform platform25 = new Platform();
        addObject(platform25, 720, 580);
        Platform platform26 = new Platform();
        addObject(platform26, 750, 580);
        Platform platform27 = new Platform();
        addObject(platform27, 780, 580);
        Platform platform28 = new Platform();
        addObject(platform28, 810, 580);
    }
}

Wednesday, January 8, 2014

Platformer: Getting Started

Today we will build a basic platformer game. We will start with a player and a platform, and we will have the player "fall" until it hits the "ground" (or platform).
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    private int vSpeed = 0;
    private int acceleration = 1;

    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkFall();
    }   
    
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        if(vSpeed <=9)
        {
            vSpeed = vSpeed + acceleration;
        }  
    }

    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int lookForGround = (int)(spriteHeight/2) + 5;
        Actor ground = getOneObjectAtOffset(0, lookForGround, Platform.class);
        if(ground == null)
        {
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
    }

    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
    }
    
    public void checkFall()
    {
        if(onGround())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
}