Articles Archive
Articles Search
Director Wiki
 

PseudoCode Works!

September 28, 1999
by Jim Rudnick

If you're like me (and you may not want to admit it!) the Lingo programming skills you have came from trial and error -- rather than from schools, institutions or any type of delivered and structured learning environment. Director is a great program and Lingo is first-class...but together, they make the learning curve near straight up at first. And for those of us who strive to increase our programming skills, that up-ramp can be deadly!

One thing I do know, though, is English. Oh, not as well as when I got out of my formal liberal arts education decades ago...but I do think (judging by the web!) that my skill in using the language means that I'm usually understood and that's quite a feat in the 90's! And my English skills are so good not because I HAD that formal education, but because I use it every day; spoken, written and read. I like to think that English for me, allows me to be what I am because it defines me via my usage of same. I use English, therefore, I am -- so to speak! Yes, I admit that I know English much better than Lingo. And that's where my own problems lie.

You see, in English I know just how to program anything. Nothing is too tough; I can use English to create a Director movie that is the best of the best. No one on this planet's better than me (IMHO) when it comes to using English to create movies...it's so easy to simply say "and then when the user clicks here, the movie will automatically do this and then the user will be very very happy!" One thing though....my Lingo skills aren't up to that level...my Lingo can't keep up.

So how do I handle that problem? Simple... code in English first! Or what I've learned to call pseudoCode. You see, when I approach the task of writing any script, I do it first in longhand, on paper, in English or psuedoCode

Yup, I know, that's not the way most of us code....but it is my way. I begin on paper, using psuedoCode first, to outline my thoughts/needs/desires for each and every movie before I turn to the computer. This works very well for me, in that it lets me outline in psuedoCode first; it then lets me move into Lingo in stages, replacing psuedoCode with Lingo in a modularized fashion -- section by section; it allows me to 'code' anywhere, not just in the office at my box; and lastly, it allows me to just scratch out a section and rewrite it instantly without having to type in changes. This is very important, as it keeps me on-track and focused in a single push towards the Lingo I need, without being diverted by the keyboard or syntax.

An example here will do nicely. In a recent movie for a client, I needed to add in a button that would return the user back to the last frame visited where they'd made a selection, to track the users movement through the movie. So on paper, I merely wrote in psuedoCode the following...

on mouseUp
  take user to previously visited frame where 
  they'd made an entry
end

As you can see, psuedoCode is great...because I wrote only that single line, and then pressed ahead with the on-paper writing of much more pseudoCode to stay on track. But obviously, psuedoCode only works on paper...at some point you must change that psuedoCode into Lingo.

Which is exactly what I did do, when I turned to actually developing the movie itself. For that button sprite, I read my psuedoCode text written out days earlier, and knowing some Lingo, wrote the following as a simple sprite script for that sprite.

on mouseUp
  -- tracking user location using gjustBeenHere
  -- already set when user left last frame 
  -- global holds the frameLabel
  global gjustBeenHere
  
  go to frame gjustBeenHere
end 

While this is quite a simple example, let's take a look at a recent game I created for a multimedia class I taught on basic gaming in Director. What I wanted to do is to show them how to create a basic game from scratch; creating their own assets and then using Director to develop the game itself. Because they are very new to the software, it had to be simple and easy to understand, as well as small enough to be created in a few hours of lab time...hence the game Stanley's Cup was created.

Using my psuedoCode methodology, I was able to write the concept and code quite quickly...

Stanley's Cup: (basic psuedoCode example)
Game to run in a single frame at a fast FPS for 
quickness of reaction time...
Need a puck, the Stanley Cup, on screen score for 
misses, score for caught pucks...
Need to be able to move cup to catch falling puck...
Need to be able to tell user they're a winner or a loser 
after a certain number of pucks go by...
If user misses/catches so many, stop game.

As you can see, this simple description is all you need do at first to understand what you're trying to create. Once that's done, my next task is to expand that psuedoCode into a much fuller description of the game.

Stanley's Cup: (expanded psuedoCode example)
on exitFrame
  set up puck, Stanley Cup, score for miss, 
  score for caught
  get rid of cursor
  have puck drop from top of stage in a twisty-turny 
  hard to catch way
  move Stanley Cup by using 2 easy-to-reach keys
  if puck is caught, update "caught" score and 
  hide puck from user view
  if puck is missed, update "missed" score and 
  hide puck from user view
  count misses...if more'n a certain number, then stop 
  game and insult user
  count catches...if more'n a certain number, then stop 
  game and praise user
  reset the puck back to the top of the stage to fall again, 
  in a random spot tho...
  offer a reset button to begin again with 
  scores reset also
end

