• Create Account

### #ActualPsionicTransvection

Posted 17 August 2012 - 07:52 AM

So here is the thing Im working on a small game which it looks like this

there are 2 tanks shooting at each other in turns and you are supposed to change the angle (moving arrow up or arrow down) and the power with which the projectile is flying (holding down the space the longer you hold the more power the projectile gets).

So far managed to make a small figure moving around the screen and shooting but until this point I made it shoot only in a straight line
searched a lot of links and videos about this problem I found the formulas needed for calculating the curve but still have problems applying it in my code :/ Im open to any suggestions or criticisms Im sure Im doing something wrong all advices and code examples will be gladly appreciated looking forward for your posts I really need help with the parabola problem .

Here is the shoot method which makes the projectile move its a rectangle :

(sorry for not putting the code in tags for some reason when I do it does not print the whole code)

public void shoot()
{
if (shot)
{
bullet.x -= 5;
bullet.y--;

System.out.println(bullet.y);
if (bullet.x <= 0)
{
System.out.println("Boom");
boom = true;
//power = 0;
}

}
}

I also attached a rar with the whole code so far its only 2 classes its not much

### #7PsionicTransvection

Posted 17 August 2012 - 07:12 AM

So here is the thing Im working on a small game which it looks like this

there are 2 tanks shooting at each other in turns and you are supposed to change the angle (moving arrow up or arrow down) and the power with which the projectile is flying (holding down the space the longer you hold the more power the projectile gets).

So far managed to make a small figure moving around the screen and shooting but until this point I made it shoot only in a straight line
searched a lot of links and videos about this problem I found the formulas needed for calculating the curve but still have problems applying it in my code :/ Im open to any suggestions or criticisms Im sure Im doing something wrong all advices and code examples will be gladly appreciated looking forward for your posts I really need help with the parabola problem .

Here is the shoot method which makes the projectile move its a rectangle :

(sorry for not putting the code in tags for some reason when I do it does not print the whole code)

public void shoot()
{
if (shot)
{
bullet.x -= 5;
bullet.y--;

System.out.println(bullet.y);
if (bullet.x <= 0)
{
System.out.println("Boom");
boom = true;
//power = 0;
}

}
}

I also attached a rar with the whole code so far its only 2 classes its not much

### #6PsionicTransvection

Posted 17 August 2012 - 07:09 AM

So here is the thing Im working on a small game which it looks like this

there are 2 tanks shooting at each other in turns and you are supposed to change the angle (moving arrow up or arrow down) and the power with which the projectile is flying (holding down the space the longer you hold the more power the projectile gets).

So far managed to make a small figure moving around the screen and shooting but until this point I made it shoot only in a straight line
searched a lot of links and videos about this problem I found the formulas needed for calculating the curve but still have problems applying it in my code :/ Im open to any suggestions or criticisms Im sure Im doing something wrong all advices and code examples will be gladly appreciated looking forward for your posts I really need help with the parabola problem .

Here is the shoot method which makes the projectile move its a rectangle :

(sorry for not putting the code in tags for some reason when I do it does not print the whole code)

public void shoot()
{
if (shot)
{
bullet.x -= 5;
bullet.y--;

System.out.println(bullet.y);
if (bullet.x <= 0)
{
System.out.println("Boom");
boom = true;
//power = 0;
}

}
}

### #5PsionicTransvection

Posted 17 August 2012 - 07:06 AM

So here is the thing Im working on a small game which it looks like this

there are 2 tanks shooting at each other in turns and you are supposed to change the angle (moving arrow up or arrow down) and the power with which the projectile is flying (holding down the space the longer you hold the more power the projectile gets).

So far managed to make a small figure moving around the screen and shooting but until this point I made it shoot only in a straight line
searched a lot of links and videos about this problem I found the formulas needed for calculating the curve but still have problems applying it in my code :/ Im open to any suggestions or criticisms Im sure Im doing something wrong all advices and code examples will be gladly appreciated looking forward for your posts I really need help with the parabola problem .

Here is my code so far:

the moving of the projectile is done in the shoot() method which is in the Ship class I think the problem is there sorry for pasting the whole code doing it just in case if you think I should change something more in general

[source lang="java"] public void shoot() { if (shot) { bullet.x -= 5;// bullet.x + xSpeed; bullet.y--; // bullet.y + ySpeed; System.out.println(bullet.y); if (bullet.x <= 0) { readyToFire = true; System.out.println("Boom"); boom = true; //power = 0; } } }[/source]

### #4PsionicTransvection

Posted 17 August 2012 - 07:03 AM

So here is the thing Im working on a small game which it looks like this

there are 2 tanks shooting at each other in turns and you are supposed to change the angle (moving arrow up or arrow down) and the power with which the projectile is flying (holding down the space the longer you hold the more power the projectile gets).

So far managed to make a small figure moving around the screen and shooting but until this point I made it shoot only in a straight line
searched a lot of links and videos about this problem I found the formulas needed for calculating the curve but still have problems applying it in my code :/ Im open to any suggestions or criticisms Im sure Im doing something wrong all advices and code examples will be gladly appreciated looking forward for your posts I really need help with the parabola problem .

Here is my code so far:

the moving of the projectile is done in the shoot() method which is in the Ship class I think the problem is there sorry for pasting the whole code doing it just in case if you think I should change something more in general

[source lang="java"]import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Shoot extends JFrame{ private Image dbImage; private Graphics dbg; private JLabel jl; static Ship s1 = new Ship(); public Shoot() { setSize(1024, 768); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setVisible(true); setLocationRelativeTo(null); addKeyListener(new AL()); } @Override public void paint(Graphics g) { dbImage = createImage(getWidth(), getHeight()); dbg = dbImage.getGraphics(); paintComponent(dbg); g.drawImage(dbImage, 0, 0, this); } public void paintComponent(Graphics g) { // Draw the ship s1.draw(g); repaint(); } public class AL extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { try { s1.keyPressed(e); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } @Override public void keyReleased(KeyEvent e) { s1.keyReleased(e); } } public static void main(String[] args) { Shoot shoot = new Shoot(); // Threads Thread ship = new Thread(s1); ship.start(); System.out.println(s1.getPower()); }}[/source]

[source lang="java"]import java.awt.*;import java.awt.event.*;public class Ship implements Runnable{ int x, y, xDirection; boolean readyToFire, shot = false; Rectangle bullet; int bx; int by; int power = 0; int xSpeed; int ySpeed; boolean boom; public Ship() { x = 799; // 175 y = 755; // 275 } public void draw(Graphics g) { g.setColor(Color.BLUE); g.fillRect(x, y, 40, 10); g.fillRect(x + 18, y - 7, 4, 7); if (shot) { g.setColor(Color.BLACK); g.fillRect(bullet.x, bullet.y, bullet.width, bullet.height); g.drawString("Power : " +Integer.toString(power), 300, 300); } } public void move() { x += xDirection; if (x <= 0) x = 0; if (x >= 1024 - 40) x = 1024 - 40; } public void setXDirection(int xdir) { xDirection = xdir; } public void keyPressed(KeyEvent e) throws InterruptedException { int keyCode = e.getKeyCode(); if (keyCode == e.VK_LEFT) { setXDirection(-5); } if (keyCode == e.VK_RIGHT) { setXDirection(5); } if (keyCode == e.VK_SPACE) { power++; if (bullet == null) { readyToFire = true; if (readyToFire) { // while (power<=2000000) // { // power = (float) (power +0.00001); // Thread.sleep(1000); } // by = y - 7; // initial y coordinate // bx = x + 18; // initial x coordinate // bullet = new Rectangle(bx, by, 3, 5); // shot = true; } } } public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == e.VK_LEFT) { setXDirection(0); } if (keyCode == e.VK_RIGHT) { setXDirection(0); } if (keyCode == e.VK_SPACE) { if (readyToFire) { by = y - 7; // initial y coordinate bx = x + 18; // initial x coordinate bullet = new Rectangle(bx, by, 3, 5); shot = true; readyToFire = false; System.out.println(power); power = 0; } // if (bullet.y <= -300 && bullet.x <= -400) // { // bullet = null; // shot = false; // System.out.println(turn + " " + power); // power = 0; // } // reset(); } } public void shoot() { if (shot) { bullet.x -= 5;// bullet.x + xSpeed; bullet.y--; // bullet.y + ySpeed; // if (bullet.y <= 700) // { // update(); // } System.out.println(bullet.y); if (bullet.x <= 0) { readyToFire = true; System.out.println("Boom"); boom = true; //power = 0; } // while (currentX != bullet.x && currentY != currentY) // { // bullet.x = bullet.x - xSpeed; // bullet.y = bullet.y - ySpeed; // ySpeed = (int) (ySpeed - (-9.8)); // } } } public void update() { if (shot) { bullet.y += 50; } } public void reset() { bullet = null; shot = false; readyToFire = true; // System.out.println(power); power = 0; } @Override public void run() { try { while (true) { move(); shoot(); Thread.sleep(5); } } catch (Exception e) { System.err.println(e.getMessage()); } } public int getPower() { return power; }}[/source]

### #3PsionicTransvection

Posted 17 August 2012 - 07:02 AM

So here is the thing Im working on a small game which it looks like this

[attachment=10697:tanks.PNG]

there are 2 tanks shooting at each other in turns and you are supposed to change the angle (moving arrow up or arrow down) and the power with which the projectile is flying (holding down the space the longer you hold the more power the projectile gets).

So far managed to make a small figure moving around the screen and shooting but until this point I made it shoot only in a straight line
searched a lot of links and videos about this problem I found the formulas needed for calculating the curve but still have problems applying it in my code :/ Im open to any suggestions or criticisms Im sure I`m doing something wrong all advices and code examples will be gladly appreciated looking forward for your posts I really need help with the parabola problem .

Here is my code so far:

the moving of the projectile is done in the shoot() method which is in the Ship class I think the problem is there sorry for pasting the whole code doing it just in case if you think I should change something more in general

[source lang="java"]import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Shoot extends JFrame{ private Image dbImage; private Graphics dbg; private JLabel jl; static Ship s1 = new Ship(); public Shoot() { setSize(1024, 768); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setVisible(true); setLocationRelativeTo(null); addKeyListener(new AL()); } @Override public void paint(Graphics g) { dbImage = createImage(getWidth(), getHeight()); dbg = dbImage.getGraphics(); paintComponent(dbg); g.drawImage(dbImage, 0, 0, this); } public void paintComponent(Graphics g) { // Draw the ship s1.draw(g); repaint(); } public class AL extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { try { s1.keyPressed(e); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } @Override public void keyReleased(KeyEvent e) { s1.keyReleased(e); } } public static void main(String[] args) { Shoot shoot = new Shoot(); // Threads Thread ship = new Thread(s1); ship.start(); System.out.println(s1.getPower()); }}[/source]

[source lang="java"]import java.awt.*;import java.awt.event.*;public class Ship implements Runnable{ int x, y, xDirection; boolean readyToFire, shot = false; Rectangle bullet; int bx; int by; int power = 0; int xSpeed; int ySpeed; boolean boom; public Ship() { x = 799; // 175 y = 755; // 275 } public void draw(Graphics g) { g.setColor(Color.BLUE); g.fillRect(x, y, 40, 10); g.fillRect(x + 18, y - 7, 4, 7); if (shot) { g.setColor(Color.BLACK); g.fillRect(bullet.x, bullet.y, bullet.width, bullet.height); g.drawString("Power : " +Integer.toString(power), 300, 300); } } public void move() { x += xDirection; if (x <= 0) x = 0; if (x >= 1024 - 40) x = 1024 - 40; } public void setXDirection(int xdir) { xDirection = xdir; } public void keyPressed(KeyEvent e) throws InterruptedException { int keyCode = e.getKeyCode(); if (keyCode == e.VK_LEFT) { setXDirection(-5); } if (keyCode == e.VK_RIGHT) { setXDirection(5); } if (keyCode == e.VK_SPACE) { power++; if (bullet == null) { readyToFire = true; if (readyToFire) { // while (power<=2000000) // { // power = (float) (power +0.00001); // Thread.sleep(1000); } // by = y - 7; // initial y coordinate // bx = x + 18; // initial x coordinate // bullet = new Rectangle(bx, by, 3, 5); // shot = true; } } } public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == e.VK_LEFT) { setXDirection(0); } if (keyCode == e.VK_RIGHT) { setXDirection(0); } if (keyCode == e.VK_SPACE) { if (readyToFire) { by = y - 7; // initial y coordinate bx = x + 18; // initial x coordinate bullet = new Rectangle(bx, by, 3, 5); shot = true; readyToFire = false; System.out.println(power); power = 0; } // if (bullet.y <= -300 && bullet.x <= -400) // { // bullet = null; // shot = false; // System.out.println(turn + " " + power); // power = 0; // } // reset(); } } public void shoot() { if (shot) { bullet.x -= 5;// bullet.x + xSpeed; bullet.y--; // bullet.y + ySpeed; // if (bullet.y <= 700) // { // update(); // } System.out.println(bullet.y); if (bullet.x <= 0) { readyToFire = true; System.out.println("Boom"); boom = true; //power = 0; } // while (currentX != bullet.x && currentY != currentY) // { // bullet.x = bullet.x - xSpeed; // bullet.y = bullet.y - ySpeed; // ySpeed = (int) (ySpeed - (-9.8)); // } } } public void update() { if (shot) { bullet.y += 50; } } public void reset() { bullet = null; shot = false; readyToFire = true; // System.out.println(power); power = 0; } @Override public void run() { try { while (true) { move(); shoot(); Thread.sleep(5); } } catch (Exception e) { System.err.println(e.getMessage()); } } public int getPower() { return power; }}[/source]

PARTNERS