# Can someone Help me? (highscore)

**URL:** <https://discourse.processing.org/t/can-someone-help-me-highscore/28453>\
**Category:** Processing for Android\
**Created:** [March 11, 2021, 10:07pm UTC](https://discourse.processing.org/t/can-someone-help-me-highscore/28453 "2021-03-11T22:07:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rogo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogo/32/12349_2.png) [@Rogo](https://discourse.processing.org/u/Rogo)\
**Post date:** [March 11, 2021, 10:07pm UTC](https://discourse.processing.org/t/can-someone-help-me-highscore/28453/1 "2021-03-11T22:07:14Z")

</div>

I need Help with the Score / highscore… can someone Help me please?

```auto
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.content.Context;
import android.app.Activity;
Activity act;
Context cnt;
SharedPreferences sp;
SharedPreferences.Editor editor;
int myScore;
String myScoreFile;
int hiScore;
String hiScoreFile;
int spieler_x;
int spieler_y;
int spieler_w;
int spieler_h; 
float ball_x;
float ball_y;
float ball_ge_x;
float ball_ge_y;
int ball_d;
int seite = 0;
void setup() { 
  frameRate(120);
  PFont font = createFont("SansSerif", 24 * displayDensity); 
  textFont(font); 
  fullScreen(); 
  rectMode(CENTER); 
  spieler_w = width/15; 
  spieler_h = height/5; 
  ball_d = width/12;
  act = this.getActivity();
  cnt = act.getApplicationContext();
  sp = PreferenceManager.getDefaultSharedPreferences(cnt);
  editor = sp.edit();
  hiScoreFile = "hiScores";
  hiScore = loadScore(hiScoreFile);
  myScoreFile = "Scores";
  myScore = loadScore(myScoreFile);
} 
void draw(){
  if(myScore>hiScore){
    background(255);
    textSize(30);
    fill(0);
    saveScore(hiScore, hiScoreFile);
  }
  background(255);
  if (seite == 0) {
    showStart();
    text("Highscore: " + hiScore, width/2, height/14); 
    if (mousePressed) { 
      if (mouseX > width/2 - width/3/2 && mouseX < width/2 + width/3/2 && mouseY > height/1.5 - height/12/2 && mouseY < height/1.5 + height/12/2) { 
        reset(); 
        seite = 1;
      }
    }
  } 
  if (seite == 1) { 
    showGame(); 
    berechnePlayerY(); 
    ball_x = ball_x + ball_ge_x; 
    ball_y = ball_y + ball_ge_y; 
    berechneBall();
    text(myScore + "/" + hiScore, width/2, height/14);
  } 
  if (seite == 2) { 
    showEnd(); 
    if (mousePressed) { 
      if (mouseX > width/2 - width/3/2 && mouseX < width/2 + width/3/2 && mouseY > height/1.5 - height/12/2 && mouseY < height/1.5 + height/12/2) { 
        reset();
        delay(300);
        seite = 0;
      }
    }
  }
} 
void reset() { 
  spieler_x = width/10; 
  spieler_y = height/2; 
  ball_x = width/2; 
  ball_y = height/2; 
  ball_ge_x = -4; 
  ball_ge_y = 0;
} 
void showGame() {
  fill(0);
  fill(255);
  rect(spieler_x, spieler_y, spieler_w, spieler_h);
  ellipse(ball_x, ball_y, ball_d, ball_d);
} 
void showStart() { 
  fill(255); 
  rect(width/2, height/1.5, width/3, height/12, 7); 
  textAlign(CENTER); 
  textSize(40); 
  fill(0); 
  text("SPONG", width/2, height/5); 
  textSize(20); 
  fill(0); 
  text("START", width/2, height/1.49);
}
void showEnd() { 
  fill(255); 
  rect(width/2, height/1.5, width/3, height/12, 7); 
  textAlign(CENTER); 
  textSize(40); 
  fill(0); 
  text("VERLOREN", width/2, height/5); 
  textSize(20); 
  fill(0); 
  text("TRY AGAIN", width/2, height/1.49);
} 
void berechnePlayerY() { 
  if (mousePressed) { 
    if (mouseY > height / 2) { 
      if (spieler_y < height - spieler_h/2) { 
        spieler_y = spieler_y + 10;
      }
    } 
    if (mouseY < height / 2) { 
      if (spieler_y > spieler_h/2) { 
        spieler_y = spieler_y - 10;
      }
    }
  }
} 
void berechneBall() { 
  if (ball_x < spieler_x + spieler_w) { 
    if (ball_y < (spieler_y + spieler_h/2) && ball_y > (spieler_y - spieler_h/2)) { 
      ball_ge_x = (-ball_ge_x) + 1 * 1; 
      ball_ge_y = ball_ge_y - (spieler_y - ball_y) * 0.1;
      myScore++;
      saveScore(hiScore, hiScoreFile);
    } else {
      seite = 2;
    }
  } 
  if (ball_y > height-ball_d/2 || ball_y < ball_d/2) { 
    ball_ge_y = -ball_ge_y;
  } 
  if (ball_x > width-ball_d/2) { 
    ball_ge_x = -ball_ge_x;
  }
}

void saveScore(int hiscore, String name) {
  //editor.clear();
  editor.putInt(name, hiscore);
  editor.commit();
}

int loadScore(String name) {
  int getScore = sp.getInt(name, 0);
  return getScore;
}

void onPause() {
  super.onPause();
  saveScore(hiScore, hiScoreFile);
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 12, 2021, 8:42am UTC](https://discourse.processing.org/t/can-someone-help-me-highscore/28453/2 "2021-03-12T08:42:12Z")

</div>

Sure

What is the Problem?

---

<div class="post-metadata">

**Author:** ![Rogo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogo/32/12349_2.png) [@Rogo](https://discourse.processing.org/u/Rogo)\
**Post date:** [March 12, 2021, 10:31am UTC](https://discourse.processing.org/t/can-someone-help-me-highscore/28453/3 "2021-03-12T10:31:05Z")

</div>

I don’t get any further with the score / highscore

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [March 12, 2021, 12:23pm UTC](https://discourse.processing.org/t/can-someone-help-me-highscore/28453/4 "2021-03-12T12:23:11Z")

</div>

How to create a highscore program (in Processing):  
First, you need a variable storing the highscore.

```auto
float highscore = 0;

```

After it, you need a way to compare it. So you need to:

```auto
if(testScore > highscore) highscore = testScore;

```

So you have to keep calling this function whenever the score changes.

If you have any questions, feel free to ask.  
(I didn’t read the code since it is in german? Well in any case, I don’t speak that language so it was confusing)

---

<div class="post-metadata">

**Author:** ![Rogo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogo/32/12349_2.png) [@Rogo](https://discourse.processing.org/u/Rogo)\
**Post date:** [March 12, 2021, 3:25pm UTC](https://discourse.processing.org/t/can-someone-help-me-highscore/28453/5 "2021-03-12T15:25:03Z")

</div>

Yes sorry its was German… @CodeMasterX can we 📝 with discord

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [March 12, 2021, 5:09pm UTC](https://discourse.processing.org/t/can-someone-help-me-highscore/28453/6 "2021-03-12T17:09:53Z")

</div>

why do you need to talk? Just curious

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [March 14, 2021, 7:46pm UTC](https://discourse.processing.org/t/can-someone-help-me-highscore/28453/7 "2021-03-14T19:46:42Z")

</div>

@Rogo === though i dont speak german i can see (or imagine!) what you want and @CodeMasterX is “generally” right; the problem is that you want to use SharedPreferences API which is an excellent idea, but your code for that is not the good one; if you want i can explain why and if necessary put a working code…
