LiveCode Tower Jump 2: Part 1

Posted by Scott McDonald

For The Very First LiveCode Game Jam I planned to produce a game more ambitious than the games previously posted here. It is called UFO Reporter: Ground Zero Sydney. If you can, think of a story driven game based on Duck Hunt (1984) for the Nintendo Entertainment System to get some idea of what I was hoping for. With the game jam deadline getting closer each week, it was clear that it would not be finished in time.

So with days remaining in the game jam, a solution was to improve a simpler game and make it suitable for release. The result is Tower Jump 2 which you can see here: Tower Jump 2.

Tower Jump 2 is based on the game in Chapter 7 of Coding Nine LiveCode Games. So with that headstart and a few days left, work was done to make it presentable for the game jam. For the curious, my UFO game is not abandoned and will be the focus of a future post here at LiveCode Game Developer.

Some history behind the game. Before the indie game scene really took off and became the cool label for individual and small teams of independent game developers, Free Lunch Design was (and still is) a good example of what a small team can do. In 2001, Johan Peitz and team made Icy Tower, a very fun addictive free game. Which according to Wikipedia is based on an earlier Linux game named Xjump or Falling Tower.

Which leads to Tower Jump. This is a simplified version of the original Icy Tower written in LiveCode. Simplified to reduce the amount of code required and to make it possible to write in a weekend. The Tower Jump in Coding Nine LiveCode Games has a few problems that prevent it from being a finished game. The issues are:

  • No sound
  • No recording of your score
  • Difficulty does not change as you play, boring!
  • The platform positioning is random

The last problem in my first Tower Jump means eventually a time will come when the gap between two platforms is too large and so is impossible to successfully jump between. Whether intentional or accidental, a game that make progress literally impossible is very annoying.

For The Very First LiveCode Game Jam extra code was added to Tower Jump. While Tower Jump 2 is an improvement and is "finished" it could still do with more work. But since it is only around 500 lines of code, it shows how LiveCode can make a real game with not much code.

In part 1 of this game you will see:

  • One way of restricting what is entered in a field
  • Storing the game configuration in an appropriate folder
  • A simple way to call code on a server
  • Server-side code to check login details

To play Tower Jump 2 you need to enter your 3 initials, or any three letters. This is necessary for the high score table. A record of high scores is kept for you, and all other players of the game. The high scores are stored on a file on a remote server that includes LiveCode Server.

Three letters are used instead of a longer login name, to recreate the atmosphere of early arcade games. A decision was made to have three separate Text Field controls a bit like three on- screen slots in an old arcade machine for the letters. The controls are named editInitial1, editInitial2 and editInitial3 and have basic code to ensure only one letter is entered in each one, and that the cursor automatically moves to the next field.

To validate and handle the entering into the three fields, the command PutLoginLetter is in the card script. This command does a basic check and handles the switching of the focus, so the player does not need to click on the next field.

command PutLoginLetter pKey, pFieldName, pNextFieldName
  if "ABCDEFGHIJKLMNOPQRSTUVWXYZ" contains pKey then
    put empty into field "labeMessage"
    put upper(pKey) into field pFieldName
    if exists(field pNextFieldName) then
      put empty into field pNextFieldName
      focus field pNextFieldName
    else
      focus on nothing
      put "Click on START to continue" into field "labeMessage"
    end if
  else
    put "Please enter letters only for the initials" into field "labeMessage"
  end if
end PutLoginLetter

The keyDown handler for editInitial1 then calls the PutLoginLetter command.

on keyDown pKey
  PutLoginLetter pKey, "editInitial1", "editInitial2"
end keyDown

With similar calls in the keyDown handlers of the editInitial2 and editInitial3 controls, respectively. With the call in editInitial3 having empty as the third parameter, so that prompt to click on START is shown.

on keyDown pKey
  PutLoginLetter pKey, "editInitial2", "editInitial3"
end keyDown

on keyDown pKey
  PutLoginLetter pKey, "editInitial3", empty
end keyDown

After entering three letters, the player must click on the Start button to enter the main screen of the game. The Start button is a button control with the style property set to transparent. This allows the button to overlay an area of the background that already looked suitable for clicking.

The start button has its code the mouseUp handler. Looking at it here, it is broken up into 3 sections. The first section checks that 3 letters have been entered. You may be wondering why the length and contents of the name is checked? After all, the keyDown handler of each field only accepts one letter. So why the second check? The answer is that a curious player could try to paste text from the clipboard "just to see what happens." As it happens, it is possible to paste like this. This is why another check is done after the click on the Start button.

Sure, for a simple game this may be overly cautious, but since the 3 initials are about to be sent to a server, such a check can help prevent any "funny" business with a hacking attempt.

put field "editInitial1" & field "editInitial2" & field "editInitial3" into gPlayerName
put length(gPlayerName) = 3 into isValid
repeat for each char loopCh in gPlayerName
  if loopCh is not among the chars of "ABCDEFGHIJKLMNOPQRSTUVWXYZ" then
    put false into isValid
    exit repeat
  end if
