Advertisement
Guest User

Untitled

a guest
May 28th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.96 KB | None | 0 0
  1. import java.lang.Object.*;
  2. import java.applet.*;
  3. import java.awt.*;
  4. import java.net.*;
  5. import java.util.ArrayList;
  6.  
  7. public class BlockBreaker extends Applet implements Runnable
  8. {
  9. int loaded = 0;
  10. boolean gameStarted = false;
  11. boolean paused = false;
  12. int Key;
  13. int save, save1;
  14. int applet_x = 600;
  15. int applet_y = 500;
  16. int mouse_x;
  17. int mouse_y;
  18. private Image dbImage;
  19. private Graphics dbg;
  20. int x = 55;
  21. int y = 50;
  22. int number = 1;
  23. int yN = 1;
  24. int amountBroken;
  25. Image ballFast, ballGrow, ballReduce, ballSlow, caught, extraLife, strike;
  26. Image background, logo1, logo2, logo3, gui, gameover;
  27. Image blue, broken1, broken2, brown, cyan, green, gray, magenta, orange, red, violet, white, yellow;
  28. Image ball0, ball1, ball2, ball3, ball4, ball5;
  29. Image center, left, right;
  30. Toolkit tk = Toolkit.getDefaultToolkit();
  31. Rectangle block[] = new Rectangle[9];
  32. Rectangle block1[] = new Rectangle[9];
  33. Rectangle block2[] = new Rectangle[9];
  34. Rectangle block3[] = new Rectangle[9];
  35. Rectangle block4[] = new Rectangle[9];
  36.  
  37. public void init() {
  38. background = getImage (getCodeBase(), "./images/Background.jpg");
  39. logo1 = getImage (getCodeBase(), "./images/splash/brick.png");
  40. logo2 = getImage (getCodeBase(), "./images/splash/breaker.png");
  41. logo3 = getImage (getCodeBase(), "./images/splash/pressanykey.png");
  42. gameover = getImage (getCodeBase(), "./images/gameover.png");
  43. gui = getImage (getCodeBase(), "./images/gui.png");
  44. brickSetup();
  45. ObjectLoader.loadImages(this);
  46. }
  47.  
  48. public void brickSetup() {
  49. for (int i = 0; i < 9; i++) {
  50. block[i] = new Rectangle(x*number, y, 57, 41);
  51. block1[i] = new Rectangle(x*number, y*2, 57, 41);
  52. block2[i] = new Rectangle(x*number, y*3, 57, 41);
  53. block3[i] = new Rectangle(x*number, y*4, 57, 41);
  54. block4[i] = new Rectangle(x*number, y*5, 57, 41);
  55. number++;
  56. }
  57. }
  58.  
  59. public void updateI(Graphics g) {
  60. Gui.score += 20;
  61. amountBroken++;
  62. }
  63.  
  64. public void ballHitBlock(Graphics g) {
  65. for (int i = 0; i < 9; i++) {
  66. if (block[i].contains(Ball.ballX, Ball.ballY) && Brick.brick[i] != false) {
  67. updateI(g);
  68. Ball.ballSpeedY = -Ball.ballSpeedY;
  69. if (Brick.random(1) == 1) {
  70. Ball.ballSpeedX = -Ball.ballSpeedX;
  71. }
  72. Brick.brick[i] = false;
  73. levelClear();
  74. repaint();
  75. }
  76. if (block1[i].contains(Ball.ballX, Ball.ballY) && Brick.brick1[i] != false) {
  77. updateI(g);
  78. Ball.ballSpeedY = -Ball.ballSpeedY;
  79. if (Brick.random(1) == 1) {
  80. Ball.ballSpeedX = -Ball.ballSpeedX;
  81. }
  82. Brick.brick1[i] = false;
  83. levelClear();
  84. repaint();
  85. }
  86. if (block2[i].contains(Ball.ballX, Ball.ballY) && Brick.brick2[i] != false) {
  87. updateI(g);
  88. Ball.ballSpeedY = -Ball.ballSpeedY;
  89. if (Brick.random(1) == 1) {
  90. Ball.ballSpeedX = -Ball.ballSpeedX;
  91. }
  92. Brick.brick2[i] = false;
  93. levelClear();
  94. repaint();
  95. }
  96. if (block3[i].contains(Ball.ballX, Ball.ballY) && Brick.brick3[i] != false) {
  97. updateI(g);
  98. Ball.ballSpeedY = -Ball.ballSpeedY;
  99. if (Brick.random(1) == 1) {
  100. Ball.ballSpeedX = -Ball.ballSpeedX;
  101. }
  102. Brick.brick3[i] = false;
  103. levelClear();
  104. repaint();
  105. }
  106. if (block4[i].contains(Ball.ballX, Ball.ballY) && Brick.brick4[i] != false) {
  107. updateI(g);
  108. Ball.ballSpeedY = -Ball.ballSpeedY;
  109. if (Brick.random(1) == 1) {
  110. Ball.ballSpeedX = -Ball.ballSpeedX;
  111. }
  112. Brick.brick4[i] = false;
  113. levelClear();
  114. repaint();
  115. }
  116. }
  117. }
  118.  
  119. public void levelClear() {
  120. if (checkForClear() && checkForClear1() && checkForClear2() && checkForClear3() && checkForClear4()) {
  121. for(int i = 0; i < 9; i++) {
  122. Brick.imageChosen = 0;
  123. Brick.brick[i] = true;
  124. Brick.brick1[i] = true;
  125. Brick.brick2[i] = true;
  126. Brick.brick3[i] = true;
  127. Brick.brick4[i] = true;
  128. }
  129. }
  130. }
  131.  
  132. public boolean checkForClear() {
  133. return Brick.brick[0] != true && Brick.brick[1] != true && Brick.brick[2] != true && Brick.brick[3] != true && Brick.brick[4] != true && Brick.brick[5] != true && Brick.brick[6] != true && Brick.brick[7] != true && Brick.brick[8] != true;
  134. }
  135. public boolean checkForClear1() {
  136. return Brick.brick1[0] != true && Brick.brick1[2] != true && Brick.brick1[2] != true && Brick.brick1[3] != true && Brick.brick1[4] != true && Brick.brick1[5] != true && Brick.brick1[6] != true && Brick.brick1[7] != true && Brick.brick1[8] != true;
  137. }
  138. public boolean checkForClear2() {
  139. return Brick.brick2[0] != true && Brick.brick2[2] != true && Brick.brick2[2] != true && Brick.brick2[3] != true && Brick.brick2[4] != true && Brick.brick2[5] != true && Brick.brick2[6] != true && Brick.brick2[7] != true && Brick.brick2[8] != true;
  140. }
  141. public boolean checkForClear3() {
  142. return Brick.brick3[0] != true && Brick.brick3[2] != true && Brick.brick3[2] != true && Brick.brick3[3] != true && Brick.brick3[4] != true && Brick.brick3[5] != true && Brick.brick3[6] != true && Brick.brick3[7] != true && Brick.brick3[8] != true;
  143. }
  144. public boolean checkForClear4() {
  145. return Brick.brick4[0] != true && Brick.brick4[2] != true && Brick.brick4[2] != true && Brick.brick4[3] != true && Brick.brick4[4] != true && Brick.brick4[5] != true && Brick.brick4[6] != true && Brick.brick4[7] != true && Brick.brick4[8] != true;
  146. }
  147.  
  148. public void start() {
  149. Thread th = new Thread (this);
  150. th.start();
  151. }
  152.  
  153. public void stop() {
  154. }
  155.  
  156. public void destroy() {
  157. }
  158.  
  159. public void run ()
  160. {
  161. Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  162. while (true)
  163. {
  164. Ball.setBoundries(this);
  165. Ball.ballX += Ball.ballSpeedX;
  166. Ball.ballY += Ball.ballSpeedY;
  167. Paddle.ballHitBat();
  168. repaint();
  169. try
  170. {
  171. Thread.sleep (20);
  172. }
  173. catch (InterruptedException ex)
  174. {
  175. }
  176. Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  177. }
  178.  
  179. }
  180.  
  181. public void update (Graphics g)
  182. {
  183.  
  184. if (dbImage == null)
  185. {
  186. dbImage = createImage (this.getSize().width, this.getSize().height);
  187. dbg = dbImage.getGraphics();
  188. }
  189.  
  190. dbg.setColor (getBackground());
  191. dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
  192.  
  193. dbg.setColor (getForeground());
  194. paint (dbg);
  195.  
  196. g.drawImage (dbImage, 0, 0, this);
  197.  
  198. }
  199.  
  200. public void paint (Graphics g) {
  201. ballHitBlock(g);
  202. g.drawImage (background, 0, 0, this);
  203. g.drawImage (gui, 600, 0, this);
  204. Gui.paint(g);
  205. Paddle.paint(g, this);
  206. if (Paddle.lost != true) {
  207. if (gameStarted && !paused) {
  208. g.drawImage (center, Paddle.paddleX, Paddle.paddleY, this);
  209. Brick.row0(g, this);
  210. } else {
  211. if (!paused) {
  212. g.drawImage (center, Paddle.paddleX, Paddle.paddleY, this);
  213. Ball.ballY = 450;
  214. Ball.ballX = Paddle.middle;
  215. }
  216. g.drawImage (logo1, 117, 25, this);
  217. g.drawImage (logo2, 45, 219, this);
  218. g.drawImage (logo3, 64, 407, this);
  219. }
  220. if (!paused) {
  221. switch(Ball.ball) {
  222. case 0:
  223. g.drawImage (ball0, Ball.ballX, Ball.ballY, this);
  224. break;
  225. case 1:
  226. case 2:
  227. case 3:
  228. case 4:
  229. case 5:
  230. break;
  231. default:
  232. g.drawImage (ball0, mouse_x + 10, mouse_y - 10, this);
  233. }
  234. }
  235. }
  236. }
  237.  
  238. // Method to handle mouse down events
  239. public boolean mouseDown (Event e, int x, int y)
  240. {
  241. if (!gameStarted || Paddle.lost != false) {
  242. Gui.lives = 3;
  243. Gui.score = 0;
  244. Ball.ballSpeedX = Ball.ballSpeedY = 3;
  245. gameStarted = true;
  246. if (Paddle.lost != false) {
  247. Paddle.lost = false;
  248. for(int i = 0; i < 9; i++) {
  249. Brick.imageChosen = 0;
  250. Brick.brick[i] = true;
  251. Brick.brick1[i] = true;
  252. Brick.brick2[i] = true;
  253. Brick.brick3[i] = true;
  254. Brick.brick4[i] = true;
  255. }
  256. }
  257. repaint();
  258. }
  259. mouse_x = x;
  260. mouse_y = y;
  261. return true;
  262. }
  263. public boolean mouseUp (Event e, int x, int y) { //handles events that occur if mouse button is released again.
  264. return true;
  265. }
  266. public boolean mouseMove (Event e, int x, int y) { //handles events that occur if mouse is moved over the applet
  267. if (x < 500) {
  268. Paddle.paddleX = x;
  269. }
  270. repaint();
  271. return true;
  272. }
  273. public boolean mouseDrag (Event e, int x, int y) { //handles events that occur if mouse is moved over the applet with pressed mouse button
  274. return true;
  275. }
  276. public boolean mouseEnter(Event e, int x, int y) { //handles events that occur if mouse enters the applet
  277. return true;
  278. }
  279. public boolean mouseExit (Event e, int x, int y) { //handles events that occur if mouse leaves the applet
  280. return true;
  281. }
  282. public boolean keyDown (Event e, int key)
  283. {
  284. Key = key;
  285. if (!gameStarted) {
  286. if (key > 0) {
  287. Ball.ballSpeedX = Ball.ballSpeedY = 3;
  288. gameStarted = true;
  289. }
  290. }
  291. if (paused) {
  292. if (key > 0) {
  293. paused = false;
  294. Ball.ballSpeedX = save;
  295. Ball.ballSpeedY = save1;
  296. }
  297. }
  298. if (key == 1006) {
  299. if (Paddle.paddleX > 0) {
  300. Paddle.paddleX-= 15;
  301. }
  302. }
  303. if (key == 1007) {
  304. if (Paddle.paddleX < 495) {
  305. Paddle.paddleX+= 15;
  306. }
  307. }
  308. if (key == 112 && !paused) {
  309. paused = true;
  310. save = Ball.ballSpeedX;
  311. save1 = Ball.ballSpeedY;
  312. Ball.ballSpeedX = Ball.ballSpeedY = 0;
  313. }
  314. repaint();
  315. return true;
  316. }
  317. public static void lose() {
  318. Gui.lives--;
  319. if (Gui.lives == 0) {
  320. Ball.ballY = 300;
  321. Paddle.lost = true;
  322. Ball.ballSpeedX = Ball.ballSpeedY = 0;
  323. }
  324. }
  325. }
  326.  
  327. import java.lang.Object.*;
  328. import java.applet.*;
  329. import java.awt.*;
  330. import java.net.*;
  331. import java.util.ArrayList;
  332. import java.net.URL;
  333. import java.net.URLConnection;
  334. import javax.swing.*;
  335. import javax.sound.midi.*;
  336. import javax.sound.sampled.*;
  337. import java.util.Enumeration;
  338. import java.util.zip.ZipEntry;
  339. import java.util.zip.ZipFile;
  340.  
  341.  
  342. public class ObjectLoader
  343. {
  344.  
  345. public static void loadImages(BlockBreaker b) {
  346. bricks(b);
  347. ball(b);
  348. bat(b);
  349. specials(b);
  350. }
  351. public static void bricks(BlockBreaker b) {
  352. b.blue = b.getImage (b.getCodeBase(), "./images/brick/blue.png");
  353. b.broken1 = b.getImage (b.getCodeBase(), "./images/brick/broken1.png");
  354. b.broken2 = b.getImage (b.getCodeBase(), "./images/brick/broken2.png");
  355. b.brown = b.getImage (b.getCodeBase(), "./images/brick/brown.png");
  356. b.cyan = b.getImage (b.getCodeBase(), "./images/brick/cyan.png");
  357. b.green = b.getImage (b.getCodeBase(), "./images/brick/green.png");
  358. b.gray = b.getImage (b.getCodeBase(), "./images/brick/grey.png");
  359. b.magenta = b.getImage (b.getCodeBase(), "./images/brick/magenta.png");
  360. b.orange = b.getImage (b.getCodeBase(), "./images/brick/orange.png");
  361. b.red = b.getImage (b.getCodeBase(), "./images/brick/red.png");
  362. b.violet = b.getImage (b.getCodeBase(), "./images/brick/violet.png");
  363. b.white = b.getImage (b.getCodeBase(), "./images/brick/white.png");
  364. b.yellow = b.getImage (b.getCodeBase(), "./images/brick/yellow.png");
  365. }
  366. public static void ball(BlockBreaker b) {
  367. b.ball0 = b.getImage (b.getCodeBase(), "./images/ball/ball0.png");
  368. b.ball1 = b.getImage (b.getCodeBase(), "./images/ball/ball1.png");
  369. b.ball2 = b.getImage (b.getCodeBase(), "./images/ball/ball2.png");
  370. b.ball3 = b.getImage (b.getCodeBase(), "./images/ball/ball3.png");
  371. b.ball4 = b.getImage (b.getCodeBase(), "./images/ball/ball4.png");
  372. b.ball5 = b.getImage (b.getCodeBase(), "./images/ball/ball5.png");
  373. }
  374. public static void bat(BlockBreaker b) {
  375. b.center = b.getImage (b.getCodeBase(), "./images/bat/center.png");
  376. b.left = b.getImage (b.getCodeBase(), "./images/bat/left.png");
  377. b.right = b.getImage (b.getCodeBase(), "./images/bat/right.png");
  378. }
  379. public static void specials(BlockBreaker b) {
  380. b.ballFast = b.getImage (b.getCodeBase(), "./images/bonus/ballfast.png");
  381. b.ballGrow = b.getImage (b.getCodeBase(), "./images/bonus/ballgrow.png");
  382. b.ballReduce = b.getImage (b.getCodeBase(), "./images/bonus/ballreduce.png");
  383. b.ballSlow = b.getImage (b.getCodeBase(), "./images/bonus/ballslow.png");
  384. b.caught = b.getImage (b.getCodeBase(), "./images/bonus/catch.png");
  385. b.extraLife = b.getImage (b.getCodeBase(), "./images/bonus/extralife.png");
  386. b.strike = b.getImage (b.getCodeBase(), "./images/bonus/strike.png");
  387. }
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement