Following chat in previous threads I’ve put together a (hopefully) super simplistic example for creating a lunar lander game. I would love to hear feedback on it, particularly from beginners as to how easy it is to use, understand, follow and how useful it is. I’ve added a few suggested challenges as potential next steps once you’ve understood the basics.
Video of gameplay:http://youtu.be/0IZYlqKAVj0
-- Noob Lander -- by West -- This is a very simple example lunar lander game. It is intended for use as an introduction into creating your first game in codea and is aimed at absolute beginners. --The game was built using the following steps: --1. Place the ship sprite on the screen at a pre-defined position (use one of the pre-loaded sprite packs) --2. Add x, y and shipsize variables to set up and use these to position the sprite. Try different values of x,y and shipsize --3. Set y=HEIGHT-100 and x=WIDTH/2 to position the ship near the top centre of the screen. A default shipsize of 25 works well --4. Add a y=y-1 in the draw loop (ship should move down the screen). --5. Add in a tap function - if the current touch is BEGAN or MOVING then draw a second "Thruster jet" sprite at the x, y-offset location --6. Modify 4 to introduce gravity --7. Add thrust when the screen is touched. This should be factored into the equations of motion --8. Draw a line to represent the ground --9. Test to see if the ship has gone below the line (this will be a test on the y coordinate plus an offset equal to half the height of the sprite). If it has, test the current speed of the ship - too fast then crash otherwise win --10. Implement the "Crash" and "Win" screens --11. Add a fuel parameter - can only thrust if there is fuel in the tank. Display current fuel on the screen --12. Add sounds for thrust and explosions -- I've tried to associate comments with most of the steps but the program is the result after all 12 have been implemented. I could separate out the code if there was real interest in a broken down step by step tutorial. -- Code modification challenges --These are aimed at taking this code and tweaking it to see the effects --1. Double the size of the ship --2. Change the level of the ground --3. Decrease the amount of fuel you start with --4. Make it harder by reducing the fastest speed at which you can land. at the moment this is hard coded - can you make this a parameter which is set in setup? --5. Turn the ground into a solid filled rectangle which extends to the bottom of the screen --Code addition challenges -- These are aimed at improving the code by adding extra functions. Please share your updates on the forum to help/inspire others --1. Add a starfield to the background --2. Turn the thrust into a spray of particles.. --2b ..that bounce off the ground and fade with time --3. Add a start screen --4. Add a high score table --5. change the controls to use tilting the ipad for thrust --6. Add horizontal thrusting (and a safe landing area) --7. implement the graphics as a sprite sheet using meshes (this may be required to keep the performance up if you have a starfield and particle based thrust) --Test yourself! --Can you take the 12 steps above and without looking at the code implement a version of the lander game? --Hide the sidebar from the screen displayMode(FULLSCREEN) -- Use this function to perform your initial setup function setup() --2. add x and y variables and use to position the sprite x=300 --x position of the centre of the ship on the screen y=300 --y position of the centre of the ship on the screen --set up a parameter to hold the size of the ship sprite in pixels assume the ship image is a square so the width and height of the ship are the same shipsize=25 --3. position ship near top of screen x=WIDTH/2 --x position of the ship on the screen - set to half the width of the screen y=HEIGHT-100 --y position of the ship on the screen - set to the height of the screen minus 100 pixels --10. Set up a simple finite state machine which will control what parts of the program are run. Finite State Machine sounds fancy but all it means is that there is a limited number of known states (finite) which can be swapped between by some program (machine) --setting CONSTANT parameters makes for more meaningful code, and by convention CONSTANTS are written in captials (WIDTH and HEIGHT are special types of constant) --The three possible states are as follows PLAYING=1 CRASHED=2 LANDED=3 --set up a parameter which will hold the current state of the game gamestate=PLAYING --8. set a parameter to hold the level of the ground in pixels from the bottom of the screen groundlevel=50 --6. set a parameter to hold the speed of the ship speed=0 --6. set a parameter to hold the strength of gravity gravity=-0.02 --6. set a parameter to hold the level of thrust of the ship thrust=0 --11.set a parameter to hold the fuel level fuel=500 end -- This function gets called once every frame function draw() --This sets a dark background color background(40, 40, 50) -- 1. display the ship sprite on the screen --sprite("Space Art:Red Ship",300,300) --10. if the current state of the game is playing then do the following if gamestate==PLAYING or gamestate==LANDED then --5. Thrust sprite - want this to appear behind spaceship so draw before it --test to see if the screen is being touched if (CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING) and gamestate==PLAYING and fuel>0 then --draw the flames from the ship sprite("Tyrian Remastered:Flame 2",x,y-shipsize,shipsize,shipsize) --increase the thrust thrust = thrust + 0.005 --reduce the fuel level fuel = fuel -1 --12. play a thrust sound sound(SOUND_HIT, 16609) --limit the thrust to a maximum level if thrust>0.05 then thrust=0.05 end else --the screen is no longer being touched so remove the thrust thrust=0 end --2 add x and y variables and use to position the sprite and a variable to set the size of the sprite sprite("Space Art:Red Ship",x,y,shipsize,shipsize) --4. Move ship down the screen --y = y -1 --6. and 7. add gravity and thrust -- use variable a to calculate the difference between the amount of thrust and gravity (equivalent of acceleration) a=gravity+thrust --alter the speed of the ship speed=speed+a -- add terminal velocity constraint - sets a maximum speed for the ship --a positive speed means upward travel, the larger the speed then the faster the upward speed --a negative speed means downward travel. The more negative the speed, the faster the ship moves down the screen if speed<-3 then speed=-3 end --use this version of the equation of motion to calculate the distance to move the ship in the y direction between frames y=y+speed+0.5*a --10. if the game state has been set to crashed then draw the explosion and show a message() elseif gamestate==CRASHED then --add the explosion sprite to the screen at the ship position. Each frame add adjust the size of the ship by a random amount - this will give a wobbly explosion effect sprite("Tyrian Remastered:Explosion Huge",x,y,shipsize+math.random(10),shipsize+math.random(10)) text("Crash",WIDTH/2,HEIGHT/2) end -- --8.draw line representing the ground --set the thickness of the line strokeWidth(3) --set the colour of the line stroke(48, 216, 25, 255) --draw the line at the level set by the groundlevel parameter line(0,groundlevel,WIDTH,groundlevel) --9. check to see if the y position of the ship is less than the ground level to see if the ship has reached the ground. --note that we need to take into account the height of the sprite as the y vale is the centre of the ship sprite if y-shipsize/2<groundlevel then --the ship has reached the ground so check to see what speed it was travelling at --the speed must be negative because the ship is moving down towards the ground. --if the speed is slow (close to zero) then have a successful landing if speed>-1 then text("Landed",WIDTH/2,HEIGHT/2) --10 change the gamestate to landed gamestate=LANDED --otherwise the ship is travelling too fast and has crashed else --12. play an explosion sound sound(SOUND_EXPLODE, 23062) --10.change the state of the game to crashed gamestate=CRASHED end --set the position of the ship to rest on the ground. It is possible that the ship will have overshot the ground level as when travelling fast the ship can cover more than 1 pixel at a time between frames y=groundlevel+shipsize/2 --set the speed and thrust of the ship to 0 speed=0 thrust=0 end --11. print out the current fuel level --pick a font font("Courier-Bold") --set the size of the font fontSize(30) --print in the top left corner of the screen. Use .. to join bits of string tp parameters text("Fuel:"..fuel,150,HEIGHT-50) end