This new psuedoCode script has expanded the needs that the movie has, and has listed them in a somewhat ordered fashion. You'll note that I have used terms like "if" and "set" -- words that are Lingo commands too, but used here simply show a decision in the programming that Lingo will make based on user action during run time, for example. My psuedoCode is not tight, it's very loose but it shows me what's needed, and in this fairly basic example, I knew as I was writing psuedoCode that the Lingo skills I'd need later I already had in my skill set.

Once the above psuedoCode was written, my last task was to move to full Lingo...the script below shows the end result of the psuedoCode development method...

on exitFrame
         
  global ggotEm, gmissedEm, gFall
  
  cursor (200)
  
  -- below sets the sprites for interaction
  set cupSprite = 1
  set puckSprite = 2
  set hideSprite = 3
  
  -- below, sets the cup to match the cursor movement..
  set the locH of sprite cupSprite = the mouseH
  set the locH of sprite hideSprite = the mouseH
  
  
  -- below, sets the rate of fall and warp for the 
  -- puck...randomizing any warping of the path
  if random(2) = 1 then
    set the locV of sprite puckSprite = the locV of sprite ¬
          puckSprite + gFall
    set the locH of sprite puckSprite = the locH of sprite ¬
          puckSprite + random(5)
    set the locH of sprite puckSprite = the locH of sprite ¬
          puckSprite - random(5)
  else
    set the locV of sprite puckSprite = the locV of sprite ¬
          puckSprite + gFall
  end if
  
  
  -- loop here for a catch or a miss..
  if the locV of sprite puckSprite >= the locV of sprite ¬
    cupSprite then
    
  -- intersection here
  if the locH of sprite puckSprite < the left of the rect of ¬
    sprite cupSprite or  the locH of sprite puckSprite > the ¬
        right of the rect of sprite cupSprite then
      
    --this is a miss...
    puppetSound "miss"
    set the text of member "missedEm" = string(gmissedEm + 1)
    set gmissedEm = gmissedEm + 1
    updateStage
  
    if gmissedEm > 20 then
      alert "We have a loser!"
      halt
    end if
      
    else
      -- this is a catch...
      puppetSound "caught"
      set the text of member "gotEm" = string(ggotEm + 1)
      set ggotEm = ggotEm + 1
      updateStage
      if ggotEm > 20 then
      alert "We have a winner!"
      halt
  end if
  
  end if
    
  -- reset the puck back to the top of the stage, and randomize 
  -- where it comes in...
  set the locV of sprite puckSprite = 0
  set the locH of sprite puckSprite = random(380)+20
  end if
  
  go to the frame
  
end

A bonus here, for this movie, is that I didn't have to leave any of the psuedoCode unconverted because I didn't know the actual Lingo to replace it. But when that does happen, you've at least pared down the hundreds of lines of code to the few that you must research or ask for help on...another bonus to using psuedoCode in my book!

As you can see, the game itself is not polished. It runs fine though, as an introductory item for new students to learn gaming basics, and has spun off many varying, much funnier games from that class as the learning (as usual) spawns new creative leaps. But it does teach how to design your programming using psuedoCode and it shows off what I think the advantages of doing that actually are -- that this style of scripting allows me to target my thinking into easily understood pieces. It lets me move towards my goal of a completed movie, by allowing me to get there via stages, that is, expanded from psuedoCode to Lingo. What I've learned by this methodology, is that psuedoCode often leads directly to Lingo as sometimes one is almost equal to the other which is a real bonus. And, this movement in stages allows me to learn at the same time.

And as a multimedia developer, that's important. I can both court clients and produce what I sell because of my English skills. But when it comes to Lingo, I must learn at all times. It's always challenging to know that your psuedoCode points to something that you have no idea how to accomplish in Lingo...but nothing is a better driver to attain that knowledge than having a last line of psuedoCode staring you in the face. And learning how to move that psuedoCode into Lingo is truly an education!

Download "Stanley's Cup" for Mac or Win.

Beginning with HyperCard on a Mac Plus over 12 years ago, Jim began his digital career as a developer, programmer and owner of his own multimedia firm, KKT Interactive. Moving from HyperCard to SuperCard was a natural transition, as was the move to Director 4.0 almost 5 years ago. Since then, Jim has worked on honing his knowledge by teaching both Director and Lingo for community colleges and digital studios in and around the Toronto area. Jim was also a member of the Director 6 BetaTeam, which successfully brought that version of Director to the marketplace in mid 1997. Jim's true strengths are his ability to create assets while conceiving, coding and test programming routines at great length, all the while remaining on-time and on-budget contracting for clients that include QuakerState Oil, MercedesBenz, Reuters, the AdLib AdAgency, Simon & Schuster, SAAB, the New York Yankees and the VanHorne Winery.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.