Question: Hello, I ' m having issues implementing my animation timer and getting my program to run. This program is suppose to mimic the game asteroids.

Hello, I'm having issues implementing my animation timer and getting my program to run. This program is suppose to mimic the game asteroids. Can someone help fix my line of code below so it runs properly? In JAVA.
public abstract class Character {
private Polygon character;
private Point2D movement;
public Character(Polygon polygon, int x, int y){
this.character = polygon;
this.character.setTranslateX(x);
this.character.setTranslateY(y);
this.movement = new Point2D(0,0);
}
public Polygon getCharacter(){
return character;
}
public void turnLeft(){
this.character.setRotate(this.character.getRotate()-5);
}
public void turnRight(){
this.character.setRotate(this.character.getRotate()+5);
}
public void move(){
this.character.setTranslateX(this.character.getTranslateX()+ this.movement.getX());
this.character.setTranslateY(this.character.getTranslateY()+ this.movement.getY());
// Wrap around the screen
if (this.character.getTranslateX()<0){
this.character.setTranslateX(this.character.getTranslateX()+ AsteroidsApplication.LEVEYS);
}
if (this.character.getTranslateX()> AsteroidsApplication.LEVEYS){
this.character.setTranslateX(this.character.getTranslateX()% AsteroidsApplication.LEVEYS);
}
if (this.character.getTranslateY()<0){
this.character.setTranslateY(this.character.getTranslateY()+ AsteroidsApplication.KORKEUS);
}
if (this.character.getTranslateY()> AsteroidsApplication.KORKEUS){
this.character.setTranslateY(this.character.getTranslateY()% AsteroidsApplication.KORKEUS);
}
}
public void accelerate(){
double changeX = Math.cos(Math.toRadians(this.character.getRotate()));
double changeY = Math.sin(Math.toRadians(this.character.getRotate()));
changeX *=0.05;
changeY *=0.05;
this.movement = this.movement.add(changeX, changeY);
}
}
public class AsteroidsApplication extends Application {
public static int WIDTH =300;
public static int HEIGHT =200;
public static int BUFFER_SIZE =2; // Buffer size for animation timer
public static int LEVEYS =100; // Placeholder value
public static int KORKEUS =100; // Placeholder value
private List pressedKeys = new ArrayList<>();
private List projectiles = new ArrayList<>();
private List asteroids = new ArrayList<>();
private Ship ship;
@Override
public void start(Stage stage) throws Exception {
Pane pane = new Pane();
pane.setPrefSize(WIDTH, HEIGHT);
ship = new Ship(WIDTH /2, HEIGHT /2);
for (int i =0; i <5; i++){
Random rnd = new Random();
Asteroid asteroid = new Asteroid(rnd.nextInt(WIDTH /3), rnd.nextInt(HEIGHT));
asteroids.add(asteroid);
}
pane.getChildren().add(ship.getCharacter());
asteroids.forEach(asteroid -> pane.getChildren().add(asteroid.getCharacter()));
Scene scene = new Scene(pane);
scene.setOnKeyPressed(event ->{
KeyCode keyCode = event.getCode();
if (!pressedKeys.contains(keyCode)){
pressedKeys.add(keyCode);
}
});
scene.setOnKeyReleased(event -> pressedKeys.remove(event.getCode()));
stage.setScene(scene);
stage.show();
new AnimationTimer(){
@Override
public void handle(long now){
if (pressedKeys.contains(KeyCode.LEFT)){
ship.turnLeft();
}
if (pressedKeys.contains(KeyCode.RIGHT)){
ship.turnRight();
}
if (pressedKeys.contains(KeyCode.UP)){
ship.accelerate();
}
ship.move();
asteroids.forEach(Asteroid::move);
projectiles.forEach(Projectile::move);
asteroids.forEach(asteroid ->{
if (ship.collide(asteroid)){
stop();
}
});
}
}.start();
}
public static void main(String[] args){
launch(args);
}
}
public class Ship extends Character {
public Ship(int x, int y){
super(new Polygon(-5,-5,10,0,-5,5), x, y);
}
}
public class Projectile extends Character {
public Projectile(int x, int y){
super(new Polygon(2,-2,2,2,-2,2,-2,-2), x, y);
}
}
public class PolygonFactory {
public Polygon createPolygon(){
Random rnd = new Random();
double size =10+ rnd.nextInt(10);
Polygon polygon = new Polygon();
double c1= Math.cos(Math.PI *2/5);
double c2= Math.cos(Math.PI /5);
double s1= Math.sin(Math.PI *2/5);
double s2= Math.sin(Math.PI *4/5);
polygon.getPoints().addAll(
size, 0.0,
size * c1,-1* size * s1,
-1* size * c2,-1* size * s2,
-1* size * c2, size * s2,
size * c1, size * s1);
for (int i =0; i < polygon.getPoints().size(); i++){
int change = rnd.nextInt(5)-2;
polygon.getPoints().set(i, polygon.getPoints().get(i)+ change);
}
return polygon;
}
}
public class Asteroid extends Character {
private double rotationalMovement;
public Asteroid(int x, int y){
super(new PolygonFactory().createPolygon(), x, y);
Random rnd = new Random();
super.getCharacter().setRotate(rnd.nextInt(360));
int accelerationAmount =1+ rnd.nextInt(10);
for (int i =0; i < accelerationAmount; i++){
accelerate();
}
this.rotationalMovement =0.5- rnd.nextDouble();
}
@Override
public void move(){
super.move();
super.getCharacter().setRotate(super.getCharacter().getRotate()+ rotationalMovement);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!