Created
August 24, 2013 12:54
-
-
Save dermotbalson/6327961 to your computer and use it in GitHub Desktop.
Class2
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
--# Notes | |
-- 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) | |
--ignore the line below, it just helps manage the code tabs | |
tabs={} f="" | |
for i=1,13 do f=f.."c"..i.."={} table.insert(tabs,c"..i..") " end | |
ff=loadstring(f) ff() ff=nil f=nil | |
--# 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) | |
-- Use this function to perform your initial setup | |
function c1.setup() | |
print("In which we choose a sprite") | |
end | |
-- This function gets called once every frame | |
function c1.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 | |
function c2.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 | |
print("We position the sprite") | |
end | |
function c2.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 | |
function c3.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 | |
print("We set a starting position high up on the screen") | |
end | |
function c3.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). | |
function c4.setup() | |
x=WIDTH/2 | |
y=HEIGHT-100 | |
shipsize=25 | |
print("We start the ship moving downwards") | |
end | |
function c4.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 c5.setup() | |
x=WIDTH/2 | |
y=HEIGHT-100 | |
shipsize=25 | |
print("We show a rocket flame if you tap the screen") | |
end | |
function c5.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 | |
-- Use this function to perform your initial setup | |
function c6.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 | |
print("We simulate gravity") | |
end | |
function c6.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 | |
-- Use this function to perform your initial setup | |
function c7.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 | |
print("We provide thrust when the screen is touched") | |
end | |
function c7.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 | |
function c8.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 | |
print("Add ground") | |
end | |
function c8.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 | |
function c9.setup() | |
x=WIDTH/2 | |
y=HEIGHT-100 | |
shipsize=25 | |
speed=0 | |
gravity=-0.02 | |
thrust=0 | |
groundlevel=50 | |
print("Test for a crash") | |
end | |
function c9.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 | |
function c10.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 | |
print("Manage the different stages of the game") | |
end | |
function c10.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 | |
function c11.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 | |
print("Add limited fuel") | |
end | |
function c11.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) | |
textMode(CORNER) --left align the text | |
--print in the top left corner of the screen. Use .. to join bits of string tp parameters | |
text("Fuel:"..fuel,50,HEIGHT-50) | |
text("Height:"..math.floor(y-groundlevel),50,HEIGHT-80) | |
text("Speed:"..math.floor(-speed),50,HEIGHT-110) | |
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 | |
function c12.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 | |
yyy=readImage("Documents:alien") | |
print("Add sound effects") | |
end | |
function c12.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) | |
textMode(CORNER) --left align the text | |
--print in the top left corner of the screen. Use .. to join bits of string tp parameters | |
text("Fuel:"..fuel,50,HEIGHT-50) | |
text("Height:"..math.floor(y-groundlevel),50,HEIGHT-80) | |
text("Speed:"..math.floor(-speed),50,HEIGHT-110) | |
end | |
--# Step13 | |
-- 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. | |
--13. Add ability to move sideways | |
function c13.setup() | |
x=WIDTH/2 | |
y=HEIGHT-100 | |
shipsize=25 | |
READY=0 | |
PLAYING=1 | |
CRASHED=2 | |
LANDED=3 | |
gamestate=READY | |
groundlevel=50 | |
speed=0 | |
gravity=-0.02 | |
thrust=0 | |
fuel=500 | |
imgShip=readImage("Space Art:Red Ship") | |
imgThrust=readImage("Tyrian Remastered:Flame 2") | |
imgSideThrust=readImage("Tyrian Remastered:Bullet Fire B") | |
targetX=math.random(100,WIDTH-100) | |
output.clear() | |
print("Add start screen, ability to replay") | |
print("Add sideways movement and a target site") | |
print("Touch left and right 1/4 of screen to move right/left") | |
end | |
function c13.draw() | |
background(40, 40, 50) | |
if gamestate==READY then | |
font("Courier-Bold") | |
fontSize(30) | |
textMode(CENTER) | |
text("Touch the screen to start",WIDTH/2,HEIGHT/2) | |
if CurrentTouch.state==BEGAN then gamestate=PLAYING end | |
elseif gamestate==PLAYING then | |
if (CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING) and gamestate==PLAYING and fuel>0 then | |
if CurrentTouch.x<WIDTH/4 then | |
sprite(imgSideThrust,x-shipsize/2,y-shipsize*.2,shipsize/3) | |
fuel = fuel -.5 | |
x=x+1 | |
elseif CurrentTouch.x>WIDTH*3/4 then | |
sprite(imgSideThrust,x+shipsize/2,y-shipsize*.2,shipsize/3) | |
fuel = fuel - .5 | |
x=x-1 | |
else | |
sprite(imgThrust,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 | |
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==LANDED then | |
sprite(imgShip,x,y,shipsize,shipsize) | |
textMode(CENTER) | |
text("Landed - touch to restart",WIDTH/2,HEIGHT/2) | |
if CurrentTouch.state==BEGAN then | |
c13.setup() | |
gamestate=PLAYING | |
end | |
elseif gamestate==CRASHED then | |
sprite("Tyrian Remastered:Explosion Huge",x,y,shipsize+math.random(10),shipsize+math.random(10)) | |
textMode(CENTER) | |
text("Crash - touch to restart",WIDTH/2,HEIGHT/2) | |
if CurrentTouch.state==BEGAN then | |
c13.setup() | |
gamestate=PLAYING | |
end | |
end | |
if gamestate~=READY then | |
strokeWidth(3) | |
stroke(48, 216, 25, 255) | |
line(0,groundlevel,WIDTH,groundlevel) | |
stroke(255,0,0,255) --red | |
line(targetX-50,groundlevel,targetX+50,groundlevel) | |
if y-shipsize/2<groundlevel then | |
if speed>-1 then | |
textMode(CENTER) | |
text("Landed - touch to replay",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) | |
textMode(CORNER) --left align the text | |
--print in the top left corner of the screen. Use .. to join bits of string tp parameters | |
text("Fuel:"..fuel,50,HEIGHT-50) | |
text("Height:"..math.floor(y-groundlevel-12),50,HEIGHT-80) | |
text("Speed:"..math.floor(-speed),50,HEIGHT-110) | |
text("Target:"..math.floor(x-targetX),50,HEIGHT-140) | |
end | |
end | |
--# Main | |
--This code manages which Code tab is run | |
--it remembers your last choice, and if you select a different one, it runs that instead | |
function setup() | |
LastCode=readProjectData("Code") or 1 | |
parameter.integer("Choose_a_tab",1,#tabs,LastCode,RunCode) | |
end | |
function RunCode() | |
output.clear() | |
saveProjectData("Code",Choose_a_tab) | |
print("CODE "..Choose_a_tab) | |
c=tabs[Choose_a_tab] | |
c.setup() | |
end | |
function draw() | |
c.draw() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment