Hey
I am doing a java applet game, atm i have two balls. One moving kinda randomly and another moving with the arrow keys.
The think i need help whit is that i can figure out how to put in a third ball in the game. And the third ball need to move whit some keys like, w, a, s, d or somthing like that.
plz can anyone help me??
Here is my code:
PHP Code:
import java.applet.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.*;
public class TE extends Applet implements KeyListener {
Image bild1;
float x = 80.0f, xPlayer = 20.0f;
float y = 20.0f, yPlayer = 60.0f;
int horPlayer = 0, verPlayer = 0;
float hor = 1.0f, ver = 0.5f;
Random rnd = new Random();
public void init() {
this.addKeyListener(this);
}
public void paint(Graphics g) {
bild1=getImage(getCodeBase(),"prog3.GIF");
BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
Graphics bufferedGraphics = image.getGraphics();
bufferedGraphics.setColor(Color.WHITE);
bufferedGraphics.fillRect(0, 0, image.getWidth(), image.getHeight());
bufferedGraphics.setColor(Color.BLACK);
bufferedGraphics.drawRect(10, 10, 150, 150);
bufferedGraphics.setColor(Color.RED);
int x = (int) this.x;
int y = (int) this.y;
bufferedGraphics.fillOval(x, y, 10, 10);
bufferedGraphics.setColor(Color.BLUE);
x = (int) this.xPlayer;
y = (int) this.yPlayer;
bufferedGraphics.fillOval(x, y, 10, 10);
bufferedGraphics.dispose();
g.drawImage(image, 0, 0, null);
try {
Thread.sleep(15);
} catch (InterruptedException e) {
System.out.println("EERRRROOOOORRRR");
}
uppdatera();
}
public void uppdatera() {
if (x + hor > 150.0 || x + hor < 10.0)
hor = -hor;
x += hor;
if (y + ver > 150.0 || y + ver < 10.0)
ver = -ver;
y += ver;
if (xPlayer + horPlayer > 150.0 || xPlayer + horPlayer < 10.0)
horPlayer = 0;
xPlayer += horPlayer;
if (yPlayer + verPlayer > 150.0 || yPlayer + verPlayer < 10.0)
verPlayer = 0;
yPlayer += verPlayer;
Shape circlePC = new Ellipse2D.Float(x, y, 10.0f, 10.0f);
Shape circlePlayer = new Ellipse2D.Float(xPlayer, yPlayer, 10.0f, 10.0f);
if (circlePC.intersects(circlePlayer.getBounds())) {
hor = -hor;
ver = -ver;
do {
circlePC = new Ellipse2D.Float(x, y, 10.0f, 10.0f);
circlePlayer = new Ellipse2D.Float(xPlayer, yPlayer, 10.0f, 10.0f);
x += hor;
y += ver;
} while (circlePC.intersects(circlePlayer.getBounds()));
}
repaint();
}
private int mathrandom() {
// TODO Auto-generated method stub
return 0;
}
public void keyTyped(KeyEvent e) {
//empty
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
horPlayer = -2;
if (key == KeyEvent.VK_RIGHT) {
horPlayer = 2;
System.out.println("right");
}
if (key == KeyEvent.VK_UP)
verPlayer = -2;
if (key == KeyEvent.VK_DOWN)
verPlayer = 2;
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT && horPlayer != 0)
horPlayer = 0;
if (key == KeyEvent.VK_RIGHT && horPlayer != 0)
horPlayer = 0;
if (key == KeyEvent.VK_UP && verPlayer != 0)
verPlayer = 0;
if (key == KeyEvent.VK_DOWN && verPlayer != 0)
verPlayer = 0;
}
}