We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I want four letters to appear behind the last added score. With the arrow keys, you can switch between the letters (Left & Right) and you can scroll through the alphabet (Up & Down). The letter that is selected should be displayed in white and the others should remain red. As soon as the enter key is pressed, the name should be saved to the current score and can no longer be changed. This function is to be implemented so that it is added with the current score and is also saved after the enter key is pressed.
The function should look something like this.
int Letter = 65;
void setup(){
size(1260, 720);
}
void draw(){
background(0);
textSize(80);
fill(255, 0, 0, 255);
text(char(Letter)+"", width/2, height/2);
}
void keyPressed(){
if(keyCode == UP){
if(Letter > 64){
Letter--;
if(Letter == 64){
Letter = 90;
}
}
}
if(keyCode == DOWN){
if (Letter < 91){
Letter++;
if(Letter == 91){
Letter = 65;
}
}
}
}
Here I try to implement the solution
int[] scores = new int[5];
int score;
Table table;
String savegame = "savescore.csv";
void setup(){
size(1260, 720);
for (int i=0; i<scores.length; i++){
scores[i] = 0;
}
table = null;
table = loadTable(savegame, "header");
if(table!=null){
for(TableRow row : table.rows()){
int id = row.getInt("id");
scores[id] = row.getInt("savedata");
}
}else{
table = new Table();
table.addColumn("id");
table.addColumn("savedata");
for(int i = 0; i<scores.length; i++){
TableRow newRow = table.addRow();
newRow.setInt("id", table.lastRowIndex());
newRow.setInt("savedata", scores[i]);
}
}
}
void draw(){
background(0);
textSize(80);
fill(255, 0, 0, 255);
text("Highscores ", 300, 160);
for (int i=0; i<scores.length; i++) {
textSize(60);
text(scores[i], 570, 300+80*i);
}
}
void keyPressed(){
if(key == ' '){ // Press space to add score
score = int(random(100));
addNewScore(score);
updateTable();
saveScores();
}
}
void addNewScore(int score){
for(int i=0; i<scores.length; i++){
if(score>=scores[i]){
for(int j=scores.length-1; j>=max(i,1); j--){
scores[j] = scores[j-1];
}
scores[i] = score;
break;
}
}
}
void updateTable(){
for(TableRow row : table.rows()){
int id = row.getInt("id");
table.setInt(id, "savedata", scores[id]);
}
}
void saveScores(){
saveTable(table, "data/savescore.csv");
}
Answers
I'm not able to do that. I know it is hard. If anyone can do this, I'll be very happy. If not, I'm ok with it. :)
Your implementation doesn't look anything like your description or your pseudo-code. Pressing up and down doesn't do anything.
First, I would suggest separating editing a score string from loading / saving scores or displaying a score table. Start by making an editing sketch.
So create a sketch based on your top sketch -- with no table load or save code that just lets you manipulate a four-letter string on the screen. When you press enter (or whatever), that would save it, but that it separate.
You might also want to consider saving a String -- or a char[] instead of an int[] -- just seems like it would make life easier....
Here is one simple example based on your code. Yours does not need to work this way. I still might recommend switching from int[] to char[] or String.
@jeremydouglass you are great. thanks for your help. My first code works only up and down and my second code was the code that I want to implement this function. Because of your help, I'm one step closer now.
here is the entire thing:
hit space to enter a new score
enter name (when your score is high enough) and finish with Enter
we only use the table now, no additional
score[]
array anymoreChrisir
@Chrisir You are the dumbledore of processing. Thank you so much. Thanks for the explaination entries in the code. I'll try to learn what you've done and implement this to my game. Thank you guys, you are great. :D
;-)
To get a single value from the table use
With i being eg 0
@Chrisir
text("Highscore:" + scores[0] , 380, 45);
I want to Display the scores[0] value as the Highscore in the header of my game but scores[0] doesn't exist anymore. :) Is it possible to show a single row from the table in my text?
again:
@Chrisir thanks it works fine. Here is my last problem... If you use
score = str(int(random(20)));
The Table sort the values in a wrong position. Do you've got an idea what the problem is?
line 122: you just changed 100 to 20 or did you change more?
I've only changed line 122: 100 to 20.
Weired.
The sorting is done by line 173
I am not at home but I can look tonight
Or you look into it: it's called debugging. Insert some println (score) to check where the code goes astray....
I think, the table only sorts from 0 to 9. he sorted like this:
The table don't mind, that it is a to figured value
No. With numbers from 0 to 100 it sorted correctly.
Did you change the column he is sorting by? Must be savedata
from 0 to 100, is the chance to get a 1 figured value much smaller. I think it would be like this
9
75
5
43
11
I don't change the column or something. I'll try to figure it out and use your tip with the debugging. I think that the problem can not be very big. If I can figure it out, I'll post the solution here.
Maybe it's because we convert from string to int and back - not really necessary
Processing has inbuilt means to debug, check the menus
The problem is probably bigger than I thougt. I tried to use only Strings or only Integers but nothing solve the Problem. The debugging tool, doesn't find any failures or something. I think, I have to sort it with arrays and for loops just like before and use the table, only to display save and load scores.
When posting feedback like that (like you just did) it is always practically if you post your current version (entire code) so that we are on the same ground
I'll take a look right now at my old version
I solved it.
It is necessary to delete your old high score from hard drive or start with a new folder and high score in it.
Start with a new high score.
Reason was that the high score had a String type (for the score savedata) in the table (even on the hard drive).
Seen as Strings,
19
is smaller than8
or9
(they are seen as "8_
" and "9_
" with a space after them, so higher than 19, so 19 didn't get into the high score list after sorting and cutting the table inaddNewScore
), hence the error. It's like Max is before Maximilian in the dictionary (When it's a String. When it's int, it's 19>9).When making the table the very first time we need
so we have this column as int (the default is String and that's what we had).
Hard to spot.That error was hard to find.
By the way I also thought of a way that lets the player select A-Z_!?-_* as signs and not only A-Z.
Best, Chrisir ;-)
@Chrisir Thank you so much for spending so much time. I appreciate that. I've learned many things from you. Thanks to all other volunteers too. If you want to see the final result.
https://www.dropbox.com/sh/xnzhx4v9hd0s7tp/AAALJYZ06S01rVvhVDR5Cvb3a?dl=0
That's my very first game and I'll always remember, that you guys spend so much time to help me. Now I'll try a new game and maybe I can help here someone in the near future. :)
If anyone find a bug or a failure in my game, please give me a notice.
Great!
@MigiModo -- Congratulations, and thanks for sharing your work!
Three things:
@jeremydouglass thanks for your feedback. I think that the Problems only appears on Mac. I'll share the complete sketch and post the new link. Thanks for your time :)
Here is the new Link.
https://www.dropbox.com/sh/xnzhx4v9hd0s7tp/AAALJYZ06S01rVvhVDR5Cvb3a?dl=0
I thought the high score list is sorted by score?
The high score list is not sorted by score -- at least not correctly. That is one of my suggestions for improvement. Here is what happens when I play once and get a "12" (JHDZ).
Sorry, this is a misunderstanding... I believe you and I believed you when I saw your post.
I meant to say to the OP why it doesn't work - since I wrote the code for him and it worked....
Hmm.. Delete the savescore.txt file in all data folders. After that, it should work :)
Ohh. I see what's happen.. When you close the game and start it again, the Table.INT doesn't work anymore. :\"> I'll try to fix it. Thanks @jeremydouglass :)
Initially after using Table.INT you need to delete old table on hard drive - also when uploading
It works only one time. When you close and restart the game, the scores are sorted bad again. :)
Then debug it....
Is it somewhere loaded as String e.g.?
This works for scores from 0 to 99
For higher scores you need to work on it
It works with a leading zero that gets hidden when we display the score
@Chrisir That was a very smart idea. It works fine. Thanks again. I appreciate your explanations in the code. :-bd
I changed all link posts to this link. This is the last Version and I hope this is bug free. Thanks for help :)
https://www.dropbox.com/sh/xnzhx4v9hd0s7tp/AAALJYZ06S01rVvhVDR5Cvb3a?dl=0