Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. import java.util.*;
  2. import java.awt.event.*;
  3.  
  4. import javax.swing.*;
  5.  
  6. import java.awt.*;
  7.  
  8. import javax.swing.*;
  9. import javax.swing.Timer;
  10.  
  11. public class Gameplay extends JPanel implements KeyListener, ActionListener
  12. {
  13. private boolean play = false;
  14. private int score = 0;
  15.  
  16. private int totalBricks = 48;
  17.  
  18. private Timer timer;
  19. private int delay=8;
  20.  
  21. private int playerX = 310;
  22.  
  23. private int ballposX = 120;
  24. private int ballposY = 350;
  25. private int ballXdir = -1;
  26. private int ballYdir = -2;
  27.  
  28. private MapGenerator map;
  29.  
  30. public Gameplay()
  31. {
  32. map = new MapGenerator(4, 12);
  33. addKeyListener(this);
  34. setFocusable(true);
  35. setFocusTraversalKeysEnabled(false);
  36. timer=new Timer(delay,this);
  37. timer.start();
  38. }
  39.  
  40. public void paint(Graphics g)
  41. {
  42. // background
  43. g.setColor(Color.black);
  44. g.fillRect(1, 1, 692, 592);
  45.  
  46. // drawing map
  47. map.draw((Graphics2D) g);
  48.  
  49. // borders
  50. g.setColor(Color.yellow);
  51. g.fillRect(0, 0, 3, 592);
  52. g.fillRect(0, 0, 692, 3);
  53. g.fillRect(691, 0, 3, 592);
  54.  
  55. // the scores
  56. g.setColor(Color.white);
  57. g.setFont(new Font("serif",Font.BOLD, 25));
  58. g.drawString(""+score, 590,30);
  59.  
  60. // the paddle
  61. g.setColor(Color.green);
  62. g.fillRect(playerX, 550, 100, 8);
  63.  
  64. // the ball
  65. g.setColor(Color.yellow);
  66. g.fillOval(ballposX, ballposY, 20, 20);
  67.  
  68. // when you won the game
  69. if(totalBricks <= 0)
  70. {
  71.  
  72. play = false;
  73. ballXdir = 0;
  74. ballYdir = 0;
  75. g.setColor(Color.RED);
  76. g.setFont(new Font("serif",Font.BOLD, 30));
  77. g.drawString("You Won", 260,300);
  78.  
  79. g.setColor(Color.RED);
  80. g.setFont(new Font("serif",Font.BOLD, 20));
  81. g.drawString("Press (Enter) to Restart", 230,350);
  82. }
  83.  
  84. // when you lose the game
  85. if(ballposY > 570)
  86. {
  87. play = false;
  88. ballXdir = 0;
  89. ballYdir = 0;
  90. g.setColor(Color.RED);
  91. g.setFont(new Font("serif",Font.BOLD, 30));
  92. g.drawString("Game Over, Scores: "+score, 190,300);
  93.  
  94. g.setColor(Color.RED);
  95. g.setFont(new Font("serif",Font.BOLD, 20));
  96. g.drawString("Press (Enter) to Restart", 230,350);
  97. }
  98.  
  99. g.dispose();
  100. }
  101.  
  102. public void keyPressed(KeyEvent e)
  103. {
  104. if (e.getKeyCode() == KeyEvent.VK_RIGHT)
  105. {
  106. if(playerX >= 600)
  107. {
  108. playerX = 600;
  109. }
  110. else
  111. {
  112. moveRight();
  113. }
  114. }
  115.  
  116. if (e.getKeyCode() == KeyEvent.VK_LEFT)
  117. {
  118. if(playerX < 10)
  119. {
  120. playerX = 10;
  121. }
  122. else
  123. {
  124. moveLeft();
  125. }
  126. }
  127. if (e.getKeyCode() == KeyEvent.VK_ENTER)
  128. {
  129. if(!play)
  130. {
  131. play = true;
  132. ballposX = 120;
  133. ballposY = 350;
  134. ballXdir = -1;
  135. ballYdir = -2;
  136. playerX = 310;
  137. score = 0;
  138. totalBricks = 21;
  139. map = new MapGenerator(3, 7);
  140.  
  141. repaint();
  142. }
  143. }
  144. }
  145.  
  146. public void keyReleased(KeyEvent e) {}
  147. public void keyTyped(KeyEvent e) {}
  148.  
  149. public void moveRight()
  150. {
  151. play = true;
  152. playerX+=20;
  153. }
  154.  
  155. public void moveLeft()
  156. {
  157. play = true;
  158. playerX-=20;
  159. }
  160.  
  161. public void actionPerformed(ActionEvent e)
  162. {
  163. timer.start();
  164. if(play)
  165. {
  166. if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 30, 8)))
  167. {
  168. ballYdir = -ballYdir;
  169. ballXdir = -2;
  170. }
  171. else if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX + 70, 550, 30, 8)))
  172. {
  173. ballYdir = -ballYdir;
  174. ballXdir = ballXdir + 1;
  175. }
  176. else if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX + 30, 550, 40, 8)))
  177. {
  178. ballYdir = -ballYdir;
  179. }
  180.  
  181. // check map collision with the ball
  182. A: for(int i = 0; i<map.map.length; i++)
  183. {
  184. for(int j =0; j<map.map[0].length; j++)
  185. {
  186. if(map.map[i][j] > 0)
  187. {
  188. //scores++;
  189. int brickX = j * map.brickWidth + 80;
  190. int brickY = i * map.brickHeight + 50;
  191. int brickWidth = map.brickWidth;
  192. int brickHeight = map.brickHeight;
  193.  
  194. Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
  195. Rectangle ballRect = new Rectangle(ballposX, ballposY, 20, 20);
  196. Rectangle brickRect = rect;
  197.  
  198. if(ballRect.intersects(brickRect))
  199. {
  200. map.setBrickValue(0, i, j);
  201. score+=5;
  202. totalBricks--;
  203.  
  204. // when ball hit right or left of brick
  205. if(ballposX + 19 <= brickRect.x || ballposX + 1 >= brickRect.x + brickRect.width)
  206. {
  207. ballXdir = -ballXdir;
  208. }
  209. // when ball hits top or bottom of brick
  210. else
  211. {
  212. ballYdir = -ballYdir;
  213. }
  214.  
  215. break A;
  216. }
  217. }
  218. }
  219. }
  220.  
  221. ballposX += ballXdir;
  222. ballposY += ballYdir;
  223.  
  224. if(ballposX < 0)
  225. {
  226. ballXdir = -ballXdir;
  227. }
  228. if(ballposY < 0)
  229. {
  230. ballYdir = -ballYdir;
  231. }
  232. if(ballposX > 670)
  233. {
  234. ballXdir = -ballXdir;
  235. }
  236.  
  237. repaint();
  238. }
  239. }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement