Advertisement
MoksliukasM

Untitled

Mar 24th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.46 KB | None | 0 0
  1. package lt.mchackers.anothercargame.main;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.Toolkit;
  9. import java.awt.event.KeyAdapter;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.image.BufferedImage;
  12. import java.io.BufferedReader;
  13. import java.io.FileNotFoundException;
  14. import java.io.FileReader;
  15. import java.io.IOException;
  16. import java.util.LinkedHashMap;
  17. import java.util.Map;
  18.  
  19. import javax.swing.JPanel;
  20.  
  21. import lt.mchackers.anothercargame.textures.Black;
  22. import lt.mchackers.anothercargame.textures.Gray;
  23.  
  24.  
  25. public class GamePanel extends JPanel implements Runnable
  26. {
  27. private static final long serialVersionUID = 6892533030374996243L;
  28. public static final int WIDTH = 320;
  29. public static final int HEIGHT = 320;
  30.  
  31. int xa = 0;
  32. int x;
  33. int ya = 0;
  34. int y;
  35.  
  36.  
  37. private Thread animator;
  38. private Thread timeThread;
  39.  
  40. BufferedImage backBuffer;
  41.  
  42. private volatile boolean running = false;
  43. private volatile boolean isGameOver = false;
  44. private volatile boolean isUserPaused = false;
  45. private volatile boolean isWindowPaused = false;
  46. public static Map<Coordinates, String> hm = new LinkedHashMap<Coordinates, String>();
  47.  
  48. public BufferedImage gray;
  49. public BufferedImage black;
  50. private Graphics dbg;
  51. private Image dbImage = null;
  52. public int timeSpentInSeconds;
  53.  
  54. private static final int NO_DELAYS_PER_YIELD = 16;
  55. private static final int MAX_FRAME_SKIPS = 5;
  56.  
  57. private static final Color backgroundColor = Color.BLACK;
  58.  
  59. private static long fps = 30;
  60. private static long period = 1000000L * (long) 1000.0 / fps;
  61.  
  62. private static volatile boolean isPainted = false;
  63.  
  64. public GamePanel()
  65. {
  66.  
  67.  
  68. setBackground(backgroundColor);
  69. setPreferredSize(new Dimension(WIDTH, HEIGHT));
  70.  
  71. setFocusable(true);
  72. requestFocus();
  73. readyForPause();
  74.  
  75. addKeyListener(new KeyAdapter()
  76. {
  77. public void keyPressed(KeyEvent e)
  78. {
  79. try{
  80. System.out.println(hm.keySet());
  81. int keyCode = e.getKeyCode();
  82. if ((keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A)
  83. && isPainted && hm.get(new Coordinates(x - 32, y)).equalsIgnoreCase("gray"))
  84. {
  85. x -= 32;
  86. }
  87. if ((keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D)
  88. && isPainted && hm.get(new Coordinates(x + 32, y)).equalsIgnoreCase("gray"))
  89. {
  90. x += 32;
  91. }
  92. if ((keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W)
  93. && isPainted && hm.get(new Coordinates(x, y - 32)).equalsIgnoreCase("gray"))
  94. {
  95. y -= 32;
  96. }
  97. if ((keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S)
  98. && isPainted && hm.get(new Coordinates(x, y + 32)).equalsIgnoreCase("gray"))
  99. {
  100. y += 32;
  101. }
  102. if (x > WIDTH - 32 && isPainted)
  103. {
  104. x = WIDTH - 32;
  105. }
  106. if (y > HEIGHT - 32 && isPainted)
  107. {
  108. y = HEIGHT - 32;
  109. }
  110. if (y < 0 && isPainted)
  111. {
  112. y = 0;
  113. }
  114. if (x < 0 && isPainted)
  115. {
  116. x = 0;
  117. }
  118. isPainted = false;
  119. }
  120. catch (Exception ex)
  121. {
  122. ex.printStackTrace();
  123. }
  124. }
  125. });
  126. }
  127.  
  128. public void addNotify()
  129. {
  130. super.addNotify();
  131. startGame();
  132. }
  133.  
  134. void startGame()
  135. {
  136.  
  137. if (animator == null || !running)
  138. {
  139. animator = new Thread(this);
  140. animator.start();
  141. timeThread = new Thread(new TimeThread());
  142. timeThread.start();
  143. }
  144. }
  145.  
  146. void stopGame()
  147. {
  148. running = false;
  149. }
  150.  
  151. private void readyForPause()
  152. {
  153. addKeyListener(new KeyAdapter()
  154. {
  155. public void keyPressed(KeyEvent e)
  156. {
  157. int keyCode = e.getKeyCode();
  158. if ((keyCode == KeyEvent.VK_ESCAPE) || (keyCode == KeyEvent.VK_Q)
  159. || (keyCode == KeyEvent.VK_END) || (keyCode == KeyEvent.VK_P)
  160. || ((keyCode == KeyEvent.VK_C) && e.isControlDown()))
  161. {
  162. if (!isUserPaused)
  163. setUserPaused(true);
  164. else
  165. setUserPaused(false);
  166. }
  167. }
  168. });
  169. }
  170.  
  171. public void run()
  172. {
  173.  
  174. BufferedReader reader = null;
  175. try {
  176. reader = new BufferedReader(new FileReader("map"));
  177. } catch (FileNotFoundException e1) {
  178. e1.printStackTrace();
  179. }
  180. String line = null;
  181. try {
  182. while ((line = reader.readLine()) != null) {
  183. for(String s : line.split(""))
  184. {
  185. if (s.contains("*"))
  186. {
  187. hm.put(new Coordinates(xa-32, ya), "gray");
  188. }
  189. else if (s.contains("#"))
  190. {
  191. hm.put(new Coordinates(xa-32, ya), "black");
  192. }
  193. if (xa < 320)
  194. {
  195. xa += 32;
  196. }
  197. else
  198. {
  199. ya += 32;
  200. xa = 0;
  201. }
  202.  
  203. //System.out.println(xa);
  204. //System.out.println(ya);
  205. }
  206. }
  207. } catch (IOException e) {
  208. e.printStackTrace();
  209. }
  210. xa = 0;
  211. ya = 0;
  212. long beforeTime, afterTime, timeDiff, sleepTime;
  213. long overSleepTime = 0L;
  214. int noDelays = 0;
  215. long excess = 0L;
  216.  
  217.  
  218. beforeTime = System.nanoTime();
  219.  
  220. running = true;
  221. for (Map.Entry<Coordinates, String> entry : hm.entrySet()) {
  222. System.out.println(entry.getValue().equalsIgnoreCase("gray"));
  223. if (entry.getValue().equalsIgnoreCase("gray"))
  224. {
  225. createImage(WIDTH, HEIGHT).getGraphics().setColor(Color.WHITE);
  226. createImage(WIDTH, HEIGHT).getGraphics().drawRect(entry.getKey().getX(), entry.getKey().getY(), 32, 32);
  227. x = entry.getKey().getX();
  228. y = entry.getKey().getY();
  229. break;
  230. }
  231. }
  232. System.out.println(x);
  233.  
  234. System.out.println(y);
  235. while (running)
  236. {
  237. requestFocus();
  238. gameUpdate();
  239. gameRender();
  240. paintScreen();
  241.  
  242. afterTime = System.nanoTime();
  243.  
  244. timeDiff = afterTime - beforeTime;
  245. sleepTime = (period - timeDiff) - overSleepTime;
  246.  
  247. if (sleepTime > 0)
  248. {
  249. try
  250. {
  251. Thread.sleep(sleepTime / 1000000L);
  252. }
  253. catch (InterruptedException e)
  254. {
  255. }
  256.  
  257. overSleepTime = (System.nanoTime() - afterTime - sleepTime);
  258. }
  259. else
  260. {
  261. excess -= sleepTime;
  262. overSleepTime = 0L;
  263.  
  264. if (++noDelays >= NO_DELAYS_PER_YIELD)
  265. {
  266. Thread.yield();
  267. noDelays = 0;
  268. }
  269. }
  270.  
  271. beforeTime = System.nanoTime();
  272.  
  273. int skips = 0;
  274.  
  275. while ((excess > period) && (skips < MAX_FRAME_SKIPS))
  276. {
  277. excess -= period;
  278. gameUpdate();
  279. skips++;
  280. }
  281.  
  282. isPainted = true;
  283. }
  284. System.exit(0);
  285. }
  286.  
  287. private void gameUpdate()
  288. {
  289. if (!isUserPaused && !isWindowPaused && !isGameOver)
  290. {
  291. }
  292. }
  293.  
  294. private void gameRender()
  295. {
  296. if (dbImage == null)
  297. {
  298. dbImage = createImage(WIDTH, HEIGHT);
  299. if (dbImage == null)
  300. {
  301. System.out.println("Image is null.");
  302. return;
  303. }
  304. else
  305. dbg = dbImage.getGraphics();
  306. }
  307.  
  308. dbg.setColor(backgroundColor);
  309. dbg.fillRect(0, 0, WIDTH, HEIGHT);
  310.  
  311. BufferedReader reader = null;
  312. try {
  313. reader = new BufferedReader(new FileReader("map"));
  314. } catch (FileNotFoundException e1) {
  315. e1.printStackTrace();
  316. }
  317. String line = null;
  318. try {
  319.  
  320. while ((line = reader.readLine()) != null) {
  321. for(String s : line.split(""))
  322. {
  323. if (s.contains("*"))
  324. {
  325. dbg.drawImage(Gray.getGray(), xa-32, ya, null);
  326. }
  327. else if (s.contains("#"))
  328. {
  329. dbg.drawImage(Black.getBlack(), xa-32, ya, null);
  330. }
  331. if (xa < 320)
  332. {
  333. xa += 32;
  334. }
  335. else
  336. {
  337. ya += 32;
  338. xa = 0;
  339. }
  340. //System.out.println(xa);
  341. //System.out.println(ya);
  342. }
  343. }
  344. } catch (IOException e) {
  345. e.printStackTrace();
  346. }
  347. xa = 0;
  348. ya = 0;
  349. dbg.setColor(Color.WHITE);
  350. dbg.fillRect(x,y, 32,32);
  351. if (isGameOver)
  352. gameOverMessage(dbg);
  353. }
  354.  
  355. private void gameOverMessage(Graphics g)
  356. {
  357. g.setColor(new Color(33, 33, 33));
  358. g.setFont(new Font("Comic Sans MS", Font.BOLD, 45));
  359. g.drawString("Game Over!", 265, 250);
  360. g.setColor(new Color(0, 64, 0));
  361. g.setFont(new Font("Comic Sans MS", Font.BOLD, 40));
  362. }
  363.  
  364. private void paintScreen()
  365. {
  366. Graphics g;
  367.  
  368. try
  369. {
  370. g = this.getGraphics();
  371. if ((g != null) && (dbImage != null))
  372. g.drawImage(dbImage, 0, 0, null);
  373. Toolkit.getDefaultToolkit().sync();
  374. g.dispose();
  375. }
  376. catch (Exception e)
  377. {
  378. System.out.println("Graphics context error : " + e);
  379. }
  380. }
  381.  
  382. public void setWindowPaused(boolean isPaused)
  383. {
  384. isWindowPaused = isPaused;
  385. }
  386.  
  387. public void setUserPaused(boolean isPaused)
  388. {
  389. isUserPaused = isPaused;
  390. }
  391.  
  392. public class TimeThread implements Runnable
  393. {
  394. public void run()
  395. {
  396. while (!isUserPaused && !isWindowPaused && !isGameOver)
  397. {
  398. timeSpentInSeconds += 1;
  399. try
  400. {
  401. Thread.sleep(1000);
  402. }
  403. catch (InterruptedException e)
  404. {
  405. }
  406. }
  407. }
  408. }
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement