This is a spinoff of West’s side scroller code. We need help with code for collision between the hero and the plate objects. Any help would be appreciated. This isn’t going on the App Store, just for personal use. We have split the code in two.
-- 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
x=math.random(401,500)
y=math.random(301,400)
z=math.random(201,300)
r=math.random(100,200)
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("Toasty",WIDTH/2,HEIGHT/2+150)
fontSize(25)
--add your name
text("by: Peter Valesares",WIDTH/2,HEIGHT/2+75)
text("& Jack Dew",WIDTH/2,HEIGHT/2+50)
text("Music by: John Hutton", WIDTH/2, HEIGHT/2)
text("Art by: Katie Hood", 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={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
--an array of height values for the ceiling
--make sure that the two arrays are the same length
roof={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
plate={0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}
plate2={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
plate3={0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0}
plate4={0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0}
t=0.1 -- a time variable used to calculate the position and velocity between frames
acc=0 --an acceleration variable calculated each frame
grav=-7--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("Documents:Butter",xpos+100*i-100,-40+70*h,100,100)
end
--repeat the process for the ceiling
for h=1,roof[i] do
sprite("Documents:Butter",xpos+100*i-100,HEIGHT+90-70*h,100,100)
end
end
for i = 1,20 do
for h=1,plate[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("Documents:Plate",xpos+100*i, x, 100,100)
end
for h=1,plate2[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("Documents:Plate",xpos+100*i, y,100,100)
end
end
for i = 1,20 do
for h=1,plate3[i] do
sprite("Documents:Plate",xpos+100*i, z,100,100)
end
for h=1,plate4[i] do
sprite("Documents:Plate",xpos+100*i, r,100,100)
end
end
--Display the hero sprite
sprite("Documents:Fancy Toast",herox,heroy,110,160)
--[[
--Gravity based version, touch anywhere to provide upthrust to counter gravity
if CurrentTouch.state == MOVING or CurrentTouch.state== BEGAN then
upthrust = upthrust + speed/5
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
--]]
if CurrentTouch.state==MOVING or CurrentTouch.state==BEGAN then
upthrust = upthrust + 13--cumulatively add some upward force - alter the 10 to get different levels of force
--limit the force
if upthrust>16 then
upthrust=16
end
end
--decrease the upward force when the finger is removed, but not immediately
if CurrentTouch.state==ENDED then
upthrust=upthrust-2
if upthrust<0 then
upthrust=0
end
accx=0
end
acc=upthrust+grav -- change the vertical displacemnt according to the amount of force
--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<(30*(ground[2]+1)) then
gameState=2
end
if heroy>(HEIGHT+70-70*(roof[2]+1)) then
gameState=2
end
if plate2[i] == xpos and plate2[i] ==heroy then
gamestate=2
end
if plate4[i] == xpos and plate1[i] ==heroy then
gamestate=2
end