How to make an advanced Score in Processing?


I’m developing a game in processing. It’s about hitting a ball with a bat so that the ball is thrown back. If you hit the ball, you get a point or the score increases by 1.

I want to add a shop to the main menu, where you can buy something with the points later.

Now the problem:
I can’t manage it that when I leave the game and restart that the points remain saved.
I already tried saveStrings() and loadStrings(), but it didn’t work.
Every time the score changes, the new score is in the text file and the old score is deleted. There is only one line.

I actually wanted it to be that the points list themselves one below the other and are added with loadStrings() when they are retrieved. Then I would have all the points.

I am slowly really despairing.
Any help is welcome.


1 Like

Hey and welcome to the forum!

We need to see your code or essential parts of it

This assumes there’s only one person using the computer.

(Otherwise you need to ask the player for his or her name and store the score with this name.)

my approach:

  • the file contains only one line

  • on startup of the sketch check if the file is there.

  • if so set the score from file: score = strList[0];

  • if not score = 0;

  • during sketch say score++; or whatever

  • on ending the sketch you need a routine that is executed before leaving. You can achieve this when you catch ESC in keyPressed() (or quit in a menu point) and do the saving there.

  • save the current score to the file (overwriting the old file when it’s there)

Regards, Chrisir

1 Like

in this sketch we make a file in the data path upon leaving as described above:

  • score gets loaded and then changed via random (instead of your game logic).

  • Then we save it.

You can test the sketch when you multiple times start and end it (with ESCAPE)

String filename = "fileScore.txt";
int score=0;

void setup() {
  size(660, 660);

  File f = new File(dataPath(filename));

  if (f.exists()) {  
    // do something
    String[] strList = loadStrings(filename); 
    score = int(strList[0]);
  else {
    println("not found");

  println("start: "+score); 
  score += int(random(6));

void draw() {
  text(score, 122, 122);

void keyPressed() {
  if (key==ESC) {
    println("quit: " + score );
    String[] strList = new String[1];
    strList[0] = str(score);
    saveStrings(dataPath(filename), strList);