Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.38 KB | None | 0 0
  1. import kareltherobot.*;
  2. import java.awt.KeyboardFocusManager;
  3. import java.awt.event.KeyEvent;
  4. import java.awt.KeyEventDispatcher;
  5. import javax.swing.KeyStroke;
  6. import java.awt.Color;
  7. import java.util.Timer;
  8. import java.util.TimerTask;
  9.  
  10.  
  11. public class PacmanMovement1 extends Robot implements Runnable
  12. {
  13. public PacmanMovement1(int s, int a, Direction d, int b, Color badgeColor)
  14. {
  15. super(s, a, d, b, badgeColor);
  16. World.setupThread(this);
  17. pacAv = a;
  18. pacStr = s;
  19. waka = new EasySound("pacman_chomp.wav");
  20. death = new EasySound("pacman_death.wav");
  21. inv = new EasySound("DING.wav");
  22. intro = new EasySound("pacman_beginning.wav");
  23. }
  24.  
  25. private static EasySound inv;
  26. private static EasySound death;
  27. private static EasySound waka;
  28. private static EasySound intro;
  29. public static int pacAv;
  30. public static int pacStr;
  31. private char d = 'e';
  32. private int dx;
  33. private int dy;
  34. private static int x = 1;
  35. private static int y = 1;
  36. private int i;
  37. private static int beeperCount;
  38. private Timer timer;
  39. boolean pacmanDied = false;
  40. boolean invincible = false;
  41.  
  42.  
  43. /*This code needs to be accessed by the world creator
  44. in order to know how to redraw pacman.
  45. in order to differntiate between beepers, pacman
  46. should test if a certain beeper's (within the while loop)
  47. image location is the location of the special beeper, setting
  48. c = 1 (which is otherwise 0),
  49. which can then be accessed by the ghost creator in order
  50. to change the behavior of the ghosts and by the world creator in order to
  51. know when to change the photo of the ghosts.
  52. */
  53.  
  54. public void turnAround()
  55. {
  56. turnLeft();
  57. turnLeft();
  58. }
  59.  
  60. public void turnRight()
  61. {
  62. turnLeft();
  63. turnLeft();
  64. turnLeft();
  65. }
  66.  
  67. public void faceWest()
  68. {
  69. while(!facingWest())
  70. {
  71. World.setDelay(0);
  72. turnLeft();
  73. World.setDelay(10);
  74. }
  75. }
  76.  
  77. public void faceNorth()
  78. {
  79. while(!facingNorth())
  80. {
  81. World.setDelay(0);
  82. turnLeft();
  83. World.setDelay(10);
  84. }
  85. }
  86.  
  87. public void faceSouth()
  88. {
  89. while(!facingSouth())
  90. {
  91. World.setDelay(0);
  92. turnLeft();
  93. World.setDelay(10);
  94. }
  95. }
  96.  
  97. public void faceEast()
  98. {
  99. while(!facingEast())
  100. {
  101. World.setDelay(0);
  102. turnLeft();
  103. World.setDelay(10);
  104. }
  105. }
  106.  
  107. public void spin()
  108. {
  109. for(int x = 15; x > 0; x--)
  110. {
  111. turnLeft();
  112. }
  113. }
  114.  
  115. public boolean isWest()
  116. {
  117. if(facingWest())
  118. {
  119. return true;
  120. }
  121. else
  122. return false;
  123. }
  124.  
  125. public boolean isEast()
  126. {
  127. if(facingEast())
  128. {
  129. return true;
  130. }
  131. else
  132. return false;
  133. }
  134.  
  135. public boolean isSouth()
  136. {
  137. if(facingSouth())
  138. return true;
  139. else
  140. return false;
  141. }
  142.  
  143. public boolean isNorth()
  144. {
  145. if(facingNorth())
  146. return true;
  147. else
  148. return false;
  149. }
  150.  
  151. public boolean invincible()
  152. {
  153. if(nextToABeeper())
  154. {
  155. pickBeeper();
  156. beeperCount++;
  157. i = 15;
  158. return true;
  159.  
  160. }
  161. else
  162. {
  163. return false;
  164. }
  165. }
  166.  
  167. /*public boolean eastIsClear()
  168. {
  169. World.setDelay(0);
  170. faceEast();
  171. if(frontIsClear())
  172. {
  173. turnLeft();
  174. World.setDelay(10);
  175. return true;
  176. }
  177. else
  178. {
  179. turnLeft();
  180. World.setDelay(10);
  181. return false;
  182. }
  183.  
  184. }*/
  185.  
  186.  
  187. public void run()
  188. {
  189. {
  190. findStr();
  191. findAv();
  192. // Setup Keyboard Focus Manager
  193. KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
  194. focusManager.addKeyEventDispatcher(
  195. new KeyEventDispatcher() {
  196. @Override
  197. public boolean dispatchKeyEvent(KeyEvent e) {
  198. String key = KeyStroke.getKeyStrokeForEvent(e).toString();
  199. if(key.equals("pressed A")||key.equals("pressed LEFT")) //moves pacman west
  200. {
  201. faceWest();
  202. }
  203. if(key.equals("pressed W")||key.equals("pressed UP")) //moves pacman north
  204. {
  205. faceNorth();
  206. }
  207. if(key.equals("pressed S")||key.equals("pressed DOWN")) //moves pacman south
  208. {
  209. faceSouth();
  210. }
  211. if(key.equals("pressed D")||key.equals("pressed RIGHT")) //moves pacman east
  212. {
  213. //if(rightIsClear())
  214. faceEast();
  215. }
  216. if (key.equals("pressed 1"))
  217. {
  218. World.setNeutroniumColor(Color.BLUE);
  219. World.setStreetColor(Color.BLACK);
  220. World.setWorldColor(Color.BLACK);
  221. World.setBeeperColor(Color.YELLOW);
  222. }
  223. if (key.equals("pressed 2")) //changes world color
  224. {
  225. World.setNeutroniumColor(Color.CYAN);
  226. World.setStreetColor(Color.RED);
  227. World.setWorldColor(Color.RED);
  228. World.setBeeperColor(Color.BLACK);
  229. }
  230. if (key.equals("pressed 3"))
  231. {
  232. World.setNeutroniumColor(Color.BLACK);
  233. World.setStreetColor(Color.GRAY);
  234. World.setWorldColor(Color.GRAY);
  235. World.setBeeperColor(Color.WHITE);
  236. }
  237. if(key.equals("pressed K")) //pacman dies(test only)
  238. {
  239. pacmanDied=true;
  240. }
  241.  
  242. return false;
  243. }
  244. });
  245.  
  246. int superpacmanCounter=0;
  247. int beeperCounter=0;
  248. boolean pacmanDied = false;
  249. int invincibleCounter = 0;
  250. while(!pacmanDied)
  251. {
  252. invincible = false;
  253. if(nextToARobot() && invincible == false)
  254. {
  255. pacmanDied = true;
  256. death.play();
  257. spin();
  258. turnOff();
  259. }
  260. if(nextToABeeper())
  261. {
  262. World.setDelay(0);
  263. pickBeeper();
  264. waka.play();
  265. beeperCount++;
  266. World.setDelay(10);
  267. }
  268. if(nextToABeeper())
  269. {
  270. World.setDelay(0);
  271. pickBeeper();
  272. waka.play();
  273. invincible=true;
  274. beeperCount++;
  275. World.setDelay(10);
  276. }
  277. if(frontIsClear())
  278. {
  279. move();
  280. findStr();
  281. findAv();
  282. if(invincible==true)
  283. {
  284. inv.play();
  285. invincibleCounter = 15;
  286. for(;invincibleCounter > 0; invincibleCounter--)
  287. {
  288. if(nextToABeeper())
  289. {
  290. World.setDelay(0);
  291. pickBeeper();
  292. waka.play();
  293. beeperCount++;
  294. World.setDelay(10);
  295. }
  296. if(nextToABeeper())
  297. {
  298. World.setDelay(0);
  299. pickBeeper();
  300. waka.play();
  301. invincible=true;
  302. beeperCount++;
  303. World.setDelay(10);
  304. }
  305. if(frontIsClear())
  306. {
  307. move();
  308. findStr();
  309. findAv();
  310. }
  311. }
  312. }
  313. }
  314.  
  315. //System.out.println("Pacman Died");
  316.  
  317. }
  318. }
  319. }
  320. public int findStr()
  321. {
  322. return pacStr;
  323.  
  324. }
  325. public int findAv()
  326. {
  327. return pacAv;
  328. }
  329. public static void main(String [] args)
  330. {
  331. World.setVisible(true);
  332. World.setDelay(10);
  333. World.readWorld("pacmanworld");
  334. World.setNeutroniumColor(Color.BLUE);
  335. World.setStreetColor(Color.BLACK);
  336. World.setWorldColor(Color.BLACK);
  337. World.setBeeperColor(Color.YELLOW);
  338. World.setTrace(false);
  339. World.showSpeedControl(true);
  340. PacmanMovement1 p = new PacmanMovement1(5, 4, East, 0, Color.BLACK);
  341.  
  342.  
  343. //15, 15-18
  344. Blinky blinky = new Blinky (15, 15, North, 0, Color.RED);
  345. Pinky pinky = new Pinky (15, 16, North, 0, Color.PINK);
  346. Inky inky = new Inky (15, 17, North, 0, Color.BLUE);
  347. Clyde clyde = new Clyde (15, 18, North, 0, Color.ORANGE);
  348. intro.play();
  349. }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement