Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.Object.*;
- import java.applet.*;
- import java.awt.*;
- import java.net.*;
- import java.util.ArrayList;
- public class BlockBreaker extends Applet implements Runnable
- {
- int loaded = 0;
- boolean gameStarted = false;
- boolean paused = false;
- int Key;
- int save, save1;
- int applet_x = 600;
- int applet_y = 500;
- int mouse_x;
- int mouse_y;
- private Image dbImage;
- private Graphics dbg;
- int x = 55;
- int y = 50;
- int number = 1;
- int yN = 1;
- int amountBroken;
- Image ballFast, ballGrow, ballReduce, ballSlow, caught, extraLife, strike;
- Image background, logo1, logo2, logo3, gui, gameover;
- Image blue, broken1, broken2, brown, cyan, green, gray, magenta, orange, red, violet, white, yellow;
- Image ball0, ball1, ball2, ball3, ball4, ball5;
- Image center, left, right;
- Toolkit tk = Toolkit.getDefaultToolkit();
- Rectangle block[] = new Rectangle[9];
- Rectangle block1[] = new Rectangle[9];
- Rectangle block2[] = new Rectangle[9];
- Rectangle block3[] = new Rectangle[9];
- Rectangle block4[] = new Rectangle[9];
- public void init() {
- background = getImage (getCodeBase(), "./images/Background.jpg");
- logo1 = getImage (getCodeBase(), "./images/splash/brick.png");
- logo2 = getImage (getCodeBase(), "./images/splash/breaker.png");
- logo3 = getImage (getCodeBase(), "./images/splash/pressanykey.png");
- gameover = getImage (getCodeBase(), "./images/gameover.png");
- gui = getImage (getCodeBase(), "./images/gui.png");
- brickSetup();
- ObjectLoader.loadImages(this);
- }
- public void brickSetup() {
- for (int i = 0; i < 9; i++) {
- block[i] = new Rectangle(x*number, y, 57, 41);
- block1[i] = new Rectangle(x*number, y*2, 57, 41);
- block2[i] = new Rectangle(x*number, y*3, 57, 41);
- block3[i] = new Rectangle(x*number, y*4, 57, 41);
- block4[i] = new Rectangle(x*number, y*5, 57, 41);
- number++;
- }
- }
- public void updateI(Graphics g) {
- Gui.score += 20;
- amountBroken++;
- }
- public void ballHitBlock(Graphics g) {
- for (int i = 0; i < 9; i++) {
- if (block[i].contains(Ball.ballX, Ball.ballY) && Brick.brick[i] != false) {
- updateI(g);
- Ball.ballSpeedY = -Ball.ballSpeedY;
- if (Brick.random(1) == 1) {
- Ball.ballSpeedX = -Ball.ballSpeedX;
- }
- Brick.brick[i] = false;
- levelClear();
- repaint();
- }
- if (block1[i].contains(Ball.ballX, Ball.ballY) && Brick.brick1[i] != false) {
- updateI(g);
- Ball.ballSpeedY = -Ball.ballSpeedY;
- if (Brick.random(1) == 1) {
- Ball.ballSpeedX = -Ball.ballSpeedX;
- }
- Brick.brick1[i] = false;
- levelClear();
- repaint();
- }
- if (block2[i].contains(Ball.ballX, Ball.ballY) && Brick.brick2[i] != false) {
- updateI(g);
- Ball.ballSpeedY = -Ball.ballSpeedY;
- if (Brick.random(1) == 1) {
- Ball.ballSpeedX = -Ball.ballSpeedX;
- }
- Brick.brick2[i] = false;
- levelClear();
- repaint();
- }
- if (block3[i].contains(Ball.ballX, Ball.ballY) && Brick.brick3[i] != false) {
- updateI(g);
- Ball.ballSpeedY = -Ball.ballSpeedY;
- if (Brick.random(1) == 1) {
- Ball.ballSpeedX = -Ball.ballSpeedX;
- }
- Brick.brick3[i] = false;
- levelClear();
- repaint();
- }
- if (block4[i].contains(Ball.ballX, Ball.ballY) && Brick.brick4[i] != false) {
- updateI(g);
- Ball.ballSpeedY = -Ball.ballSpeedY;
- if (Brick.random(1) == 1) {
- Ball.ballSpeedX = -Ball.ballSpeedX;
- }
- Brick.brick4[i] = false;
- levelClear();
- repaint();
- }
- }
- }
- public void levelClear() {
- if (checkForClear() && checkForClear1() && checkForClear2() && checkForClear3() && checkForClear4()) {
- for(int i = 0; i < 9; i++) {
- Brick.imageChosen = 0;
- Brick.brick[i] = true;
- Brick.brick1[i] = true;
- Brick.brick2[i] = true;
- Brick.brick3[i] = true;
- Brick.brick4[i] = true;
- }
- }
- }
- public boolean checkForClear() {
- 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;
- }
- public boolean checkForClear1() {
- 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;
- }
- public boolean checkForClear2() {
- 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;
- }
- public boolean checkForClear3() {
- 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;
- }
- public boolean checkForClear4() {
- 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;
- }
- public void start() {
- Thread th = new Thread (this);
- th.start();
- }
- public void stop() {
- }
- public void destroy() {
- }
- public void run ()
- {
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
- while (true)
- {
- Ball.setBoundries(this);
- Ball.ballX += Ball.ballSpeedX;
- Ball.ballY += Ball.ballSpeedY;
- Paddle.ballHitBat();
- repaint();
- try
- {
- Thread.sleep (20);
- }
- catch (InterruptedException ex)
- {
- }
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- }
- }
- public void update (Graphics g)
- {
- if (dbImage == null)
- {
- dbImage = createImage (this.getSize().width, this.getSize().height);
- dbg = dbImage.getGraphics();
- }
- dbg.setColor (getBackground());
- dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
- dbg.setColor (getForeground());
- paint (dbg);
- g.drawImage (dbImage, 0, 0, this);
- }
- public void paint (Graphics g) {
- ballHitBlock(g);
- g.drawImage (background, 0, 0, this);
- g.drawImage (gui, 600, 0, this);
- Gui.paint(g);
- Paddle.paint(g, this);
- if (Paddle.lost != true) {
- if (gameStarted && !paused) {
- g.drawImage (center, Paddle.paddleX, Paddle.paddleY, this);
- Brick.row0(g, this);
- } else {
- if (!paused) {
- g.drawImage (center, Paddle.paddleX, Paddle.paddleY, this);
- Ball.ballY = 450;
- Ball.ballX = Paddle.middle;
- }
- g.drawImage (logo1, 117, 25, this);
- g.drawImage (logo2, 45, 219, this);
- g.drawImage (logo3, 64, 407, this);
- }
- if (!paused) {
- switch(Ball.ball) {
- case 0:
- g.drawImage (ball0, Ball.ballX, Ball.ballY, this);
- break;
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- break;
- default:
- g.drawImage (ball0, mouse_x + 10, mouse_y - 10, this);
- }
- }
- }
- }
- // Method to handle mouse down events
- public boolean mouseDown (Event e, int x, int y)
- {
- if (!gameStarted || Paddle.lost != false) {
- Gui.lives = 3;
- Gui.score = 0;
- Ball.ballSpeedX = Ball.ballSpeedY = 3;
- gameStarted = true;
- if (Paddle.lost != false) {
- Paddle.lost = false;
- for(int i = 0; i < 9; i++) {
- Brick.imageChosen = 0;
- Brick.brick[i] = true;
- Brick.brick1[i] = true;
- Brick.brick2[i] = true;
- Brick.brick3[i] = true;
- Brick.brick4[i] = true;
- }
- }
- repaint();
- }
- mouse_x = x;
- mouse_y = y;
- return true;
- }
- public boolean mouseUp (Event e, int x, int y) { //handles events that occur if mouse button is released again.
- return true;
- }
- public boolean mouseMove (Event e, int x, int y) { //handles events that occur if mouse is moved over the applet
- if (x < 500) {
- Paddle.paddleX = x;
- }
- repaint();
- return true;
- }
- public boolean mouseDrag (Event e, int x, int y) { //handles events that occur if mouse is moved over the applet with pressed mouse button
- return true;
- }
- public boolean mouseEnter(Event e, int x, int y) { //handles events that occur if mouse enters the applet
- return true;
- }
- public boolean mouseExit (Event e, int x, int y) { //handles events that occur if mouse leaves the applet
- return true;
- }
- public boolean keyDown (Event e, int key)
- {
- Key = key;
- if (!gameStarted) {
- if (key > 0) {
- Ball.ballSpeedX = Ball.ballSpeedY = 3;
- gameStarted = true;
- }
- }
- if (paused) {
- if (key > 0) {
- paused = false;
- Ball.ballSpeedX = save;
- Ball.ballSpeedY = save1;
- }
- }
- if (key == 1006) {
- if (Paddle.paddleX > 0) {
- Paddle.paddleX-= 15;
- }
- }
- if (key == 1007) {
- if (Paddle.paddleX < 495) {
- Paddle.paddleX+= 15;
- }
- }
- if (key == 112 && !paused) {
- paused = true;
- save = Ball.ballSpeedX;
- save1 = Ball.ballSpeedY;
- Ball.ballSpeedX = Ball.ballSpeedY = 0;
- }
- repaint();
- return true;
- }
- public static void lose() {
- Gui.lives--;
- if (Gui.lives == 0) {
- Ball.ballY = 300;
- Paddle.lost = true;
- Ball.ballSpeedX = Ball.ballSpeedY = 0;
- }
- }
- }
- import java.lang.Object.*;
- import java.applet.*;
- import java.awt.*;
- import java.net.*;
- import java.util.ArrayList;
- import java.net.URL;
- import java.net.URLConnection;
- import javax.swing.*;
- import javax.sound.midi.*;
- import javax.sound.sampled.*;
- import java.util.Enumeration;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipFile;
- public class ObjectLoader
- {
- public static void loadImages(BlockBreaker b) {
- bricks(b);
- ball(b);
- bat(b);
- specials(b);
- }
- public static void bricks(BlockBreaker b) {
- b.blue = b.getImage (b.getCodeBase(), "./images/brick/blue.png");
- b.broken1 = b.getImage (b.getCodeBase(), "./images/brick/broken1.png");
- b.broken2 = b.getImage (b.getCodeBase(), "./images/brick/broken2.png");
- b.brown = b.getImage (b.getCodeBase(), "./images/brick/brown.png");
- b.cyan = b.getImage (b.getCodeBase(), "./images/brick/cyan.png");
- b.green = b.getImage (b.getCodeBase(), "./images/brick/green.png");
- b.gray = b.getImage (b.getCodeBase(), "./images/brick/grey.png");
- b.magenta = b.getImage (b.getCodeBase(), "./images/brick/magenta.png");
- b.orange = b.getImage (b.getCodeBase(), "./images/brick/orange.png");
- b.red = b.getImage (b.getCodeBase(), "./images/brick/red.png");
- b.violet = b.getImage (b.getCodeBase(), "./images/brick/violet.png");
- b.white = b.getImage (b.getCodeBase(), "./images/brick/white.png");
- b.yellow = b.getImage (b.getCodeBase(), "./images/brick/yellow.png");
- }
- public static void ball(BlockBreaker b) {
- b.ball0 = b.getImage (b.getCodeBase(), "./images/ball/ball0.png");
- b.ball1 = b.getImage (b.getCodeBase(), "./images/ball/ball1.png");
- b.ball2 = b.getImage (b.getCodeBase(), "./images/ball/ball2.png");
- b.ball3 = b.getImage (b.getCodeBase(), "./images/ball/ball3.png");
- b.ball4 = b.getImage (b.getCodeBase(), "./images/ball/ball4.png");
- b.ball5 = b.getImage (b.getCodeBase(), "./images/ball/ball5.png");
- }
- public static void bat(BlockBreaker b) {
- b.center = b.getImage (b.getCodeBase(), "./images/bat/center.png");
- b.left = b.getImage (b.getCodeBase(), "./images/bat/left.png");
- b.right = b.getImage (b.getCodeBase(), "./images/bat/right.png");
- }
- public static void specials(BlockBreaker b) {
- b.ballFast = b.getImage (b.getCodeBase(), "./images/bonus/ballfast.png");
- b.ballGrow = b.getImage (b.getCodeBase(), "./images/bonus/ballgrow.png");
- b.ballReduce = b.getImage (b.getCodeBase(), "./images/bonus/ballreduce.png");
- b.ballSlow = b.getImage (b.getCodeBase(), "./images/bonus/ballslow.png");
- b.caught = b.getImage (b.getCodeBase(), "./images/bonus/catch.png");
- b.extraLife = b.getImage (b.getCodeBase(), "./images/bonus/extralife.png");
- b.strike = b.getImage (b.getCodeBase(), "./images/bonus/strike.png");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement