MoksliukasM

Trailing..

Mar 24th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 KB | None | 0 0
  1. package lt.mchackers.gametest.main;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Insets;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.image.BufferedImage;
  7. import java.io.BufferedReader;
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileReader;
  11. import java.io.IOException;
  12.  
  13. import javax.imageio.ImageIO;
  14. import javax.swing.JFrame;
  15.  
  16. import lt.mchackers.gametest.handlers.InputHandler;
  17.  
  18. /**
  19. * Main class for the game
  20. */
  21. public class Main extends JFrame
  22. {
  23. private static final long serialVersionUID = -828018325337767157L;
  24. boolean isRunning = true;
  25. int fps = 30;
  26. int windowWidth = 320;
  27. int windowHeight = 320;
  28. int speed;
  29.  
  30. BufferedImage backBuffer;
  31. Insets insets;
  32. InputHandler input;
  33.  
  34. int x = 0;
  35. int y = 0;
  36. int xa = 0;
  37. int ya = 0;
  38. Coordinates coords = new Coordinates(0, 0);
  39.  
  40. public static void main(String[] args)
  41. { Main game = new Main();
  42. game.run();
  43. System.exit(0);
  44. }
  45.  
  46. /**
  47. * This method starts the game and runs it in a loop
  48. */
  49. public void run()
  50. {
  51. initialize();
  52.  
  53. while(isRunning)
  54. {
  55.  
  56. long time = System.currentTimeMillis();
  57.  
  58. update();
  59. draw();
  60.  
  61. // delay for each frame - time it took for one frame
  62. time = (1000 / fps) - (System.currentTimeMillis() - time);
  63.  
  64. if (time > 0)
  65. {
  66. try
  67. {
  68. Thread.sleep(time);
  69. }
  70. catch(Exception e){}
  71. }
  72. }
  73.  
  74. setVisible(false);
  75. }
  76.  
  77. /**
  78. * This method will set up everything need for the game to run
  79. */
  80. void initialize()
  81. {
  82. setTitle("Game Tutorial");
  83. setSize(windowWidth, windowHeight);
  84. setResizable(false);
  85. setDefaultCloseOperation(EXIT_ON_CLOSE);
  86. setVisible(true);
  87.  
  88. insets = getInsets();
  89. setSize(insets.left + windowWidth + insets.right,
  90. insets.top + windowHeight + insets.bottom);
  91.  
  92. backBuffer = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);
  93. input = new InputHandler(this);
  94. Graphics bbg = backBuffer.getGraphics();
  95. BufferedReader reader = null;
  96. try {
  97. reader = new BufferedReader(new FileReader("map"));
  98. } catch (FileNotFoundException e1) {
  99. e1.printStackTrace();
  100. }
  101. String line = null;
  102. try {
  103. BufferedImage gray = ImageIO.read(new File("gray.png"));
  104. BufferedImage black = ImageIO.read(new File("black.png"));
  105. while ((line = reader.readLine()) != null) {
  106. for(String s : line.split(""))
  107. {
  108. if (s.contains("*"))
  109. {
  110. bbg.drawImage(gray, xa-32, ya, null);
  111. }
  112. else if (s.contains("#"))
  113. {
  114. bbg.drawImage(black, xa-32, ya, null);
  115. }
  116. if (xa < 320)
  117. {
  118. xa += 32;
  119. }
  120. else
  121. {
  122. ya += 32;
  123. xa = 0;
  124. }
  125. System.out.println(xa);
  126. System.out.println(ya);
  127. }
  128. }
  129. } catch (IOException e) {
  130. e.printStackTrace();
  131. }
  132.  
  133. }
  134.  
  135. /**
  136. * This method will check for input, move things
  137. * around and check for win conditions, etc
  138. */
  139. void update()
  140. {
  141. if (input.isKeyDown(KeyEvent.VK_NUMPAD0))
  142. {
  143. speed -= 1;
  144. }
  145. if (input.isKeyDown(KeyEvent.VK_NUMPAD1))
  146. {
  147. speed += 1;
  148. }
  149. if (input.isKeyDown(KeyEvent.VK_RIGHT))
  150. {
  151. coords.setCoords(coords.getX() + 32, coords.getY());
  152. }
  153. if (input.isKeyDown(KeyEvent.VK_LEFT))
  154. {
  155. coords.setCoords(coords.getX() - 32, coords.getY());
  156. }
  157. if (input.isKeyDown(KeyEvent.VK_UP))
  158. {
  159. coords.setCoords(coords.getX(), coords.getY() - 32);
  160. }
  161. if (input.isKeyDown(KeyEvent.VK_DOWN))
  162. {
  163. coords.setCoords(coords.getX(), coords.getY() + 32);
  164. }
  165. //System.out.println(x);
  166. //System.out.println(y);
  167. //System.out.println(speed);
  168. if (coords.getY() < 0)
  169. {
  170. coords.setCoords(coords.getX(), 0);
  171. }
  172. if (coords.getX() < 0)
  173. {
  174. coords.setCoords(0, coords.getY());
  175. }
  176. if (coords.getX() > windowWidth - 32)
  177. {
  178. coords.setCoords(windowWidth - 32, coords.getY());
  179. }
  180. if (coords.getY() > windowHeight - 32)
  181. {
  182. coords.setCoords(coords.getX(), windowHeight - 32);
  183. // y = windowHeight - 32;
  184. }
  185.  
  186. }
  187.  
  188. /**
  189. * This method will draw everything
  190. */
  191. void draw()
  192. {
  193. Graphics g = getGraphics();
  194. //this.setBackground(Color.BLACK);
  195. //super.paintComponents(g);
  196. backBuffer.setRGB(x, y, Color.BLACK.getRGB());
  197.  
  198. Graphics bbg = backBuffer.getGraphics();
  199.  
  200. BufferedReader reader = null;
  201. try {
  202. reader = new BufferedReader(new FileReader("map"));
  203. } catch (FileNotFoundException e1) {
  204. e1.printStackTrace();
  205. }
  206. String line = null;
  207. try {
  208. BufferedImage gray = ImageIO.read(new File("gray.png"));
  209. BufferedImage black = ImageIO.read(new File("black.png"));
  210. while ((line = reader.readLine()) != null) {
  211. for(String s : line.split(""))
  212. {
  213. if (s.contains("*") && xa + 32!= coords.getX() && ya - 32 != coords.getY())
  214. {
  215. bbg.drawImage(gray, xa-32, ya, null);
  216. }
  217. else if (s.contains("#") && xa + 32 != coords.getX() && ya - 32 != coords.getY())
  218. {
  219. bbg.drawImage(black, xa-32, ya, null);
  220. }
  221. if (xa < 320)
  222. {
  223. xa += 32;
  224. }
  225. else
  226. {
  227. ya += 32;
  228. xa = 0;
  229. }
  230. //System.out.println(xa);
  231. //System.out.println(ya);
  232. }
  233. }
  234. } catch (IOException e) {
  235. e.printStackTrace();
  236. }
  237. bbg.setColor(Color.WHITE);
  238.  
  239. xa = 0;
  240. ya = 0;
  241. System.out.println(coords.getX());
  242. bbg.setColor(Color.WHITE);
  243. bbg.fillRect(coords.getX(),coords.getY(), 32,32);
  244. System.out.println(coords.getY());
  245. //bbg.setColor(Color.BLACK);
  246. //bbg.drawOval(x, y, 20, 20);
  247.  
  248.  
  249. g.drawImage(backBuffer, insets.left, insets.top, this);
  250. }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment