# Worked on my game for class a bit more

Sorry about the sprites that you won’t be able to see. The only ones you really need to see are the movement arrows - up and down at the bottom left corner of the screen, left and right at the bottom right.

The game is a red rectangle that collects coins that spawn randomly at the right side of the screen.

At the moment I’m trying to make the movement buttons work better. At the moment if you hold your finger on the same spot the collector rectangle stays put, but if you keep moving your finger on the same direction button the collector will keep moving. Any tips?

``````function setup()
displayMode(FULLSCREEN_NO_BUTTONS)

-- setting up the score and gameLost to be used later
score = 0
highScore = readLocalData("highscore", 0)
gameLost = 0               -- used to determine if the player has lost or not

-- setting up both coin and collector speed
rectSpeed = 15
coinSpeed = 2

-- setting up coordinates for movement arrows
upDownX = 150
leftRightY = 124
arrowWH = 75

-- setting up y coordinates for coins - they spawn above the movement arrows and a bit below the top of the screen no matter what
coin1Y = math.random(250, HEIGHT-75)
coin2Y = math.random(250, HEIGHT-75)
coin3Y = math.random(250, HEIGHT-75)
coin4Y = math.random(250, HEIGHT-75)
coin5Y = math.random(250, HEIGHT-75)

-- setting up x coordinates for coins
coin1X = WIDTH + 1
coin2X = coin1X + 150
coin3X = coin2X + 150
coin4X = coin3X + 150
coin5X = coin4X + 150

-- setting up the coin sizes
coinDim = 50

-- setting up rectangle positions
rectX = 100
rectY = HEIGHT/2 - 5
end

function draw()
-- setting the background color
background(255, 255, 255, 255)

-- creating the collector rectangle
pushStyle()
fill(255, 0, 0, 255)
rect(rectX, rectY, 100, 100)
popStyle()

-- creating the barrier
pushStyle()
fill(89, 89, 89, 255)
rect (WIDTH-400, 0, 100, HEIGHT+1)
popStyle()

pushStyle()
fill(255, 250, 0, 255)                          -- setting the coin's fill color

-- drawing the coins
rect(coin1X, coin1Y, coinDim, coinDim)
rect(coin2X, coin2Y, coinDim, coinDim)
rect(coin3X, coin3Y, coinDim, coinDim)
rect(coin4X, coin4Y, coinDim, coinDim)
rect(coin5X, coin5Y, coinDim, coinDim)

-- making the coins move left across the screen
coin1X = coin1X - coinSpeed
coin2X = coin2X - coinSpeed
coin3X = coin3X - coinSpeed
coin4X = coin4X - coinSpeed
coin5X = coin5X - coinSpeed

if coin1X <= 0 or coin2X <= 0 or coin3X <= 0 or coin4X <= 0 or coin5X <= 0 then
lost()
end

-- coin/collector collision detection
if coin1X <= rectX + 100 and coin1Y + 50 >= rectY and coin1Y + 50 <= rectY + 150 then
score = score + 1
coin1X = WIDTH
coin1Y = math.random(250, HEIGHT-75)

coinSpeed = coinSpeed + 0.05
end

if coin2X <= rectX + 100 and coin2Y + 50 >= rectY and coin2Y + 50 <= rectY + 150 then
score = score + 1
coin2X = WIDTH
coin2Y = math.random(250, HEIGHT-75)

coinSpeed = coinSpeed + 0.05
end

if coin3X <= rectX + 100 and coin3Y + 50 >= rectY and coin3Y + 50 <= rectY + 150 then
score = score + 1
coin3X = WIDTH
coin3Y = math.random(250, HEIGHT-75)

coinSpeed = coinSpeed + 0.05
end

if coin4X <= rectX + 100 and coin4Y + 50 >= rectY and coin4Y + 50 <= rectY + 150 then
score = score + 1
coin4X = WIDTH
coin4Y = math.random(250, HEIGHT-75)

coinSpeed = coinSpeed + 0.05
end

if coin5X <= rectX + 100 and coin5Y + 50 >= rectY and coin5Y + 50 <= rectY + 150 then
score = score + 1
coin5X = WIDTH
coin5Y = math.random(250, HEIGHT-75)

coinSpeed = coinSpeed + 0.05
end
popStyle()

-- creating the arrow sprites
sprite("Dropbox:Movement-arrows", WIDTH/2, HEIGHT/2)

-- drawing the score
pushStyle()
fill(0, 0, 0, 255)
font("SourceSansPro-Bold")
fontSize(77)

text(score, WIDTH-50, 725)
popStyle()

-- displaying the game over screen if the game has been lost
pushStyle()
fill(0, 0, 0, 255)
font("SourceSansPro-Bold")
fontSize(77)

if gameLost == 1 then
sprite("Dropbox:Death screen", WIDTH/2, HEIGHT/2)
text("High score: ", WIDTH/2 - 100, HEIGHT/2)
text(highScore,(WIDTH/2 + 125), HEIGHT/2)
end
popStyle()

if gameLost == 1 then
pushStyle()
fill(255, 255, 255, 255)
rect(890, 700, 150, 100)
popStyle()
end
end

function lost()
gameLost = 1
coinSpeed = 0

if score > highScore then
saveLocalData("highscore", score)
highScore = readLocalData("highscore", 0)
end
end

function touched(touch)
if gameLost == 1 and touch.state == BEGAN then
restart()
end

-- touch detection for up and down movement buttons
if touch.x >= 0 and touch.x <= 187 then
if rectY > 0 then                           -- this creates a barrier so that the collector cannot go past the bottom of the screen
if touch.y >= 0 and touch.y <= 124 then            -- the down arrow
rectY = rectY - rectSpeed
end
end

if rectY + 100 < HEIGHT then                -- this creates a barrier so that the collector cannot go past the top of the screen
if touch.y >= 126 and touch.y <= 250 then          -- the up arrow
rectY = rectY + rectSpeed
end
end
end

-- touch detection for left and right movement buttons
if touch.y >= 0 and touch.y <= 250 then
if rectX > 0 then                           -- this creates a barrier so that the collector cannot go past the left of the screen
if touch.x >= 774 and touch.x <= 898 then          -- the left arrow
rectX = rectX - rectSpeed
end
end

if rectX + 100 < WIDTH-400 then             -- this creates a barrier so that the collector cannot go past the barrier on the right side of the screen
if touch.x >= 900 and touch.x <= 1024 then         -- the right arrow
rectX = rectX + rectSpeed
end
end
end
end

``````

@whatsavet The touched() function processes a screen touch. If you touch the screen then the touched() function processes 1 touch and defines it as BEGAN. If you move your finger on the screen, then the touched() function processes that movement as multiple touches and defines it as MOVING. If you lift your finger from the screen, then the touched() function processes that as 1 touch and defines it as ENDED. That’s why when you touch a button, it only processes 1 touch and nothing else. If you move your finger on that button, then it processes it as multiple touches. What you need to do is set a flag when the touch starts (BEGAN) and then reset that flag when you lift your finger (ENDED). As long as the flag is set, then add or subtract the rectspeed.

Nice game. But just a note - please try and keep all updates on your game in one discussion, rather than creating a new one for each version/big progress you make.