hi, im creating a game where you need to eat fishes to grow.
I just have one problem. How can I make it that when my main fish touches another fish, the other fish dissapears?
heres my code:
Code:package visje;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JApplet;
/**
*
* @author 111946
*/
public class Visje extends JApplet implements MouseMotionListener {
private Image main_fish, greenfish_r, blueredfish_r, bluefish_l, butterflyfish_l;
private Image rockfish_r, rockfish_l;
private Image background1, scoreboard;
int mouseX, mouseY, fishX, fishY;
Graphics bufferGraphics;
Image offscreen;
Dimension dim;
Font scorefont = new Font( "Arial", Font.BOLD, 17 );
int delay = 0;
int mainsize = 2;
int blueredfishsize = 1;
int greenfishsize = 3;
int bluefishsize = 3;
int score = 0;
int xp = 0;
@Override
public void init() {
background1 = getImage( getDocumentBase(), "background 1.png");
main_fish = getImage( getDocumentBase(), "main_right1.png" );
greenfish_r = getImage( getDocumentBase(), "greenfish_r.png" );
bluefish_l = getImage( getDocumentBase(), "bluefish_l.png" );
blueredfish_r = getImage( getDocumentBase(), "blueredfish_r.png" );
butterflyfish_l = getImage( getDocumentBase(), "butterflyfish_l.png" );
rockfish_r = getImage( getDocumentBase(), "rockfish_r.png" );
rockfish_l = getImage( getDocumentBase(), "rockfish_l.png" );
scoreboard = getImage( getDocumentBase(), "scoreboard.png" );
addMouseMotionListener(this);
setSize( 1000, 600 );
dim = getSize();
offscreen = createImage( dim.width, dim.height );
bufferGraphics = offscreen.getGraphics();
}
int gf1X = -100, gf1Y = 200;
int bf1X = 1200, bf1Y = 250;
int bf2X = 1300, bf2Y = 275;
int brf1X = -80, brf1Y = 140;
int brf2X = -200, brf2Y = 100;
int bff1X = 1300, bff1Y = 170;
int bff2X = 1400, bff2Y = 200;
int bff3X = 1050, bff3Y = 290;
int rf1X = -300, rf1Y = 400;
int rf2X = 1600, rf2Y = 450;
@Override
public void paint( Graphics g ) {
dim = getSize();
bufferGraphics.clearRect( 0, 0, dim.width, dim.height );
bufferGraphics.drawImage( background1, 0, 0, this);
bufferGraphics.drawImage( main_fish, fishX, fishY, this );
bufferGraphics.drawImage( greenfish_r, gf1X, gf1Y, this );
bufferGraphics.drawImage( bluefish_l, bf1X, bf1Y, this );
bufferGraphics.drawImage( bluefish_l, bf2X, bf2Y, this );
bufferGraphics.drawImage( blueredfish_r, brf1X, brf1Y, this );
bufferGraphics.drawImage( blueredfish_r, brf2X, brf2Y, this );
bufferGraphics.drawImage( butterflyfish_l, bff1X, bff1Y, this);
bufferGraphics.drawImage( butterflyfish_l, bff2X, bff2Y, this);
bufferGraphics.drawImage( butterflyfish_l, bff3X, bff3Y, this);
bufferGraphics.drawImage( rockfish_r, rf1X, rf1Y, this );
bufferGraphics.drawImage( rockfish_l, rf2X, rf2Y, this );
bufferGraphics.drawImage( scoreboard, 1, 0, this );
bufferGraphics.setFont( scorefont );
bufferGraphics.setColor( Color.WHITE );
bufferGraphics.drawString( "Score: " + score, 10, 20 );
bufferGraphics.drawString( "XP points: " + xp, 10, 40 );
g.drawImage( offscreen, 0, 0, this );
}
@Override
public void update( Graphics g ) {
paint(g);
}
@Override
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
t.start();
}
@Override
public void mouseDragged(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
javax.swing.Timer t = new javax.swing.Timer( 0, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if( fishX < mouseX ) {
fishX +=2;
main_fish = getImage( getDocumentBase(), "main_right1.png" );
}
if( fishX > mouseX ) {
fishX -=2;
main_fish = getImage( getDocumentBase(), "main_left1.png" );
}
if( fishY < mouseY ) {
fishY +=2;
}
if( fishY > mouseY ) {
fishY -=2;
}
repaint();
if( gf1X > 1500 ) {
gf1X = -100;
} else {
gf1X += 2;
}
if( bf1X < -100 ) {
bf1X = 1200;
} else {
bf1X -= 2;
}
if( bf2X < -150 ) {
bf2X = 1100;
} else {
bf2X -= 1;
}
if( brf1X > 1600 ) {
brf1X = -80;
} else {
brf1X += 1;
}
if( brf2X > 1700 ) {
brf2X = -200;
} else {
brf2X += 2;
}
if( bff1X < -200 ) {
bff1X = 1200;
} else {
bff1X -= 1;
}
if( bff2X < -200 ) {
bff2X = 1500;
} else {
bff2X -= 1;
}
if( bff3X < -200 ) {
bff3X = 1350;
} else {
bff3X -= 1;
}
if( rf1X > 1600 ) {
rf1X = -300;
} else {
rf1X +=1;
}
if( rf2X < -300 ) {
rf2X = -1600;
} else {
rf2X -=1;
}
}
});
}

