ok i have this game where you pick the angle and power and then shoot. as is i draw things in the run statement. however i think im going about this all wrong. for example when the ball hits the ground i would like to draw an explosion. is there a good way to get different animations to pop up when things happen? perhaps make them their own threads? for example when space bar is pressed stop current thread and start thread that draws the flight of the ball? then when it hits ground stop that thread and start one that draws exposion?
thanks for any help…
here is the code i have …
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class Test extends Applet implements Runnable, KeyListener {
int x = 0;
int y = 500;
int z = 0;
int q = 44;
int d=0, p =0;
double xfloat = 0;
double yfloat = 0;
double vx = 0,vy = 0;
double angle = 0;
double power = 0;
final double gravity = 0.3;
double pi = 2*Math.PI;
Thread th = new Thread(this);
BufferedImage offscreen = new BufferedImage(1000,600,BufferedImage.TYPE_INT_RGB);
Graphics2D bufferGraphics = offscreen.createGraphics();
boolean keys[];
boolean paused;
Image sprite[] = new Image[30];
shot shoot = new shot();
public void init() {
setBackground(Color.white);
addKeyListener(this);
keys = new boolean[65536];
th.start();
Characters create = new Characters();
for (int i = 0; i < 30; i++)
{
sprite[i] = create.createBSprite(q);
q--;
}
paused = false;
}
public void start() {
paused = false;
}
public void stop() {
paused = true;
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
//override paint and update to stop any system rendering
public void paint(Graphics g) {}
public void update(Graphics g) {}
public void run() {
while (th != null) {
//game loop
if(!paused) {
Graphics2D g = (Graphics2D)getGraphics();
//game logic
xfloat += vx;
yfloat += vy;
vy += 0.3;
if(keys[KeyEvent.VK_RIGHT] && x < 374) {
x++;
}
if(keys[KeyEvent.VK_LEFT] && x > -3) {
x--;
}
if(keys[KeyEvent.VK_UP] && z < 15) {
z++;
angle += 5;
}
if(keys[KeyEvent.VK_DOWN] && z > 0) {
z--;
angle -= 5;
}
if(keys[KeyEvent.VK_SPACE] ) {
d = 10;
p = 10;
xfloat = x+63;
yfloat = y+20;
power += 2;
vx = (power / 10.0) * (float)Math.cos((pi * (double)angle) / 360.0);
vy = -((power / 10.0) * (float)Math.sin((pi * (double)angle) / 360.0));
}
if (xfloat > 450 && xfloat < 550 && yfloat > 280 && yfloat < 290)
vy = -((power / 15.0) * (float)Math.sin((pi * (double)angle) / 360.0));
if(yfloat > 600 || xfloat > 1000)
power = 0;
if(yfloat > 564)
{
d = 0;
p = 0;
}
//rendering
bufferGraphics.setColor(Color.WHITE);
bufferGraphics.fillRect(0, 0, getWidth(), getHeight());
bufferGraphics.setColor(Color.red);
bufferGraphics.fillRect(0,300,30,300);
bufferGraphics.fillRect(450,300,100,300);
bufferGraphics.setColor(Color.green);
bufferGraphics.fillRect(0,565,1000,35);
bufferGraphics.setColor(Color.magenta);
bufferGraphics.fillOval((int)xfloat,(int)yfloat,d,p);
bufferGraphics.setColor(Color.blue);
bufferGraphics.fillRect(0,0,(int)power,10);
bufferGraphics.drawImage(sprite[z], x, y,this);
g.drawImage(offscreen,0,0,this);
g.dispose();
try {
Thread.sleep(20);
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
}
else {
try {
Thread.yield();
Thread.sleep(100);
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
}
class Characters
{
BufferedImage createBSprite(int x)
{
BufferedImage sprite = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2D = sprite.createGraphics();
g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
g2D.fillRect(0,0,100,100);
g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
g2D.setColor(Color.black);
//head
g2D.fillOval(42,20,17,16);
//neck
g2D.fillRect(48,35,7,4);
//body
g2D.fillArc(42,36,17,16,0,180);
g2D.fillRect(43,44,16,10);
//left leg and foot
g2D.fillRect(44,54,3,8);
g2D.fillRect(44,62,6,3);
//right leg and foot
g2D.fillRect(55,54,3,8);
g2D.fillRect(55,62,6,3);
//right arm
//g2D.fillRect(58,44,10,3);
//g2D.fillOval(64,41,12,8);
g2D.drawLine(58,44,68,x);
g2D.drawLine(58,45,68,x+1);
g2D.drawLine(58,46,68,x+2);
//left arm
g2D.fillRect(36,44,8,3);
g2D.fillOval(32,43,5,5);
return sprite;
}
}