commit c8d40ecc9b8bb0423a20a5450962eca443d2b942 Author: Jeena Paradies Date: Thu Nov 22 14:22:41 2012 +0100 first commit diff --git a/Box.class b/Box.class new file mode 100644 index 0000000..e016464 Binary files /dev/null and b/Box.class differ diff --git a/Box.java b/Box.java new file mode 100644 index 0000000..62e5eb2 --- /dev/null +++ b/Box.java @@ -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 = new LinkedList(); + private LinkedList collisionable = new LinkedList(); + // Bullet 1, Tank 2, Plane 3, + + public Box(String title) + { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + public void step() + { + // Move + + for(Iterator 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 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 it = this.drawable.iterator(); it.hasNext();) + { + it.next().draw(g); + } + } + + public void addObject(Drawable graphic) + { + this.drawable.add(graphic); + this.collisionable.add((Collisionable)graphic); + } +} \ No newline at end of file diff --git a/Bullet.class b/Bullet.class new file mode 100644 index 0000000..3f0cafb Binary files /dev/null and b/Bullet.class differ diff --git a/Bullet.java b/Bullet.java new file mode 100644 index 0000000..c459db6 --- /dev/null +++ b/Bullet.java @@ -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; + } + + } +} \ No newline at end of file diff --git a/Collisionable.class b/Collisionable.class new file mode 100644 index 0000000..d360c32 Binary files /dev/null and b/Collisionable.class differ diff --git a/Collisionable.java b/Collisionable.java new file mode 100644 index 0000000..e2289b4 --- /dev/null +++ b/Collisionable.java @@ -0,0 +1,11 @@ + +interface Collisionable +{ + int getX(); + int getY(); + int getWidth(); + int getHeight(); + int collisionCheckSum(); + void collisionedWith(int o); + boolean isDead(Box box); +} \ No newline at end of file diff --git a/Drawable.class b/Drawable.class new file mode 100644 index 0000000..3e4aeb8 Binary files /dev/null and b/Drawable.class differ diff --git a/Drawable.java b/Drawable.java new file mode 100644 index 0000000..eb52aa8 --- /dev/null +++ b/Drawable.java @@ -0,0 +1,7 @@ +import java.awt.Graphics; + +interface Drawable +{ + void step(); + void draw(Graphics g); +} \ No newline at end of file diff --git a/MANIFEST.MF b/MANIFEST.MF new file mode 100644 index 0000000..e3ef28b --- /dev/null +++ b/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 +Main-Class: Main diff --git a/Main.class b/Main.class new file mode 100644 index 0000000..159b6d5 Binary files /dev/null and b/Main.class differ diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..00e7662 --- /dev/null +++ b/Main.java @@ -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; + } + } + } +} \ No newline at end of file diff --git a/Plane.class b/Plane.class new file mode 100644 index 0000000..e85cbf3 Binary files /dev/null and b/Plane.class differ diff --git a/Plane.java b/Plane.java new file mode 100644 index 0000000..f7b7035 --- /dev/null +++ b/Plane.java @@ -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; + } + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/SinCosLookup.class b/SinCosLookup.class new file mode 100644 index 0000000..39165d6 Binary files /dev/null and b/SinCosLookup.class differ diff --git a/SinCosLookup.java b/SinCosLookup.java new file mode 100644 index 0000000..9f53000 --- /dev/null +++ b/SinCosLookup.java @@ -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]; + } +} \ No newline at end of file diff --git a/Tank.class b/Tank.class new file mode 100644 index 0000000..c1731df Binary files /dev/null and b/Tank.class differ diff --git a/Tank.jar b/Tank.jar new file mode 100644 index 0000000..487a45d Binary files /dev/null and b/Tank.jar differ diff --git a/Tank.java b/Tank.java new file mode 100644 index 0000000..e954104 --- /dev/null +++ b/Tank.java @@ -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; + } + } +} \ No newline at end of file diff --git a/TankEvent.class b/TankEvent.class new file mode 100644 index 0000000..3d8189d Binary files /dev/null and b/TankEvent.class differ diff --git a/TankEvent.java b/TankEvent.java new file mode 100644 index 0000000..256bd93 --- /dev/null +++ b/TankEvent.java @@ -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(); + } +} \ No newline at end of file diff --git a/Wihicle.java b/Wihicle.java new file mode 100644 index 0000000..1f66e82 --- /dev/null +++ b/Wihicle.java @@ -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; + } + } +} \ No newline at end of file