first commit

This commit is contained in:
Jeena Paradies 2012-11-22 14:22:41 +01:00
commit c8d40ecc9b
22 changed files with 817 additions and 0 deletions

BIN
Box.class Normal file

Binary file not shown.

74
Box.java Normal file
View file

@ -0,0 +1,74 @@
import java.awt.Graphics;
import java.util.LinkedList;
import java.util.Iterator;
import javax.swing.*;
public class Box extends JFrame
{
private LinkedList<Drawable> drawable = new LinkedList<Drawable>();
private LinkedList<Collisionable> collisionable = new LinkedList<Collisionable>();
// Bullet 1, Tank 2, Plane 3,
public Box(String title)
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void step()
{
// Move
for(Iterator<Drawable> it = this.drawable.iterator(); it.hasNext();)
{
it.next().step();
}
// Check collision
for(int i = 0; i < this.collisionable.size(); i++)
{
Collisionable object = this.collisionable.get(i);
for(Iterator<Collisionable> it = this.collisionable.iterator(); it.hasNext();)
{
Collisionable object2 = it.next();
if(
object != object2 &&
object.getX() <= object2.getX() &&
object.getX() + object.getWidth() >= object2.getX() &&
object.getY() <= object2.getY() &&
object.getY() + object.getHeight() >= object2.getY()
)
{
object.collisionedWith(object2.collisionCheckSum());
object2.collisionedWith(object.collisionCheckSum());
}
}
if(object.isDead(this))
{
this.drawable.remove(i);
this.collisionable.remove(i);
}
}
}
public void paint(Graphics g)
{
super.paint(g);
for(Iterator<Drawable> it = this.drawable.iterator(); it.hasNext();)
{
it.next().draw(g);
}
}
public void addObject(Drawable graphic)
{
this.drawable.add(graphic);
this.collisionable.add((Collisionable)graphic);
}
}

BIN
Bullet.class Normal file

Binary file not shown.

122
Bullet.java Normal file
View file

@ -0,0 +1,122 @@
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Canvas;
public class Bullet implements Drawable, Collisionable
{
private int x, y, width = 1, height = 1, muzzleAngle, startX, startY;
private double speedX, speedY, startSpeedX, startSpeedY, v0;
private JFrame frame;
private boolean dead = false;
private long start;
public Bullet(
int muzzleAngle,
int x,
int y,
double parentSpeedX,
double parentSpeedY
)
{
this.start = System.currentTimeMillis();
this.startX = x;
this.startY = y;
this.x = x;
this.y = y;
this.frame = frame;
int startSpeed = 10;
this.muzzleAngle = muzzleAngle;
this.speedX =
SinCosLookup.getCos(muzzleAngle) * startSpeed +
SinCosLookup.getCos(muzzleAngle) * parentSpeedX
;
this.startSpeedX = this.speedX;
this.speedY =
SinCosLookup.getSin(muzzleAngle) * startSpeed +
SinCosLookup.getSin(muzzleAngle) * parentSpeedY
;
this.startSpeedY = this.speedY;
this.v0 = Math.sqrt(Math.pow(this.startSpeedX, 2) + Math.pow(this.startSpeedY, 2));
}
public void step()
{
double t = (System.currentTimeMillis() - this.start);
this.x = (int)(this.v0 * t * SinCosLookup.getCos(this.muzzleAngle)) + this.startX;
double g = -0.001;
this.y = (int)(this.startY + this.v0 * t * SinCosLookup.getSin(this.muzzleAngle) - (g / 2) * Math.pow(t, 2));
System.out.println("y = " + this.y + "\tx = " + this.x + "\tt = " + t);
// this.x += (int)this.speedX;
// this.speedY += 0.4;
// this.y += (int)this.speedY;
}
// Drawable methods
public void draw(Graphics g)
{
g.drawRect(
this.x,
this.y,
1,
1
);
}
public boolean isDead(Box box)
{
if(this.y > box.getHeight())
{
return true;
}
return this.dead;
}
// Collisionable methods
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public int getWidth()
{
return this.width;
}
public int getHeight()
{
return this.height;
}
public int collisionCheckSum()
{
return 6;
}
public void collisionedWith(int checkSum)
{
if((checkSum&0xFF & collisionCheckSum()&0xFF) == 0)
{
this.dead = true;
}
}
}

BIN
Collisionable.class Normal file

