Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import acm.graphics.*;
- import acm.program.*;
- import acm.util.*;
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
- public class game extends GraphicsProgram {
- AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au");
- AudioClip Clip = MediaTools.loadAudioClip("The Walking Dead - Main theme 8-bit.au");
- /** Offset of the paddle up from the bottom */
- private static final int PADDLE_Y_OFFSET = 30;
- private static final long serialVersionUID = 1L;
- /** Width and height of application window in pixels */
- public static final int APP_WIDTH = 700;
- public static final int APP_HEIGHT = 600;
- /** Dimensions of game board (usually the same) */
- private static final int WIDTH = APP_WIDTH;
- private static final int HEIGHT = APP_HEIGHT;
- /** Dimensions of the paddle */
- private static final int PADDLE_WIDTH = 100;
- private static final int PADDLE_HEIGHT = 20;
- /** Number of bricks per row */
- private static final int NBRICKS_PER_ROW = 3;
- /** Number of rows of bricks */
- private static final int NBRICK_ROWS = 3;
- /** Separation between bricks */
- private static final int BRICK_SEP = 4;
- /** Width of a brick */
- private static final int BRICK_WIDTH = (WIDTH - (NBRICKS_PER_ROW - 1)
- * BRICK_SEP)
- / NBRICKS_PER_ROW;
- /** Height of a brick */
- private static final int BRICK_HEIGHT = 20;
- /** Radius of the ball in pixels */
- private static final int DIAM_BALL = 20;
- /** Offset of the top brick row from the top */
- private static final int BRICK_Y_OFFSET = 70;
- /** Offset of the top brick row from the top */
- /** Number of turns */
- private static final int NTURNS = 3;
- /** Create start position */
- RandomGenerator rgen = RandomGenerator.getInstance();
- /** направление и скорость */
- private double xVel = rgen.nextDouble(9, 13);
- private double yVel = rgen.nextDouble(9, 13);
- /** start position */
- private int X_START = WIDTH / 2;
- private int Y_START = HEIGHT / 2;
- private int degt = 0;
- public GRect paddle;
- public int paddle2;
- private GOval ball;
- public GRect brick;
- private GLabel scoreLabel;
- private int score;
- private int bricksLeft;
- private GLabel bricksLabel;
- private GLabel livesLabel;
- private GRect finish;
- private GLabel finishLabel;
- private GLabel startAgain;
- private int lives;
- GImage bac = new GImage("pz_0929.jpg");
- /* Method: run() */
- /** Runs the Breakout program. */
- public void run() {
- this.setSize(APP_WIDTH,APP_HEIGHT);
- setup();
- Clip.loop();
- while (bricksLeft>0) {
- moveBall();
- checkForWalls();
- checkForBrics();
- checklose();
- pause(15);
- }
- endOfGame();
- startAgain();
- }
- //метод який заново починає гру
- private void startAgain() {
- startAgain = new GLabel ("Click to start the game again");
- startAgain.setLocation((getWidth()-startAgain.getWidth())/2, getHeight()/2+finishLabel.getAscent()*2);
- add(startAgain);
- waitForClick();
- remove(finishLabel);
- remove(startAgain);
- remove(finish);
- run();
- }
- // метод який завершує гру
- private void endOfGame() {
- removeAll();
- finish = new GRect (APP_WIDTH, APP_HEIGHT);
- finish.setFilled(true);
- finish.setLocation(0,0);
- finish.setColor(Color.yellow);
- add(finish);
- finishLabel = new GLabel ("The game is over");
- finishLabel.setColor(Color.BLACK);
- finishLabel.setLocation((getWidth()-finishLabel.getWidth())/2, getHeight()/2);
- add(finishLabel);
- }
- /** Створюємо і розміщуємо м'ячик */
- private void setup() {
- bac = new GImage("pz_0929.jpg");
- bac.setSize(HEIGHT, HEIGHT);
- // add(bac, 0, 0);
- drawBricks();
- drawBricks();
- if (rgen.nextBoolean(0.5))
- xVel = -xVel;
- addMouseListeners();
- this.setSize(APP_WIDTH, APP_HEIGHT);
- drawPaddle();
- ball = new GOval(X_START, Y_START, DIAM_BALL, DIAM_BALL);
- ball.setFilled(true);
- add(ball);
- setupScore();
- setupBrickCounter();
- setupLives();
- }
- // ініціалізуємо табло очків
- private void setupScore() {
- score = 0;
- scoreLabel = new GLabel("Score: " + score);
- scoreLabel.setLocation(getWidth() - scoreLabel.getWidth(),
- scoreLabel.getAscent() + 10);
- add(scoreLabel);
- }
- // табло рахунку
- private void addScore() {
- score++;
- scoreLabel.setLabel("Score: " + score + " ");
- scoreLabel.setLocation(getWidth() - scoreLabel.getWidth(),
- scoreLabel.getAscent() + 10);
- }
- // ініціалізуємо лічильник цеглин
- private void setupBrickCounter() {
- bricksLeft = NBRICKS_PER_ROW * NBRICK_ROWS*2;
- bricksLabel = new GLabel(" Bricks left: " + bricksLeft);
- bricksLabel.setLocation(0, bricksLabel.getAscent() + 10);
- add(bricksLabel);
- }
- // додаємо інкременуємо рахівник цеглин
- private void addBrickCounter() {
- bricksLeft--;
- bricksLabel.setLabel(" Bricks left: " + bricksLeft);
- bricksLabel.setLocation(0, bricksLabel.getAscent() + 10);
- }
- private void setupLives() {
- livesLabel = new GLabel("Times dead: " + lives);
- livesLabel.setLocation(getWidth() / 2, livesLabel.getAscent() + 10);
- lives = 0;
- add(livesLabel);
- }
- // інкременуємо рахунок життів
- private void addLives() {
- lives++;
- livesLabel.setLabel("Times dead: " + lives);
- livesLabel.setLocation(getWidth() / 2, livesLabel.getAscent() + 10);
- }
- // ****************************************************************
- private void moveBall() {ball.move(xVel, yVel);}
- // **********************************************************
- // відбиття від цеглин
- public void checkForBrics() {
- if ((getElementAt(ball.getX(), ball.getY()) != null)) {
- // верхня ліва
- deleteBricks(getElementAt(ball.getX(), ball.getY()));
- } else if ((getElementAt(ball.getX(), ball.getY() + DIAM_BALL) != null)) {
- // нижня ліва
- deleteBricks(getElementAt(ball.getX(), ball.getY() + DIAM_BALL));
- } else if ((getElementAt(ball.getX() + DIAM_BALL, ball.getY()) != null)) {
- // верхня права
- deleteBricks(getElementAt(ball.getX() + DIAM_BALL, ball.getY()));
- } else if ((getElementAt(ball.getX() + DIAM_BALL, ball.getY()
- + DIAM_BALL) != null)) {
- // пробелемесы
- deleteBricks(getElementAt(ball.getX() + DIAM_BALL, ball.getY()
- + DIAM_BALL));
- }
- }
- // **********************************************************
- // перевіряємо відбиття від стінок
- public void checkForWalls() {
- /**
- * отскоки от стенки
- */
- // снизу
- // справа и слева
- if (ball.getX() < 0 || ball.getX() > WIDTH - DIAM_BALL) {
- xVel = -xVel;
- paddle2 = 0;
- }
- // от верха
- else if (ball.getY() <= 0) {
- yVel = -yVel;
- paddle2 = 0;
- }
- }
- // *****************************************************************
- // малюємо ракетку
- public void drawPaddle() {
- double x = getWidth() / 2 - PADDLE_WIDTH / 2;
- double y = HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT;
- paddle = new GRect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
- paddle.setFilled(true);
- add(paddle);
- }
- // слухач переміщеня миші
- public void mouseMoved(MouseEvent e) {
- if ((e.getX() < getWidth() - PADDLE_WIDTH / 2)
- && (e.getX() > PADDLE_WIDTH / 2)) {
- paddle.setLocation(e.getX() - PADDLE_WIDTH / 2, getHeight()
- - PADDLE_Y_OFFSET - PADDLE_HEIGHT);
- }
- }
- // **************************************************************
- // малюємо цеглини
- public void drawBricks() {
- for (int i = 0; i < NBRICK_ROWS; i++) {
- Color color = rgen.nextColor();
- for (int j = 0; j < NBRICKS_PER_ROW; j++) {
- brick = new GRect(1 + j * (BRICK_WIDTH + BRICK_SEP),
- BRICK_Y_OFFSET + i * (BRICK_HEIGHT + BRICK_SEP),
- BRICK_WIDTH, BRICK_HEIGHT);
- brick.setColor(color);
- add(brick);
- brick.setFilled(true);
- }
- }
- }
- // **********************************************
- // видаляємо цеглини
- public void deleteBricks(GObject obj) {
- if(obj.getWidth()==BRICK_WIDTH){
- try {
- xVel = xVel;
- paddle2 = 0;
- remove(obj);
- addScore();
- addBrickCounter();
- if (rgen.nextBoolean())
- yVel = -yVel + 0.04;
- else
- yVel = -yVel - 0.04;
- bounceClip.play();
- }
- catch(Exception e){
- }
- }
- else
- {
- if(obj.equals(paddle)&&paddle2==0&&ball.getY()>=APP_HEIGHT-PADDLE_HEIGHT-PADDLE_Y_OFFSET-DIAM_BALL)
- {
- xVel = xVel;
- if (rgen.nextBoolean())
- yVel = -yVel + 0.04;
- else
- yVel = -yVel - 0.04;
- paddle2 = 1;
- }
- }
- }
- // перевіряємо чи мьячик не випав за межі поля
- private void checklose() {
- if (ball.getY() >= HEIGHT - PADDLE_HEIGHT) {
- if(lives>=2){
- endOfGame();
- startAgain();
- }
- ball.setVisible(false);
- addLives();
- pause(500);
- ball.setVisible(true);
- ball.setLocation(X_START, Y_START);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement