Java problem for my project in school

class Bullet {
private static final int BULLET_SPEED = 5;
private Rectangle bullet;

public Bullet(int x, int y) {
    bullet = new Rectangle(x, y, 5, 10);
    bullet.setColor(Color.RED);
    bullet.draw();
}

public void fire() {
    while (bullet.getY() > 0) {
        bullet.translate(0, - BULLET_SPEED);
        try {
Thread.sleep(20);

} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
public class Ship implements KeyboardHandler {

private Rectangle ship;
private Position pos;

public Ship() {
    ship = new Rectangle(260, 700, 20, 20);
    pos = new Position(ship.getX(), ship.getY());
    ship.setColor(Color.BLUE);
    ship.draw();
    keyboardInit();
}

public void keyboardInit () {

    Keyboard keyboard = new Keyboard(this);

    //Right key
    KeyboardEvent rightPress = new KeyboardEvent();
    rightPress.setKeyboardEventType(KeyboardEventType.KEY_PRESSED);
    rightPress.setKey(KeyboardEvent.KEY_RIGHT);

    KeyboardEvent leftPress = new KeyboardEvent();
    leftPress.setKeyboardEventType(KeyboardEventType.KEY_PRESSED);
    leftPress.setKey(KeyboardEvent.KEY_LEFT);

    KeyboardEvent spacePress = new KeyboardEvent();
    spacePress.setKeyboardEventType(KeyboardEventType.KEY_PRESSED);
    spacePress.setKey(KeyboardEvent.KEY_SPACE);

    keyboard.addEventListener(rightPress);
    keyboard.addEventListener(leftPress);
    keyboard.addEventListener(spacePress);
}


@Override
public void keyPressed(KeyboardEvent keyboardEvent) {
    switch (keyboardEvent.getKey()) {
        case KeyboardEvent.KEY_RIGHT:

            if (ship.getX() < 470) {
                ship.translate(30, 0);
            }
            break;

        case KeyboardEvent.KEY_LEFT:

            if (ship.getX() > 30) {
                ship.translate(-30, 0);
            }
            break;

        case KeyboardEvent.KEY_SPACE:

            shootBullet();
            break;

    }
}

@Override
public void keyReleased(KeyboardEvent keyboardEvent) {

}

private void shootBullet() {
    Bullet bullet = new Bullet(ship.getX() + ship.getWidth() / 2, ship.getY());

    bullet.fire();
    System.out.println("PEW PEW");

}

}
So im creating a game with a library and my bullet only shows in final position outside the rectangle , i think My canvas is a rectangle and I think it goes under the square and I don’t know how to solve this problem

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.