Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
/
P5js High Scores

P5js High Scores

Jul 30, 2021

The following code example uses a csv file to store high scores. If the doesn't exist already, the code creates a new .csv file to store the date.

High Score
Table table;
String file = "textfile.csv";
void setup() {
  size(200, 200);
  table = loadTable("data/"+file, "header");
  if (table == null) { 
    // if there is no file yet, create a new one
    makeFile();
  } else {
    retrieveDate();
  }
}

void draw() {
}

void saveDate() {
  // save a new score into the csv file  
  TableRow newRow = table.addRow();
  newRow.setString("Name", "Luke_"+floor(random(0, 100)));
  newRow.setString("Score", str(random(100, 300)));
  saveTable(table, "data/"+file);
  println("saved");
}

void retrieveDate() {
  // sort the date in order of best score
  table.sort("Score");
  String HighScore = "";
  for (TableRow row : table.rows()) {
    println(row.getString("Name") + ": " + row.getString("Score"));
    HighScore = row.getString("Name");
  }
  println("highest score is:"+HighScore);
}

void makeFile() {
  table = new Table();
  table.addColumn("Score");
  table.addColumn("Name");
  TableRow newRow = table.addRow();
  newRow.setString("Name", "Luke_"+floor(random(0, 100)));
  newRow.setString("Score", str(random(100, 300)));
  saveTable(table, "data/"+file);
}

void mouseClicked() {
  saveDate();
}


, multiple selections available,
For you

Programming
Results will update as you type.
  • Programming Basics: Parametric and Generative Graphic Design 2016
  • Archive
  • High Scores
  • Artificial Neural Network
  • Alternatives to the Processing IDE
  • p5.js Programming
    • p5.js Introduction
    • p5.js Variables
    • p5.js Random Numbers
    • p5.js SVG + Images
    • p5.js WebGL
    • p5.js Classes and Objects
    • p5.js Events and Functions
    • p5.js Loops
    • p5.js Coordinates
    • P5js Nested Loops
    • p5.js Animation Exercise 1
    • p5.js Animation Exercise 2
    • p5.js Conditionals
    • p5.js Arrays and Lists
    • p5.js Simple Collision Detection
    • p5.js Reactors
    • p5.js Tiling and Repetition
    • p5.js Vectors
    • p5.js Animation Solution with Objects
    • p5.js Easing
    • p5.js Perlin Noise
    • p5.js Particle System
    • p5.js Sound
    • p5j.s Typography
    • P5js Archive
      • P5js Motion Reactor
      • P5js Multiple Reactor Points
      • P5js Programming Basics: Parametric and Generative Graphic Design 2016
      • P5js End Exercise 2014
      • P5js End Exercise 2015
      • P5js Ext. Lektion 1 (Force Fields)
      • P5js Ext.Lektionen 2 (Rekursive Funktionen / Fraktale)
      • P5js How Computers Think
      • P5js End Exercise 2016 - Asteroids
      • P5js High Scores
      • P5js Artificial Neural Network
      • P5js Gesture Interactions
      • P5js Lesson 4.2 – Sine function
      • P5js Lesson 4.5 - Exercises
  • Programming in Processing (java)

{"serverDuration": 66, "requestCorrelationId": "dab4d0d708e94427b9fce06505e51ca5"}