Version 0 of Moon Lander

Updated 2008-10-02 21:02:54 by nedbrek

nedbrek - This page will document the development of a simple game, similar to the old games "Moon Lander" and "Hey! Taxi!".

The code will be presented piecemeal, in the history. (Please comment in the history based on what type of changes added - "added $feature", "fixed bug", "added comments")

It can also be used to demonstrate simple physics (gravity/acceleration, velocity, and position).

 # gravity in the Y direction (constant)
 set ::gravY 1

 # current lander velocity in the Y direction
 set ::velY 1

 # canvas item id for our lander/taxi rectangle
 set ::taxiID 0

 # main event loop, currently running at 1 Hz (poor refresh, simple logic!)
 proc eventLoop {} {
   # check for the taxi being destroyed
   if {$::taxiID == 0} { return }

   # find the x1 y1 x2 y2 coords for the taxi
   set coords [.c coords taxi]

   # if the taxi is moving up (taking off) or has not moved into the ground (aka hard crash or soft land)
   if {$::velY < 0 || [lindex $coords 3] < [.c cget -height]} {
      # accelerate
      incr ::velY $::gravY

      # move according to velocity
      .c move taxi 0 $::velY
   }
   # post next update event
   after 1000 eventLoop
 }

 # callback for thruster
 proc thrustUp {} {
   incr ::velY -10
 }

 ### GUI
 # main frame
 pack [frame .fVel] -side top

 # current Y velocity 
 pack [label .fVel.lVY -textvariable velY] -side right
 pack [label .fVel.lLY -text "Y Velocity:"] -side right

 # main playing area 
 pack [canvas .c] -side top

 # create our 'taxi', a yellow rectangle
 set ::taxiID [.c create rectangle 10 10 20 20 -fill yellow -tags taxi]

 # allow the player to thrust up
 bind . <Up>    thrustUp

 # start the game event loop 
 eventLoop

Category Games Category Animation Category Example Category Tk