Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Breakout Game - Need help making blocks

Status
Not open for further replies.

glock2

Technical User
Apr 16, 2007
2
US
We're making the game Breakout in my comp sci. class but I cannot figure out how to make the bricks appear, and then automatically make the specific block(s) disappear when the ball collides into them. (We're currently using intersect() to detect collisions).

Currently, I can get the ball to bounce off both the paddle and the walls, but not off of the bricks. There are no compilation errors; the bricks just don't show.

Any help would be greatly appreciated!
Thanks!

BrickCollection.java
Code:
import apcslib.*;
import chn.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class BrickCollection
{
// int l = n.gotLength();
// int w = n.gotWidth();
	ArrayList<Brick> bricklist=
		new ArrayList<Brick>(); 
	public void add(Brick k)
	{
		bricklist.add(k);
	}

	public void Show(Graphics g)
	{
		for(Brick k:bricklist)
		{
			int x=100;
			int y=100;
			Brick bk = new Brick(x,y);
			k.show(g);
		}
	}
	
	public void Remove(Brick k)
	{
		bricklist.remove(k);
	}
// public Rectangle getBounds(){
 //     int width=(int)w; 
//      int length=(int)h;
  //    return new Rectangle(x,y,width,length);
 //  }
 
}

Brick.java
Code:
import apcslib.*;
import chn.util.*;
import java.awt.*;
import java.awt.event.*;

public class Brick extends DrawableRegularPolygon
{	                                 
	int h,w;
		
	public Brick(int x, int y)
	{
		this.x = x;
		this.y = y;
	}

	public void move()
	{
		
	}

	public void show(Graphics g)
	{
		w = 50;
		h = 10;
			
		g.setColor(Color.black);
		g.drawRect(x,y,w,h);
		g.fillRect(x,y,w,h);
	}
	
	public Rectangle getBounds()
	{
		return new Rectangle(x,y,w,h);
	}
}
 

Are you actually adding any bricks to the collection? What calls add(Brick k) on the BrickCollection instance? What is providing BrickCollecion with its Graphics context? If BrickCollection is part of some bigger infrastructure which calls the Show(Graphics g) method, when does that happen? On a component / container repaint? If so, the add(Brick k) method maybe should trigger a repaint of the BrickCollection. I'm tempted to say 'more info please' ... but the terms of this site forbids posters getting help with homework and such like, and you really should go to your tutor or lecturer with this instead.

By the way, try to keep your naming conventions consistent. Your mixing uppercase and lowercase for the first letter of your method names. The Java convention is lowercase, but pick one and stick with it.

Tim
 
Thanks for the reply!

I think I'm supposed to add the bricks using an Array List but I have no clue how to do that.

And yes, it's part of a larger infrastructure, but I don't think mine is coded correctly.

I zipped up the entire project if that helps:
And I'll try to keep the naming convention in mind; I just got a little frustrated so I started typing more messy

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top