Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Game

843789Jan 18 2010 — edited Apr 27 2010
I'm was making a simple game in Java until I came across something I couldn't solve. I want the user to click anywhere on the screen, and the box guy will shoot a bullet in the mouse's direction. All I need is the logic of how to go about doing this. Below are the code segments where I'm stuck.
public void mousePressed(MouseEvent e) {
		int xdiff = e.getX() - boxguy1.getxLoc(); /*gets the difference of the X-axes of where the mouse is clicked and*
*where the boxguy1(aka, the player ) is located*/
		int ydiff = e.getY() - boxguy1.getyLoc(); //same as above but for Y-axes.

		int deltaX = xdiff / ydiff; /*deltaX is the # of pixels to move across the X-axis everytime the bullet image is updated.*/
		int deltaY = ydiff / xdiff; /*deltaY is the # of pixels to move across the Y-axis everytime the bullet image is updated*/
		shootBullet(null, deltaX, deltaY, boxguy1.getxLoc(), boxguy1.getyLoc() , 4, 4, "img-audio/bullet.gif");
	}
Obviously, the mousePressed method above is overriding the one from the MouseListener interface. So when the mouse is pressed, the bullet is shot using the shootBullet() method shown below. This is where I am having the problem, because I'm not sure how to get the bullet to fire in the direction of the mouse.
public void shootBullet(Player player, int deltaX, int deltaY,  int x, int y, int width, int height, String imgfile) {
		Bullet boxGuyBullet = new Bullet(player, deltaX, deltaY, x, y, 4, 4, imgfile);
		bullets.add(boxGuyBullet); //bullets is an ArrayList to store the bullets.
	}
The method above basically creates a new bullet and adds it an ArrayList so that on collision, the bullet will be removed.
	public Bullet(BoxGuy player, int deltaX, int deltaY,  int x, int y, int width, int height, String imgfile) {		
		this.deltaX = deltaX;
		this.deltaY = deltaY;
		this.xLoc = x;
		this.yLoc = y;
		this.width = width;
		this.height = height;
		this.file = imgfile;		
		this.img = getImage(imgfile);
		this.rect = new Rectangle(x, y, width, height);
		this.player = player;	
	}
This is the Bullet constructor, assume that there is a instance variable in Bullet for each of the arguments of the constructor. getImage() just gets the image file used for the bullet.
	void draw(Graphics g) {
		g.drawImage(img, xLoc, yLoc , width, height, null);
	}

	void update(BoxGame bg, Graphics g) {
		if(rect.intersects(player.rect)) {
			player.setHealth(player.getHealth() - 1); //if it hits a player, subtract 1 from their health.
		 } 
		if( xLoc < 6 || xLoc > bg.getWidth() - 21  ) {
			bg.bullets.remove(this); //if it goes of the screen, remove the Bullet from the ArrayList
		} else {
			xLoc += deltaX; //updates the X location of the Bullet
			yLoc+ = deltaY; //updates the Y location of the Bullet
			rect.x += deltaX; //updates X location of the bounding rectangle of the Bullet for detecting collision
		        rect.y+ = deltaY; //updates Y location of the bounding rectangle of the Bullet for detecting collision
		}

	}
The code above is also in class Bullet. The draw method is called repeatedly called, so that the frame refreshes and shows the new location of a Bullet. The update method is called to update the locations of the Bullet, so that the bullet actually moves. deltaX and deltaY here are values that determine the direction and speed of the Bullet. e.g. if deltaX is 3, then everytime the image is updated and drawn, the bullet will move right at a speed of three pixels per frame. The argument BoxGame to the update() method is the class that has the main() method and the class that contains the ArrayList of Bullets. It is also the class that implements MouseListener

I can give more info about the problem on request.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 25 2010
Added on Jan 18 2010
8 comments
270 views