Quantcast Spaceship - eLua Wiki
Locked History Attachments

Spaceship

Information


Runs on: EK-LM3S8962, EK-LM3S6965

Description


Tired of hitting a ball? What about hitting a spaceship instead? Or, even better, more spaceships? Defend your base with this little game that also plays on the OLED display of your LM3S board and uses the on-board switches as paddles. Once again, take a look at the code to get a feeling of eLua.

Videos


eLua Spaceship v0.1

Code

   1 -------------------------------------------------------------------------------
   2 --
   3 --		eLua SpaceShip Game
   4 --
   5 --
   6 -- LED Lab @ PUC-Rio - 2009
   7 --   Dado Sutter
   8 --   Ives Negreiros
   9 --   Ricardo Rosa
  10 --   Pedro Bittencourt
  11 --   Teo Benjamin
  12 --
  13 --
  14 -- Initial Version by Téo Benjamin, Aug 2009
  15 --
  16 -------------------------------------------------------------------------------
  17 
  18 local xcanvas = 124						-- Real screen X size
  19 local xcanvas = xcanvas - 12			-- Canvas size with wall
  20 local ycanvas = 96						-- Screen Y size
  21 local pressed = {}						-- This table is used to help the buttonClicked function. If the btn if pressed,
  22 --	pressed[ btn ] = true, when it is released, pressed[ btn ] = nil
  23 local ShipChar = ">"					-- This is the char that is printed to represent the SpaceShip
  24 local shotChar = "-"					-- This char is printed to represent the shots
  25 local enemyChar = "@"					-- This char is used to represent the enemies
  26 local sy = 16							-- Ship's Y position
  27 local sx = 0							-- Ship's X position
  28 local bullets = 70						-- Number of bullets left
  29 local score = 0							-- Player's score
  30 local numOfEnemies = 5					-- Number of simultaneous enemies
  31 local won = false						-- If the player won the game, this value is true. If not, it's false.
  32 
  33 -- Initialization
  34 local kit = require( pd.board() )
  35 
  36 local canvasMap = {}	-- canvasMap[ line ][ #shot ]
  37 -- canvasMap[ i ][ j ]: i represents the y position and j is a numeric index for each shot in that line. The value is the x position.
  38 -- canvasMap[ i ].e represents the x position of the enemy in that line, if any.
  39 -- Enemies can only appear in lines 5, 15, 25, 35, ..., 75 and 85
  40 for i = 1, ycanvas, 1 do
  41   canvasMap[ i ] = {}
  42 end
  43 for i = 5, 85, 10 do
  44   canvasMap[ i ].e = false
  45 end
  46 
  47 function drawWall( x, y )
  48   for i = 0, y, 7 do
  49     lm3s.disp.print( "|", xcanvas + 1, i, 0 )
  50   end
  51   xcanvas = x
  52   for i = 0, y, 7 do
  53     lm3s.disp.print( "|", xcanvas + 1, i, 6 )
  54   end
  55 
  56 end
  57 
  58 function drawShip( x, y, color, movement )
  59 
  60   if ( movement == 0 ) then
  61       lm3s.disp.print( ShipChar, x, y,  color )
  62   elseif ( movement > 0 ) then -- Moving Down
  63     if y < 8 then
  64       lm3s.disp.print( ShipChar, x, 0,  0 )
  65     else
  66       lm3s.disp.print( ShipChar, x, y - 8 , 0 )
  67     end
  68     lm3s.disp.print( ShipChar, x, y,  color )
  69   elseif ( movement < 0 ) then -- Moving Up
  70     lm3s.disp.print( ShipChar, x, y + 8, 0 )
  71     lm3s.disp.print( ShipChar, x, y,  color )
  72   end
  73 end
  74 
  75 
  76 function updateShipPos()
  77   if kit.btn_pressed( kit.BTN_UP ) then
  78     if ( sy > 1 ) then
  79       sy = sy - 1
  80       drawShip( sx, sy, 11, -1 )
  81     else
  82       tmr.delay( 1, 1700 )
  83     end
  84   elseif kit.btn_pressed( kit.BTN_DOWN ) then
  85     if ( sy + 7 < ycanvas ) then
  86       sy = sy + 1
  87       drawShip( sx, sy, 11, 1 )
  88     else
  89       tmr.delay( 1, 1600 )
  90     end
  91   else
  92     drawShip( sx, sy, 11, 0 )
  93     tmr.delay( 1, 300 ) -- Maintain function processing time aprox the same
  94   end
  95 end
  96 
  97 
  98 
  99 function updateShots()
 100   for i in ipairs( canvasMap ) do
 101     for j in ipairs( canvasMap[ i ] ) do
 102       if canvasMap[ i ][ j ] then
 103         lm3s.disp.print( shotChar, canvasMap[ i ][ j ], i, 0 )
 104         canvasMap[ i ][ j ] = canvasMap[ i ][ j ] + 2
 105         lm3s.disp.print( shotChar, canvasMap[ i ][ j ], i, 13 )
 106         if canvasMap[ i ][ j ] + 4 >= xcanvas then
 107           lm3s.disp.print( shotChar, canvasMap[ i ][ j ], i, 0 )
 108           table.remove( canvasMap[ i ], j )
 109           break
 110         end
 111         local en = math.floor( i / 10 ) * 10 + 5
 112         if canvasMap[ en ].e then
 113           if ( canvasMap[ i ][ j ] <= canvasMap[ en ].e ) and ( canvasMap[ i ][ j ] + 4 >= canvasMap[ en ].e ) then
 114             destroyEnemy( i, j, en )
 115             createEnemy()
 116           end
 117         end
 118       else
 119         tmr.delay( 1, 1200 )
 120       end
 121     end
 122   end
 123 end
 124 
 125 function shot()
 126   if bullets > 0 then
 127     table.insert( canvasMap[ sy ], sx + 6 )
 128     bullets = bullets - 1
 129   end
 130   sound()
 131 end
 132 
 133 function buttonClicked( button )
 134   if kit.btn_pressed( button ) then
 135     pressed[ button ] = true
 136   else
 137     if pressed[ button ] then
 138       pressed[ button ] = false
 139       return true
 140     end
 141     pressed[ button ] = false
 142   end
 143   return false
 144 end
 145 
 146 function printBulletsNum()
 147   lm3s.disp.print( string.format( "%2d", bullets ), xcanvas + 4, 2, 6 )
 148 end
 149 
 150 function printScore()
 151   lm3s.disp.print( string.format( "%2d", score ), xcanvas + 4, ycanvas - 10, 6 )
 152 end
 153 
 154 function sound()
 155   pwm.start( 1 )
 156   tmr.delay( 0, 20000 )
 157   pwm.stop( 1 )
 158 end
 159 
 160 
 161 function updateEnemiesPos()
 162   for i = 5, 85, 10 do
 163     if canvasMap[ i ].e then
 164       lm3s.disp.print( enemyChar, canvasMap[ i ].e, i, 0 )
 165       canvasMap[ i ].e = canvasMap[ i ].e - 1
 166       lm3s.disp.print( enemyChar, canvasMap[ i ].e, i, 11 )
 167       if canvasMap[ i ].e <= 0 then
 168         lm3s.disp.print( enemyChar, canvasMap[ i ].e, i, 0 )
 169         canvasMap[ i ].e = nil
 170         createEnemy()
 171       end
 172     end
 173   end
 174 end
 175 
 176 function createEnemy()
 177   while true do
 178     local en = ( math.random( 0, 8 ) )*10 + 5
 179     if not canvasMap[ en ].e then
 180       canvasMap[ en ].e = xcanvas - 5
 181       break
 182     end
 183   end
 184 end
 185 
 186 function addEnemy()
 187   if numOfEnemies < 9 then
 188     numOfEnemies = numOfEnemies + 1
 189     createEnemy()
 190   end
 191 end
 192 function destroyEnemy( i, j, en )
 193   lm3s.disp.print( shotChar, canvasMap[ i ][ j ], i, 0 )
 194   table.remove( canvasMap[ i ], j )
 195   lm3s.disp.print( enemyChar, canvasMap[ en ].e, en, 0 )
 196   canvasMap[ en ].e = nil
 197   score = score + 1
 198 end
 199 
 200 function destroyAll()
 201   for i in ipairs( canvasMap ) do
 202     for j in ipairs( canvasMap[ i ] ) do
 203       lm3s.disp.print( shotChar, canvasMap[ i ][ j ], i, 0 )
 204       table.remove( canvasMap[ i ], j )
 205     end
 206     local en = math.floor( i / 10 ) * 10 + 5
 207     if canvasMap[ en ].e then
 208       lm3s.disp.print( enemyChar, canvasMap[ en ].e, en, 0 )
 209       canvasMap[ en ].e = nil
 210     end
 211   end
 212 
 213 end
 214 
 215 for i = 1, numOfEnemies, 1 do
 216   createEnemy()
 217 end
 218 
 219 
 220 lm3s.disp.init( 1000000 )
 221 
 222 -- Initial information
 223 lm3s.disp.print( "eLua SpaceShip", 10, 10, 11 )
 224 lm3s.disp.print( "Press SELECT -->", 10, 40, 11 )
 225 local seed = 0
 226 while not buttonClicked( kit.BTN_SELECT ) do
 227   seed = seed + 1
 228 end
 229 math.randomseed( seed )
 230 lm3s.disp.clear()
 231 
 232 -------------------------------------------------------------------------------
 233 --
 234 --				MAIN LOOP
 235 --
 236 -------------------------------------------------------------------------------
 237 pwm.setclock( 1, 25000000 )
 238 pwm.setup( 1, 1000, 70 )
 239 
 240 drawWall( xcanvas, ycanvas )
 241 
 242 while true do
 243   updateEnemiesPos()
 244   for i = 1, 3, 1 do
 245     updateShipPos()
 246     updateShots()
 247   end
 248   printBulletsNum()
 249   printScore()
 250   if buttonClicked( kit.BTN_SELECT ) then shot() end
 251   if buttonClicked( kit.BTN_RIGHT ) then
 252     destroyAll()
 253     for i = 1, numOfEnemies, 1 do
 254       createEnemy()
 255     end
 256   end
 257   if score >= 50 then
 258     won = true
 259     break
 260   end
 261   if bullets <= 0 then
 262     won = false
 263     break
 264   end
 265   tmr.delay(1, 12000)
 266   collectgarbage("collect")
 267 end
 268 if won then
 269   lm3s.disp.clear()
 270   lm3s.disp.print( "You won!", 20, 30, 11 )
 271   lm3s.disp.print( "Congratulations!", 5, 50, 11 )
 272 else
 273   lm3s.disp.clear()
 274   lm3s.disp.print( "Game Over! :(", 20, 20, 11 )
 275 end
 276 

Files