@Keebo
Your post got me thinking so I put together a simple side scroller along the lines of the “helicopter game”. It’s super basic and not as smooth as I’d like so the next stage is to try out some meshes but meanwhile here is the code.
-- Use this function to perform your initial setup
function setup()
displayMode(FULLSCREEN)
--gameState is the current state of play
-- 0 is the splash screen
-- 1 is normal gameplay
-- 2 is gameover
gameState=0
end
-- This function gets called once every frame
function draw()
--One if statement checks what game state we are currently in and executes the relevant code
if gameState==0 then
--Draw the splash screen
background(0,0,0,255)
font("AmericanTypewriter-Bold")
fontSize(40)
--Set up a title
text("Sideways Scroller Demo",WIDTH/2,HEIGHT/2)
fontSize(25)
--add your name
text("by West",WIDTH/2,HEIGHT/2-50)
--Draw a start button
fill(127, 220, 23, 255)
ellipse(WIDTH/2,HEIGHT/2-200,100)
stroke(220, 217, 172, 255)
fill(223, 230, 222, 255)
fontSize(50)
text("Go",WIDTH/2,HEIGHT/2-200)
--Calculate the distance of the edge away from the centre of the button
dist=math.sqrt(((CurrentTouch.x-WIDTH/2)^2)+((CurrentTouch.y-(HEIGHT/2)+200))^2)
--check to see if the user has touched the button
if CurrentTouch.state==ENDED and dist<50 then
--put into the play game state
gameState=1
--Initialise some variables
xpos=0 --used for the movement of the background
speed=5 --initial speed
speedCounter=0 --a counter to increment the speed
speedMax=200 --the value at which the speed will increment
topSpeed=50 --the top speed
upthrust=0 --the upward force applied to the hero character
herovel=0 --the vertical velocity of the hero character
herox=100 --the horizontal position of the hero
heroy=HEIGHT/2 -- the vertical position of the hero
--an array of height values for the ground
--add more values into the array to create a longer level
ground={3,3,3,3,3,4,3,2,1,1,1,1,2,2,3,4,4,3,3,4,4,4,4,5,4,3}
--an array of height values for the ceiling
--make sure that the two arrays are the same length
roof={3,3,2,3,4,4,5,6,7,6,6,5,4,4,4,3,2,3,4,5,6,5,5,4,3,2}
t=0.1 -- a time variable used to calculate the position and velocity between frames
acc=0 --an acceleration variable calculated each frame
grav=-10 --a constant gravitational force
score=0 --the current score
restartwait=0 --a timer for restarting the game at the end
end
elseif gameState==1 then
--Stretch an sprite to fill the background
--As this is the furthest away thing draw first and everything else will be drawn on top
sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
--the screen in landscape will allow 12 sprites with a width of 100 to be drawn
--try reducing the second number to 10 to see the effect (make sure you hold in landcsape)
for i = 1,12 do
--the value in the current position (i) of the ground array is the number of blocks high to
--draw at the current position
for h=1,ground[i] do
--draw the sprite at the current x position i * width of one sprite with the xpos
--offset which creates the movement
--the y position is calculated as a multiplier of the the height
sprite("Planet Cute:Ramp South",xpos+100*i-100,-40+70*h,100,100)
end
--repeat the process for the ceiling
for h=1,roof[i] do
sprite("Planet Cute:Ramp South",xpos+100*i-100,HEIGHT+50-70*h,100,100)
end
end
--Display the hero sprite
sprite("Planet Cute:Enemy Bug",herox,heroy,110,160)
--Simple version with no gravity, touch above the hero sprite to move up and below to move down
--handle a touch input from the user
if CurrentTouch.state == MOVING or CurrentTouch.state== BEGAN then
if CurrentTouch.y > heroy then
heroy = heroy + 3
elseif CurrentTouch.y<heroy then
heroy = heroy - 3
end
end
--Gravity based version, touch anywhere to provide upthrust to counter gravity
-- if CurrentTouch.state == MOVING or CurrentTouch.state== BEGAN then
-- upthrust = upthrust + speed/2
-- if upthrust>20 then
-- upthrust=20
-- end
-- end
-- if CurrentTouch.state==ENDED then
-- upthrust=0
-- end
-- acc=upthrust+grav
--equation of motion to calculate the displacement in the y direction s=ut+0.5at*t
-- heroy=heroy+herovel*t+0.5*acc*t*t
--equation of motion for the new velocity v=u+at
-- herovel=herovel+acc*t
--stop the sprite going off the top of the screen
--redundant here, but if you wanted to have a narrower field of play then this what you would use
--try replacing both HEIGHT with HEIGHT/1.5 to try it out
if heroy>HEIGHT then
heroy=HEIGHT
end
if heroy<0 then
heroy=0
end
--dealing with collisions
--normally we would have to check to see if the hero has hit anything on the screen
--however as we are restricting the movement to up and down only then we only
--need to check the sprites in the same vertical location
if heroy<(-40+70*(ground[2]+1)) then
gameState=2
end
if heroy>(HEIGHT+70-70*(roof[2]+1)) then
gameState=2
end
--xpos is a variable used to control the position of the sprites on the screen
--the variable speed determines how much each sprite is moved each time the screen is redrawn
xpos=xpos-speed
--score is based on distance travelled
score = score + speed
fill(255, 255, 255, 255)
text("Score: "..score,WIDTH/2,HEIGHT-30)
--need to reset xpos at an appropriate point to get wraparound
if xpos<-100 then
xpos=0
--store the value of the current first element of the array
--for example, for the array {a,b,c,d} store the first element a in the variable temp
temp=ground[1]
tempr=roof[1]
--shift the ground array along by one, overwriting the first element by the second element,
--the second element by the third element, etc, etc
--Note the last element is not overwritten yet
-- {a,b,c,d} becomes {b,c,d,d}
for c=1,#ground-1 do
ground[c]=ground[c+1]
roof[c]=roof[c+1]
end
--Move the previously saved first element (saved in the temp variable)to the last position
--{b,c,d,d} becomes {b,c,d,a}
ground[#ground]=temp
roof[#roof]=tempr
end
--get faster over time
speedCounter = speedCounter + 1
if speedCounter>speedMax then
speed=speed+1
speedCounter=0
--set an upper limit on the speed
if speed>topSpeed then
speed=topSpeed
end
end
--game over state
elseif gameState==2 then
background(96, 77, 77, 255)
text("Game over",WIDTH/2,HEIGHT/2)
text("Score: "..score,WIDTH/2,HEIGHT/2-50)
--add a delay to stop overspill from the gameplay restarting the game instantly
if restartwait>100 then
text("Tap screen to restart",WIDTH/2,HEIGHT/2-100)
end
if CurrentTouch.state==BEGAN and restartwait>100 then
gameState=0
end
restartwait = restartwait + 1
end
end