Programming with Lua and Corona Self-Paced Course
©
Robert P. Cook 2014
  1. The course is designed for kids or adults that do not know much about computers but who would like to learn to program using game technology.  All software is free.  If you are a K-8 student, you might first want to complete the very gentle, free introduction to programming at www.code.org. If you want to jump right into game programming without buying the book or learning a lot of concepts, try my online lesson here.
  2. The text book is available at amazon.com for $9.99.  All my books are e-books, which means that I can sell them cheaply.  I recommend using the free Kindle reader for the PC, Mac, or Linux. Introduction to Programming with Lua and the Corona Game Lab by Robert Cook
  3. Read Chapter One, Introduction and Parts of a Computer.
  4. Take Test 1. There are 24 questions. If you miss more than 3, review the material.
  5. [optional If you have a helper, such as a parent, who knows something about computers, skip steps 5, 6, 7, 8.
    Review Number Systems here. There are Appendices in the book on Computer Number Systems and the ASCII table.
  6. Take Test 2. There are 12 questions. If you miss more than 2, review the material.
  7. Read Chapter One, Parts of an Operating System.
  8. Take Test 3. There are 21 questions. If you miss more than 3, review the material. optional end]
  9. Read the color introduction. Click here. You may need to refer to the color chart here from time to time.
  10. Read Chapter One, Installing the Corona SDK. Install the Corona Starter SDK using the book's directions. The link to the SDK is at the very bottom of the coronalabs.com web page:
    GetCorona
    Starter: Publish Free

    Email kindlecbook@gmail.com to get the source code for the examples. Since the early days of computing, users have wanted to transmit directories of files from one place to another.  Often, trying to minimize size was a requirement.  The "ZIP" program transforms a directory of files into a single file and compresses them at the same time.  "ZIP" files have the .zip suffix.  The LuaExamples directory is stored in the "ZIP" format.

    To unzip the examples, double-click the downloaded zip file's name. Click on LuaExamples in the unzip application and drag its icon to the Desktop. Double-click the LuaExamples icon to verify that the directory contains the sample programs beginning with aaFirstExample.

    The next steps associate the WordPad text editor as the application to "Open With" .lua files. Double-click the aaFirstExample icon.  Right-click main.lua, choose OpenWith from the first menu, ChooseDefaultProgram for the second menu, then select WordPad and check the box that says "use this app for all .lua files".

    Double-click the Corona Simulator beach-ball-like icon on the Desktop. Notice that two separate windows appear.  One is a duplicate of a physical device, such as an Android phone; the other window is a console window, which displays messages and all printed output.

    Click the OPEN icon (gray circle enclosing an up-arrow). Navigate to the LuaExamples directory in the file dialog.  Double-click aaFirstExample, then select the file main.lua, and click the Open button.  The program runs automatically and displays a white rectangle on a black background in the device window and Hello World in the console window.
  11. If needed, review Cartesian Coordinates. Select menu item File/OpenInEditor.  The code listing for aaFirstExample should be displayed in WordPad in a separate window.  The file contains  two lines, one for screen drawing and one to print to the console window.
    display.newRect(140,140, 100, 100)
    print('Hello World')
    The first two numbers following newRect select the x-y coordinate of the center of the rectangle to draw.  The second two numbers indicate the width of the rectangle in pixels and the height, respectively.  Change the values to learn their purpose.  After changes, select the menu item File/Save in the WordPad editor.  Corona automatically recognizes the change and asks the user about relaunching the simulator. Select the Yes button. You may wonder why the Corona window is small.  It is the size of an Android cell phone window.  The View/ViewAs menu item displays all the platforms on which any program that you write can execute, which is pretty cool.
  12. You may need to review plane geometry here.  All GPUs draw everything that you see on the screen using triangles.  A point is a triangle with all three vertices coincident.  A line is a triangle in which the third point is located between the two other points.  All curves are drawn using line approximations.
  13. Navigate to the Corona documentation for the API (Application Programming Interface). Click on display.*.  Scroll down to display.newRect() to view the rectangle documentation.  A bracketed item i.e. [parentGroup] in the syntax specification can be omitted.
  14. Run the abFillStroke example and change the program to experiment with fill and stroke colors as well as stroke width. The program extends aaFirstExample by naming (x) the rectangle.  The name can then be used to set different properties, such as the fill (interior) or stroke (edge) colors.

    Note that Corona colors are set with real numbers (0.0 to 1.0) not integers (0 to 255) or hexadecimal values (0 to ff).  However, the color chart here lists values by hexadecimal (Indigo #4B0082). The hex values can be coded in Corona as setFillColor(0x4B/255, 0x00/255, 0x82/255).
  15. Run the acShapes example and change the program to experiment with different options.  You may want to copy and paste the examples to create duplicate project directories.  That way, if you mess up, the original can always be copied again to start over.
  16.  Google "jpeg versus png gif" to learn more about image formats. When you download an image, always right click on the name, then select Properties, then copy down the width and height in pixels as well as the image format.  For example if the image name is 'dog' and the format is '.png', you need to use the name 'dog.png' in the Corona program.
  17. The best way to write a correct program is to copy correct code (legally of course).  In any media project, titles (or splash screens) are very important.  Watch the Vimeo video A Brief History of Titles. Check out the Corona online documentation examples for setFillColor (Text, Gradient, Image Tint).  Now design and implement a title screen. Any images that are downloaded should be moved to the same  directory as the main.lua file. The background color of the window can be set as follows:
    display.setDefault('background', 0,1,0)  --green
  18. In the modern world, people are challenged to keep learning for a lifetime.  Thus, the purpose of courses is just to start students on that path.  Try learning about transitions on your own.

    When switching from one scene to another in a movie or video game, it is common to apply a transition effect. Navigate to the Corona documentation for the API. Click on transition.*.   Try out each of the examples then integrate one or more transitions into your splash screen design.  If an example refers to an image file, you will need to copy your own image file and then change the name in the program.
  19. Congratulations on finishing Chapter One.
  20. Read Chapter Two, Introduction, Constants and Line Syntax. Run and study the example adConstants.
  21. Take Test 4. There are 11 questions. If you miss more than 1, review the material.
  22. Read Chapter Two, Expressions and Math Functions. Run and study the example aeExpressions.  Review sines, cosines, angles and radians at the MathIsFun web site.
  23. Take Test 5. There are 13 questions. If you miss more than 2, review the material.
  24. Read Chapter Two, Formulas and Tables.
    1. Modify the Centigrade to Fahrenheit program in the book to print a table of 50 values.
    2. Modify the program to print a Fahrenheit to Centigrade table.  The human formula is (F-32)*(5/9).
    3. Notice that whatever code is in the for loop is repeated a number of times.
    4. Replace the print statement with a new circle, line or rectangle.  You can use the same code to plot functions!
  25. Congratulations on finishing Chapter Two.
  26. Read Chapter Three up to, but not including, the Conditional Statements section.
  27. Take Test 6. There are 10 questions. If you miss any, review the material.
    • So far, except for transitions, all drawing has been static figures, which is not very interesting. The Corona Lua Runtime library enables easy animated drawing.  Earlier we introduced a looping code pattern by explaining how to use it but not how it was implemented.  The pattern listed next is the standard for animation.
    • The "addEventListener" together with the "enterFrame" option references a block of code ("update") that is called every time the window should be redrawn. "enterFrame" events occur at the FPS interval of the application (30 or 60 Frames Per Second).
      --create shapes here
      function update()
      --update shape properties (x, y, color) here           
      end           
      Runtime:addEventListener( "enterFrame", update )
    • Play around with, modify and extend the following two examples.
      circle = display.newCircle(100,200,8) --x y radius in pixels
      function update()
       circle.x = circle.x+0.2
      end          
      Runtime:addEventListener( "enterFrame", update )

      function update()
       circle = display.newCircle(0,0,8)
       circle.x = math.random(300)+100
       circle.y = math.random(300)+100
       circle:setFillColor(math.random(),math.random(),0)
      end          
      Runtime:addEventListener( "enterFrame", update )

  28. Run and study the afCurve example.  [optional Read about projectile trajectories here.]
  29. Run and study the agCurve2 example.  [optional Read about parametric equations here. Google the term 'parametric equations' then click on Images at the top of the page to see more examples.]
  30. Read Chapter Three from Conditional Statements up to, but not including, the While, Repeat, Do Statements section.
  31. Take Test 7. There are 24 questions. If you miss more than 5, review the material.
  32. Take Test 8. There are 10 questions. If you miss more than 1, review the material.
  33. [optional The bitset module is only supported in the most recent versions of Lua. An 8-bit byte contains 8 Boolean values. A 32-bit integer contains 32 Boolean values.  In C or C++, the 'and or not' operators can be applied to every bit in an integer.  In Lua, the bitset module must be used to achieve the same purpose.  Study this section of the book and try out the example.]
  34. [optional Sometimes it is desirable to simplify a Boolean expression such as ( not (a>b and r<t) ). deMorgan's Laws from logic are applied to simplify such expressions by removing the 'not' operator.  The explanation is here.]
  35. Run the ahInput example to explore how keyboard input works on a PC/Mac/Linux.  On a cell phone, typically a soft keyboard would be displayed to allow user typing; however, that option's implementation is more complicated.  ahInput implements a very simple dialog that displays a title and then accumulates typed input until the 'enter' or 'return' key is pressed.
  36. Review the Pythagorean Theorem for right triangles here.  Run the aiTriangle example to draw a right triangle.  For some reason, the default stroke width of 1 drew invisible lines on the PC Droid simulator so the example in the sample code has been updated from the one in the book.
  37. Run the ajTriangle2 example.  Use the test input listed in the book to exercise the code.  A famous computer scientist Edsger Dijkstra said "testing can never prove the absence of errors".  However, it is an essential first step. The code first tests for "three disjoint points" by computing the area of the triangle and comparing to zero.  Since the test involves real numbers, "equal to zero" is only to within a certain tolerance.  The second part of the code checks each of the three sides to see if it is the hypotenuse using the Pythagorean Theorem.
  38. Read more about Decision Tables here.  Run and study the How to Write a Simple Program example.
  39. Run and study the akTriangle3 example.
  40. Read and study the remainder of Chapter Three.  The while, repeat and for statements execute a group of statements repeatedly.  The looping usually involves a variable that may be incremented positively from 1 to 50 or negatively from 47 to 3.  Often the variable is used to access members of a set of numbers or shapes, which is referred to as iteration.  The variable in this case is termed an iteration variable.
    • The following steps can be used as a guide for how to program a loop.
    • Solve the problem for 1 case.

    • Solve the problem for 2 cases.

    • Look for common statements.

    • Identify the initial and termination conditions.
    • Add a loop to enclose common statements for N cases.

  41. Take Test 9. There are 26 questions. If you miss more than 4, review the material.
  42. Congratulations on finishing Chapter Three.
  43. Read Chapter Four, Procedures.
  44. Run and study the alfruit example. Download an image from the Internet then change the background in alfruit.  Download the image of a different fruit or ball-shaped object and modify the program to bounce it around the screen.  Use the alfruit program to experiment with the translate, scale and rotate methods.  Just type their names in the "search" box at the Corona documentation web site to learn more details.
  45. Take Test 10. There are 23 questions. If you miss more than 4, review the material.
  46. Review the physics concepts of speed and velocity here.
  47. [optional Run and study the amRecursion example.]
  48. At this stage in your programming studies, it is more important to learn how to use procedures than to write them.  Remember that statements that are reused are best enclosed by a function definition.  Try using the definitions in Corona's "easing" and "audio" modules. Watch a short video on sound formats here.
local circle = display.newCircle( 100, 100, 40 )
circle:setFillColor( 0, 0, 1 )
transition.to( circle, { time=800, y=400, transition=easing.inExpo } )
local soundEffect = audio.loadSound( "explosion.mp3" )
audio.play( soundEffect )
  1. Congratulations on finishing Chapter Four.
  2. Read Chapter Five, Introduction, Particles, Array Parameters.
  3. Run and study the anParticles example.  Modify the example to delete particles that fly off the screen. Modify the example to create rotating particles.
  4. Take Test 11. Answer the first 20 questions. If you miss more than 3, review the material.
  5. [optional Read and study the Algorithms Section.]
  6. Read Chapter Five, Sprites and Tables.
  7. Take Test 12. Answer the first 18 questions. If you miss more than 3, review the material.
  8. View a short movie about sprite sheets here. Run and study the aoCat, apCatBackground, aqAlert and arInput examples.  Update one of  the cat examples so that the cat reverses direction at the edge of the screen.  Download your own sprite sheet from the Internet and write a  program to animate it.
  9. Run and study the asParticle and atFountain examples.
  10. Congratulations on finishing Chapter Five.
  11. [optional Chapter Six can be bypassed.  It is not a prerequisite to the remaining Chapters. Come back and read it when you have time.]
  12. Read all of Chapter Six, Strings.
  13. Take Test 13. Answer all 38 questions. You can miss answers but be sure you know how to get it right when referencing the documentation.
  14. Congratulations on finishing Chapter Six.
  15. Read Chapter Seven, Introduction, Structures, Bottom-Up Programming, Top-Down Programming.
    The programming data concepts discussed so far are listed next:
    constants
    expressions
    variables
    formulas (expressions involving variables)
    arrays (collections of values and possibly other arrays)
    tables/structures  (associative, unordered list of name/value pairs)

    All of these data options are built into Lua.

    One of the revolutions in programming was the invention of the "class" or "abstract data type" concept that allowed users to extend a programming language with their own data types, such as date, money, weight, person, pet, teapot etc. Chapter Seven introduces Lua class programming. Read, study, and execute the Class Design Tutorial.
  16. Run and study the asParticle and atFountain examples.
  17. [optional Read Chapter Seven, Lists, Iterators, Abstract Data Types and Variable Lifetime Model. Run and study the auList, avDelete, awDoubleList and axIterator examples.]
  18. Go here to review the definitions for vectors, unit vectors, dot and cross products.
  19. Read Chapter Seven, Class Design, Class Visual Testing, Class Chaining and Vector Class.
  20. Run and study the ayClass, azRectangle, baVector, bbBouncy, bcEllipse and bdEllipse examples.
  21. Congratulations on finishing Chapter Seven.
  22. Read Chapter Eight, Debugging. Chapter Eight introduces the ZeroBrane integrated development environment (IDE) for Lua and Corona.  With the introduction of class programming, coding is more challenging.  For example, a program will likely use classes written by other people.  A debugger allows the user to set program breakpoints, examine the values of all variables when a program pauses at a breakpoint, and then to continue execution.
  23. Congratulations on finishing Chapter Eight.
  24. Read Chapter Nine, Box2D Rigid-Body Physics.
  25. Congratulations on finishing Chapter Nine.
  26. Congratulations on finishing the course!!