OK I give up! I’m trying to add high score functionality to the MineSweeper tutorial and I can’t get my columns to line up. Saving and restoring the scores is fine I just cant make it look pretty. I know I could just use a separate text() function for each column but I think it would be more elegant to use the string.format formatting abilities and it is killing me not being able to fix this.
According to what I have read %w.ts should format a fixed column of width w, truncated at t characters for string s. This will be right justified. If you use %-w.ts it should be left justified. So in the code below %-30.30s should print “Easy” left justified in a column 30 characters wide and it shouldn’t be truncated. It almost works but the “Medium” line doesn’t format like I would expect it to. The size of the string shouldn’t matter but it does.
I’m not sure if this is a peculiarity of the text() function, string.format or I am just misunderstanding how this works. I have tried different combinations of textAlign and textMode but can’t get the right effect.
A test stub is shown below.
I’m sure someone must have already solved this problem so any nudges in the right direction would be appreciated.
--# Main -- Use this function to perform your initial setup function setup() print("Hello World!") end function draw() background(0) font("Arial-BoldMT") fill(0,0,255) fontSize(72) textAlign(CENTER) text("High Scores", WIDTH/2, HEIGHT/2 + 220) fill(255) fontSize(24) local str str = string.format("%-30.30s\\t%-20.20s\\t%10d", "Easy", "Player 1", 1000) text(str, WIDTH/2, HEIGHT/2 + 40) str = string.format("%-30.30s\\t%-20.20s\\t%10d", "Medium", "Player 2", 2000) text(str, WIDTH/2, HEIGHT/2) str = string.format("%-30.30s\\t%-20.20s\\t%10d", "Hard", "Player 3", 3000) text(str, WIDTH/2, HEIGHT/2 - 40) end