Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.57 KB | None | 0 0
  1. import acm.graphics.*;
  2. import acm.program.*;
  3. import acm.util.*;
  4.  
  5. import java.applet.*;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8.  
  9. public class game extends GraphicsProgram {
  10. AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au");
  11. AudioClip Clip = MediaTools.loadAudioClip("The Walking Dead - Main theme 8-bit.au");
  12. /** Offset of the paddle up from the bottom */
  13. private static final int PADDLE_Y_OFFSET = 30;
  14. private static final long serialVersionUID = 1L;
  15. /** Width and height of application window in pixels */
  16. public static final int APP_WIDTH = 700;
  17. public static final int APP_HEIGHT = 600;
  18.  
  19. /** Dimensions of game board (usually the same) */
  20. private static final int WIDTH = APP_WIDTH;
  21. private static final int HEIGHT = APP_HEIGHT;
  22.  
  23. /** Dimensions of the paddle */
  24. private static final int PADDLE_WIDTH = 100;
  25. private static final int PADDLE_HEIGHT = 20;
  26.  
  27. /** Number of bricks per row */
  28. private static final int NBRICKS_PER_ROW = 3;
  29.  
  30. /** Number of rows of bricks */
  31. private static final int NBRICK_ROWS = 3;
  32.  
  33. /** Separation between bricks */
  34. private static final int BRICK_SEP = 4;
  35.  
  36. /** Width of a brick */
  37. private static final int BRICK_WIDTH = (WIDTH - (NBRICKS_PER_ROW - 1)
  38. * BRICK_SEP)
  39. / NBRICKS_PER_ROW;
  40.  
  41. /** Height of a brick */
  42. private static final int BRICK_HEIGHT = 20;
  43.  
  44. /** Radius of the ball in pixels */
  45. private static final int DIAM_BALL = 20;
  46.  
  47. /** Offset of the top brick row from the top */
  48. private static final int BRICK_Y_OFFSET = 70;
  49. /** Offset of the top brick row from the top */
  50.  
  51. /** Number of turns */
  52. private static final int NTURNS = 3;
  53.  
  54. /** Create start position */
  55. RandomGenerator rgen = RandomGenerator.getInstance();
  56. /** направление и скорость */
  57. private double xVel = rgen.nextDouble(9, 13);
  58. private double yVel = rgen.nextDouble(9, 13);
  59.  
  60. /** start position */
  61. private int X_START = WIDTH / 2;
  62. private int Y_START = HEIGHT / 2;
  63. private int degt = 0;
  64. public GRect paddle;
  65. public int paddle2;
  66. private GOval ball;
  67. public GRect brick;
  68. private GLabel scoreLabel;
  69. private int score;
  70. private int bricksLeft;
  71. private GLabel bricksLabel;
  72. private GLabel livesLabel;
  73. private GRect finish;
  74. private GLabel finishLabel;
  75. private GLabel startAgain;
  76. private int lives;
  77. GImage bac = new GImage("pz_0929.jpg");
  78.  
  79.  
  80. /* Method: run() */
  81. /** Runs the Breakout program. */
  82.  
  83. public void run() {
  84. this.setSize(APP_WIDTH,APP_HEIGHT);
  85.  
  86.  
  87. setup();
  88.  
  89. Clip.loop();
  90.  
  91. while (bricksLeft>0) {
  92. moveBall();
  93. checkForWalls();
  94. checkForBrics();
  95. checklose();
  96. pause(15);
  97. }
  98. endOfGame();
  99. startAgain();
  100. }
  101. //метод який заново починає гру
  102. private void startAgain() {
  103. startAgain = new GLabel ("Click to start the game again");
  104. startAgain.setLocation((getWidth()-startAgain.getWidth())/2, getHeight()/2+finishLabel.getAscent()*2);
  105. add(startAgain);
  106. waitForClick();
  107. remove(finishLabel);
  108. remove(startAgain);
  109. remove(finish);
  110.  
  111. run();
  112. }
  113. // метод який завершує гру
  114. private void endOfGame() {
  115. removeAll();
  116. finish = new GRect (APP_WIDTH, APP_HEIGHT);
  117. finish.setFilled(true);
  118. finish.setLocation(0,0);
  119. finish.setColor(Color.yellow);
  120. add(finish);
  121. finishLabel = new GLabel ("The game is over");
  122. finishLabel.setColor(Color.BLACK);
  123. finishLabel.setLocation((getWidth()-finishLabel.getWidth())/2, getHeight()/2);
  124. add(finishLabel);
  125.  
  126. }
  127.  
  128. /** Створюємо і розміщуємо м'ячик */
  129. private void setup() {
  130. bac = new GImage("pz_0929.jpg");
  131.  
  132. bac.setSize(HEIGHT, HEIGHT);
  133. // add(bac, 0, 0);
  134.  
  135. drawBricks();
  136. drawBricks();
  137. if (rgen.nextBoolean(0.5))
  138. xVel = -xVel;
  139. addMouseListeners();
  140. this.setSize(APP_WIDTH, APP_HEIGHT);
  141. drawPaddle();
  142. ball = new GOval(X_START, Y_START, DIAM_BALL, DIAM_BALL);
  143. ball.setFilled(true);
  144. add(ball);
  145. setupScore();
  146. setupBrickCounter();
  147. setupLives();
  148.  
  149. }
  150. // ініціалізуємо табло очків
  151. private void setupScore() {
  152. score = 0;
  153. scoreLabel = new GLabel("Score: " + score);
  154. scoreLabel.setLocation(getWidth() - scoreLabel.getWidth(),
  155. scoreLabel.getAscent() + 10);
  156. add(scoreLabel);
  157. }
  158. // табло рахунку
  159. private void addScore() {
  160. score++;
  161. scoreLabel.setLabel("Score: " + score + " ");
  162. scoreLabel.setLocation(getWidth() - scoreLabel.getWidth(),
  163. scoreLabel.getAscent() + 10);
  164. }
  165. // ініціалізуємо лічильник цеглин
  166. private void setupBrickCounter() {
  167. bricksLeft = NBRICKS_PER_ROW * NBRICK_ROWS*2;
  168. bricksLabel = new GLabel(" Bricks left: " + bricksLeft);
  169. bricksLabel.setLocation(0, bricksLabel.getAscent() + 10);
  170. add(bricksLabel);
  171. }
  172. // додаємо інкременуємо рахівник цеглин
  173. private void addBrickCounter() {
  174. bricksLeft--;
  175. bricksLabel.setLabel(" Bricks left: " + bricksLeft);
  176. bricksLabel.setLocation(0, bricksLabel.getAscent() + 10);
  177. }
  178.  
  179. private void setupLives() {
  180. livesLabel = new GLabel("Times dead: " + lives);
  181. livesLabel.setLocation(getWidth() / 2, livesLabel.getAscent() + 10);
  182. lives = 0;
  183. add(livesLabel);
  184. }
  185. // інкременуємо рахунок життів
  186. private void addLives() {
  187. lives++;
  188. livesLabel.setLabel("Times dead: " + lives);
  189. livesLabel.setLocation(getWidth() / 2, livesLabel.getAscent() + 10);
  190. }
  191.  
  192. // ****************************************************************
  193. private void moveBall() {ball.move(xVel, yVel);}
  194. // **********************************************************
  195. // відбиття від цеглин
  196. public void checkForBrics() {
  197. if ((getElementAt(ball.getX(), ball.getY()) != null)) {
  198. // верхня ліва
  199. deleteBricks(getElementAt(ball.getX(), ball.getY()));
  200.  
  201. } else if ((getElementAt(ball.getX(), ball.getY() + DIAM_BALL) != null)) {
  202. // нижня ліва
  203.  
  204. deleteBricks(getElementAt(ball.getX(), ball.getY() + DIAM_BALL));
  205.  
  206. } else if ((getElementAt(ball.getX() + DIAM_BALL, ball.getY()) != null)) {
  207. // верхня права
  208. deleteBricks(getElementAt(ball.getX() + DIAM_BALL, ball.getY()));
  209.  
  210. } else if ((getElementAt(ball.getX() + DIAM_BALL, ball.getY()
  211. + DIAM_BALL) != null)) {
  212. // пробелемесы
  213. deleteBricks(getElementAt(ball.getX() + DIAM_BALL, ball.getY()
  214. + DIAM_BALL));
  215.  
  216. }
  217. }
  218.  
  219. // **********************************************************
  220. // перевіряємо відбиття від стінок
  221. public void checkForWalls() {
  222. /**
  223. * отскоки от стенки
  224. */
  225. // снизу
  226.  
  227. // справа и слева
  228. if (ball.getX() < 0 || ball.getX() > WIDTH - DIAM_BALL) {
  229. xVel = -xVel;
  230.  
  231. paddle2 = 0;
  232. }
  233. // от верха
  234. else if (ball.getY() <= 0) {
  235.  
  236. yVel = -yVel;
  237. paddle2 = 0;
  238.  
  239. }
  240. }
  241.  
  242. // *****************************************************************
  243. // малюємо ракетку
  244. public void drawPaddle() {
  245. double x = getWidth() / 2 - PADDLE_WIDTH / 2;
  246. double y = HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT;
  247. paddle = new GRect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
  248. paddle.setFilled(true);
  249. add(paddle);
  250.  
  251. }
  252. // слухач переміщеня миші
  253. public void mouseMoved(MouseEvent e) {
  254. if ((e.getX() < getWidth() - PADDLE_WIDTH / 2)
  255. && (e.getX() > PADDLE_WIDTH / 2)) {
  256.  
  257. paddle.setLocation(e.getX() - PADDLE_WIDTH / 2, getHeight()
  258. - PADDLE_Y_OFFSET - PADDLE_HEIGHT);
  259. }
  260. }
  261.  
  262. // **************************************************************
  263. // малюємо цеглини
  264. public void drawBricks() {
  265.  
  266. for (int i = 0; i < NBRICK_ROWS; i++) {
  267. Color color = rgen.nextColor();
  268. for (int j = 0; j < NBRICKS_PER_ROW; j++) {
  269.  
  270. brick = new GRect(1 + j * (BRICK_WIDTH + BRICK_SEP),
  271. BRICK_Y_OFFSET + i * (BRICK_HEIGHT + BRICK_SEP),
  272. BRICK_WIDTH, BRICK_HEIGHT);
  273. brick.setColor(color);
  274.  
  275. add(brick);
  276. brick.setFilled(true);
  277. }
  278.  
  279. }
  280. }
  281.  
  282. // **********************************************
  283. // видаляємо цеглини
  284. public void deleteBricks(GObject obj) {
  285.  
  286.  
  287. if(obj.getWidth()==BRICK_WIDTH){
  288. try {
  289. xVel = xVel;
  290. paddle2 = 0;
  291. remove(obj);
  292. addScore();
  293. addBrickCounter();
  294. if (rgen.nextBoolean())
  295. yVel = -yVel + 0.04;
  296. else
  297. yVel = -yVel - 0.04;
  298. bounceClip.play();
  299. }
  300. catch(Exception e){
  301. }
  302.  
  303. }
  304. else
  305. {
  306. if(obj.equals(paddle)&&paddle2==0&&ball.getY()>=APP_HEIGHT-PADDLE_HEIGHT-PADDLE_Y_OFFSET-DIAM_BALL)
  307. {
  308. xVel = xVel;
  309. if (rgen.nextBoolean())
  310. yVel = -yVel + 0.04;
  311. else
  312. yVel = -yVel - 0.04;
  313. paddle2 = 1;
  314. }
  315. }
  316.  
  317. }
  318. // перевіряємо чи мьячик не випав за межі поля
  319. private void checklose() {
  320. if (ball.getY() >= HEIGHT - PADDLE_HEIGHT) {
  321. if(lives>=2){
  322. endOfGame();
  323. startAgain();
  324. }
  325. ball.setVisible(false);
  326. addLives();
  327. pause(500);
  328.  
  329. ball.setVisible(true);
  330. ball.setLocation(X_START, Y_START);
  331. }
  332.  
  333. }
  334.  
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement