Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Last active December 22, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermotbalson/6395908 to your computer and use it in GitHub Desktop.
Save dermotbalson/6395908 to your computer and use it in GitHub Desktop.
Lander3
--# Main
-- Noob Lander
-- by West, extended by ignatz
-- 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. 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
--13. Add a target on the ground to aim at, allow ship to move sideways, add start and restart options
--14. Add meteorites that must be avoided (even on the ground)
--WHAT YOU NEED TO DO
-- Most important - DO NOT RUSH
--Try to understand everything you can, it will help toward your own projects.
--read through each tab and see how it solves part of the overall problem
--think about other ways of doing it
--understand how gravity is included, how the thrusters work
--figure out how the meteorites are created
--how does Codea know if the ship hits meteorites or the ground?
--understand how the game manages the start, the playing, and the end phases
--figure out an overall score taking into account proximity to target and remaining fuel
--maybe change some of the meteorites so they add to the score if hit
--what else could be improved without making the programming too complex?
--It might be fun to share the best ideas, and maybe program some of them.
--ignore the lines below,they help manage the code tabs
tabs={} f=""
for i=1,14 do f=f.."c"..i.."={} table.insert(tabs,c"..i..") " end
ff=loadstring(f) ff() ff=nil f=nil
--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
--# Step1
--Step1
--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
--Step2
-- 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=40
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
--Step3
-- 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=40
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
-- Step4
-- 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=40
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
-- Step5
-- 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=40
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 CurrentTouch to test if the screen is being touched.
--The .state part gives the state of the touch. CurrentTouch also has other properties such as .x and .y
--which will return the position of the touch, but these aren't required here
--BEGAN means the touch has just started, MOVING means it is continuing, and ENDED means (guess!)
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
-- Step6
-- 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=40
--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
-- Step7
-- 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=40
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
-- Step8
-- Draw a line to represent the ground
function c8.setup()
x=WIDTH/2
y=HEIGHT-100
shipsize=40
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
-- Step9
-- 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=40
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
-- Step10
-- Implement the "Crash" and "Win" screens
function c10.setup()
x=WIDTH/2
y=HEIGHT-100
shipsize=40
--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 capitals (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
-- Step11
-- 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=40
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
-- Step12
-- Add sound effects
function c12.setup()
x=WIDTH/2
y=HEIGHT-100
shipsize=40
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
-- Step13
--13. Add ability to move sideways
function c13.setup()
x=WIDTH/2
y=HEIGHT-100
shipsize=40
READY=0 --new, used at the very beginning before the game starts
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")
--NEW include new image for side thrusters
imgSideThrust=readImage("Tyrian Remastered:Bullet Fire B")
--NEW select a random target area
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 --this bit is NEW to ask the user the user to start the game when ready
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 --start when user touches screen
elseif gamestate==PLAYING then
if (CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING) and gamestate==PLAYING and fuel>0 then
--NEW test if player touched left or right side, if so, fire side thrusters
if CurrentTouch.x<WIDTH/4 then --left thruster
sprite(imgSideThrust,x-shipsize/2,y-shipsize*.2,shipsize/3) --draw flame
fuel = fuel -.5 --reduce fuel
x=x+1 --adjust x
elseif CurrentTouch.x>WIDTH*3/4 then --right thruster
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))
--NEW allow player to restart
textMode(CENTER)
text("Crash - touch to restart",WIDTH/2,HEIGHT/2)
--when player touches screen, rerun setup function to set everything up again
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)
--NEW draw target area in red
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) --NEW
end
end
--# Step14
-- Step14
--14. Add space rocks that have to be avoided
function c14.setup()
x=WIDTH/2
y=HEIGHT-100
shipsize=40
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")
imgRock=readImage("Tyrian Remastered:Rock 3")
targetX=math.random(100,WIDTH-100)
--NEW set up rocks
rocks={} --create a table to hold the rocks that will fall ****
rockTimer=1 --time between rocks, in seconds ****
timer=0 --timer that will tell us if it's time for a new rock ****
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 c14.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
timer=timer+DeltaTime --add time since last redraw, to timer ****NEW
if timer>rockTimer then --time for a rock! ****NEW
AddRock() --use a function for this to avoid clutter ****NEW
timer=timer-rockTimer --reset timer ****NEW
end -- ****NEW
--draw rocks
for i,r in pairs(rocks) do -- ****NEW
--first check for collisions
if vec2(r.x,r.y):dist(vec2(x,y))<shipsize/2+r.size/2 then -- ****NEW
sound(SOUND_EXPLODE, 23062) --we hit a rock
gamestate=CRASHED
end
--adjust rock position if not already on the ground
if r.y>groundlevel then
r.x=r.x+r.dx
r.y=r.y+r.dy
end
--draw rock
sprite(imgRock,r.x,r.y,r.size)
end
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
c14.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
c14.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
function AddRock() -- all new ****
--select random point at top of screen, and random point on ground, and move from one to the other
--we'll create all the settings for a rock, then add it to our table of rocks
r={} --all the settings for the new rock will be in this table.
--get random x, y will be at top of screen
r.x=math.random(0,WIDTH)
r.y=HEIGHT+20 --just push it off the top of the screen a little
r.gx=r.x+400*(math.random()-.5) --impact point on ground will be within 200 pixels either side of start x
r.speed=math.random(50,100) --speed in pixels per second
--figure out what fraction of the distance between start and finish will be covered in each redraw
--we know we are drawing 60 times a second
--so first get the distance we are going to travel
--use Pythagoras, the change in x and change in y are two sides of the triangle, and the path taken
--by the rock is the hypotenuse. So if the rock starts at x1,y and travels to x2,y2, then the
--distance travelled is ((x2-x1)^2 + (y2-y1)^2)^0.5 (square root of sum of squares)
r.dist=((r.x-r.gx)^2 + (r.y-groundlevel)^2)^0.5
--this is a more elegant method using vectors (fancy name for a little table with x and y values)
--Codea has a "dist" function that gives you the distance between two points, like this
--we have to turn them into "vec2" first
r.dist=vec2(r.x,r.y):dist(vec2(r.gx,groundlevel)) --distance
r.redraws=(r.dist/r.speed)*60 --number of redraws taken to get to ground
r.dx = (r.gx-r.x)/r.redraws --movement in x on each draw
r.dy = (groundlevel-r.y)/r.redraws --movement in y on each draw
r.size=math.random(5,25) --random size
table.insert(rocks,r) --add to rocks table (yes, you can add a table to another table)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment