Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Breakout game ( When im out, i want the ball again to hit the remaining bricks)

  1. #1
    tulip is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Breakout game ( When im out, i want the ball again to hit the remaining bricks)

    Hi,
    Im new to java programming. Im learning to develop a game similar to the one called breakout. Here's how it works
    > you have a set of bricks to hit using a ball and the paddle

    Im caught in a situation here,

    Whenever i miss the ball, itl take me to the else loop where im sending "Game over" message.
    Instead i want the ball back again, and the other left over bricks to hit.

    Here's the snippet
    Java Code:
     public void paint(Graphics g) {
            super.paint(g);
    
            if (ingame) {
                g.drawImage(ball.getImage(), ball.getX(), ball.getY(),
                            ball.getWidth(), ball.getHeight(), this);
                g.drawImage(paddle.getImage(), paddle.getX(), paddle.getY(),
                            paddle.getWidth(), paddle.getHeight(), this);
    
                for (int i = 0; i < 30; i++) {
                    if (!bricks[i].isDestroyed())
                        g.drawImage(bricks[i].getImage(), bricks[i].getX(),
                                    bricks[i].getY(), bricks[i].getWidth(),
                                    bricks[i].getHeight(), this);
                }
            } else {
    
                Font font = new Font("Verdana", Font.BOLD, 18);
                FontMetrics metr = this.getFontMetrics(font);
    
                g.setColor(Color.BLACK);
                g.setFont(font);
                g.drawString(message,
                             (Commons.WIDTH - metr.stringWidth(message)) / 2,
                             Commons.WIDTH / 2);
            }
    
    
            Toolkit.getDefaultToolkit().sync();
            g.dispose();
        }
    Thanks in advance

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)

    Instead i want the ball back again
    You need to show more of your code with the logic that controls when the ball is moving and when it is not.

    What is the ingame variable used for? When is its value changed?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)

    I don't see how the code you show, your paint method, has anything to do with your problem at hand. But regarding that code, I don't think that you should be disposing of a Graphics object that is given by the JVM. Yes dispose of one you yourself create, but one given by the JVM may be needed elsewhere after the paint method is through with it. Also I am unfamiliar with your call to Toolkit.getDefaultToolkit().sync();. what does this do for your application?

  4. #4
    tulip is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)

    Here is how it works!
    Suppose i have 5 bricks to hit, I take my first shot and hit a brick, it vanishes, there remains other 4 bricks, i miss the ball for my second shot. now i want the ball and the paddle and the remaining 4 bricks, so that i can resume playing
    Java Code:
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Point;
    import java.awt.Toolkit;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    import javax.swing.JPanel;
    
    
    public class Board extends JPanel implements Commons {
    
        Image ii;
        Timer timer;
        String message = "Game Over";
        Ball ball;
        Paddle paddle;
        Brick bricks[];
        int count;
        private static final int NTURNS = 3;
        boolean ingame = true;
        int timerId;
        private int lives = NTURNS;
    
        public Board() {
    
            addKeyListener(new TAdapter());
            setFocusable(true);
    
            bricks = new Brick[30];
            setDoubleBuffered(true);
            timer = new Timer();
            timer.scheduleAtFixedRate(new ScheduleTask(), 1000, 10);
        }
    
            public void addNotify() {
                super.addNotify();
                gameInit();
            }
    
        public void gameInit() {
    
            ball = new Ball();
            paddle = new Paddle();
    
    
            int k = 0;
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 6; j++) {
                    bricks[k] = new Brick(j * 40 + 30, i * 10 + 50);
                    k++;
                }
            }
        }
    
    
        public void paint(Graphics g) {
            super.paint(g);
    
            if (ingame) {                                                                         //WHEN THE GAME STARTS
            
                g.drawImage(ball.getImage(), ball.getX(), ball.getY(),                   //DRAW A BALL
                            ball.getWidth(), ball.getHeight(), this);
                g.drawImage(paddle.getImage(), paddle.getX(), paddle.getY(),      //DRAW A PADDLE
                            paddle.getWidth(), paddle.getHeight(), this);
    
                for (int i = 0; i < 30; i++) {
                    if (!bricks[i].isDestroyed())                                           //THEN BRICKS - HERE isDestroyed() CONSISTS OF A BOOLEAN VALUE FALSE
                        g.drawImage(bricks[i].getImage(), bricks[i].getX(),
                                    bricks[i].getY(), bricks[i].getWidth(),
                                    bricks[i].getHeight(), this);
                    
                }
              
           
            }else {
            	                                                                         // WHEN I MISS A SHOT PRINT "GAME OVER"
            	
                /*Font font = new Font("Verdana", Font.BOLD, 18);
                FontMetrics metr = this.getFontMetrics(font);
    
                g.setColor(Color.BLACK);
                g.setFont(font);
                g.drawString(message,
                             90,
                             Commons.WIDTH / 4);*/
            }
    
    
            //Toolkit.getDefaultToolkit().sync();
           // g.dispose();
        }
    
        private class TAdapter extends KeyAdapter {
    
            public void keyReleased(KeyEvent e) {
                paddle.keyReleased(e);
            }
    
            public void keyPressed(KeyEvent e) {
                paddle.keyPressed(e);
            }
        }
    
    
        class ScheduleTask extends TimerTask {
    
            public void run() {
    
                ball.move();
                paddle.move();
                checkCollision();
                repaint();
    
            }
        }
    
        public void stopGame() {
            ingame = false;
            timer.cancel();
        }
    
    
        public void checkCollision() {
    
            if (ball.getRect().getMaxY() > Commons.BOTTOM) {
                stopGame();
            }
    
            for (int i = 0, j = 0; i < 30; i++) {
                if (bricks[i].isDestroyed()) {
                    j++;
                }
                if (j == 30) {
                    message = "Victory";
                    stopGame();
                }
            }
    
            if ((ball.getRect()).intersects(paddle.getRect())) {
    
                int paddleLPos = (int)paddle.getRect().getMinX();
                int ballLPos = (int)ball.getRect().getMinX();
    
                int first = paddleLPos + 8;
                int second = paddleLPos + 16;
                int third = paddleLPos + 24;
                int fourth = paddleLPos + 32;
    
                if (ballLPos < first) {
                    ball.setXDir(-1);
                    ball.setYDir(-1);
                }
    
                if (ballLPos >= first && ballLPos < second) {
                    ball.setXDir(-1);
                    ball.setYDir(-1 * ball.getYDir());
                }
    
                if (ballLPos >= second && ballLPos < third) {
                    ball.setXDir(0);
                    ball.setYDir(-1);
                }
    
                if (ballLPos >= third && ballLPos < fourth) {
                    ball.setXDir(1);
                    ball.setYDir(-1 * ball.getYDir());
                }
    
                if (ballLPos > fourth) {
                    ball.setXDir(1);
                    ball.setYDir(-1);
                }
    
    
            }
    
    
            for (int i = 0; i < 30; i++) {
                if ((ball.getRect()).intersects(bricks[i].getRect())) {
    
                    int ballLeft = (int)ball.getRect().getMinX();
                    int ballHeight = (int)ball.getRect().getHeight();
                    int ballWidth = (int)ball.getRect().getWidth();
                    int ballTop = (int)ball.getRect().getMinY();
    
                    Point pointRight =
                        new Point(ballLeft + ballWidth + 1, ballTop);
                    Point pointLeft = new Point(ballLeft - 1, ballTop);
                    Point pointTop = new Point(ballLeft, ballTop - 1);
                    Point pointBottom =
                        new Point(ballLeft, ballTop + ballHeight + 1);
    
                    if (!bricks[i].isDestroyed()) {
                        if (bricks[i].getRect().contains(pointRight)) {
                            ball.setXDir(-1);
                        }
    
                        else if (bricks[i].getRect().contains(pointLeft)) {
                            ball.setXDir(1);
                        }
    
                        if (bricks[i].getRect().contains(pointTop)) {
                            ball.setYDir(1);
                        }
    
                        else if (bricks[i].getRect().contains(pointBottom)) {
                            ball.setYDir(-1);
                        }
    
                        bricks[i].setDestroyed(true);
                    }
                }
            }
        }
    }

    And heres the code for brick

    Java Code:
    package breakout;
    
    import javax.swing.ImageIcon;
    
    
    public class Brick extends Sprite {
    
        String brickie = "brickie.png";
    
        boolean destroyed;
    
    
    
        public Brick(int x, int y) {
          this.x = x;
          this.y = y;
    
          ImageIcon ii = new ImageIcon(this.getClass().getResource(brickie));
          image = ii.getImage();
    
          width = image.getWidth(null);
          heigth = image.getHeight(null);
    
          destroyed = false;
    
        }
    
        public boolean isDestroyed()
        {
          return destroyed;
        }
       
    
        public void setDestroyed(boolean destroyed)
        {
          this.destroyed = destroyed;
        }
    
    }
    Last edited by tulip; 10-01-2011 at 03:18 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)

    The problem isn't in the paint method (which should be paintComponent by the way since you're drawing in a Swing JPanel), it's where you call stopGame:

    Java Code:
            if (ball.getRect().getMaxY() > Commons.BOTTOM) {
                stopGame();
            }
    If you don't want the game stopped here, don't call the stopGame method, simple as that.
    tulip likes this.

  6. #6
    tulip is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)

    Thank you! One more doubt....hw can i add some powers to bricks like expand paddle, reduce, bomb etc...

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)

    Make them properties (like destroyed) of the classes and use their values to change the class's behaviour and the way it displays.

  8. #8
    tulip is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)

    Im not that good working on properties, can you suggest with a piece of code ,
    when i hit a particular brick, a property has to be droped, when the paddle takes that property, there shoud be change in the behaviour of the paddle

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)

    Im not that good working on properties
    You have given the Brick a "destroyed" property that you use to control what the brick does.

    You have to decide what behavior of the paddle you want to change and how you want to show that behavior.
    Then how to put a variable into the paddle class that will allow you to do it.

  10. #10
    linux_devil is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Exclamation Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)

    public Paddle(int a){
    if(a ==1){
    ImageIcon ii = new ImageIcon(this.getClass().getResource(paddle1));
    image = ii.getImage();
    }
    else if(a==2) {
    ImageIcon ii = new ImageIcon(this.getClass().getResource(paddle2));
    image = ii.getImage();
    }
    else{
    ImageIcon ii = new ImageIcon(this.getClass().getResource(paddle));
    image = ii.getImage();

    }
    width = image.getWidth(null);
    height = image.getHeight(null);
    resetState();
    }
    public Paddle(){
    this(0);
    }
    I am trying to call a constructor depending on the property , the way i want to change paddle length , but game stops moment this constructor is called , there is no compile time error , but run time . Please help

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)

    Quote Originally Posted by linux_devil View Post
    I am trying to call a constructor depending on the property , the way i want to change paddle length , but game stops moment this constructor is called , there is no compile time error , but run time . Please help
    Please don't hijack someone else's thread for your question. Instead if your question is important, start your own new question thread for this, post your code surrounded by [code] [/code] tags not quote tags, and provide all the details necessary for us to be able to understand your problem so we can help you.

    closing this old thread.

Similar Threads

  1. Learning to use classes - breakout game
    By isnie in forum New To Java
    Replies: 4
    Last Post: 07-11-2011, 02:07 PM
  2. Breakout game in the assignment 3 cs106
    By dage in forum New To Java
    Replies: 5
    Last Post: 03-16-2011, 05:07 PM
  3. Making a breakout Game
    By JavaIsSoEasy?? in forum Java Gaming
    Replies: 0
    Last Post: 03-05-2011, 10:52 AM
  4. Need help with Breakout game
    By tfitz666 in forum New To Java
    Replies: 9
    Last Post: 03-22-2010, 05:26 AM
  5. Breakout Game code help.
    By Ceasar in forum New To Java
    Replies: 6
    Last Post: 10-10-2009, 01:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •