import java.applet.*;
import java.awt.*;
import java.net.*;
public class Main extends Applet implements Runnable
{
// variables
private Thread th;
private Player player;
private Shot [] shots;
private Player2 player2;
private Shot2 [] shots2;
// speed constants
private final int shotSpeed = -2;
private final int shot2Speed = -2;
private final int playerLeftSpeed = -2;
private final int playerRightSpeed = 2;
private final int playerUpSpeed = -2;
private final int playerDownSpeed = 2;
//player2 speed
private final int player2LeftSpeed = -2;
private final int player2RightSpeed = 2;
private final int player2UpSpeed = -2;
private final int player2DownSpeed = 2;
// move flags
private boolean playerMoveLeft;
private boolean playerMoveRight;
private boolean playerMoveUp;
private boolean playerMoveDown;
//player 2 move
private boolean player2MoveLeft;
private boolean player2MoveRight;
private boolean player2MoveUp;
private boolean player2MoveDown;
// double buffering
private Image dbImage;
private Graphics dbg;
public void init()
{
setBackground (Color.white);
player = new Player(200, 400);
player2 = new Player2(200, 100);
shots = new Shot[10];
shots2 = new Shot2[10];
}
public void start ()
{
th = new Thread(this);
th.start ();
}
public void stop()
{
th.stop();
}
public void destroy()
{
th.stop();
}
public void run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
// do operations on shots in shots array
for(int i=0; i<shots.length; i++)
{
if(shots[i] != null)
{
// move shot
shots[i].moveShot(shotSpeed);
if(shots[i].getYPos() < 0)
{
shots[i] = null;
}
else if(shot2[i].cointains(int x_pos, int y_pos));
{
Player[i] = null;
//KOLLOISION funktioniert hier NICHT!!! Fehlermeldung: .class expected
}
}
for(int i=0; i<shots2.length; i++)
{
if(shots2[i] != null)
{
// move shot
shots2[i].moveShot2(shot2Speed);
// test if shot is out
if(shots2[i].getYPos() > 500)
{
// remove shot from array
shots2[i] = null;
}
//collision
}
}
// move player
if(playerMoveLeft)
{
player.moveX(playerLeftSpeed);
}
else if(playerMoveRight)
{
player.moveX(playerRightSpeed);
}
if(playerMoveUp)
{
player.moveY(playerUpSpeed);
}
else if(playerMoveDown)
{
player.moveY(playerDownSpeed);
}
// move player 2
if(player2MoveLeft)
{
player2.moveX(player2LeftSpeed);
}
else if(player2MoveRight)
{
player2.moveX(player2RightSpeed);
}
if(player2MoveUp)
{
player2.moveY(player2UpSpeed);
}
else if(player2MoveDown)
{
player2.moveY(player2DownSpeed);
}
// repaint applet
repaint();
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public boolean keyDown(Event e, int key)
{
if(key == Event.LEFT)
{
playerMoveLeft = true;
}
else if(key == Event.RIGHT)
{
playerMoveRight = true;
}
if(key == Event.UP)
{
playerMoveUp = true;
}
else if(key == Event.DOWN)
{
playerMoveDown = true;
}
//shot
else if(key == 32)
{
for(int i=0; i<shots.length; i++)
{
if(shots[i] == null)
{
shots[i] = player.generateShot();
bumm.play();
break;
}
}
}
//player2
if(key == 97)
{
player2MoveLeft = true;
}
else if(key == 100)
{
player2MoveRight = true;
}
if(key == 119)
{
player2MoveUp = true;
}
else if(key == 115)
{
player2MoveDown = true;
}
//shot2
else if(key == 118)
{
for(int i=0; i<shots2.length; i++)
{
if(shots2[i] == null)
{
shots2[i] = player2.generateShot2();
break;
}
}
}
return true;
}
public boolean keyUp(Event e, int key)
{
if(key == Event.LEFT)
{
playerMoveLeft = false;
}
else if(key == Event.RIGHT)
{
playerMoveRight = false;
}
if(key == Event.UP)
{
playerMoveUp = false;
}
else if(key == Event.DOWN)
{
playerMoveDown = false;
}
//player2
if(key == 97)
{
player2MoveLeft = false;
}
else if(key == 100)
{
player2MoveRight = false;
}
if(key == 119)
{
player2MoveUp = false;
}
else if(key == 115)
{
player2MoveDown = false;
}
return true;
}
public void update (Graphics g)
{
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
dbg.setColor (getForeground());
paint (dbg);
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
// draw player
player.drawPlayer(g);
//draw player2
player2.drawPlayer(g);
// draw shots
for(int i=0; i<shots.length; i++)
{
if(shots[i] != null)
{
shots[i].drawShot(g);
}
}
for(int i=0; i<shots2.length; i++)
{
if(shots2[i] != null)
{
shots2[i].drawShot2(g);
}
}
}
}