Tuesday, February 11, 2014

Scrolling Mario Platformer (Part 3)

Now that we have a Mario that will run back and forth, it's time to do some fine-tuning. Here we will make sure the player isn't trying to move if they are pressing both the Left AND Right keys:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Mario here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mario extends Mover
{
    private final GreenfootImage idleRight = new GreenfootImage("mario-idle.gif");
    private final GreenfootImage walk1Right = new GreenfootImage("mario-walk1.gif");
    private final GreenfootImage walk2Right = new GreenfootImage("mario-walk2.gif");
    private final GreenfootImage walk3Right = new GreenfootImage("mario-walk3.gif");
    
    private final GreenfootImage idleLeft = new GreenfootImage(idleRight);
    private final GreenfootImage walk1Left = new GreenfootImage(walk1Right);
    private final GreenfootImage walk2Left = new GreenfootImage(walk2Right);
    private final GreenfootImage walk3Left = new GreenfootImage(walk3Right);
    
    private boolean facingRight;
    private boolean isKeyPressed;
    
    public Mario()
    {
        this.speed = 7;
        this.facingRight = true;
        
        setImage(idleRight);
        
        idleRight.scale(38,50);
        walk1Right.scale(48,50);
        walk2Right.scale(36,50);
        walk3Right.scale(44,50);
        
        idleLeft.scale(38,50);
        walk1Left.scale(48,50);
        walk2Left.scale(36,50);
        walk3Left.scale(44,50);
        
        idleLeft.mirrorHorizontally();
        walk1Left.mirrorHorizontally();
        walk2Left.mirrorHorizontally();
        walk3Left.mirrorHorizontally();
    }

    /**
     * Act - do whatever the Mario wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKeys();
    }    
    
    /**
     * Checks keys and responds with an action
     */
    private void checkKeys()
    {
        isKeyPressed = false;
        
        if (Greenfoot.isKeyDown("right") && Greenfoot.isKeyDown("left"))
        {
            stopWalking();
        }
        else if (Greenfoot.isKeyDown("right"))
        {
            animationCounter ++;
            walkRight();
            isKeyPressed = true;
            facingRight = true;
        }
        else if (Greenfoot.isKeyDown("left"))
        {
            animationCounter ++;
            walkLeft();
            isKeyPressed = true;
        }
        if (!isKeyPressed)
        {
            stopWalking();
        }
    }
    
    /**
     * Moves Mario right and applies the right-facing images.
     */
    private void walkRight()
    {
        moveRight();
        if(animationCounter < 4)
        {
            setImage(idleRight);
        }
        else if(animationCounter < 8)
        {
            setImage(walk1Right);
        }
        else if(animationCounter < 12)
        {
            setImage(walk2Right);
        }
        else if(animationCounter < 16)
        {
            setImage(walk3Right);
            animationCounter = 0;
            return;
        }
    }
    
    /**
     * Moves Mario left and applies the left-facing images.
     */
    private void walkLeft()
    {
        moveLeft();
        if(animationCounter < 4)
        {
            setImage(idleLeft);
        }
        else if(animationCounter < 8)
        {
            setImage(walk1Left);
        }
        else if(animationCounter < 12)
        {
            setImage(walk2Left);
        }
        else if(animationCounter < 16)
        {
            setImage(walk3Left);
            animationCounter = 0;
            return;
        }
    }
    
    /**
     * Stop Mario from walking and apply correct idle image.
     */
    private void stopWalking()
    {
    }
}
And the Mover class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Mover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mover extends Actor
{
    protected int speed;
    protected int animationCounter;
    
    /**
     * Move an object to the right
     */
    protected void moveRight()
    {
        setLocation (getX() + speed, getY());
    }
    /**
     * Move an object to the left
     */
    protected void moveLeft()
    {
        setLocation (getX() - speed, getY());
    }
}

No comments:

Post a Comment