Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Break Out Assignment
- This was an assignment from my java class in college. Still looking for the other assignments and will get them up here ASAP
- */
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class BreakOut extends JPanel implements KeyListener,
- ActionListener, Runnable {
- // movement keys
- static boolean right = false;
- static boolean left = false;
- //ball variables
- int ballx = 160;
- int bally = 218;
- //paddle variables
- int paddlex = 160;
- int paddley = 245;
- //block variables
- int blockx = 70;
- int blocky = 50;
- //initialize objects
- Rectangle[] Block = new Rectangle[18];
- Rectangle Ball = new Rectangle(ballx, bally, 5, 5);
- Rectangle Paddle = new Rectangle(paddlex, paddley, 40, 5);
- Thread t;
- BreakeOut() {
- addKeyListener(this);
- setFocusable(true);
- t = new Thread(this);
- t.start();
- }
- public static void main(String[] args) {
- JFrame frame = new JFrame();
- BreakeOut game = new BreakeOut();
- JButton button = new JButton("restart");
- frame.setSize(400, 500);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(game);
- frame.add(button, BorderLayout.SOUTH);
- frame.setLocationRelativeTo(null);
- frame.setResizable(false);
- frame.setVisible(true);
- button.addActionListener(game);
- }
- //configuring objects
- public void paint(Graphics g) {
- g.setColor(Color.LIGHT_GRAY);
- g.fillRect(0, 0, 400, 500);
- g.setColor(Color.cyan);
- g.fill3DRect(Paddle.x, Paddle.y, Paddle.width, Paddle.height, true);
- g.setColor(Color.magenta);
- g.fillOval(Ball.x, Ball.y, Ball.width, Ball.height);
- g.setColor(Color.gray);
- g.fillRect(0, 251, 500, 200);
- g.setColor(Color.orange);
- g.drawRect(0, 0, 343, 250);
- for (int i = 0; i < Block.length; i++) {
- if (Block[i] != null) {
- g.fill3DRect(Block[i].x, Block[i].y, Block[i].width,
- Block[i].height, true);
- }
- }
- if (ballDrop == true || noBlocks == true) {
- g.drawString(status, 70, 120);
- ballDrop = false;
- noBlocks = false;
- }
- }
- //game setup
- boolean ballDrop = false;
- boolean noBlocks = false;
- int vx = -1;
- int vy = -1;
- int count = 0;
- String status;
- //game loop
- public void run() {
- //creating blocks
- for (int i = 0; i < Block.length; i++) {
- Block[i] = new Rectangle(blockx, blocky, 30, 10);
- if (i == 5) {
- blockx = 30;
- blocky = 62;
- }
- if (i == 9) {
- blockx = 30;
- blocky = 74;
- }
- blockx += 31;
- }
- while (ballDrop == false && noBlocks == false) {
- //ball movement
- repaint();
- Ball.x += vx;
- Ball.y += vy;
- //paddle interactivity
- if (left == true) {
- Paddle.x -= 5;
- right = false;
- }
- if (right == true) {
- Paddle.x += 5;
- left = false;
- }
- if (Paddle.x <= 4) {
- Paddle.x = 4;
- } else if (Paddle.x >= 298) {
- Paddle.x = 298;
- }
- //paddle collisions
- if (Ball.intersects(Paddle)) {
- vy = -vy;
- }
- //block collisions
- for (int i = 0; i < Block.length; i++) {
- if (Block[i] != null) {
- if (Block[i].intersects(Ball)) {
- Block[i] = null;
- vy = -vy;
- count++;
- }
- }
- }
- //ending game
- if (count == Block.length) {
- noBlocks = true;
- status = "YOU WON!!";
- repaint();
- }
- //boundary collisions
- if (Ball.x <= 0 || Ball.x + Ball.height >= 343) {
- vx = -vx;
- }
- if (Ball.y <= 0) {
- vy = -vy;
- }
- if (Ball.y >= 250) {
- ballDrop = true;
- status = "YOU LOST THE GAME";
- repaint();
- }
- try {
- Thread.sleep(8);
- } catch (Exception ex) {
- }
- }
- }
- //handling keys
- @Override
- public void keyPressed(KeyEvent e) {
- int keyCode = e.getKeyCode();
- if (keyCode == KeyEvent.VK_LEFT) {
- left = true;
- }
- if (keyCode == KeyEvent.VK_RIGHT) {
- right = true;
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- int keyCode = e.getKeyCode();
- if (keyCode == KeyEvent.VK_LEFT) {
- left = false;
- }
- if (keyCode == KeyEvent.VK_RIGHT) {
- right = false;
- }
- }
- @Override
- public void keyTyped(KeyEvent arg0) {
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- String str = e.getActionCommand();
- if (str.equals("restart")) {
- System.out.print("hi");
- this.restart();
- }
- }
- public void restart() {
- requestFocus(true);
- ballx = 160;
- bally = 218;
- paddlex = 160;
- paddley = 245;
- blockx = 70;
- blocky = 50;
- Ball = new Rectangle(ballx, bally, 5, 5);
- Paddle = new Rectangle(paddlex, paddley, 40, 5);
- Block = new Rectangle[18];
- vx = -1;
- vy = -1;
- ballDrop = false;
- noBricks = false;
- count = 0;
- status = null;
- for (int i = 0; i < Block.length; i++) {
- Block[i] = new Rectangle(blockx, blocky, 30, 10);
- if (i == 5) {
- blockx = 30;
- blocky = 62;
- }
- if (i == 11) {
- blockx = 30;
- blocky = 74;
- }
- blockx += 31;
- }
- repaint();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement