import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; // allows creation of lists using Java API /** * Write a description of class Pinball here. * * @author (your name) * @version (a version number or a date) */ public class Pinball extends Actor { public static final int EAST = 0; public static final int NORTH = 1; public static final int WEST = 2; public static final int SOUTH = 3; private int direction; private int lives; private int score; private int pinballId; public Pinball(int initialDirection, int initialLives, int id) { pinballId = id; setDirection(initialDirection); setRotation(0); lives = initialLives; score = 0; } /** * Act - do whatever the Pinball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { updateLives(1); if(facingWall()) { int wallCost = getWallCost(); updateLives(wallCost); if(lives <= 0) { removeBall(); return; } turnRight(); } else if (facingBumper()) { int bumperCost = getBumperCost(); updateLives(bumperCost); if(lives <= 0) { removeBall(); return; } int bumperValue = getBumperValue(); updateScore(bumperValue); turnRight(); } else { move(); updateLives(1); if(lives <= 0) { removeBall(); return; } } } /** * Updates current lives of pinballs. */ private void updateLives(int cost) { lives = lives - cost; } /** * Updates score for pinballs. */ private void updateScore(int value) { score = score + value; } /** * Removes ball from world after it has lost all its lives. */ private void removeBall() { PBWorld pinworld = (PBWorld)getWorld(); pinworld.totalScore(score); reportValue(); getWorld().removeObject(this); } /** * Reports values for pinballs that have lost all their lives in a popup window. */ private void reportValue() { System.out.println(score); } /** * Moves the object according to parameters. * If facing Wall or Bumper, the Pinball does not move. */ public void move() { if(facingWall() || facingBumper()) { return; } switch(direction) { case SOUTH : setLocation(getX(), getY() + 1); break; case EAST : setLocation(getX() + 1, getY()); break; case NORTH : setLocation(getX(), getY() - 1); break; case WEST : setLocation(getX() - 1, getY()); break; } } /** * Determines if facing a wall. */ public boolean facingWall() { World myWorld = getWorld(); int x = getX(); int y = getY(); switch(direction) { case SOUTH : y++; break; case EAST : x++; break; case NORTH : y--; break; case WEST : x--; break; } // test for wall List wall = getWorld().getObjectsAt(x, y, Wall.class); if (wall.size() > 0) { return true; } return false; } /** * Returns cost of hitting a wall. */ private int getWallCost() { World myWorld = getWorld(); int x = getX(); int y = getY(); switch(direction) { case SOUTH : y++; break; case EAST : x++; break; case NORTH : y--; break; case WEST : x--; break; } List wall = getWorld().getObjectsAt(x, y, Wall.class); if(wall.size() > 0) { Wall wallObj = (Wall)wall.get(0); return wallObj.getCost(); } return 0; } /** * Determines if facing a bumper. */ public boolean facingBumper() { World myWorld = getWorld(); int x = getX(); int y = getY(); switch(direction) { case SOUTH : y++; break; case EAST : x++; break; case NORTH : y--; break; case WEST : x--; break; } // test for bumper List bumper = getWorld().getObjectsAt(x, y, Bumper.class); if(bumper.size() > 0) { return true; } return false; } /** * Returns cost of hitting bumper. */ private int getBumperCost() { World myWorld = getWorld(); int x = getX(); int y = getY(); switch(direction) { case SOUTH : y++; break; case EAST : x++; break; case NORTH : y--; break; case WEST : x--; break; } List bumper = getWorld().getObjectsAt(x, y, Bumper.class); if(bumper.size() > 0) { Bumper bumperObj = (Bumper)bumper.get(0); return bumperObj.getCost(); } return 0; } /** * Returns value of the bumper. */ private int getBumperValue() { World myWorld = getWorld(); int x = getX(); int y = getY(); switch(direction) { case SOUTH : y++; break; case EAST : x++; break; case NORTH : y--; break; case WEST : x--; break; } List bumper = getWorld().getObjectsAt(x, y, Bumper.class); if(bumper.size() > 0) { Bumper bumperObj = (Bumper)bumper.get(0); bumperObj.getValue(); } return 0; } /** * Turns to the left. */ public void turnLeft() { switch(direction) { case SOUTH : setDirection(EAST); setImage("gold-ball3.png"); setRotation(90); break; case EAST : setDirection(NORTH); setImage("Gold-ball2.png"); setRotation(0); break; case NORTH : setDirection(WEST); setImage("gold-ball5.png"); setRotation(270); break; case WEST : setDirection(SOUTH); setImage("gold-ball4.png"); setRotation(180); break; } } /** * Turns to the right. */ public void turnRight() { turnLeft(); turnLeft(); turnLeft(); } public void setDirection(int direction) { setRotation(1); } }

