From 28193b8cfbb4e5c9de9da5710a280126b1e761b9 Mon Sep 17 00:00:00 2001 From: 17mgeffert Date: Thu, 2 Jun 2016 09:18:12 -0400 Subject: [PATCH] added fields and methods to player class/turret class --- src/tanks/Tanks.java | 51 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/src/tanks/Tanks.java b/src/tanks/Tanks.java index 018620b..e4cf7e7 100644 --- a/src/tanks/Tanks.java +++ b/src/tanks/Tanks.java @@ -159,6 +159,7 @@ public class Tanks public Player() { + t=new Turret(this); speed = 5; x = frame.getWidth()/2; y = frame.getHeight()/2; @@ -175,20 +176,23 @@ public class Tanks { super.move(-1); + t.move(-1); } if(down==true) { - + t.move(1); super.move(1); } if(left==true) { super.direction -=5; + t.rotate(-1); } if(right==true) { super.direction+=5; + t.rotate(1); } if(y<=0) { @@ -206,6 +210,8 @@ public class Tanks { y-=speed; } + t.x = x+12; + t.y = y+12; } void updateDir(KeyEvent e, boolean pressed) @@ -227,6 +233,14 @@ public class Tanks { right=pressed; } + else if(id==KeyEvent.VK_A) + { + t.rotate(-1); + } + else if(id==KeyEvent.VK_D) + { + t.rotate(1); + } else if(id==KeyEvent.VK_SPACE) { if(pressed) @@ -238,7 +252,13 @@ public class Tanks } void shoot() { - bullets.add(new Bullet (this)); + bullets.add(new Bullet (t)); + } + public void draw (Graphics g) + { + super.draw(g); + t.draw(g); + } } @@ -363,13 +383,34 @@ public class Tanks /* a turret is drawn ontop of the tank - a turret can rotate and shoot bullets + a turret can rotate and shoot bullets, + if rotate is called, put in -1 or 1, if -1, it will + turn left, 1 will cause + it to turn right */ private class Turret extends RotationalElement { - + public Turret(RotationalElement e) + { + width = 25; + height = 25; + x = e.x; + y = e.y; + direction = e.direction; + speed = 10; + imageLocation = "bullet.png"; + super.loadImage(); + } + public void shoot() + { + //bullets.add(new Bullet (this)); + } + + public void rotate(int e) + { + super.direction = super.direction + 5*e; + } } - /* */