Results 1 to 11 of 11
- 10-01-2011, 01:05 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 6
- Rep Power
- 0
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
Thanks in advanceJava 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(); }
- 10-01-2011, 02:27 PM #2
Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)
You need to show more of your code with the logic that controls when the ball is moving and when it is not.Instead i want the ball back again
What is the ingame variable used for? When is its value changed?
-
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?
- 10-01-2011, 03:14 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 6
- Rep Power
- 0
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.
-
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:
If you don't want the game stopped here, don't call the stopGame method, simple as that.Java Code:if (ball.getRect().getMaxY() > Commons.BOTTOM) { stopGame(); }
- 10-01-2011, 07:05 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 6
- Rep Power
- 0
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...
- 10-01-2011, 07:13 PM #7
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.
- 10-02-2011, 06:35 AM #8
Member
- Join Date
- Oct 2011
- Posts
- 6
- Rep Power
- 0
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
- 10-02-2011, 01:55 PM #9
Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)
You have given the Brick a "destroyed" property that you use to control what the brick does.Im not that good working on properties
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.
- 06-25-2012, 11:06 PM #10
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0
Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)
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 helppublic 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);
}
-
Re: Breakout game ( When im out, i want the ball again to hit the remaining bricks)
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
-
Learning to use classes - breakout game
By isnie in forum New To JavaReplies: 4Last Post: 07-11-2011, 02:07 PM -
Breakout game in the assignment 3 cs106
By dage in forum New To JavaReplies: 5Last Post: 03-16-2011, 05:07 PM -
Making a breakout Game
By JavaIsSoEasy?? in forum Java GamingReplies: 0Last Post: 03-05-2011, 10:52 AM -
Need help with Breakout game
By tfitz666 in forum New To JavaReplies: 9Last Post: 03-22-2010, 05:26 AM -
Breakout Game code help.
By Ceasar in forum New To JavaReplies: 6Last Post: 10-10-2009, 01:30 AM


1Likes
LinkBack URL
About LinkBacks


Bookmarks