Advertisement
Guest User

main.java bomberman allahu akbar

a guest
Jan 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.66 KB | None | 0 0
  1. import java.applet.*;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10. import java.util.Random;
  11.  
  12. import javax.imageio.ImageIO;
  13.  
  14. public class main extends Input implements Runnable {
  15. Input key = new Input();
  16. Thread th = new Thread(this);
  17. Random gen = new Random();
  18. boolean going = true;
  19. Bombs bomb1 = new Bombs(0,0,0,false,false);
  20. Bombs bomb2 = new Bombs(0,0,0,false,false);
  21. Player player1 = new Player(5, 5, 5, 200, 3, 0, 0, true);
  22. Player player2 = new Player(550, 550, 5, 200, 3, 0, 0, true);
  23. int[][][] p1Ani = new int[4][12][4];
  24. int[][][] p2Ani = new int[4][12][4];
  25. int[] bombCoord = new int[2];
  26. SolidObject[][] wall = new SolidObject[6][6];
  27. SolidObject[][] bombGrid = new SolidObject[18][18];
  28. SolidObject[] p = new SolidObject[2];
  29. SolidObject[] flames = new SolidObject[4];
  30. SolidObject[] flames1 = new SolidObject[4];
  31. Image Image;
  32. Graphics Graphics;
  33. BufferedImage img = null;
  34. public void playerNet() {
  35. bombCoord[0] = 25;
  36. bombCoord[1] = 620;
  37. for (int i = 0; i < 3; i++) {
  38. for (int x = 0; x < 3; x++) {
  39. p1Ani[x][i][0] = 14 + i * 84;
  40. p1Ani[x][i][1] = 5 + x * 98;
  41. p1Ani[x][i][2] = p1Ani[0][0][0] + 79 + i * 84;
  42. p1Ani[x][i][3] = p1Ani[0][0][1] + 92 + x * 98;
  43. }
  44. }
  45. for (int i = 0; i < 3; i++) {
  46. p1Ani[3][i][0] = 14 + i * 84;
  47. p1Ani[3][i][1] = 495;
  48. p1Ani[3][i][2] = p1Ani[3][0][0] + 79 + i * 84;
  49. p1Ani[3][i][3] = p1Ani[3][0][1] + 92;
  50. }
  51. for (int i = 0; i < 3; i++) {
  52. for (int x = 0; x < 3; x++) {
  53. p2Ani[x][i][0] = 509 + i * 84;
  54. p2Ani[x][i][1] = 5 + x * 98;
  55. p2Ani[x][i][2] = p2Ani[0][0][0] + 79 + i * 84;
  56. p2Ani[x][i][3] = p2Ani[0][0][1] + 92 + x * 98;
  57. }
  58. }
  59. for (int i = 0; i < 3; i++) {
  60. p2Ani[3][i][0] = 509 + i * 84;
  61. p2Ani[3][i][1] = 495;
  62. p2Ani[3][i][2] = p2Ani[3][0][0] + 79 + i * 84;
  63. p2Ani[3][i][3] = p2Ani[3][0][1] + 92;
  64. }
  65. }
  66. public void movePlayers() {
  67. if (p1D){
  68. p[0].posY += player1.speed;
  69. if (collision(p[0]))
  70. p[0].posY -= player1.speed;
  71. }
  72. if (p1U){
  73. p[0].posY -= player1.speed;
  74. if (collision(p[0]))
  75. p[0].posY += player1.speed;
  76. }
  77. if (p1L){
  78. p[0].posX -= player1.speed;
  79. if (collision(p[0]))
  80. p[0].posX += player1.speed;
  81. }
  82. if (p1R){
  83. p[0].posX += player1.speed;
  84. if (collision(p[0]))
  85. p[0].posX -= player1.speed;
  86. }
  87. if (p2D){
  88. p[1].posY += player2.speed;
  89. if (collision(p[1]))
  90. p[1].posY -= player2.speed;
  91. }
  92. if (p2U){
  93. p[1].posY -= player2.speed;
  94. if (collision(p[1]))
  95. p[1].posY += player2.speed;
  96. }
  97. if (p2L){
  98. p[1].posX -= player2.speed;
  99. if (collision(p[1]))
  100. p[1].posX += player2.speed;
  101. }
  102. if (p2R){
  103. p[1].posX += player2.speed;
  104. if (collision(p[1]))
  105. p[1].posX -= player2.speed;
  106. }
  107. }
  108. public void bombGrid() {
  109. for (int i = 0; i < 18; i++) {
  110. for (int x = 0; x < 18; x++) {
  111. bombGrid[i][x] = new SolidObject(0+x*45, 0+i*45, 45, 45);
  112. bombGrid[i][x].compileHull();
  113. }
  114. }
  115. }
  116. public void init() {
  117. walls();
  118. addKeyListener(this);
  119. }
  120. public void deadBombs() {
  121. for (int i = 0; i < 10; i++) {
  122. bomb1 = new Bombs(0,0,0,false,false);
  123. }
  124. for (int i = 0; i < 10; i++) {
  125. bomb2 = new Bombs(0,0,0,false,false);
  126. }
  127. }
  128. public void start() {
  129. th.start();
  130. p[0] = new SolidObject(player1.x, player1.y, 30, 30);
  131. p[0].compileHull();
  132. p[1] = new SolidObject(player2.x, player2.y, 30, 30);
  133. flames[0]= new SolidObject(bomb1.x-10, bomb1.y-100, 20, 100);
  134. flames[1] = new SolidObject(bomb1.x-100, bomb1.y-10, 100, 20);
  135. flames[2] = new SolidObject(bomb1.x-10, bomb1.y, 20, 100);
  136. flames[3] = new SolidObject(bomb1.x, bomb1.y-10, 100, 20);
  137. flames1[0]= new SolidObject(bomb1.x-10, bomb1.y-100, 20, 100);
  138. flames1[1] = new SolidObject(bomb1.x-100, bomb1.y-10, 100, 20);
  139. flames1[2] = new SolidObject(bomb1.x-10, bomb1.y, 20, 100);
  140. flames1[3] = new SolidObject(bomb1.x, bomb1.y-10, 100, 20);
  141. p[1].compileHull();
  142. deadBombs();
  143. try {
  144. img = ImageIO.read(new File("E:\\Documents and Settings\\Sysop\\workspace\\Bomberman\\src\\bomber.bmp"));
  145. } catch(IOException e){
  146. System.out.print("No image;");
  147. }
  148. }
  149. public void stop() {
  150. going = false;
  151. }
  152. public void walls() {
  153. for (int i = 0; i < 6; i++) {
  154. for (int x = 0; x < 6; x++) {
  155. wall[i][x] = new SolidObject(45+x*90, 45+i*90, 45, 45);
  156. wall[i][x].compileHull();
  157. }
  158. }
  159. }
  160. public boolean collision(SolidObject p) {
  161. boolean collision = false;
  162. for (int i = 0; i < wall.length; i++) {
  163. for (int x = 0; x < wall[i].length; x++) {
  164. if (p.isCollidingWith(wall[i][x])) {
  165. //collision = true;
  166. }
  167. }
  168. }
  169. return collision;
  170. }
  171. public void animate() {
  172. if (p1D) {
  173. player1.direction = 0;
  174. //p1D = false;
  175. player1.ani++;
  176. if (player1.ani == 3)
  177. player1.ani = 0;
  178. }
  179. if (p1U) {
  180. player1.direction = 2;
  181. //p1U = false;
  182. player1.ani++;
  183. if (player1.ani == 3)
  184. player1.ani = 0;
  185. }
  186. if (p1L) {
  187. player1.direction = 1;
  188. //p1L = false;
  189. player1.ani++;
  190. if (player1.ani == 3)
  191. player1.ani = 0;
  192. }
  193. if (p1R) {
  194. player1.direction = 3;
  195. //p1R = false;
  196. player1.ani++;
  197. if (player1.ani == 3)
  198. player1.ani = 0;
  199. }
  200. if (p2D) {
  201. player2.direction = 0;
  202. //p2D = false;
  203. player2.ani++;
  204. if (player2.ani == 3)
  205. player2.ani = 0;
  206. }
  207. if (p2U) {
  208. player2.direction = 2;
  209. //p2U = false;
  210. player2.ani++;
  211. if (player2.ani == 3)
  212. player2.ani = 0;
  213. }
  214. if (p2L) {
  215. player2.direction = 1;
  216. //p2L = false;
  217. player2.ani++;
  218. if (player2.ani == 3)
  219. player2.ani = 0;
  220. }
  221. if (p2R) {
  222. player2.direction = 3;
  223. //p2R = false;
  224. player2.ani++;
  225. if (player2.ani == 3)
  226. player2.ani = 0;
  227. }
  228. if (p1bomb) {
  229. if (!bomb1.alive) {
  230. deployBomb(bomb1, p[0]);
  231. }
  232. }
  233. if (p2bomb) {
  234. if (!bomb2.alive) {
  235. deployBomb(bomb2, p[1]);
  236. }
  237. }
  238. }
  239. public void deployBomb(Bombs bomb, SolidObject p) {
  240. SolidObject check = new SolidObject(p.posX, p.posY, 30, 30);
  241. bomb.x = p.posX;
  242. bomb.y = p.posY;
  243. bomb.alive = true;
  244. bomb.bombFrame = 0;
  245. check.compileHull();
  246. for (int i = 0; i < 13; i++) {
  247. for (int x = 0; x < 13; x++) {
  248. if (check.isCollidingWith(bombGrid[i][x])) {
  249. if (bombGrid[i][x].posX+30 > bomb.x){
  250. bomb.x = bombGrid[i][x].posX+12;
  251. check.posX = bombGrid[i][x].posX+12;
  252. }
  253. if (bombGrid[i][x].posY+30 > bomb.y){
  254. bomb.y = bombGrid[i][x].posY+12;
  255. check.posY = bombGrid[i][x].posY+12;
  256. }
  257. }
  258. }
  259. }
  260. }
  261. public void bombLoop() {
  262. if (bomb1.alive && !bomb1.explosion) {
  263. bomb1.bombFrame++;
  264. if (bomb1.bombFrame >= bomb1.maxFrame)
  265. bomb1.explosion = true;
  266. if (bomb1.explosion) {
  267. flames[0]= new SolidObject(bomb1.x-12, bomb1.y-player1.distance, 45, player1.distance);
  268. flames[1] = new SolidObject(bomb1.x-player1.distance, bomb1.y-12, player1.distance, 45);
  269. flames[2] = new SolidObject(bomb1.x-12, bomb1.y, 45, player1.distance+12);
  270. flames[3] = new SolidObject(bomb1.x, bomb1.y-12, player1.distance+12, 45);
  271. for (int i = 0; i < flames.length; i++) {
  272. if (flames[i].isCollidingWith(p[1])) {
  273. System.out.print("\n\n\n DEAD!");
  274. player2.alive = false;
  275. }
  276. if (flames[i].isCollidingWith(p[0])) {
  277. System.out.print("\n\n\n DEAD!");
  278. player1.alive = false;
  279. }
  280. }
  281. bomb1.alive = false;
  282. bomb1.explosion = false;
  283. }
  284. }
  285. if (bomb2.alive && !bomb2.explosion) {
  286. bomb2.bombFrame++;
  287. if (bomb2.bombFrame >= bomb2.maxFrame)
  288. bomb2.explosion = true;
  289. if (bomb2.explosion) {
  290. flames1[0]= new SolidObject(bomb2.x-12, bomb2.y-player2.distance, 45, player2.distance);
  291. flames1[1] = new SolidObject(bomb2.x-player2.distance, bomb2.y-12, player2.distance, 45);
  292. flames1[2] = new SolidObject(bomb2.x-12, bomb2.y, 45, player2.distance+12);
  293. flames1[3] = new SolidObject(bomb2.x, bomb2.y-12, player2.distance+12, 45);
  294. for (int i = 0; i < flames1.length; i++) {
  295. if (flames1[i].isCollidingWith(p[1])) {
  296. System.out.print("\n\n\n DEAD!");
  297. player2.alive = false;
  298. }
  299. if (flames1[i].isCollidingWith(p[0])) {
  300. System.out.print("\n\n\n DEAD!");
  301. player1.alive = false;
  302. }
  303. }
  304. bomb2.alive = false;
  305. bomb2.explosion = false;
  306. }
  307. }
  308. }
  309. public void run() {
  310. init();
  311. bombGrid();
  312. while(going) {
  313. playerNet();
  314. movePlayers();
  315. animate();
  316. bombLoop();
  317. repaint();
  318. try {
  319. Thread.sleep(10);
  320. }
  321. catch(InterruptedException ie) {
  322. }
  323. }
  324. }
  325. public void update(Graphics g) {
  326. if(Image == null) {
  327. Image = createImage(this.getSize().width, this.getSize().height);
  328. Graphics = Image.getGraphics();
  329. }
  330. Graphics.setColor(this.getBackground());
  331. this.setBackground(Color.LIGHT_GRAY);
  332. Graphics.fillRect(0,0,this.getSize().width,this.getSize().height);
  333. Graphics.setColor(this.getForeground());
  334. paint(Graphics);
  335. g.drawImage(Image, 0, 0, this);
  336. }
  337. public void paint(Graphics g) {
  338. if (player1.alive)
  339. g.drawImage(img, p[0].posX, p[0].posY, p[0].posX+p[0].sizeX, p[0].posY+p[0].sizeY, p1Ani[player1.direction][player1.ani][0], p1Ani[player1.direction][player1.ani][1], p1Ani[player1.direction][player1.ani][2], p1Ani[player1.direction][player1.ani][3], null);
  340. if (player2.alive)
  341. g.drawImage(img, p[1].posX, p[1].posY, p[1].posX+p[1].sizeX, p[1].posY+p[1].sizeY, p2Ani[player2.direction][player2.ani][0], p2Ani[player2.direction][player2.ani][1], p2Ani[player2.direction][player2.ani][2], p1Ani[player2.direction][player2.ani][3], null);
  342. //for (int i = 0; i < wall.length; i++) {
  343. // for (int x = 0; x < wall[i].length; x++) {
  344. // //g.fillRect(wall[i][x].posX, wall[i][x].posY, wall[i][x].sizeX, wall[i][x].sizeY);
  345. // }
  346. //}
  347. for (int i = 0; i < 13; i++) {
  348. for (int x = 0; x < 13; x++)
  349. g.drawRect(bombGrid[i][x].posX, bombGrid[i][x].posY, bombGrid[i][x].sizeX, bombGrid[i][x].sizeY);
  350. }
  351. if (bomb1.alive) {
  352. g.drawImage(img, bomb1.x, bomb1.y, bomb1.x+30, bomb1.y+30, bombCoord[0], bombCoord[1], bombCoord[0]+50, bombCoord[1]+50, null);
  353. }
  354. if (bomb2.alive) {
  355. g.drawImage(img, bomb2.x, bomb2.y, bomb2.x+30, bomb2.y+30, bombCoord[0], bombCoord[1], bombCoord[0]+50, bombCoord[1]+50, null);
  356. }
  357. g.setColor(Color.red);
  358. //if (bomb1.explosion) {
  359. for (int i = 0; i < flames.length; i++)
  360. g.fillRect(flames[i].posX, flames[i].posY, flames[i].sizeX, flames[i].sizeY);
  361. //}
  362. //if (bomb2.explosion) {
  363. for (int i = 0; i < flames1.length; i++)
  364. g.fillRect(flames1[i].posX, flames1[i].posY, flames1[i].sizeX, flames1[i].sizeY);
  365. //}
  366. }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement