Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.KeyEvent;
- import java.awt.*;
- import java.io.PrintStream;
- import kareltherobot.*;
- import javax.swing.KeyStroke;
- public class Pacman extends SuperKarel implements Runnable
- {
- public int up;
- public int down;
- public int left;
- public int right;
- public int beeperCount;
- public static int atAvenue;
- public static int atStreet;
- public boolean isKill;
- public int isSuper;
- public int lives = 3;
- public Pacman(int street, int avenue, Direction direction, int beepers)
- {
- super(street, avenue, direction, beepers);
- World.setupThread(this);
- atAvenue = avenue;
- atStreet = street;
- }
- public void run()
- {
- startPacman();
- wakawaka = new EasySound("wakawaka.wav");
- movepacman();
- }
- private void movepacman()
- {
- while (true)
- {
- if (left > 1)
- {
- faceWest();
- if (frontIsClear())
- {
- grabBeeper();
- left--;
- atAvenue--;
- superTimer();
- move();
- }
- }
- else if (right > 1)
- {
- faceEast();
- if (frontIsClear())
- {
- grabBeeper();
- right--;
- atAvenue++;
- superTimer();
- move();
- }
- }
- else if (up > 1)
- {
- faceNorth();
- if (frontIsClear())
- {
- grabBeeper();
- up--;
- atStreet++;
- superTimer();
- move();
- }
- }
- else if (down > 1)
- {
- faceSouth();
- if (frontIsClear())
- {
- grabBeeper();
- down--;
- atStreet--;
- superTimer();
- move();
- }
- }
- if ((nextToARobot()) && isSuper<1)
- {
- if (lives > 0) {
- lives--;
- turnOff();
- //teleports back
- }
- else
- {
- System.out.println("You lose.");
- turnOff();
- }
- }
- System.out.println("" + atStreet + " " +atAvenue + " " + beeperCount);
- if (beeperCount > 338)
- break;
- }
- }
- private void superTimer()
- {
- if(isSuper>0)
- {
- isSuper--;
- }
- }
- private void grabBeeper()
- {
- World.setDelay(0);
- if (nextToABeeper())
- {
- pickBeeper();
- eatingSound();
- beeperCount++;
- if (nextToABeeper())
- {
- pickBeeper();
- isSuper = 300; //Number of moves Pacman is invincible for
- }
- //No witches detected
- }
- World.setDelay(5);
- }
- private void eatingSound()
- {
- wakawaka.play();
- }
- /*
- public void warp()
- {
- if(atAvenue==26 && atStreet==17)
- {
- turnOff();
- Pacman = new Pacman(17, 2, East, beeperCount);
- Pacman.setVisible(true);
- }
- else if(atAvenue==2 && atStreet==17)
- {
- turnOff();
- Pacman = new Pacman(17, 26, West, beeperCount);
- Pacman.setVisible(true);
- }
- }
- */
- public void startPacman()
- {
- KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
- focusManager.addKeyEventDispatcher(
- new KeyEventDispatcher()
- {
- @Override
- public boolean dispatchKeyEvent(KeyEvent e)
- {
- {
- if (e.getKeyCode() == 38) {
- up = 25;
- left = 0;
- down = 0;
- right = 0;
- }
- else if (e.getKeyCode() == 37) {
- up = 0;
- left = 25;
- down = 0;
- right = 0;
- }
- else if (e.getKeyCode() == 40) {
- up = 0;
- left = 0;
- down = 25;
- right = 0;
- }
- else if (e.getKeyCode() == 39) {
- up = 0;
- left = 0;
- down = 0;
- right = 25;
- }
- return false;
- }
- }
- });
- }
- public static void main(String[] args)
- {
- World.readWorld("pacworldfixedv2.kwld");
- World.setDelay(5);
- World.setTrace(false);
- World.showSpeedControl(true);
- World.setVisible(true);
- Pacman first = new Pacman(11, 14, North, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement