Question: Modify the program so that it displays 10 random numbers between 1 and 100 in random locations and as the alien moves, it eats the
Modify the program 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, the program shows the total. When a number is consumed, the consumed number must disappear, while other numbers remain (in the same or different locations). When all the numbers are consumed, the game is over and asks the user to play one more.
The code is below, it's functional but not doing what the question is asking. It is not displaying the total or adding the numbers consumed at all. It also doesn't restart after the game is over.
import java.util.Random; import javafx.application.Application; import javafx.collections.ObservableList; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; import javax.swing.JOptionPane;
// defined a class with extends application public class AlienDirection extends Application { // declare as required variables public final static int Jump = 10; private ImageView ImgView; private Text Txt[]; private Group Rt; @Override // to start the program public void start(Stage primaryStage) { // set the image Image ALIEN = new Image("file:ghost1.gif"); ImgView = new ImageView(ALIEN); ImgView.setX(20); ImgView.setY(20); ImgView.setFitHeight(10); ImgView.setFitWidth(10); Rt = new Group(ImgView); ObservableList LIST = Rt.getChildren(); System.out.println(" Initially List Size:"+ LIST.size()); // Declare a variables int rand; // Define class random Random Rand = new Random(); // texting Txt = new Text[10]; for(int i=0;i<10;i++){ Txt[i] = new Text();
Txt[i].setFont(new Font(15)); Txt[i].setFill(Color.RED); rand=10+Rand.nextInt(39)*10; Txt[i].setX(rand); rand=10+Rand.nextInt(19)*10; Txt[i].setY(rand);
rand=1+Rand.nextInt(100); Txt[i].setText(""+rand);
LIST.add(Txt[i]); } // define a scene color Scene scene = new Scene(Rt, 400, 200, Color.BLACK); scene.setOnKeyPressed(this::processKeyPress);
primaryStage.setTitle("Alien Direction"); primaryStage.setScene(scene); primaryStage.show(); }
// perform processing public void processKeyPress(KeyEvent event) { switch (event.getCode()) { case UP: ImgView.setY(ImgView.getY() - Jump); checkConsumed(); break; case DOWN: ImgView.setY(ImgView.getY() + Jump); checkConsumed(); break; case RIGHT: ImgView.setX(ImgView.getX() + Jump); checkConsumed(); break; case LEFT: ImgView.setX(ImgView.getX() - Jump); checkConsumed(); break; default: break; } } // user defined constraint public void checkConsumed() { for(int i=0;i<10;i++) { if(ImgView.getX()==Txt[i].getX() && ImgView.getY()==Txt[i].getY()){ Rt.getChildren().remove(Txt[i]); System.out.println("Consumed at:"+Txt[i].getX()+","+Txt[i].getY()); } } // checks condition if(Rt.getChildren().size()<=1) { JOptionPane.showMessageDialog(null,"Game Completed!!!","Result",JOptionPane.WARNING_MESSAGE); } } // main program public static void main(String[] args) { launch(args); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
