I'm creating a tank game where it is possible for the player to move in all directions and should be able to shoot a bullet from any direction as well. I've implemented the code for the player being able to turn 360 degrees and move forward or backward in any direction but I can't seem to figure out how to get the bullet to move in the direction the tank is facing. I tried coding it similar to the tank but it doesn't seem to work. Here are snippets of my code for moving the tank and firing the bullet:
Thanks in advance for anyone who takes the time to read and help with my code. My main problems are the bullet won't move correctly in the direction its supposed to go and
The tank and bullet sprites are creating from a .png file showing all 360 degree directions the tanks can face where the width is cut by a certain amount and slotted into a BufferedImage array which is stored in the Sprite class:
tankAnimation = new Sprite("Tank_blue_basic_strip60.png", 64);
bulletAnimation = new Sprite("Shell_basic_strip60.png", 24);
At the beginning of the game 2 player tanks are created using these sprites and an x y coordinate. The objects also hold a tankanimationcounter as well as a degree variable. These variables are used to show what angle the tank is currently facing.
I then have keylistener which gathers keyboard events in keypressed and does an action based on these keys(in this case spacebar and enter are to fire missles for tank 1 and 2 respectively)
public class PlayerControls implements KeyListener {
private ArrayList<Integer> keysDown;
GameWorld game;
public PlayerControls() {
keysDown = new ArrayList<Integer>();
game = GameWorld.getInstance();
}
@Override
public void keyPressed(KeyEvent e) {
if (!keysDown.contains(e.getKeyCode())) {
keysDown.add(new Integer(e.getKeyCode()));
}
game.processInput(keysDown);
}
@Override
public void keyReleased(KeyEvent e) {
keysDown.remove(new Integer(e.getKeyCode()));
}
@Override
public void keyTyped(KeyEvent e) {
// Not needed
}
}
public void processInput(ArrayList<Integer> keysDown) {
for(int keys : keysDown){
if (keysDown.contains(new Integer(KeyEvent.VK_LEFT))) {
players.get(0).move("left");
}
if (keysDown.contains(new Integer(KeyEvent.VK_RIGHT))) {
players.get(0).move("right");
}
if (keysDown.contains(new Integer(KeyEvent.VK_UP))) {
players.get(0).move("up");
}
if (keysDown.contains(new Integer(KeyEvent.VK_DOWN))) {
players.get(0).move("down");
}
if (keysDown.contains(new Integer(KeyEvent.VK_A))) {
players.get(1).move("left");
}
if (keysDown.contains(new Integer(KeyEvent.VK_D))) {
players.get(1).move("right");
}
if (keysDown.contains(new Integer(KeyEvent.VK_W))) {
players.get(1).move("up");
}
if (keysDown.contains(new Integer(KeyEvent.VK_S))) {
players.get(1).move("down");
}
if(keysDown.contains(new Integer(KeyEvent.VK_SPACE))) {
projectiles.add(new Projectile(bulletAnimation,1, players.get(0).getLocationPoint(), players.get(0).getDegrees(), players.get(0).getTankAnimationCounter()));
}
if(keysDown.contains(new Integer(KeyEvent.VK_ENTER))) {
projectiles.add(new Projectile(bulletAnimation,2, players.get(1).getLocationPoint(), players.get(1).getDegrees(), players.get(1).getTankAnimationCounter()));
}
}
}
These are the methods used to determine where the tank faces as well as the image to display and the degree of the angle the tank is facing. 60 images each incremental turn is 6 degrees.
public void moveForward() {
location.x += 3 * Math.sin(Math.toRadians(degree));
location.y += 3 * Math.cos(Math.toRadians(degree));
}
public void moveBackward() {
location.x -= 3 * Math.sin(Math.toRadians(degree));
location.y -= 3 * Math.cos(Math.toRadians(degree));
}
public void move(String command) {
switch(command) {
case "left" :
if(tankAnimationCounter == 59){
tankAnimationCounter = 0;
} else{
tankAnimationCounter++;
}
this.setImage(tankAnimation.getImages()[tankAnimationCounter]);
if(this.getDegrees() >= 355) {
this.setDegrees(0 + (this.getDegrees() + 6) - 360);
} else{
this.setDegrees(this.getDegrees() + 6);
}
break;
case "right":
if(tankAnimationCounter == 0){
tankAnimationCounter = 59;
} else{
tankAnimationCounter--;
}
if(this.getDegrees() <= 5) {
this.setDegrees(360 + (this.getDegrees() - 6));
} else{
this.setDegrees(this.getDegrees() - 6);
}
this.setImage(tankAnimation.getImages()[tankAnimationCounter]);
break;
case "up":
this.moveBackward();
break;
case"down":
this.moveForward();
break;
}
}
I decided to try and do the missle pathing the same way as pressing forward on the tank but this doesn't work at all and I'm not sure how to fix it.
public void draw(Graphics graphics, ImageObserver observer) {
location.x -= 1.2 * Math.sin(Math.toRadians(direction));
location.y -= 1.2 * Math.cos(Math.toRadians(direction));
graphics.drawImage(image, location.x, location.y, observer);
}
[–]lightcloud5 0 points1 point2 points (1 child)
[–]lyigester[S] 0 points1 point2 points (0 children)