Monday, June 2, 2014

Update! That Launcher got Laun-short, but the Code Got Long-cher!

            The launcher for my game was once a huge window with lots of text and boxes. I decided to modify it so that it was smaller and easier to handle when first seen. Here is the new launcher:
            I decided to remove the remember boxes, and have less text so it was significantly less confusing. Now instead of remember boxes the username from the last play session is automatically put in the username box to save time for the user. Once again to create this I used the Netbeans GUI builder to create this beautiful piece of work. As far as functionality goes, once the information is typed into the two boxes and "Sign Up!" is pressed this code runs:
            Once the action is done it sets the string variable "Username" equal to the information in the first text field, and the string variable "Password" to the information in the other text field. Then it starts up a BufferedWriter which is used for writing to files. It first creates a temporary file called the Username variable and prints the file path for easy access. Then it writes the Username and Password strings with an enter after each one and closes the writer. If I put Steve as my username, and John as my password the text file would look like this:
             This text file is then used when the other button, the "Play!" button is pressed, here is the code that is used when the other button is pressed:
            Basically what happens here is when the action happens the text from the two fields is captured as the Username and Password variables. Then a file scanner is opened which will scan for file titled the Username Variable aka whatever is typed into the first text field. Once this file is found the first and second lines are set to the string variables Fusername and Fpassword respectively. After those variables are set the information from the text fields is saved to the "LastLogin" text file which we will see in use in a later post. Then Fusername and Username as well as Fpassword and Password are checked to see fi they match, and if they do "Match Successful!" is printed into the console. Because the way the program searches for files the user must have the correct username in order to log in, because of that its possible to have "Incorrect password" Messages if the Username is matched but the Password is not, and also makes it impossible to have messages for correct password and incorrect username, not that you would need that anyways. If the password match is false, then it writes "No User Signed In" to the "LastLogin" file and prints to the console "Incorrect Password!" 
             The last function of the leaderboard, as mentioned before, is making the LastLogin information appear within the username text field when the launcher is opened. Here is the code for that:
            Basically when the launcher is loading it reads the LastLogin file and sets the string "Luser" to the information in the first line of the file and sets the information in the text field to that string. Thats a fairly simple operation once everything else is set up. 
            That is the last function of this launcher, which is actually more of a login window than anything, as it doesn't actually launch the game. I found it very difficult to make a java program open another java program within the same class, but it is possible. The main purpose of the launcher was to have a leaderboard, but I ran into trouble with "if" loops in the actual game itself as we will see in the next post. 

No comments:

Post a Comment