Skip to content

Commit

Permalink
Score is now displayed at the top
Browse files Browse the repository at this point in the history
  • Loading branch information
angela-zhou committed Jan 26, 2019
1 parent c6c99b6 commit 4858952
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
26 changes: 25 additions & 1 deletion ICS4UC_RST/src/spaceInvaders/SpaceGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class SpaceGame extends Application {
Expand All @@ -39,6 +43,11 @@ public class SpaceGame extends Application {
// game over var
boolean gameOver;
int deadInvaders;

// score var
int losses;
int wins;
Text txtScore;

// string var
String shipString = "Ship";
Expand Down Expand Up @@ -103,6 +112,17 @@ private void mainGame() {
root.setPrefSize(SCREEN_WIDTH, SCREEN_HEIGHT);
// set screen colour
root.setStyle("-fx-background-color: #000000;");

// rectangle for decorative purposes
Rectangle homeBase = new Rectangle(350, 3, Color.LIME);
homeBase.setX(25);
homeBase.setY(SCREEN_HEIGHT - 20);
root.getChildren().add(homeBase);

txtScore = new Text(14, 34, "Wins: <" + wins + "> Losses: <" + losses + ">");
txtScore.setFont(new Font("OCR A Extended", 18));
txtScore.setFill(Color.WHITE);
root.getChildren().add(txtScore);

// add player to the root
root.getChildren().add(player);
Expand Down Expand Up @@ -168,7 +188,7 @@ private void runInvaders() {
for (int row = 0; row < invaders.length; row++) {
for (int col = 0; col < invaders[row].length; col++) {
// create new invader
invaders[row][col] = new Alien(SCREEN_WIDTH / 5 + col * GAP, GAP + row * GAP, "Invader", invaderImage);
invaders[row][col] = new Alien(SCREEN_WIDTH / 5 + col * GAP, GAP + 10 + row * GAP, "Invader", invaderImage);
// display invader
root.getChildren().add(invaders[row][col]);
}
Expand Down Expand Up @@ -311,13 +331,15 @@ public void handle(long now) {

if (player.isDead) {
gameOver = true;
losses ++;
timer.stop();
gameOver("Invader Wins");

}

if (deadInvaders == (NUM_INVADERS * NUM_INVADERS)) {
gameOver = true;
wins++;
timer.stop();
gameOver("Ship Wins");
}
Expand Down Expand Up @@ -363,6 +385,8 @@ public void reset() {
}
}
runInvaders();
// set text
txtScore.setText("Wins: <" + wins + "> Losses: <" + losses + ">");
//start timer
timer.start();
}
Expand Down
2 changes: 1 addition & 1 deletion ICS4UC_RST/src/spaceInvaders/SpaceOver.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<Image url="@images/You%20Win.png" />
</image>
</ImageView>
<Button layoutX="125.0" layoutY="421.0" mnemonicParsing="false" onAction="#playAgain" prefHeight="40.0" prefWidth="150.0" style="-fx-background-color: BLACK; -fx-border-color: WHITE;" text="Play Again" textFill="WHITE">
<Button layoutX="125.0" layoutY="417.0" mnemonicParsing="false" onAction="#playAgain" prefHeight="40.0" prefWidth="150.0" style="-fx-background-color: BLACK; -fx-border-color: WHITE;" text="Play Again" textFill="WHITE">
<font>
<Font name="OCR A Extended" size="18.0" />
</font>
Expand Down

0 comments on commit 4858952

Please sign in to comment.