Binary file not shown.

11
Collisionable.java Normal file
View file

@ -0,0 +1,11 @@
interface Collisionable
{
int getX();
int getY();
int getWidth();
int getHeight();
int collisionCheckSum();
void collisionedWith(int o);
boolean isDead(Box box);
}

BIN
Drawable.class Normal file

Binary file not shown.

7
Drawable.java Normal file
View file

@ -0,0 +1,7 @@
import java.awt.Graphics;
interface Drawable
{
void step();
void draw(Graphics g);
}

2
MANIFEST.MF Normal file
View file

@ -0,0 +1,2 @@
Manifest-Version: 1.0
Main-Class: Main

BIN
Main.class Normal file

Binary file not shown.

35
Main.java Normal file
View file

@ -0,0 +1,35 @@
import javax.swing.*;
public class Main
{
public static void main(String[] args)
{
Box box = new Box("Tank");
box.setSize(600, 600);
Tank tank = new Tank(box);
box.addObject(tank);
TankEvent controll = new TankEvent(tank);
box.addKeyListener(controll);
/* game loop */
while(true)
{
controll.keyMonitoring();
box.step();
box.repaint();
try
{
Thread.sleep(15);
}
catch(InterruptedException e)
{
break;
}
}
}
}

BIN
Plane.class Normal file

Binary file not shown.

170
Plane.java Normal file
View file

@ -0,0 +1,170 @@
import java.awt.Toolkit;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Canvas;
public class Plane implements Drawable, Collisionable
{
private Box box;
public int frameHeight, frameWidth;
private int x, y, width, height;
private int wihicleX = 0, wihicleY, wihicleWidth = 30, wihicleHeight = 10, step = 3;
private int muzzleAngle = 270, muzzleStep = 5, muzzleLength = 15;
private int muzzleX, muzzleY;
private double speed = 0, muzzleSpeed = 0;
private double[] sin, cos;
private long lastBulletTime = 0;
private boolean dead = false;
public Plane(Box box)
{
this.box = box;
this.frameHeight = box.getHeight();
this.frameWidth = box.getWidth();
this.wihicleY = this.frameHeight - this.wihicleHeight;
this.width = this.wihicleWidth;
this.height = this.wihicleHeight + this.muzzleLength;
computeMove();
}
public void step()
{
if(Math.abs(this.speed) > 0.1 || Math.abs(this.muzzleSpeed) > 0.1)
{
computeMove();
}
}
private void computeMove()
{
this.speed *= 0.9;
this.wihicleX += this.speed;
this.muzzleSpeed *= 0.9;
int nowAngle = this.muzzleAngle - (int)this.muzzleSpeed;
this.muzzleX = (int)(SinCosLookup.getCos(nowAngle) * this.muzzleLength);
this.muzzleY = (int)(SinCosLookup.getSin(nowAngle) * this.muzzleLength);
this.x = this.wihicleX;
this.y = this.wihicleY + this.muzzleY;
}
public void goRight()
{
if(this.wihicleX < this.frameWidth - this.wihicleWidth)
this.speed = this.step;
}
public void goLeft()
{
if(this.wihicleX > 0)
this.speed = - this.step;
}
public void muzzleLeft()
{
if(this.muzzleAngle > 200)
{
this.muzzleSpeed -= this.muzzleStep;
this.muzzleAngle -= this.muzzleStep;
}
}
public void muzzleRight()
{
if(this.muzzleAngle < 340)
{
this.muzzleSpeed += this.muzzleStep;
this.muzzleAngle += this.muzzleStep;
}
}
public void fire()
{
long currentTime = System.currentTimeMillis();
if(currentTime - this.lastBulletTime > 500)
{
Bullet bullet = new Bullet(
this.muzzleAngle,
this.wihicleX + (int)(width*0.5) + this.muzzleX,
this.wihicleY + this.muzzleY,
this.speed,
0
);
this.box.addObject(bullet);
this.lastBulletTime = currentTime;
}
}
// Drawable methods
public void draw(Graphics g)
{
g.drawRect(
this.wihicleX,
this.wihicleY,
this.wihicleWidth,
this.wihicleHeight
);
int muzzleX = this.wihicleX + (int)(width*0.5);
g.drawLine(
muzzleX,
this.wihicleY,
muzzleX + this.muzzleX,
this.wihicleY + this.muzzleY
);
}
public boolean isDead(Box box)
{
if(this.dead)
Toolkit.getDefaultToolkit().beep();
return this.dead;
}
// Collisionable methods
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public int getWidth()
{
return this.width;
}
public int getHeight()
{
return this.height;
}
public int collisionCheckSum()
{
return 1;
}
public void collisionedWith(int checkSum)
{
if((checkSum&0xFF & collisionCheckSum()&0xFF) == 0)
{
this.dead = true;
}
}
}

