Question
Modify the AlienDirection program in Listing 7.20 so that it displays 10 random numbers between 1 and 100 in random locations and as the alien
Modify the AlienDirection program in Listing 7.20 so that it displays 10 random numbers between 1 and 100 in random locations and as the alien moves, it eats the numbers and as the numbers are consumed, it shows the total. When a number is consumed, the consumed number disappears, while other numbers remain the same. When all the numbers are consumed, the game starts over.
My code so far :
package alien;
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyEvent; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.stage.Stage;
public class AlienDirection extends Application { public final static int JUMP = 10; private ImageView imageView; @Override public void start(Stage primaryStage) { Image alien = new Image("file:Small_alien.png"); imageView = new ImageView(alien); imageView.setX(20); imageView.setY(20); Group root = new Group(imageView);
Scene scene = new Scene(root, 400, 200, Color.BLUE); scene.setOnKeyPressed(this::processKeyPress);
primaryStage.setTitle("Alien Direction"); primaryStage.setScene(scene); primaryStage.show(); }
public void processKeyPress(KeyEvent event) { switch (event.getCode()) { case UP: imageView.setY(imageView.getY() - JUMP); break; case DOWN: imageView.setY(imageView.getY() + JUMP); break; case RIGHT: imageView.setX(imageView.getX() + JUMP); break; case LEFT: imageView.setX(imageView.getX() - JUMP); break; default: break; } } public static void main(String[] args) { launch(args); } }
Transcribed image textStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started