public class Missile_Commander extends Applet
implements MouseListener ,Runnable
{
Thread myThread;
Random rand;
Color color;
Image Anzeige;
Graphics an;
ArrayList rak;
ArrayList ex;
boolean schuss = false;
int menu;
int silo[];
int maxSilo[];
boolean Stadt[];
float Speed;
int explusion;
float gSpeed;
int wellen;
boolean ende;
public void init()
{
rand = new Random();
Anzeige = createImage(700,500);
an = Anzeige.getGraphics();
rak = new ArrayList();
ex = new ArrayList();
Stadt = new boolean[6];
for(int i = 0; i != 6; i++){
Stadt[i] = true;
}
silo = new int[3];
maxSilo = new int[3];
addMouseListener( this );
}
public void start()
{
myThread = new Thread(this);
myThread.start();
for(int i = 0; i != 3; i++){
silo[i] = 10;
maxSilo[i] = 10;
}
Speed = 1.5f ;
explusion = 10;
}
public void stop(){
myThread.stop();
}
public void run(){
int level = 1;
while(1 != 26){
gSpeed = 0.5f;
wellen = level + rand.nextInt(level * 2) + 1;
ende = false;
while(!ende){
play();
gegner();
repaint();
try {Thread.sleep(10);}
catch (InterruptedException e) { }
}
}
}
public void farbe(int r , int g , int b)
{
color = new Color(r,g,b);
an.setColor(color);
}
public void mouseEntered( MouseEvent e ) { }
public void mouseExited( MouseEvent e ) { }
public void mouseClicked( MouseEvent e ) { }
public void mousePressed( MouseEvent e ) {
int button = e.getButton();
if(button == 1){
int mx = e.getX();
int my = e.getY();
if(my < 410){
int n[] = { Math.abs(50 - mx) , Math.abs(350 - mx) , Math.abs(650 - mx) };
int nah = 0;
if(silo[0] == 0){
if(silo[1] == 0){
nah = 2;
}else{
nah = 1;
}
}
if(n[nah] > n[1] && silo[1] != 0){
nah = 1;
}
if(n[nah] > n[2] && silo[2] != 0){
nah = 2;
}
if(silo[nah] != 0){
silo[nah] --;
Rakete r = new Rakete(50 + nah * 300 , 435 , mx , my , Speed , -1 );
rak.add(r);
}
}
}
e.consume();
}
public void mouseReleased( MouseEvent e ) { }
public void update( Graphics g){
malen();
g.drawImage( Anzeige ,0,0, this );
}
public void paint(Graphics g)
{
update(g);
}
public float Pytago(int von_x,int von_y,int zu_x,int zu_y){
int x_weg = zu_x - von_x;
int y_weg = zu_y - von_y;
if(x_weg < 0){x_weg = -x_weg;}
if(y_weg < 0){y_weg = -y_weg;}
return (float)Math.sqrt(x_weg * x_weg + y_weg * y_weg);
}
public void malen(){
hintergrund();
male_Raketen();
male_Explusion();
Stadt();
an.drawString("rak:"+rak.size()+"ex:"+ex.size(),10,10);
}
public void hintergrund(){
farbe(0,0,0);
an.fillRect(0,0,700,500);
farbe(255,255,0);
an.fillRect(0,450,700,50);
for(int i = 0; i != 3; i++){
hügel(15 + i * 300,i);
}
}
public void male_Raketen(){
for(int i = 0; i != rak.size();i++){
Rakete r = (Rakete)rak.get(i);
r.malen(an);
}
}
public void male_Explusion(){
for(int i = 0; i != ex.size();i++){
Explusion e = (Explusion)ex.get(i);
e.malen(an);
}
}
public void hügel(int x,int i){
int y = 435;
farbe(255,255,0);
int X[] = { x , x + 20 , x + 50 , x + 70};
int Y[] = { y + 20 , y , y , y +20};
an.fillPolygon(X,Y,4);
farbe(0,200,0);
an.drawString(silo[i] + " / " + maxSilo[i], x + 20, y + 20);
}
public void Stadt(){
an.setColor(Color.blue);
for(int i = 0; i != 6; i++){
if(Stadt[i]){
if(i < 3){
an.fillRect(i * 70 + 100,430,40,20);
}else{
an.fillRect(i * 70 + 200 ,430,40,20);
}
}
}
}
public void play(){
for(int i = 0 ; i != rak.size();i++){
Rakete r = (Rakete)rak.get(i);
r.fly();
if(r.fertig() == true){
NeuEx(r.xp,r.yp);
rak.remove(i);
i--;
}
}
for(int i = 0 ; i != ex.size();i++){
Explusion e = (Explusion)ex.get(i);
e.weiter();
if(e.fertig() == true){
ex.remove(i);
i--;
}
}
}
public void NeuEx(float x,float y){
Explusion e = new Explusion(x,y,100,0.2f,155);
ex.add(e);
}
public void gegner(){
if(rand.nextInt(200) == 1 && wellen > 0){
wellen--;
int z = rand.nextInt(8);
while(!Stadt[z]){
z = rand.nextInt(8);
}
int x;
if(z < 3){
x = z * 70 + 100;
}else{
x = z * 70 + 200;
}
Rakete r = new Rakete(rand.nextInt(701), 0 , x + 20 , 445 , gSpeed + rand.nextInt(2) / 3 , z );
rak.add(r);
}
}
}
public class Rakete
{
public float xp , yp, xo ,yo, Speed;
public int xs , ys , xz , yz;
public int böse;
public Rakete(int XS,int YS,int XZ,int YZ,float Speed, int böse)
{
xs = XS;
ys = YS;
xp = (float)XS;
yp = (float)YS;
xz = XZ;
yz = YZ;
this.Speed = Speed;
this.böse = böse;
richtung();
}
public void fly(){
xp -= xo;
yp -= yo;
}
public void malen(Graphics an){
if(böse > -1){
an.setColor(Color.red);
}else{
an.setColor(Color.blue);
}
an.drawLine((int)xp,(int)yp,xs,ys);
an.drawOval(xz - 2 , yz - 2 , 2 , 2);
}
public boolean fertig(){
boolean re = false;
if(Pytago(xp,yp,(float)xz,(float)yz) < 3){
re = true;
}
return re;
}
public void richtung(){
float x = xp - (float)xz;
float y = yp - (float)yz;
if(Math.abs(x) > Math.abs(y)){
xo = x / Math.abs(x) * Speed;
yo = y / Math.abs(x) * Speed;
}else{
xo = x / Math.abs(y) * Speed;
yo = y / Math.abs(y) * Speed;
}
}
public float Pytago(float von_x,float von_y,float zu_x,float zu_y){
float x_weg = Math.abs(zu_x - von_x);
float y_weg = Math.abs(zu_y - von_y);
return (float)Math.sqrt(x_weg * x_weg + y_weg * y_weg);
}
}
public class Explusion
{
public float ex , x , y , hinSpeed , wegSpeed , max;
public boolean zurück;
public Explusion(float x ,float y, float hinSpeed , float wegSpeed , float max)
{
ex = 1;
this.x = x;
this.y = y;
this.hinSpeed = hinSpeed;
this.wegSpeed = wegSpeed;
this.max = max;
zurück = false;
}
public void weiter(){
if(zurück == false){
ex += hinSpeed;
}else{
ex -= wegSpeed;
}
if(ex > max){
ex = max;
zurück = true;
}
}
public void malen(Graphics an){
an.setColor(Color.white);
an.fillOval( (int)x - (int)ex , (int)y - (int)ex , (int)ex * 2 , (int)ex * 2);
}
public boolean fertig(){
boolean re = false;
if(ex < 1 && zurück == true){
re = true;
}
return re;
}
}