0
README.md Normal file
View file

BIN
SinCosLookup.class Normal file

Binary file not shown.

28
SinCosLookup.java Normal file
View file

@ -0,0 +1,28 @@
public class SinCosLookup
{
private static double[] sin, cos;
static
{
SinCosLookup.sin = new double[360];
SinCosLookup.cos = new double[360];
for(int i = 0; i < 360; i++)
{
double rad = Math.toRadians(i);
SinCosLookup.sin[i] = Math.sin(rad);
SinCosLookup.cos[i] = Math.cos(rad);
}
}
public static double getSin(int pos)
{
return SinCosLookup.sin[pos];
}
public static double getCos(int pos)
{
return SinCosLookup.cos[pos];
}
}

BIN
Tank.class Normal file

Binary file not shown.

BIN
Tank.jar Normal file

Binary file not shown.

170
Tank.java Normal file
View file

@ -0,0 +1,170 @@
import java.awt.Toolkit;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Canvas;
public class Tank implements Drawable, Collisionable
{
private Box box;
public int frameHeight, frameWidth;
private int x, y, width, height;
private int wihicleX = 0, wihicleY, wihicleWidth = 30, wihicleHeight = 10, step = 3;
private int muzzleAngle = 270, muzzleStep = 5, muzzleLength = 15;
private int muzzleX, muzzleY;
private double speed = 0, muzzleSpeed = 0;
private double[] sin, cos;
private long lastBulletTime = 0;
private boolean dead = false;
public Tank(Box box)
{
this.box = box;
this.frameHeight = box.getHeight();
this.frameWidth = box.getWidth();
this.wihicleY = this.frameHeight - this.wihicleHeight;
this.width = this.wihicleWidth;
this.height = this.wihicleHeight + this.muzzleLength;
computeMove();
}
public void step()
{
if(Math.abs(this.speed) > 0.1 || Math.abs(this.muzzleSpeed) > 0.1)
{
computeMove();
}
}
private void computeMove()
{
this.speed *= 0.9;
this.wihicleX += this.speed;
this.muzzleSpeed *= 0.9;
int nowAngle = this.muzzleAngle - (int)this.muzzleSpeed;
this.muzzleX = (int)(SinCosLookup.getCos(nowAngle) * this.muzzleLength);
this.muzzleY = (int)(SinCosLookup.getSin(nowAngle) * this.muzzleLength);
this.x = this.wihicleX;
this.y = this.wihicleY + this.muzzleY;
}
public void goRight()
{
if(this.wihicleX < this.frameWidth - this.wihicleWidth)
this.speed = this.step;
}
public void goLeft()
{
if(this.wihicleX > 0)
this.speed = - this.step;
}
public void muzzleLeft()
{
if(this.muzzleAngle > 200)
{
this.muzzleSpeed -= this.muzzleStep;
this.muzzleAngle -= this.muzzleStep;
}
}
public void muzzleRight()
{
if(this.muzzleAngle < 340)
{
this.muzzleSpeed += this.muzzleStep;
this.muzzleAngle += this.muzzleStep;
}
}
public void fire()
{
long currentTime = System.currentTimeMillis();
if(currentTime - this.lastBulletTime > 500)
{
Bullet bullet = new Bullet(
this.muzzleAngle,
this.wihicleX + (int)(width*0.5) + this.muzzleX,
this.wihicleY + this.muzzleY,
this.speed,
0
);
this.box.addObject(bullet);
this.lastBulletTime = currentTime;
}
}
// Drawable methods
public void draw(Graphics g)
{
g.drawRect(
this.wihicleX,
this.wihicleY,
this.wihicleWidth,
this.wihicleHeight
);
int muzzleX = this.wihicleX + (int)(width*0.5);
g.drawLine(
muzzleX,
this.wihicleY,
muzzleX + this.muzzleX,
this.wihicleY + this.muzzleY
);
}
public boolean isDead(Box box)
{
if(this.dead)
Toolkit.getDefaultToolkit().beep();
return this.dead;
}
// Collisionable methods
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public int getWidth()
{
return this.width;
}
public int getHeight()
{
return this.height;
}
public int collisionCheckSum()
{
return 1;
}
public void collisionedWith(int checkSum)
{
if((checkSum&0xFF & collisionCheckSum()&0xFF) == 0)
{
this.dead = true;
}
}
}

