I am VERY new to java. My school had a beginners program and for our semester final we have to make a basic game. My game has an object fall from the top of an applet, and you have to shoot it with a gun that you can aim. My problem is that I can't aim my gun, have an object fall, and shoot the object all at the same time. There is probably a lot of better ways to do this, but here's my code.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class moveBox extends Applet
implements KeyListener, MouseListener {
Random rndm = new Random();
int xBomb = rndm.nextInt(450);
int x =250;
int y = 425;
int yShooter = 500;
int counter = 0;
int yBomb = 0;
char xposs;
public void init() {
addKeyListener( this );
addMouseListener( this );
}
public void keyPressed( KeyEvent e ) { }
public void keyReleased( KeyEvent e ) { }
public void keyTyped( KeyEvent e ) {
char c = e.getKeyChar();
xposs = c;
if( c != KeyEvent.CHAR_UNDEFINED)
{repaint();
e.consume();}
}
public void mouseEntered( MouseEvent e ) { }
public void mouseExited( MouseEvent e ) { }
public void mousePressed( MouseEvent e ) { }
public void mouseReleased( MouseEvent e ) { }
public void mouseClicked( MouseEvent e ) {
x= e.getX();
repaint();
e.consume();
}
public void paint( Graphics g ) {
try
{
while(y<501)
{
g.setColor( Color.white );
g.drawRect(xBomb,yBomb,5,5);
yBomb+=1;
g.setColor( Color.black );
g.drawRect(xBomb,yBomb,5,5);
Thread.sleep(10L);
}
y=0;
if(xposs == ',')
{x-=2;}
else if(xposs == '.')
{x+=2;}
else if(xposs == ' ')
while(yShooter>-8)
{
g.setColor( Color.white );
g.drawRect(x+5,yShooter,5,5);
yShooter-=1;
g.setColor( Color.black );
g.drawRect(x+5,yShooter,5,5);
Thread.sleep(1L);
g.drawRect(x,y,20,50);
g.drawRect(200,yBomb,10,10);
}
else if(xposs == 'c')
{
System.exit(1);
}
yShooter=500;
g.drawString(""+x,450,499);
g.drawRect(x,y,20,50);
g.drawRect(200,yBomb,10,10);
yBomb++;
}
catch(Exception e){}
}
}