# scrolling game starter

I was going thru programs on my iPad to see what to delete. I came across a routine that I posted a long time ago and thought I would add a few more lines of code to create this. If someone wants to take this and make it into something, that’s fine. It’s a large scrolling area where I placed 50 keys randomly on it. Use your finger to move the character around the grid to pickup the keys. You have to be directly over each key to collect it. At the top of the grid is a count showing the number of keys still remaining on the grid. When you reach the edge of the screen, the grid will scroll either up, down, right, or left. If you wander off the grid and can’t find your way back, a double tap will move the character back to the center.

``````
displayMode(FULLSCREEN)

function setup()
w2=WIDTH/2
h2=HEIGHT/2
gdx=w2
gdy=h2
dx=0
dy=0
size=3000
move=false

r={}    -- table of random key locations
for z=1,50 do
r[z]=vec2(math.random(-size,size),math.random(-size,size))
end
end

function removeKey()    -- remove key when character is over it
for z=1,#r do
if math.abs(dx-r[z].x)<10 and math.abs(dy-r[z].y)<10 then
table.remove(r,z)
return
end
end
end

function draw()
background(40, 40, 50)

if move then    -- move the character
dx=dx-(sx-mx)/2
dy=dy-(sy-my)/2
removeKey()
end

-- scroll the grid and character left or right
if dx>WIDTH-50-gdx then
gdx=WIDTH-50-dx
elseif dx<gdx*-1+50 then
gdx=50-dx
end

-- scroll the grid and character up or down
if dy>HEIGHT-50-gdy then
gdy=HEIGHT-50-dy
elseif dy<gdy*-1+50 then
gdy=50-dy
end

pushMatrix()
translate(gdx,gdy)    -- move grid and character

sprite("SpaceCute:Background",0,0,size*2,size*2)    -- draw background

stroke(255)    -- draw grid lines and grid values
strokeWidth(4)
fontSize(30)
fill(0, 255, 15, 255)
for z=-size,size,100 do
line(z,-size,z,size)
line(-size,z,size,z)
text(z,z,0)
text(z,0,z)
end

for z=1,#r do    -- draw random keys
sprite("Planet Cute:Key",r[z].x,r[z].y)
end

sprite("Planet Cute:Character Boy",dx,dy)    -- draw character
popMatrix()

fill(250, 13, 13, 255)    -- show number of keys still on the screen
text(#r,w2,1000)

end

function touched(t)
if t.tapCount==2 then    -- double tap to move character back to center position
dx=0
dy=0
gdx=w2
gdy=h2
end
if t.state==BEGAN then    -- finger starting x,y position
sx=t.x
sy=t.y
elseif t.state==MOVING then    -- finger direction to move character
move=true
mx=t.x
my=t.y
elseif t.state==ENDED then    -- finger raised, stop moving character
move=false
end
end

``````

Interesting.