我正在尝试完成我的作业,就像飞机射击游戏一样。问题是为什么我的子弹要从图片中心射击?我怎样才能将子弹位置更改为图片的侧面?位置也会改变吗?我创建了2个班级。 我正在尝试完成我的作业,就像飞机射击游戏一样。问题是为什么我的子弹从图片中心射击?我如何将子弹位置更改为图片的侧面,当我旋转位置时也会更改?我创建了2个课程。
public class ShootingGame extends Application{
private Pane root;
private GameObject player;
private List<GameObject> enemies = new ArrayList<>();
private List<GameObject> bullets = new ArrayList<>();
private Parent createContent() {
//create a new pane for player
root = new Pane();
root.setPrefSize(600 , 600);
root.setStyle("-fx-background-color: black");
//create a player object
player = new Player();
player.setVelocity(new Point2D(1,0));
//add player game object
addGameObject(player,300,300);
//create a timer for update when the game start
AnimationTimer timer = new AnimationTimer() {
public void handle(long now) {
onUpdate();
}
};
//start the timer
timer.start();
return root;
}
private void addBullet(Bullet bullet, double x, double y) {
bullets.add(bullet);
addGameObject(bullet,x,y);
}
private void addEnemy(GameObject enemy, double x, double y) {
enemies.add(enemy);
addGameObject(enemy,x,y);
}
private void addGameObject(GameObject object, double x, double y) {
object.getView().setTranslateX(x);
object.getView().setTranslateY(y);
root.getChildren().add(object.getView());
}
private void onUpdate() {
player.update();
for(GameObject bullet:bullets) {
for(GameObject enemy:enemies) {
if(bullet.isColliding(enemy)) {
bullet.setAlive(false);
enemy.setAlive(false);
root.getChildren().removeAll(bullet.getView(),enemy.getView());
}
}
}
bullets.removeIf(GameObject::isDead);
bullets.forEach(GameObject::update);
enemies.removeIf(GameObject::isDead);
enemies.forEach(GameObject::update);
if(Math.random()<0.02) {
addEnemy(new Enemy(),
Math.random()*root.getPrefHeight(),Math.random()*root.getPrefWidth());
}
}
//create inner class
private static class Player extends GameObject{
Player(){
super(new ImageView(new Image("file:image/player.png")));
}
}
//create inner class bullet
private static class Bullet extends GameObject{
Bullet(){
super(new ImageView(new Image("file:image/bullet.png")));
}
}
//create inner class enemy
private static class Enemy extends GameObject{
Enemy(){
super(new ImageView(new Image("file:image/enemy.png")));
}
}
public void start(Stage stage) throws Exception{
btn1.setOnAction(e -> {
stage.setScene(new Scene(createContent()));
stage.getScene().setOnKeyPressed(c ->{
if(c.getCode() == KeyCode.LEFT) {
player.rotateLeft();
}
else if(c.getCode() == KeyCode.RIGHT) {
player.rotateRight();
}
else if(c.getCode() == KeyCode.SPACE) {
Bullet bullet = new Bullet();
bullet.setVelocity(player.getVelocity().normalize().multiply(5));
addBullet(bullet,player.getView().getTranslateX(),player.getView().getTranslateY());
}
});
});
这是第二堂课。
public class GameObject {
private Node view;
private Point2D velocity = new Point2D(0,0);
private boolean alive = true;
public GameObject(Node view) {
this.view = view;
}
public Node getView() {
return view;
}
public boolean isAlive() {
return alive;
}
public boolean isDead() {
return !alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
public Point2D getVelocity() {
return velocity;
}
public void setVelocity(Point2D velocity) {
this.velocity = velocity;
}
public void update() {
view.setTranslateX(view.getTranslateX() + velocity.getX());
view.setTranslateY(view.getTranslateY() + velocity.getY());
}
public double getRotate() {
return view.getRotate();
}
public void rotateRight() {
view.setRotate(view.getRotate()+5);
setVelocity(new Point2D(Math.cos(Math.toRadians(getRotate())),Math.sin(Math.toRadians(getRotate()))));
}
public void rotateLeft() {
view.setRotate(view.getRotate()-5);
setVelocity(new Point2D(Math.cos(Math.toRadians(getRotate())),Math.sin(Math.toRadians(getRotate()))));
}
public boolean isColliding(GameObject other) {
return getView().getBoundsInParent().intersects(other.getView().getBoundsInParent());
}