Advertisement
Guest User

map

a guest
Apr 26th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. import java.awt.Graphics;
  2. import java.awt.Image;
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import javax.swing.ImageIcon;
  6.  
  7. public class map
  8. {
  9. protected int[][] tileMap;
  10. protected int height;
  11. protected int width;
  12. private Image FLOOR;
  13. private Image WALL;
  14. private Image GOAL;
  15.  
  16. public map(String mapFile)
  17. {
  18. FLOOR=new ImageIcon("sprites/floor.png").getImage();
  19. WALL=new ImageIcon("sprites/wall.png").getImage();
  20. GOAL=new ImageIcon("sprites/goal.png").getImage();
  21. try
  22. {
  23. BufferedReader br = new BufferedReader(new FileReader(mapFile));
  24. height=Integer.parseInt(br.readLine());
  25. width=Integer.parseInt(br.readLine());
  26. tileMap=new int[height][width];
  27. String line;
  28. for(int row=0;row<=height;row++)
  29. {
  30. line=br.readLine();
  31. String[] arLine=line.trim().split(" ");
  32. for(int col = 0; col<arLine.length; col++)
  33. {
  34. tileMap[row][col]=Integer.parseInt(arLine[col]);
  35. }
  36. }
  37. }
  38. catch(Exception e)
  39. {
  40.  
  41. }
  42. }
  43.  
  44. public void draw(Graphics g)
  45. {
  46. int ix;
  47. int iy=0;
  48. int col=0;
  49.  
  50. for(int row=0;row<height;row++)
  51. {
  52. ix=0;
  53. for(col=0;col<width;col++)
  54. {
  55. if(tileMap[row][col]==0)
  56. {
  57. g.drawImage(FLOOR, ix, iy, null);
  58. ix=ix+48;
  59. }
  60. if(tileMap[row][col]==1)
  61. {
  62. g.drawImage(WALL, ix, iy, null);
  63. ix=ix+48;
  64. }
  65. if(tileMap[row][col]==2)
  66. {
  67. g.drawImage(FLOOR, ix, iy, null);
  68. ix=ix+48;
  69. }
  70. if(tileMap[row][col]==3)
  71. {
  72. g.drawImage(GOAL, ix, iy, null);
  73. ix=ix+48;
  74. }
  75. if(tileMap[row][col]==4)
  76. {
  77. g.drawImage(FLOOR, ix, iy, null);
  78. ix=ix+48;
  79. }
  80. if(tileMap[row][col]==5)
  81. {
  82. g.drawImage(FLOOR, ix, iy, null);
  83. ix=ix+48;
  84. }
  85. }
  86. if (col==width)
  87. {
  88. iy=iy+48;
  89. }
  90. }
  91.  
  92. }
  93.  
  94. public int findSpawny()
  95. {
  96. for(int i=0;i<height;i++)
  97. {
  98. for(int j=0;j<width;j++)
  99. {
  100. if(tileMap[i][j]==2)
  101. {
  102. return i;
  103. }
  104. }
  105. }
  106. return -1;
  107. }
  108.  
  109. public int findSpawnx()
  110. {
  111. for(int i=0;i<height;i++)
  112. {
  113. for(int j=0;j<width;j++)
  114. {
  115. if(tileMap[i][j]==2)
  116. {
  117. return j;
  118. }
  119. }
  120. }
  121. return -1;
  122. }
  123.  
  124. public int goalSpawny()
  125. {
  126. for(int i=0;i<height;i++)
  127. {
  128. for(int j=0;j<width;j++)
  129. {
  130. if(tileMap[i][j]==3)
  131. {
  132. return i;
  133. }
  134. }
  135. }
  136. return -1;
  137. }
  138.  
  139. public int goalSpawnx()
  140. {
  141. for(int i=0;i<height;i++)
  142. {
  143. for(int j=0;j<width;j++)
  144. {
  145. if(tileMap[i][j]==3)
  146. {
  147. return j;
  148. }
  149. }
  150. }
  151. return -1;
  152. }
  153. public int enemySpawny()
  154. {
  155. for(int i=0;i<height;i++)
  156. {
  157. for(int j=0;j<width;j++)
  158. {
  159. if(tileMap[i][j]==4)
  160. {
  161. return i;
  162. }
  163. }
  164. }
  165. return -1;
  166. }
  167.  
  168. public int enemySpawnx()
  169. {
  170. for(int i=0;i<height;i++)
  171. {
  172. for(int j=0;j<width;j++)
  173. {
  174. if(tileMap[i][j]==4)
  175. {
  176. return j;
  177. }
  178. }
  179. }
  180. return -1;
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement