Skip to content

Commit

Permalink
Merge pull request #101 from leonardojava/Save-High-Score-Data
Browse files Browse the repository at this point in the history
Save high score data
  • Loading branch information
jvondermarck authored Dec 21, 2023
2 parents ca47692 + 67c554a commit f39a69e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
Binary file added highScore.ser
Binary file not shown.
3 changes: 3 additions & 0 deletions src/main/java/com/dinosaur/dinosaurexploder/DinosaurApp.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dinosaur.dinosaurexploder;



import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.dsl.FXGL;
Expand Down Expand Up @@ -65,5 +67,6 @@ protected void initPhysics() {
*/
public static void main(String[] args) {
launch(args);

}
}
14 changes: 14 additions & 0 deletions src/main/java/com/dinosaur/dinosaurexploder/model/HighScore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.dinosaur.dinosaurexploder.model;
import java.io.*;
public class HighScore implements Serializable {
Integer high;
public HighScore(){
this.high = 0;
}
public HighScore(Integer x){
this.high = x;
}
public Integer getHigh(){
return this.high;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import java.io.ObjectOutputStream;
import java.io.*;
/**
* Summary :
* This handles the life component of the Player implements the life interface and extends the Component
*/
public class LifeComponent extends Component implements Life {

Integer life = 3;
private Image heart = new Image(GameConstants.HEART_IMAGEPATH);

Expand Down Expand Up @@ -42,6 +44,7 @@ public void onUpdate(double ptf) {
test2.setLayoutX(test1.getLayoutX() + 30);
test3.setLayoutX(test2.getLayoutX() + 30);
// setting them for display

if (life == 3) {
setEntity(test1);
setEntity(test2);
Expand All @@ -57,6 +60,7 @@ public void onUpdate(double ptf) {
setEntity(test1);

} else {

clearEntity();

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.dinosaur.dinosaurexploder.model;


import java.io.*;

import com.almasb.fxgl.entity.component.Component;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
Expand All @@ -13,6 +16,8 @@
*/
public class ScoreComponent extends Component implements Score{
Integer score = 0;
public static HighScore highScore = new HighScore();

/**
* Summary :
* This method runs for every frame like a continues flow , without any stop until we put stop to it.
Expand All @@ -21,19 +26,45 @@ public class ScoreComponent extends Component implements Score{
*/
@Override
public void onUpdate(double ptf) {
try
{

FileInputStream file = new FileInputStream("highScore.ser");
ObjectInputStream in = new ObjectInputStream(file);

// Method for deserialization of object
highScore = (HighScore)in.readObject();

in.close();
file.close();



} catch(IOException c)
{
highScore = new HighScore();
} catch(ClassNotFoundException b)
{
System.out.println("ClassNotFoundException is caught");
}
entity.getViewComponent().clearChildren();
GridPane gridPane = new GridPane();
gridPane.setHgap(10);
Text scoreText = new Text("Score: " + score.toString());
Text highScoreText = new Text("HighScore: " + highScore.getHigh().toString());
Image image = new Image(GameConstants.GREENDINO_IMAGEPATH,25,20,false, false);
ImageView imageView = new ImageView();
imageView.setImage(image);
scoreText.setFill(Color.GREEN);
scoreText.setFont(Font.font(GameConstants.ARCADECLASSIC_FONTNAME, 20));
highScoreText.setFill(Color.GREEN);
highScoreText.setFont(Font.font(GameConstants.ARCADECLASSIC_FONTNAME, 20));
gridPane.add(scoreText,1, 0);
gridPane.add(highScoreText,1, 1);
gridPane.add(imageView,2, 0);

entity.getViewComponent().addChild(gridPane);


}
/**
Expand All @@ -57,8 +88,16 @@ public void setScore(int i) {
* This method is overriding the superclass method to increment the Score to the current Score
*/
@Override
public void incrementScore(int i) {
public void incrementScore(int i){
score += i;
if(score > highScore.getHigh()){ highScore = new HighScore(score);
try{FileOutputStream fileOut = new FileOutputStream("highScore.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(highScore);
out.close();
fileOut.close();} catch (IOException e){
e.printStackTrace();
}}
}


Expand Down

0 comments on commit f39a69e

Please sign in to comment.