BIN
TankEvent.class Normal file

Binary file not shown.

40
TankEvent.java Normal file
View file

@ -0,0 +1,40 @@
import java.awt.event.*;
public class TankEvent extends KeyAdapter
{
Tank tank;
protected static boolean keys[] = new boolean[256];
public TankEvent(Tank tank)
{
this.tank = tank;
}
public void keyPressed(KeyEvent e)
{
keys[e.getKeyCode()&0xff] = true;
}
public void keyReleased(KeyEvent e)
{
keys[e.getKeyCode()&0xff] = false;
}
public void keyMonitoring()
{
if(keys[KeyEvent.VK_LEFT&0xff])
this.tank.goLeft();
if(keys[KeyEvent.VK_RIGHT&0xff])
this.tank.goRight();
if(keys[KeyEvent.VK_UP&0xff])
this.tank.muzzleLeft();
if(keys[KeyEvent.VK_DOWN&0xff])
this.tank.muzzleRight();
if(keys[KeyEvent.VK_SPACE&0xff])
this.tank.fire();
}
}

158
Wihicle.java Normal file
View file

@ -0,0 +1,158 @@
import java.awt.Toolkit;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Canvas;
public class Wihicle implements Drawable, Collisionable
{
private Box box;
public int frameHeight, frameWidth;
private int x, y, width, height;
private int wihicleX = 0, wihicleY, wihicleWidth = 30, wihicleHeight = 10, step = 3;
private int muzzleAngle = 270, muzzleStep = 5, muzzleLength = 15;
private int muzzleX, muzzleY;
private double speed = 0, muzzleSpeed = 0;
private double[] sin, cos;
private long lastBulletTime = 0;
private boolean dead = false;
public Wihicle()
{
}
public void step()
{
if(Math.abs(this.speed) > 0.1 || Math.abs(this.muzzleSpeed) > 0.1)
{
computeMove();
}
}
private void computeMove()
{
}
public void goRight()
{
if(this.wihicleX < this.frameWidth - this.wihicleWidth)
this.speed = this.step;
}
public void goLeft()
{
if(this.wihicleX > 0)
this.speed = - this.step;
}
public void goUp()
{
}
public void goDown()
{
}
public void muzzleLeft()
{
if(this.muzzleAngle > 200)
{
this.muzzleSpeed -= this.muzzleStep;
this.muzzleAngle -= this.muzzleStep;
}
}
public void muzzleRight()
{
if(this.muzzleAngle < 340)
{
this.muzzleSpeed += this.muzzleStep;
this.muzzleAngle += this.muzzleStep;
}
}
public void fire()
{
long currentTime = System.currentTimeMillis();
if(currentTime - this.lastBulletTime > 500)
{
Bullet bullet = new Bullet(
this.muzzleAngle,
this.wihicleX + (int)(width*0.5) + this.muzzleX,
this.wihicleY + this.muzzleY,
this.speed,
0
);
this.box.addObject(bullet);
this.lastBulletTime = currentTime;
}
}
// Drawable methods
public void draw(Graphics g)
{
g.drawRect(
this.wihicleX,
this.wihicleY,
this.wihicleWidth,
this.wihicleHeight
);
int muzzleX = this.wihicleX + (int)(width*0.5);
g.drawLine(
muzzleX,
this.wihicleY,
muzzleX + this.muzzleX,
this.wihicleY + this.muzzleY
);
}
public boolean isDead(Box box)
{
if(this.dead)
Toolkit.getDefaultToolkit().beep();
return this.dead;
}
// Collisionable methods
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public int getWidth()
{
return this.width;
}
public int getHeight()
{
return this.height;
}
public int collisionCheckSum()
{
return 1;
}
public void collisionedWith(int checkSum)
{
if((checkSum&0xFF & collisionCheckSum()&0xFF) == 0)
{
this.dead = true;
}
}
}