Created
June 17, 2013 19:26
-
-
Save Westenburg/5799558 to your computer and use it in GitHub Desktop.
A step by step tutorial on creating a simple lunar lander game in Codea. Contains 13 tabs. Each tab contains one step in the tutorial and successive tabs build on each other. Drag the tab you wish to run to the far right then press the run button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# Main | |
-- 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. | |
--ONLY THE FURTHEST RIGHT HAND TAB WILL BE EXECUTED. DRAG THE TAB FOR THE STEP YOU WISH TO RUN TO THE FAR RIGHT THEN PRESS RUN | |
--The game was built using the following steps. Each step is provided in its own tab and builds upon the previous one. For each tab only comments related to that particular step are provided but a fully commented version is provided below. | |
--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 | |
--# Step1 | |
-- 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. | |
--This is the first step | |
--1. Place the ship sprite on the screen at a pre-defined position (use one of the pre-loaded sprite packs) | |
--Hide the sidebar from the screen | |
displayMode(FULLSCREEN) | |
-- Use this function to perform your initial setup | |
function setup() | |
--at the moment nothing is needed here | |
end | |
-- This function gets called once every frame | |
function draw() | |
--This sets a dark background color | |
background(40, 40, 50) | |
-- 1. display the red ship sprite on the screen at position 300 pixels along the x axis from the left and 300 pixels up from the bottom of the screen. The size of the ship is set to be 50 pixels wide and 50 pixels high | |
sprite("Space Art:Red Ship",300,300,50,50) | |
--Tap "Space Art:Red Ship" to bring up the sprite picker. Select a new image and run. Adjust the 4 numbers to alter the x position ,y position ,width and height of the image | |
end | |
--# Step2 | |
-- 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. | |
--Assuming you have already got the ship to show on the screen from step 1 | |
--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 | |
displayMode(FULLSCREEN) | |
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 | |
end | |
function draw() | |
background(40, 40, 50) | |
--2 add x and y variables and use to position the sprite and a variable to set the size of the sprite | |
--command is the same as step 1 but parameters now represent the numbers | |
sprite("Space Art:Red Ship",x,y,shipsize,shipsize) | |
--Change the values of the parameters in the setup to change the size and position of the ship | |
end | |
--# Step3 | |
-- 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. | |
--Assume you have completed steps 1 and 2 to get the ship on the screen and adjust its size and position | |
--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 | |
displayMode(FULLSCREEN) | |
function setup() | |
--3. position ship near top of screen | |
--Use special parameters HEIGHT and WIDTH (note the capital letters). These are used in place of numbers and are useful for making games work across different devices. For example the width of an iPhone screen in pixels is different from an iPad. Using these parameters takes care of this potential issue | |
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 | |
shipsize=25 | |
end | |
function draw() | |
background(40, 40, 50) | |
sprite("Space Art:Red Ship",x,y,shipsize,shipsize) | |
end | |
--# Step4 | |
-- 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. | |
--Assume you have completed steps 1, 2 and 3 to get the ship on the screen and adjust its size and position and have set the position to near the top of the screen | |
--4. Add a y=y-1 in the draw loop (ship should move down the screen). | |
displayMode(FULLSCREEN) | |
function setup() | |
x=WIDTH/2 | |
y=HEIGHT-100 | |
shipsize=25 | |
end | |
function draw() | |
background(40, 40, 50) | |
sprite("Space Art:Red Ship",x,y,shipsize,shipsize) | |
--4. Each time the main draw function is executed, reduce the value held in the y variable | |
y=y-1 | |
--this will make the ship move down the screen. Try subtracting different values. You can also remove fractions of a number. try y=y-0.05. This will make the ship move really slowly | |
end | |
--# Step5 | |
-- 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: | |
--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 | |
function setup() | |
x=WIDTH/2 | |
y=HEIGHT-100 | |
shipsize=25 | |
end | |
function draw() | |
background(40, 40, 50) | |
--5. Thrust sprite - want this to appear behind spaceship so draw before it | |
--test to see if the screen is being touched | |
--This uses a special input called current touch to test if the screen is being touched. The .state part gives the state of the touch. Current touch also has other properties such as .x and .y which will return the position of the touch, but these aren't required here | |
if (CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING) then | |
--draw the flames from the ship | |
--draw the flames using the same x and y positions except move it down. We could use y-100 to move the flames 1pp pixels but this moves the flames 100 pixels regardless of the size of the image. If we use the shipsize parameter then this will allow us to scale the ship and flames together and makes sure the sizes and locations are relative to each other. | |
sprite("Tyrian Remastered:Flame 2",x,y-shipsize,shipsize,shipsize) | |
end | |
sprite("Space Art:Red Ship",x,y,shipsize,shipsize) | |
y = y -1 | |
--Try going back and altering the shipsize parameter now | |
end | |
--# Step6 | |
-- 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. | |
--By now you should have your ship on the screen which slowly moves to the bottom and when you touch the screen then thrust appears | |
-- 6. change the downward movement to simulate gravity | |
displayMode(FULLSCREEN) | |
-- Use this function to perform your initial setup | |
function setup() | |
x=WIDTH/2 | |
y=HEIGHT-100 | |
shipsize=25 | |
--6. set a parameter to hold the speed of the ship. set to zero to start with | |
speed=0 | |
--6. set a parameter to hold the strength of gravity | |
gravity=-0.005 | |
end | |
function draw() | |
background(40, 40, 50) | |
if (CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING) then | |
sprite("Tyrian Remastered:Flame 2",x,y-shipsize,shipsize,shipsize) | |
end | |
sprite("Space Art:Red Ship",x,y,shipsize,shipsize) | |
--add gravity | |
--increase the speed of the ship by the value of gravity each time round the loop | |
speed=speed+gravity | |
-- 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<-15 then | |
speed=-15 | |
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 | |
end | |
--# Step7 | |
-- 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 ship should move under gravity and tapping the screen makes the thrust flames appear but has no effect | |
--7. Add thrust effect on the ship when the screen is touched. This should be factored into the equations of motion | |
--Hide the sidebar from the screen | |
displayMode(FULLSCREEN) | |
-- Use this function to perform your initial setup | |
function setup() | |
x=WIDTH/2 | |
y=HEIGHT-100 | |
shipsize=25 | |
speed=0 | |
gravity=-0.02 | |
--7. set a parameter to hold the level of thrust of the ship | |
thrust=0 | |
end | |
function draw() | |
background(40, 40, 50) | |
if (CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING) then | |
sprite("Tyrian Remastered:Flame 2",x,y-shipsize,shipsize,shipsize) | |
--increase the thrust | |
thrust = thrust + 0.005 | |
--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 | |
sprite("Space Art:Red Ship",x,y,shipsize,shipsize) | |
-- 7. add the effects of 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 | |
end | |
--# Step8 | |
-- 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 ship should move under gravity and tapping the screen makes the ship respond to thrust | |
--8. Draw a line to represent the ground | |
displayMode(FULLSCREEN) | |
function setup() | |
x=WIDTH/2 | |
y=HEIGHT-100 | |
shipsize=25 | |
speed=0 | |
gravity=-0.02 | |
thrust=0 | |
--add a variable to store the ground level | |
groundlevel=50 | |
end | |
function draw() | |
background(40, 40, 50) | |
if (CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING) then | |
sprite("Tyrian Remastered:Flame 2",x,y-shipsize,shipsize,shipsize) | |
thrust = thrust + 0.005 | |
if thrust>0.05 then | |
thrust=0.05 | |
end | |
else | |
thrust=0 | |
end | |
sprite("Space Art:Red Ship",x,y,shipsize,shipsize) | |
a=gravity+thrust | |
speed=speed+a | |
if speed<-3 then | |
speed=-3 | |
end | |
y=y+speed+0.5*a | |
--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. it runs for position x1,y1 to x2,y2. setting x1 to 0 and x2 to the width of the screen ensures it covers the full length. | |
line(0,groundlevel,WIDTH,groundlevel) | |
end | |
--# Step9 | |
-- 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 ship should move under gravity and tapping the screen makes the ship respond to thrust. A line to represent the ground is added though there is no interaction with it. | |
--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 | |
displayMode(FULLSCREEN) | |
function setup() | |
x=WIDTH/2 | |
y=HEIGHT-100 | |
shipsize=25 | |
speed=0 | |
gravity=-0.02 | |
thrust=0 | |
groundlevel=50 | |
end | |
function draw() | |
background(40, 40, 50) | |
if (CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING) then | |
sprite("Tyrian Remastered:Flame 2",x,y-shipsize,shipsize,shipsize) | |
thrust = thrust + 0.005 | |
if thrust>0.05 then | |
thrust=0.05 | |
end | |
else | |
thrust=0 | |
end | |
sprite("Space Art:Red Ship",x,y,shipsize,shipsize) | |
a=gravity+thrust | |
speed=speed+a | |
if speed<-3 then | |
speed=-3 | |
end | |
y=y+speed+0.5*a | |
strokeWidth(3) | |
stroke(48, 216, 25, 255) | |
line(0,groundlevel,WIDTH,groundlevel) | |
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) | |
--otherwise the ship is travelling too fast and has crashed | |
else | |
text("Crashed",WIDTH/2,HEIGHT/2) | |
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 | |
end | |
end | |
--# Step10 | |
-- 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 ship should move under gravity and tapping the screen makes the ship respond to thrust. A line to represent the ground is added and when the ship crosses the line a check is done to see if there is a successful landing | |
--10. Implement the "Crash" and "Win" screens | |
displayMode(FULLSCREEN) | |
function setup() | |
x=WIDTH/2 | |
y=HEIGHT-100 | |
shipsize=25 | |
--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 | |
groundlevel=50 | |
speed=0 | |
gravity=-0.02 | |
thrust=0 | |
end | |
function draw() | |
background(40, 40, 50) | |
--10. if the current state of the game is playing then do the following | |
if gamestate==PLAYING or gamestate==LANDED then | |
if (CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING) and gamestate==PLAYING then | |
sprite("Tyrian Remastered:Flame 2",x,y-shipsize,shipsize,shipsize) | |
thrust = thrust + 0.005 | |
if thrust>0.05 then | |
thrust=0.05 | |
end | |
else | |
thrust=0 | |
end | |
sprite("Space Art:Red Ship",x,y,shipsize,shipsize) | |
a=gravity+thrust | |
speed=speed+a | |
if speed<-3 then | |
speed=-3 | |
end | |
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 | |
sprite("Tyrian Remastered:Explosion Huge",x,y,shipsize+math.random(10),shipsize+math.random(10)) | |
text("Crash",WIDTH/2,HEIGHT/2) | |
end | |
strokeWidth(3) | |
stroke(48, 216, 25, 255) | |
line(0,groundlevel,WIDTH,groundlevel) | |
if y-shipsize/2<groundlevel then | |
if speed>-1 then | |
text("Landed",WIDTH/2,HEIGHT/2) | |
--10 change the gamestate to landed | |
gamestate=LANDED | |
else | |
--10.change the state of the game to crashed | |
gamestate=CRASHED | |
end | |
y=groundlevel+shipsize/2 | |
--set the speed and thrust of the ship to 0 | |
speed=0 | |
thrust=0 | |
end | |
end | |
--# Step11 | |
-- 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 ship should move under gravity and tapping the screen makes the ship respond to thrust. A line to represent the ground is added and when the ship crosses the line a check is done to see if there is a successful landing. A finite state machine controls the order of the game. | |
--11. Add a fuel parameter - can only thrust if there is fuel in the tank. Display current fuel on the screen | |
displayMode(FULLSCREEN) | |
function setup() | |
x=WIDTH/2 | |
y=HEIGHT-100 | |
shipsize=25 | |
PLAYING=1 | |
CRASHED=2 | |
LANDED=3 | |
gamestate=PLAYING | |
groundlevel=50 | |
speed=0 | |
gravity=-0.02 | |
thrust=0 | |
--11.set a parameter to hold the fuel level | |
fuel=500 | |
end | |
function draw() | |
background(40, 40, 50) | |
if gamestate==PLAYING or gamestate==LANDED then | |
if (CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING) and gamestate==PLAYING and fuel>0 then | |
sprite("Tyrian Remastered:Flame 2",x,y-shipsize,shipsize,shipsize) | |
thrust = thrust + 0.005 | |
fuel = fuel -1 | |
if thrust>0.05 then | |
thrust=0.05 | |
end | |
else | |
thrust=0 | |
end | |
sprite("Space Art:Red Ship",x,y,shipsize,shipsize) | |
a=gravity+thrust | |
speed=speed+a | |
if speed<-3 then | |
speed=-3 | |
end | |
y=y+speed+0.5*a | |
elseif gamestate==CRASHED then | |
sprite("Tyrian Remastered:Explosion Huge",x,y,shipsize+math.random(10),shipsize+math.random(10)) | |
text("Crash",WIDTH/2,HEIGHT/2) | |
end | |
strokeWidth(3) | |
stroke(48, 216, 25, 255) | |
line(0,groundlevel,WIDTH,groundlevel) | |
if y-shipsize/2<groundlevel then | |
if speed>-1 then | |
text("Landed",WIDTH/2,HEIGHT/2) | |
gamestate=LANDED | |
else | |
gamestate=CRASHED | |
end | |
y=groundlevel+shipsize/2 | |
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 | |
--# Step12 | |
-- 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 ship should move under gravity and tapping the screen makes the ship respond to thrust. A line to represent the ground is added and when the ship crosses the line a check is done to see if there is a successful landing. A finite state machine controls the order of the game. The ship has limited fuel. | |
--12. Add sound effects | |
displayMode(FULLSCREEN) | |
function setup() | |
x=WIDTH/2 | |
y=HEIGHT-100 | |
shipsize=25 | |
PLAYING=1 | |
CRASHED=2 | |
LANDED=3 | |
gamestate=PLAYING | |
groundlevel=50 | |
speed=0 | |
gravity=-0.02 | |
thrust=0 | |
fuel=500 | |
end | |
function draw() | |
background(40, 40, 50) | |
if gamestate==PLAYING or gamestate==LANDED then | |
if (CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING) and gamestate==PLAYING and fuel>0 then | |
sprite("Tyrian Remastered:Flame 2",x,y-shipsize,shipsize,shipsize) | |
thrust = thrust + 0.005 | |
fuel = fuel -1 | |
--12. play a thrust sound | |
sound(SOUND_HIT, 16609) | |
if thrust>0.05 then | |
thrust=0.05 | |
end | |
else | |
thrust=0 | |
end | |
sprite("Space Art:Red Ship",x,y,shipsize,shipsize) | |
a=gravity+thrust | |
speed=speed+a | |
if speed<-3 then | |
speed=-3 | |
end | |
y=y+speed+0.5*a | |
elseif gamestate==CRASHED then | |
sprite("Tyrian Remastered:Explosion Huge",x,y,shipsize+math.random(10),shipsize+math.random(10)) | |
text("Crash",WIDTH/2,HEIGHT/2) | |
end | |
strokeWidth(3) | |
stroke(48, 216, 25, 255) | |
line(0,groundlevel,WIDTH,groundlevel) | |
if y-shipsize/2<groundlevel then | |
if speed>-1 then | |
text("Landed",WIDTH/2,HEIGHT/2) | |
gamestate=LANDED | |
else | |
--12. play an explosion sound | |
sound(SOUND_EXPLODE, 23062) | |
gamestate=CRASHED | |
end | |
y=groundlevel+shipsize/2 | |
speed=0 | |
thrust=0 | |
end | |
font("Courier-Bold") | |
fontSize(30) | |
text("Fuel:"..fuel,150,HEIGHT-50) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment