Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kareltherobot.*;
- import java.awt.KeyboardFocusManager;
- import java.awt.event.KeyEvent;
- import java.awt.KeyEventDispatcher;
- import javax.swing.KeyStroke;
- import java.awt.Color;
- import java.util.Timer;
- import java.util.TimerTask;
- public class PacmanMovement1 extends Robot implements Runnable
- {
- public PacmanMovement1(int s, int a, Direction d, int b, Color badgeColor)
- {
- super(s, a, d, b, badgeColor);
- World.setupThread(this);
- pacAv = a;
- pacStr = s;
- waka = new EasySound("pacman_chomp.wav");
- death = new EasySound("pacman_death.wav");
- inv = new EasySound("DING.wav");
- intro = new EasySound("pacman_beginning.wav");
- }
- private static EasySound inv;
- private static EasySound death;
- private static EasySound waka;
- private static EasySound intro;
- public static int pacAv;
- public static int pacStr;
- private char d = 'e';
- private int dx;
- private int dy;
- private static int x = 1;
- private static int y = 1;
- private int i;
- private static int beeperCount;
- private Timer timer;
- boolean pacmanDied = false;
- boolean invincible = false;
- /*This code needs to be accessed by the world creator
- in order to know how to redraw pacman.
- in order to differntiate between beepers, pacman
- should test if a certain beeper's (within the while loop)
- image location is the location of the special beeper, setting
- c = 1 (which is otherwise 0),
- which can then be accessed by the ghost creator in order
- to change the behavior of the ghosts and by the world creator in order to
- know when to change the photo of the ghosts.
- */
- public void turnAround()
- {
- turnLeft();
- turnLeft();
- }
- public void turnRight()
- {
- turnLeft();
- turnLeft();
- turnLeft();
- }
- public void faceWest()
- {
- while(!facingWest())
- {
- World.setDelay(0);
- turnLeft();
- World.setDelay(10);
- }
- }
- public void faceNorth()
- {
- while(!facingNorth())
- {
- World.setDelay(0);
- turnLeft();
- World.setDelay(10);
- }
- }
- public void faceSouth()
- {
- while(!facingSouth())
- {
- World.setDelay(0);
- turnLeft();
- World.setDelay(10);
- }
- }
- public void faceEast()
- {
- while(!facingEast())
- {
- World.setDelay(0);
- turnLeft();
- World.setDelay(10);
- }
- }
- public void spin()
- {
- for(int x = 15; x > 0; x--)
- {
- turnLeft();
- }
- }
- public boolean isWest()
- {
- if(facingWest())
- {
- return true;
- }
- else
- return false;
- }
- public boolean isEast()
- {
- if(facingEast())
- {
- return true;
- }
- else
- return false;
- }
- public boolean isSouth()
- {
- if(facingSouth())
- return true;
- else
- return false;
- }
- public boolean isNorth()
- {
- if(facingNorth())
- return true;
- else
- return false;
- }
- public boolean invincible()
- {
- if(nextToABeeper())
- {
- pickBeeper();
- beeperCount++;
- i = 15;
- return true;
- }
- else
- {
- return false;
- }
- }
- /*public boolean eastIsClear()
- {
- World.setDelay(0);
- faceEast();
- if(frontIsClear())
- {
- turnLeft();
- World.setDelay(10);
- return true;
- }
- else
- {
- turnLeft();
- World.setDelay(10);
- return false;
- }
- }*/
- public void run()
- {
- {
- findStr();
- findAv();
- // Setup Keyboard Focus Manager
- KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
- focusManager.addKeyEventDispatcher(
- new KeyEventDispatcher() {
- @Override
- public boolean dispatchKeyEvent(KeyEvent e) {
- String key = KeyStroke.getKeyStrokeForEvent(e).toString();
- if(key.equals("pressed A")||key.equals("pressed LEFT")) //moves pacman west
- {
- faceWest();
- }
- if(key.equals("pressed W")||key.equals("pressed UP")) //moves pacman north
- {
- faceNorth();
- }
- if(key.equals("pressed S")||key.equals("pressed DOWN")) //moves pacman south
- {
- faceSouth();
- }
- if(key.equals("pressed D")||key.equals("pressed RIGHT")) //moves pacman east
- {
- //if(rightIsClear())
- faceEast();
- }
- if (key.equals("pressed 1"))
- {
- World.setNeutroniumColor(Color.BLUE);
- World.setStreetColor(Color.BLACK);
- World.setWorldColor(Color.BLACK);
- World.setBeeperColor(Color.YELLOW);
- }
- if (key.equals("pressed 2")) //changes world color
- {
- World.setNeutroniumColor(Color.CYAN);
- World.setStreetColor(Color.RED);
- World.setWorldColor(Color.RED);
- World.setBeeperColor(Color.BLACK);
- }
- if (key.equals("pressed 3"))
- {
- World.setNeutroniumColor(Color.BLACK);
- World.setStreetColor(Color.GRAY);
- World.setWorldColor(Color.GRAY);
- World.setBeeperColor(Color.WHITE);
- }
- if(key.equals("pressed K")) //pacman dies(test only)
- {
- pacmanDied=true;
- }
- return false;
- }
- });
- int superpacmanCounter=0;
- int beeperCounter=0;
- boolean pacmanDied = false;
- int invincibleCounter = 0;
- while(!pacmanDied)
- {
- invincible = false;
- if(nextToARobot() && invincible == false)
- {
- pacmanDied = true;
- death.play();
- spin();
- turnOff();
- }
- if(nextToABeeper())
- {
- World.setDelay(0);
- pickBeeper();
- waka.play();
- beeperCount++;
- World.setDelay(10);
- }
- if(nextToABeeper())
- {
- World.setDelay(0);
- pickBeeper();
- waka.play();
- invincible=true;
- beeperCount++;
- World.setDelay(10);
- }
- if(frontIsClear())
- {
- move();
- findStr();
- findAv();
- if(invincible==true)
- {
- inv.play();
- invincibleCounter = 15;
- for(;invincibleCounter > 0; invincibleCounter--)
- {
- if(nextToABeeper())
- {
- World.setDelay(0);
- pickBeeper();
- waka.play();
- beeperCount++;
- World.setDelay(10);
- }
- if(nextToABeeper())
- {
- World.setDelay(0);
- pickBeeper();
- waka.play();
- invincible=true;
- beeperCount++;
- World.setDelay(10);
- }
- if(frontIsClear())
- {
- move();
- findStr();
- findAv();
- }
- }
- }
- }
- //System.out.println("Pacman Died");
- }
- }
- }
- public int findStr()
- {
- return pacStr;
- }
- public int findAv()
- {
- return pacAv;
- }
- public static void main(String [] args)
- {
- World.setVisible(true);
- World.setDelay(10);
- World.readWorld("pacmanworld");
- World.setNeutroniumColor(Color.BLUE);
- World.setStreetColor(Color.BLACK);
- World.setWorldColor(Color.BLACK);
- World.setBeeperColor(Color.YELLOW);
- World.setTrace(false);
- World.showSpeedControl(true);
- PacmanMovement1 p = new PacmanMovement1(5, 4, East, 0, Color.BLACK);
- //15, 15-18
- Blinky blinky = new Blinky (15, 15, North, 0, Color.RED);
- Pinky pinky = new Pinky (15, 16, North, 0, Color.PINK);
- Inky inky = new Inky (15, 17, North, 0, Color.BLUE);
- Clyde clyde = new Clyde (15, 18, North, 0, Color.ORANGE);
- intro.play();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement