I am making a little asteroid shooter game for fun, and I am running into some problems. I have it set up so when you press a right or left arrow, the space ship rotates. This works well, but the issue is that it is also rotating the bullets after they have been shot. The bullets are supposed to travel straight, so I do not want to rotation to apply to the bullets.
****Updated post under this****
I have a basic JPanel
setup in a JFrame
, in my case they are set as 600x600
for size.
I have a ship
object that is being used as the users spacecraft that can be rotated. I created it using this code:
public class Ship
{
private int rotation;
private int[] xList = {310, 300, 305};
private int[] yList = {310, 310, 300};
public Ship(int rotation)
{
this.rotation = rotation;
}
public void draw(Graphics g)
{
Graphics2D g2d = (Graphics2D) g.create();
g.setColor(Color.WHITE);
g2d.rotate(Math.toRadians(this.rotation), Main.WIDTH/2 + 5, Main.HEIGHT/2 + 5);
g2d.drawPolygon(xList, yList, 3);
g2d.drawLine(305, 300, 305, 295);
g2d.dispose();
}
public int getRotation() {
return rotation;
}
public void setRotation(int rotation) {
this.rotation = rotation;
}
That class also has the draw
method that is what I am using to draw the ship and rotate it. I am trying to use g.create()
and g2d.dispose()
to only rotate the ship object, but my issue is when I add those bits of code, the ship does not rendered on the screen.
I also have a bullet
class that is being used to create the bullet objects
public class Bullet implements Runnable
{
private int x;
private int y;
private int xSpeed;
private int ySpeed;
public Bullet(int x, int y, int xSpeed, int ySpeed)
{
this.x = x;
this.y = y;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
}
public void draw(Graphics g)
{
g.drawOval(this.x, this.y, 5, 5);
}
public void move()
{
this.x += this.xSpeed;
this.y += this.ySpeed;
}
@Override
public void run() {
try
{
while(true)
{
move();
Thread.sleep(60);
}
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
I have some getters and setters in there that I did not include, but those are pretty self explanatory and likely do not pertain to the problem.
Finally, I have my main class. This is being used to draw all of the objects on the screen and to take in user input (being used for ship
rotation and spawning of bullet
objects)
public class Main extends JFrame implements KeyListener
{
private static Screen screen;
private static int WIDTH = 600;
private static int HEIGHT = 600;
private ArrayList<Bullet> bullets = new ArrayList<>();
public static Ship ship;
public Main()
{
ship = new Ship(0);
//basic code left out here that sets up JFrame and adds the JPanel
addKeyListener(this);
}
@Override
public void paint(Graphics g)
{
super.paint(g);
ship.draw(g);
for(int i = 0; i < bullets.size(); i++)
{
bullets.get(i).draw(g);
}
repaint();
}
public static void main(String[] args)
{
Main m = new Main();
ship = new Ship(WIDTH/2, HEIGHT/2, 0);
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_SPACE)
{
bullets.add(new Bullet(WIDTH/2 + 2, HEIGHT/2 + 2, 0, -5, true));
for (int i = 0; i < bullets.size(); i++)
{
new Thread(bullets.get(i)).start();
}
for(int i = 0; i < bullets.size(); i++)
{
if(bullets.get(i).getX() > WIDTH || bullets.get(i).getX() < 0
|| bullets.get(i).getY() > HEIGHT || bullets.get(i).getY() < 0)
{
bullets.remove(i);
}
}
}
}
}
At the bottom of my Main
class I have the KeyEvent
stuff that is being used to spawn bullets and to rotate the ship (I left the rotation code out because it is just using the left and right arrows to increase/decrease the rotation value in the ship
class)
My issue was that the rotation in the ship
class was rotating all the objects, including the bullets, but now my issue is once I added the .create()
and .dispose()
, it stopped rendering my ship. It did, however, stop the rotation of the bullets.