end repeat
if not isValid then
  put "Please enter 3 name initials, letters only, to play." into field "labeMessage"
  put empty into field "editInitial1"
  put empty into field "editInitial2"
  put empty into field "editInitial3"
  focus field "editInitial1"
end if

Next, if the initials in gPlayerName are valid, the name and a large random number are stored in a file created on the player's computer. If you have played Tower Jump 2 you may have noticed that when you play the game you enter your initials (sort of like a login name) but never have to choose and enter a password.

Thinking about it, since all players can see the world wide leaderboard, what stops someone else from impersonating you? Couldn't he or she enter your 3 initials seen on the leaderboard and play under your name?

The rest of the code for the start button, working with the code on the server, prevents this. The first time you play, a random number is generated and stored on your computer. This random number is then stored on the server and checked each time a player starts to play. Since no other player knows the random number associated with your initials they cannot play as you.

This is a simple validation procedure with the disadvantage that you can only ever play from one computer, but it has the advantage of not burdening the players with having to remember and enter a password each time they play.

The code has a platform specific test to choose the most appropriate folder to store the random number, and then checks if the file already exists. If not, it is created, but if you have played before, the random number is read and put into gPlayerID.

switch the platform
  case "MacOS"
    put specialFolderPath("Preferences") into userFolder
    break
  case "Win32"
    put specialFolderPath("0x001a") into userFolder
    break
  default
    put $HOME into userFolder
end switch
put slash & "Tower Jump 2" after userFolder
if there is a folder userFolder then
  put slash & gPlayerName after userFolder
  if there is a file userFolder then
    put URL("file:" & userFolder) into gPlayerID
  else
    put Random(1000000) into gPlayerID
    put gPlayerID into URL("file:" & userFolder)
  end if
else
  create folder userFolder
  put slash & gPlayerName after userFolder
  put Random(1000000) into gPlayerID
  put gPlayerID into URL("file:" & userFolder)
end if

After generating and storing the secret number, a script on a server is called with a HTTP GET request, which is what the put URL command does. The gPlayerName and gPlayerID variables store the 3 letter name and the ID from the random number in the previous section. These are passed to the script as parameters of the URL.

If the contents of gPlayerName and gPlayerID match what is on the server, OK is returned and the player can proceed to the next stage, otherwise an error message is returned and the initials need to be entered again.

put URL("http://thelivecodelab.com/gamescripts/tower-jump/tower-jump-login.lc?name=" & gPlayerName & "&id=" & gPlayerID) into buffer
if buffer = "OK" then
  put true into isValid
else
  put buffer into field "labeMessage"
  put empty into field "editInitial1"
  put empty into field "editInitial2"
  put empty into field "editInitial3"
  focus field "editInitial1"
  put false into isValid
end if

Below is the code in tower-jump-login.lc that is on a remote LiveCode server. This gets the playerName and playerID from the URL line. A check is made to make sure the name is only three letters and the id is a sensible length. Why do this when it has already been done by the code at the player's end?

As a rule, when code is running on a server, you should never assume that the inputs will not be modified by the curious or an attacker. For this reason all inputs to a server-side script should be checked for valid values. Otherwise, you are left open to hacking attempts.

If there is a configuration file based on the players name, the playerID is compared with the contents of the file. If there is a match then all is OK, otherwise it means a player is trying to use 3 initials that are already taken.

The first time a player logs in the configuration file does not exist, so the file is created and the playerID put into it.

<?lc
put $_GET["name"] into playerName
put $_GET["id"] into playerID
put (length(playerName) = 3) and (length(playerID) < 10) into isValid
repeat for each char loopCh in playerName
  if loopCh is not among the chars of "ABCDEFGHIJKLMNOPQRSTUVWXYZ" then
    put false into isValid
    exit repeat
  end if
end repeat
if isValid then
  put playerName & ".cfg" into playerFile
  if there is a file playerFile then
    if line 1 of URL("file:" & playerFile) = playerID then
      put "OK"
    else
      put "Another player with those initials already exists"
    end if
  else
    put playerID into URL("file:" & playerFile)
    put "OK"
  end if
else
  put "ERROR"
end if
?>

Assuming the code above on the server does return OK, then the game is ready to begin. All the code so far is about setting up what is needed to allow the all player leaderboard to work. Nothing to do with gameplay and having fun.

Writing the code above can be satisfying to see working in the finished game, but at the time of writing it may seem like it is keeping you from the "real" part of the game. Reminding yourself that a feature such as leaderboard may be necessary for your game can make the chore seem less tiresome. To increase the chance of a making a successful game, adding those finishing touches helps make it so.

Download standalone Tower Jump 2 executables from here:

Or get it from Itch.io

Part 2 of Tower Jump 2 continues with a look at the assets and more code, which will be posted later.

Happy Jumping.

Tagged: 2D advanced indie platform scroller simulation

Monday, December 8, 2014

0 Responses

Be the first to make a comment.


COMMENTS ARE CLOSED

 

Legal Stuff

All blog posts are copyright and cannot be re-used without permission. But all the code and scripts are dedicated to the public domain. Use such code and scripts in any way you want, but I am not responsible if they don't work for you.

Contact

Further comments or feedback? You can contact me by email at: