Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Smile Breaking out of Breakout! making new classes

    okay I have almost completed a breakout game assignment,, original code is right at the bottom..

    I am attempting to optimise the code thus.

    create a class Brick. which is just one brick

    create a class BrickFormation.. which is an arrayList containing all the bricks constructed with two for loops


    create a class Ball,
    create a class Paddle
    not sure weather to create class ball or leave it in the main method yet!

    this could be a pointless exercise but the reason im doing it is to just try it, and if i wanted to add greater functionality or more complexities as the game progresses i could it more easily if there were separate classes...I think

    Im not sure what my question is, theres a few issues over private/public static i am not completely clear about. and looking for some sugestions or nudges.

    some variables like ApplicationWidth i will need in all classes so that should be public and static it is not going to change at any time during play.

    but brickWidth i will need in the Brick class and the BrickFormation class.
    or should i just make this Public

    I have put it in the Brick class
    Java Code:
    /** Width of a brick */
    	private static double brickWidth;
    
    	/** Height of a brick */
    	private static double brickHeight;
    and have a set method
    Java Code:
    public static void setBrickWidth(double brickWidth) {
    		Brick.brickWidth = brickWidth;
    am i corrct in thinking that this has to be static variable in order to enable me to change it or intialise before I have even made a single brick, is this wat static means ie does not depend on an instance of the class.
    and can I use this set method in the BrickFormation constructor,


    Java Code:
    public BrickFormation(){
    		Brick.setBrickWidth(WIDTH - ((NumBricksPerRow + 1)* BrickSpace) / NumBrickRows);
    		Brick.setBrickWidth(8);
    		bricks = new ArrayList<Brick>();
    		for(int i = 0; i < NumBrickRows; i++){
    			Color colour = getRowColour(i);
    			for (int j = 0 ; j< NumBricksPerRow; j++){
    				bricks.add(new Brick(i, j,colour));
    				
    				
    			}
    		}	
    	}
    so the last thing im not to sure about is in which class should i determine the x and y coordinates of the brick, or where to store them..

    well i think I have to determine x and y in the BrickFormation class and pass them to brick class. because I have a
    Java Code:
    /** brick object */
    	private GRect brick;
    and a
    Java Code:
    public GRect getBrick() {
    		return brick;
    	}
    in the brick class i dont need instance variables for them in Brick class.


    my first Breakout code all inside a graphics program class
    as oppose to posting this ive linked it so a post in the gaming thread as not confuse one code with the other My First Java Game just for a reference
    Last edited by sonny; 04-19-2010 at 10:48 PM.
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    as far as static variables: yes, what you're doing will work, but its kind of... ugly... One restriction you're imposing, for example, is that ll bricks must always be the same width.

    Alternatively, you could calculate brick width in BrickFormation() [like you're doing already], and simply pass it to the constructor of Brick, and let each brick remember its own width and height.

    For what you're trying to do though either approach works so you're good.

    As far as the second question, ultimately Brick needs to have some kind of paint(Graphics g) method, right? if that's the case, it must store its own X and Y coordinates so it knows where to paint itself at.

  3. #3
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    Quote Originally Posted by iluxa View Post
    Alternatively, you could calculate brick width in BrickFormation() [like you're doing already], and simply pass it to the constructor of Brick, and let each brick remember its own width and height.
    yeah im sticking to this way of doing it

    Quote Originally Posted by iluxa View Post
    As far as the second question, ultimately Brick needs to have some kind of paint(Graphics g) method, right? if that's the case, it must store its own X and Y coordinates so it knows where to paint itself at.
    this is where i have come unstuck
    how to get the bricks out of the array and onto the canvass
    i need a public method in Brick to add that brick to the canvass??
    rather than a Brick method in BrickFormation to paint the whole array at once..[which is what I have been trying to do]
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    you can do it either way. it would be easier though if the brick knew how to draw itself. so the brick should have some kind of

    paint(Graphics g)

    method, and BrickFormation should have something like

    Java Code:
    void paintTo (Graphics g) {
      for (int i = 0; i < bricks.size (); i ++) {
        bricks.get (i).paintTo (g);
      }
    }
    and then somewhere else, you'd have smth like

    Java Code:
    class GameCanvas extends JComponent {
      BrickFormation bricks;
      //...
    
      // this gets called by Swing
      public void paint (Graphics g) {
        bricks.paintTo (g);
      }
    }

  5. #5
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Unhappy still stuck!

    i read this
    The acm.graphics.GObject Class)

    i have added this to Brick
    Java Code:
    public void paint(Graphics brick) {
    		paint(brick);
    eclipse imported java.awt.Graphics;
    i am trying to get just one brick for now
    so i am doing this
    Java Code:
    [COLOR="Purple"]public class[/COLOR] breakouttest [COLOR="Purple"]extends[/COLOR] GraphicsProgram  {
    	
    	[COLOR="Purple"]public void[/COLOR] run(){
    		Brick.setBrickHeight(20);
    		Brick.setBrickWidth(20);
    		Brick one = new Brick(100,100,Color.black);
    		Brick.[COLOR="Red"][U]paint[/U][/COLOR](one);
    	}
    }
    but i still cant get a brick onto the GCanvas:confused:

    i am using acm.GraphicsProgram,, not swing.. i dont know if this makes a difference??:confused:
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    What you want is to have your Brick class extend GRect. This means that a Brick is a GRect, and you can add() a Brick to your canvas. Don't worry about paint() -- the ACM classes hide that behind the scenes for you. You're just going to be concerned with add() and remove().

    I don't think you want brickWidth and brickHeight to be static. It just feels wrong conceptually, especially when you have a setBrickWidth() and setBrickHeight() method. The static keyword is about making one copy of a variable that covers all instances of a class. Do you really mean for every Brick in the universe to change its size when one of those methods is called? It makes more sense to set width and height in the constructor, and not have setter methods at all -- bricks don't change size once they're created. You could have a static DEFAULT_WIDTH and DEFAULT_HEIGHT, and have a default constructor (one with no parameters) that uses them. It's kind of the same thing, but really different in philosophy. The static keyword might be useful for an "int numBricks" variable, for instance (if you decide you want to keep such a thing in your Brick class).

    A Ball class (that extends GOval) is a good idea. You might at some point have multiple Balls in the game, and it would be good for each one to know its own position and velocity, rather than keeping track of all of them in your main program.

    Show us your work-in-progress classes and let's see where you are.

    -Gary-

  7. #7
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by gcalvin View Post
    What you want is to have your Brick class extend GRect. This means that a Brick is a GRect, and you can add() a Brick to your canvas. Don't worry about paint() -- the ACM classes hide that behind the scenes for you. You're just going to be concerned with add() and remove().
    Okay that appears to be more intuitive. Ill do some more bits when i get home and post later thanks

    Quote Originally Posted by gcalvin View Post
    I don't think you want brickWidth and brickHeight to be static. .......It makes more sense to set width and height in the constructor,
    do you mean the Brick constructor or the BrickFormation constructor?
    I guess it should be Brick constructor? it think i got skewd off course worrying about how to set the height and width,, eclipse had prompted me to make it static and i just accepted it and agreed without thinking round it too much.
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by sonny View Post
    do you mean the Brick constructor or the BrickFormation constructor?
    I guess it should be Brick constructor? it think i got skewd off course worrying about how to set the height and width,, eclipse had prompted me to make it static and i just accepted it and agreed without thinking round it too much.
    Yes, in the Brick constructor. When I think about these things, I try to think about them coming into being in The Real World, and when a brick comes into being, it must immediately have a width and height that do not change during its "lifetime". On the other hand, a brick in the Breakout world might have a changeable color (not usually, but maybe there's some sort of powerup that changes bricks' colors and point values, for instance) so you could set the color in the constructor, but also have a setter for color.

    So your Brick is going to have width, height, position and color all inherited from GRect. Notice that GRect has two constructors -- one with position parameters and one without. You should probably have a similar two, but add a color parameter to each, and maybe have yet another with just a color parameter, in which you would get width and height from constants stored in your class.

    Also note that since you are extending GRect, you do not need your own variables for width, height, fill color, and position -- you inherit all those from the superclass. So start simple -- just do the basics, and replace the GRects in your existing program with your new Bricks, and see if you can get that much working.

    -Gary-

  9. #9
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up Coming along nicely

    extending GRect and GOval works great. its sort of like my Brick has a GRect nested inside of it.

    i had a few problems with constructors till i realised i needed super constructors and after a bit of googling sorted that. and the this thing is starting to become much clearer now.

    my Brick class and Ball class both now work in my main code which, still looks very messy with stuff commented out all over the place.

    I had a bit of hitch with Ball because i had a "Cannot make a static reference to the non-static method" error. some more googling before it dawned on me that you cant create the ball in one method then move it in another.......(very obvious once you think about it)
    Unless..... you have an IVAR for the ball... that dawned on me about half an hour later..... But it still wouldn't work... then i realised my Ball Ivar was still of type GOval not type Ball, once i changed that.. whoopee...
    all that remains for me to do now is the Checkforcollision() method and HitThe Ball(); method. before i come back to BrickFormation class. i'm kinda half tempted to say well why do i need a BrickFormation class anyway but then again I might want to do some kind of computation on the arrayList for bonus levels or whatever.

    its funny how this top down vs Bottom up (or from the middle as JosA might suggest) starts to dawn on you. I got all tied up in BrickFormation because logically in setting up the game its one of the first things to do, from the perspective of writing OOP code to solve the problem, it doesn't help to think from one direction or the other you have to think about solving the problem from both directions. just because you cant do x yet doesn't mean you cant solve y, if you solve y then maybe it will be easier to solve x ..

    Anyway I'll spare you the main run class for now but, here is Brick class.. which is very basic for now
    Java Code:
    import java.awt.Color;
    import acm.graphics.GRect;
    
    public class Brick extends GRect{
    	
    	
    	public Brick(double x, double y, double width, double height,Color color) {
    		super(x,y,width,height);
    		this.setColor(color);
    		this.setFilled(true);
    		this.setFillColor(color);
    	}
    	 
    	
    	
    	
    	public String toString(){
    		return (this.getFillColor()+" "+
    				this.getX()+" "+
    				this.getY());
    		
    	}
    and here is the Ball class
    Java Code:
    import java.awt.Color;
    import acm.graphics.GOval;
    
    public class Ball extends GOval {
    			
    	private double Width = Breakout.APPLICATION_WIDTH;
    	private double Height = Breakout.APPLICATION_HEIGHT;
    	private double xVel;
    	private double yVel;
    	private double diam = this.getWidth();
    	
    	
    	
    	public Ball(double x,double y,double radius) {
    		super (x-radius,y-radius,radius*2,radius*2);
    		this.setFilled(true);
    		this.setColor(Color.WHITE);
    		this.setFillColor(Color.WHITE);
    		xVel = 2;
    		yVel = -2;
    		}
    	
    	public void moveBall(){
    		this.move(xVel, yVel);
    		if(this.getX() >= Width-diam || this.getX() <= 0)xVel = -xVel;
    		if(this.getY() <= 0)yVel = -yVel;
    	}
    	
    	public boolean ballIsOut(){
    		if(this.getY() < Height){
    			return false;
    		}
    		return true;
    		
    	}
    }
    and finally my BrickFormation class
    Java Code:
    import java.awt.Color;
    import java.util.ArrayList;
    
    public class BrickFormation {
    	
    	/** Dimensions of game board */
    	private static final double WIDTH = Breakout.APPLICATION_WIDTH;
    	private static final double HEIGHT = Breakout.APPLICATION_HEIGHT;
    	
    	/** Offset of the top brick row from the top */
    	private static final int BRICK_Y_OFFSET = 70;
    	
    	/** Separation between bricks */
    	private double BrickSpace = Breakout.APPLICATION_WIDTH/100;
    	
    	private int brickindex;
    	
    		
    	private ArrayList<Brick> bricks;
    	
    	
    	public BrickFormation(int NumBricksPerRow,int NumBrickRows){
    		double WidthBricks =(WIDTH - ((NumBricksPerRow + 1)* BrickSpace) / NumBrickRows);
    		double HeightBricks = 8;
    		bricks = new ArrayList<Brick>();
    		for(int i = 0; i < NumBrickRows; i++){
    			Color colour = getRowColour(i);
    			for (int j = 0 ; j< NumBricksPerRow; j++){
    				bricks.add(new Brick
    						(BrickSpace + (j * (WidthBricks + BrickSpace)),
    						BRICK_Y_OFFSET + (i * (HeightBricks + BrickSpace)),
    						WidthBricks,
    						HeightBricks,
    						colour));
    			}
    		}
    		brickindex = 0;
    	}
    
    
    	private Color getRowColour(int i) {
    		switch (i) {
    		case 1:
    			return (Color.RED);
    		case 2:
    		case 3:
    			return (Color.ORANGE);
    		case 4:
    		case 5:
    			return (Color.YELLOW);
    		case 6:
    		case 7:
    			return (Color.GREEN);
    		case 8:
    		case 9:
    			return (Color.CYAN);
    		default:
    			return (Color.RED);
    		}
    	}
    	
    	
    	
    	
    	public ArrayList<Brick> setUpBricks(){
    		return bricks;
    		
    	}
    
    }
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  10. #10
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I just looked through this post, and feel I must add this... Why do you need a ball the size of the application? You're setting Ball#Width to APPLICATION_WIDTH, (and height in the same way). That's one damn big ball. Other than that, I can't really comment, because I don't know anything about ACM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by sonny View Post
    and here is the Ball class
    Java Code:
    import java.awt.Color;
    import acm.graphics.GOval;
    
    public class Ball extends GOval {
    			
    	private double Width = Breakout.APPLICATION_WIDTH;
    	private double Height = Breakout.APPLICATION_HEIGHT;
    	private double xVel;
    	private double yVel;
    	private double diam = this.getWidth();
    	
    	
    	
    	public Ball(double x,double y,double radius) {
    		super (x-radius,y-radius,radius*2,radius*2);
    		this.setFilled(true);
    		this.setColor(Color.WHITE);
    		this.setFillColor(Color.WHITE);
    		xVel = 2;
    		yVel = -2;
    		}
    	
    	public void moveBall(){
    		this.move(xVel, yVel);
    		if(this.getX() >= Width-diam || this.getX() <= 0)xVel = -xVel;
    		if(this.getY() <= 0)yVel = -yVel;
    	}
    	
    	public boolean ballIsOut(){
    		if(this.getY() < Height){
    			return false;
    		}
    		return true;
    		
    	}
    }
    I can't say I like this much:
    Java Code:
    	private double Width = Breakout.APPLICATION_WIDTH;
    	private double Height = Breakout.APPLICATION_HEIGHT;
    It intertwines your Ball class so tightly with the Breakout class that it just makes me wince. I can't absolutely say it's wrong for your application, but it doesn't give me the nice harmonious Zen feeling that I want from my objects. :)

    Your Ball class seems to "know" too much about the world it's in, rather than just being a dumb ball. I would limit the ball's knowledge to its size, shape, color, position, and direction/velocity, and leave it to another class to actually move it around.

    But all in all, you are very much getting the idea.

    You may find that you don't strictly need the ArrayList in your BrickFormation class, at least in terms of the basic game. Simply by virtue of add()ing the Bricks to your canvas, you maintain some reference to them. You'll get a reference to each individual Brick when your collision method detects a hit on it, and that's all you need for the basic game. But a Collection could be useful if you want to make changes to Bricks on the fly -- change their color, have them march down the screen, something like that. And your BrickFormation class may be useful down the road if you want to have some sort of level editor to make custom configurations.

    Keep it up, you're doing great!

    -Gary-

  12. #12
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up

    well my thinking was that the ball needs to know where it is in relation to walls.
    i could call theses variables from the main class every time i wanted to know if the ball had hit a wall, but it seemed to make sense that when i create a new ball, to tell the ball where the walls are rather than the ball having to call the main method to discover where it is in relation to the wall, every time it moves.
    I take the point that upon first glance Width and Height might not be the best names for the variables so i'll change that...
    @ Singing_Boyo.. & dripping with sarcastic humour..."thats a lovely, lovely, Bloody marvelous suggestion" (my family hail from Maesteg theres a sarcastic joke in there somwhere)
    but i think that is work for tomorrow because i think i just passed my xkcd: Ballmer Peak

    EDIT: i say tomorrow but i just noticed 2 cans of Dulux emulsion, a pack of sand paper and a pack of three new paintbrushes and a paint roller in the hallway! Hmmm!!
    Last edited by sonny; 04-21-2010 at 04:16 AM. Reason: added comment
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  13. #13
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up the Joys of Decorating!!!!

    after a day spend up and down ladders brush in one hand paint tray in the other. I think I get the point.

    A paintbrush is just a paintbrush it doesn't need to know how close to the edge of skirting board it is, it just releases paint from the bristles to the surface. it might also know how much, or maybe just if, it has paint left in the bristles. but, it wont dip itself in the paint can when its empty. TheDecorator Class will have the DipTheBrush() method. and the MoveTheBrush();

    the point is today i've using the brush to put undercoat on the Dinning room woodwork and tomorrow i'll be using the same brush to put a new coat of gloss on the front door:(
    I might be using Ball in breakout, but next week i might be using the same ball in tennis.. :)
    if i ever get the decorating finished!


    okay Ball Class, ill add some other bounce methods later to maybe

    Java Code:
    public class Ball extends GOval {
    
    	private double xVel;
    	private double yVel;
    	
    
    	public Ball(double x, double y, double radius) {
    		super(x - radius, y - radius, radius * 2, radius * 2);
    		this.setFilled(true);
    		this.setColor(Color.WHITE);
    		this.setFillColor(Color.WHITE);
    		xVel = 2;
    		yVel = -2;
    	}
    	
    	
    	public void bounceX(){
    		xVel = -xVel;
    	}
    	public void bounceY(){
    		yVel = -yVel;
    	}
    }
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  14. #14
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You're getting it. :) You may want to add a getCollisionPoints to your Ball class that would return an array of GPoint objects. Since you're checking for collisions a lot, it may make sense to compute those points once in your Ball constructor and store them as instance variables. Initially they will probably be the four corners of the GOval's bounding box, but at some point you may want a "better" set of points, and this will give you the flexibility to do that.

    -Gary-

  15. #15
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    it may make sense to compute those points once in your Ball constructor and store them as instance variables.
    have been so busy decorating not had enough chance play with this but I am a bit confused.

    when i create the four GPoints in the constructor, they do not move when the ball moves... unless i move each GPoint every time the ball moves..... so why create IVARS

    a get collisions method then has to recalculate where the points are relative to Ball.getX() and Ball.getY().......or..... move the GPoints in the getCollisions method... hmmm, i may have just answered my own question
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  16. #16
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by sonny View Post
    when i create the four GPoints in the constructor, they do not move when the ball moves... unless i move each GPoint every time the ball moves..... so why create IVARS
    Good point. Maybe you don't want to store them as instance variables, but just have a method to calculate them on the fly, based on the Ball's location and size. You're basically going to be checking for collisions every time you move(), right? So it doesn't make much difference whether you calculate your points at move() time or at getCollisions() time. Either way, I think it makes sense in terms of encapsulation to have the Ball compute its own array of collision points, rather than have outside code do it.

    -Gary-

  17. #17
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    okay,, but move is done by outside code(main method), not the ball class itself. if i move/ recalculate the Gpoints in ball how can i be certain that they have moved the same dx,dy as the main method moved the ball?...

    EDIT dont move them recalculate them predicated on getX(),getY();
    Last edited by sonny; 04-26-2010 at 02:36 AM. Reason: re think
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  18. #18
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by sonny View Post
    okay,, but move is done by outside code(main method), not the ball class itself. if i move/ recalculate the Gpoints in ball how can i be certain that they have moved the same dx,dy as the main method moved the ball?...
    You can either override GOval's move() method(s), or just write a getCollisionPoints method that computes and returns the points when they're needed (probably better). So your main code would do something like this:
    Java Code:
            for each Ball
                    for each collisionPoint in ball.getCollisionPoints()
                            detect and do something about the collision
    By encapsulating the logic of computing the collision points you keep your code more readable, and potentially more flexible.

    -Gary-

  19. #19
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by gcalvin View Post
    By encapsulating the logic of computing the collision points you keep your code more readable, and potentially more flexible.
    thanks Gary I get that bit and now I have a method it was the IVAR thing that had me confused it makes much more sense to calculate on the fly based on Ball.getX(), Ball.getY(). and ill play with the two for each loops( one for each ball :cool:) when i get done with the paint rollers tomorrow.

    ultimately getCollission in will need to be a predicate method - i think - because if there were to be two collisions in the ArrayList, ie topRight and topLeft and i did bounceY() for each, the ball would go straight on because yVel would change twice..

    but here is my method so far,
    many thanks and kind regards
    Sonny

    Java Code:
    public ArrayList<GPoint> getCollisions(){
    		GPoint topLeft = new GPoint(this.getX(), this.getY());
    		GPoint topRight = new GPoint(this.getX()+this.getWidth(), (this.getY()));
    		GPoint botLeft = new GPoint(this.getX(), this.getY()+this.getHeight());
    		GPoint botRight = new GPoint(this.getX()+this.getWidth(), this.getY()+this.getHeight());
    		ArrayList<GPoint> collisionPoints = new ArrayList<GPoint>();
    		collisionPoints.add(topLeft);
    		collisionPoints.add(topRight);
    		collisionPoints.add(botLeft);
    		collisionPoints.add(botRight);
    		return collisionPoints;
    		
    	}
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  20. #20
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up

    Okay i finally got my check for collisions Array list to work in the main code and made some simplifications. :D (but, I havent done a nested for each Ball yet)

    I also found a wee bug while testing
    if the ball hits the edge of the paddle it appears to roll along the paddle then fall off the other end. I think this is because xVel remains the same so the ball gets caught on the paddle by the array of collision points that surround it, like a ball getting caught in a tube or a hoop on a pole. I will fix this by changing xVel aswell as yVel if the ball hits the end of the paddle, similar to the way it worked in my original code. and i perhaps could rewrite the checkForCollision() without a nested if/else and my break statement might then have the desired effect.

    in addition perhaps if i hit the side of a brick my yVel should remain the same and just change xVel. that could improve playability.

    so Ball class getCollision method
    Java Code:
    public ArrayList<GPoint> getCollisions(){
    		ArrayList<GPoint> collisionPoints = new ArrayList<GPoint>();
    		collisionPoints.add(new GPoint(this.getX(), this.getY()));
    		collisionPoints.add(new GPoint(this.getX()+this.getWidth(), (this.getY())));
    		collisionPoints.add(new GPoint(this.getX(), this.getY()+this.getHeight()));
    		collisionPoints.add(new GPoint(this.getX()+this.getWidth(), this.getY()+this.getHeight()));
    		return collisionPoints;
    		
    	}
    and the checkForCollision() method in main (which I am about to edit)
    Java Code:
    private void checkForCollision() {
    		for(GPoint points: Ball.getCollisions()){
    			this.gobj = getElementAt(points);
    			if (gobj != null) {
    				if (gobj == paddle) {
    					Ball.bounceY();
    					break;
    				} else {
    					remove(gobj);
    					bricksLeft--;
    					Ball.bounceY();
    				}
    			}
    			
    		}
    the good news is the decorating is nearly finished:D

    unfortunately the wife's code looks like this :(
    Java Code:
    if (sonny.isFinishedDecorating){
    		sonny.StartTheGardening();
    	}
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

Page 1 of 2 12 LastLast

Similar Threads

  1. Need help with Breakout game
    By tfitz666 in forum New To Java
    Replies: 9
    Last Post: 03-22-2010, 06:26 AM
  2. Replies: 1
    Last Post: 08-11-2009, 03:00 PM
  3. Another Breakout question
    By jumpstart in forum New To Java
    Replies: 3
    Last Post: 07-29-2009, 05:48 AM
  4. Making A Set Of Classes "Importable"
    By JDCAce in forum Advanced Java
    Replies: 4
    Last Post: 12-05-2008, 10:11 AM
  5. Need help with Java classes for making a program.
    By TheDarkReverend in forum Advanced Java
    Replies: 2
    Last Post: 11-28-2008, 05:50 AM

Posting Permissions

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