Swipeball

Main:

-- Swipeball
-- Author, Stephen Monroe
-- Version, 1.0.0

function setup()

    currentScreen = "start"

    gameDifficulty = 0
    gameScore = 0
    
    playerX = 0
    playerY = 0
    playerSize = 50
    playerSpeed = 3
    
    ballX = {}
    ballY = {}
    ballType = {}
    ballSizeDefault = 50
    ballSpeedDefault = 5
    ballCount = 0
    
    delay = 0
    
    displayMode(FULLSCREEN)

end

function draw()

    background(0)

    if currentScreen == "start" then GameMenu() end
    if currentScreen == "end" then GameOver() end
    if currentScreen == "game" then GameLoop() end
    
end

function GameLoop()
    
    BallEntity:draw()
    BallEntity:move()
    
    PlayerEntity:draw()
    PlayerEntity:move()
    
    if HitDetect() == true then
        delay = ElapsedTime
        currentScreen = "end"
    end
    
end

GameMenu:

GameMenu = class()

function GameMenu()
    
    font("Futura-CondensedMedium")
    fontSize(100)
    fill(255, 0, 0)
    textWrapWidth(0)
    text("Swipeball", WIDTH / 2, HEIGHT - 100)
    
    fontSize(60)
    text("Easy", WIDTH / 2, (HEIGHT / 2) + 100)
    text("Medium", WIDTH / 2, HEIGHT / 2)
    text("Hard", WIDTH / 2, (HEIGHT / 2) - 100)
    text("Insane", WIDTH / 2, (HEIGHT / 2) - 200)
    
    fontSize(40)
    text("?", WIDTH - 50, 100)
    text("X", WIDTH - 50, 40)
    
    x = CurrentTouch.x
    y = CurrentTouch.y
    
    if CurrentTouch.state == BEGAN or CurrentTouch.state == MOVING then
        if x > (WIDTH / 2) - 100 and x < (WIDTH / 2) + 100 then
            -- easy
            if y > (HEIGHT / 2) + 50 and y < (HEIGHT / 2) + 150 then
                gameDifficulty = 1
                GameNew()
            end
            -- medium
            if y > (HEIGHT / 2) - 50 and y < (HEIGHT / 2) + 50 then
                gameDifficulty = 2
                GameNew()
            end
            -- hard
            if y > (HEIGHT / 2) - 150 and y < (HEIGHT / 2) - 50 then
                gameDifficulty = 3
                GameNew()
            end
            -- insane
            if y > (HEIGHT / 2) - 250 and y < (HEIGHT / 2) - 150 then
                gameDifficulty = 4
                GameNew()
            end
        end
        if x > WIDTH - 80 and x < WIDTH - 20 then
            -- help
            if y > 70 and y < 130 then
                HelpMenu()
            end
            -- exit
            if y > 10 and y < 70 then
                close()
            end
        end
    end

end

function HelpMenu()
    
    fill(0)
    stroke(255, 0, 0, 255)
    strokeWidth(5)
    rect((WIDTH / 2) - 150, (HEIGHT / 2) - 250, 300, 400)
    
    fill(255, 0, 0, 255)
    textWrapWidth(200)
    text("Swipe your finger to move.", (WIDTH / 2), (HEIGHT / 2) + 30)
    text("Dodge the incoming balls.", (WIDTH / 2), (HEIGHT / 2) - 130)
    
end

GameOver:

GameOver = class()

function GameOver()
    
    font("Futura-CondensedMedium")
    fontSize(100)
    fill(255, 0, 0)
    textWrapWidth(0)
    text("Game Over", WIDTH / 2, (HEIGHT / 2) + 100)
    
    fontSize(60)
    text("Score:  " .. gameScore, WIDTH / 2, (HEIGHT / 2) - 50)
    
    if ElapsedTime > delay + 3 then
        currentScreen = "start"
    end
    
end

GameNew:

GameNew = class()

function GameNew()
    
    currentScreen = "game"
    
    playerX = WIDTH / 2
    playerY = HEIGHT / 2
    
    gameScore = 0
    ballSize = ballSizeDefault
    ballSpeed = ballSpeedDefault
    
    for i = 1, ballCount do
        BallEntity:destroy(i)
    end
    
    if gameDifficulty > 0 then
        BallEntity:create("n")
    end
    if gameDifficulty > 1 then
        BallEntity:create("w")
    end
    if gameDifficulty > 2 then
        BallEntity:create("e")
    end
    if gameDifficulty > 3 then
        BallEntity:create("s")
    end
    
end

PlayerEntity:

PlayerEntity = class()

function PlayerEntity:draw()
    
    fill(0, 0, 255)
    stroke(0)
    strokeWidth(playerSize / 20)
    rect(playerX - (playerSize / 2), playerY - (playerSize / 2), playerSize, playerSize)
    
end

function PlayerEntity:move()
    
    if CurrentTouch.state == MOVING then
        playerX = playerX + (CurrentTouch.deltaX * playerSpeed)
        playerY = playerY + (CurrentTouch.deltaY * playerSpeed)
    end
    
    if playerX < (playerSize / 2) then playerX = (playerSize / 2) end
    if playerX > WIDTH - (playerSize / 2) then playerX = WIDTH - (playerSize / 2) end
    if playerY < (playerSize / 2) then playerY = (playerSize / 2) end
    if playerY > HEIGHT - (playerSize / 2) then playerY = HEIGHT - (playerSize / 2) end
    
end

BallEntity:

BallEntity = class()

function BallEntity:draw()
    
    for i = 1, ballCount do
        fill(255, 0, 0)
        stroke(0)
        strokeWidth(ballSize / 20)
        ellipse(ballX[i], ballY[i], ballSize)
    end
    
end

function BallEntity:move()
    
    for i = 1, ballCount do
        
        if ballType[i] == "n" then ballY[i] = ballY[i] - ballSpeed end
        if ballType[i] == "s" then ballY[i] = ballY[i] + ballSpeed end
        if ballType[i] == "w" then ballX[i] = ballX[i] + ballSpeed end
        if ballType[i] == "e" then ballX[i] = ballX[i] - ballSpeed end
        
        if ballX[i] < 0 - ballSize or ballX[i] > WIDTH + ballSize or ballY[i] < 0 - ballSize or ballY[i] > HEIGHT + ballSize then
            BallEntity:create(ballType[i])
            BallEntity:destroy(i)
        end
        
    end
    
end

function BallEntity:create(type)
    
    ballCount = ballCount + 1
    ballType[ballCount] = type
    
    if type == "n" then
        ballX[ballCount] = playerX
        ballY[ballCount] = HEIGHT + ballSize
    elseif type == "s" then
        ballX[ballCount] = playerX
        ballY[ballCount] = 0 - ballSize
    elseif type == "w" then
        ballX[ballCount] = 0 - ballSize
        ballY[ballCount] = playerY
    elseif type == "e" then
        ballX[ballCount] = WIDTH + ballSize
        ballY[ballCount] = playerY
    end
    
end

function BallEntity:destroy(index)
    
    for i = index, ballCount - 1 do
        ballX[i] = ballX[i + 1]
        ballY[i] = ballY[i + 1]
        ballType[i] = ballType[i + 1]
    end
    ballCount = ballCount - 1
    
    gameScore = gameScore + 10
    ballSize = ballSize + 1
    ballSpeed = ballSpeed + .4
end

HitDetect:

HitDetect = class()

function HitDetect()
    
    playerHit = (playerSize / 2)
    ballHit = (ballSize / 2) - 5
    
    for i = 1, ballCount do
        if ballX[i] + ballHit > playerX - playerHit and ballX[i] - ballHit < playerX + playerHit and ballY[i] + ballHit > playerY - playerHit and ballY[i] - ballHit < playerY + playerHit then
            return true
        end
    end
    
    return false
    
end

Thanks for sharing :slight_smile:

I’m having an (slightly) easier time in “insane” if I comment out displayMode(FULLSCREEN)…
Is that cheating ? :stuck_out_tongue:

If you hold touch on your project, you can copy/paste to a forum post in one go I believe.

Keep up the good work !

Cheers,
Xavier

Really??? I wish I knew that… oh, we’ll. It will help when I post another project.

And thanks for the feedback!

.@metzyn you would get more feedback if you would post the project as a single copy/paste. It is too long to copy tab per tab.

--# Main
-- Swipeball
-- Author, Stephen Monroe
-- Version, 1.0.0

function setup()

    currentScreen = "start"

    gameDifficulty = 0
    gameScore = 0
    
    playerX = 0
    playerY = 0
    playerSize = 50
    playerSpeed = 3
    
    ballX = {}
    ballY = {}
    ballType = {}
    ballSizeDefault = 50
    ballSpeedDefault = 5
    ballCount = 0
    
    delay = 0
    
    displayMode(FULLSCREEN)

end

function draw()

    background(0)

    if currentScreen == "start" then GameMenu() end
    if currentScreen == "end" then GameOver() end
    if currentScreen == "game" then GameLoop() end
    
end

function GameLoop()
    
    BallEntity:draw()
    BallEntity:move()
    
    PlayerEntity:draw()
    PlayerEntity:move()
    
    if HitDetect() == true then
        delay = ElapsedTime
        currentScreen = "end"
    end
    
end
--# GameMenu
GameMenu = class()

function GameMenu()
    
    font("Futura-CondensedMedium")
    fontSize(100)
    fill(255, 0, 0)
    textWrapWidth(0)
    text("Swipeball", WIDTH / 2, HEIGHT - 100)
    
    fontSize(60)
    text("Easy", WIDTH / 2, (HEIGHT / 2) + 100)
    text("Medium", WIDTH / 2, HEIGHT / 2)
    text("Hard", WIDTH / 2, (HEIGHT / 2) - 100)
    text("Insane", WIDTH / 2, (HEIGHT / 2) - 200)
    
    fontSize(40)
    text("?", WIDTH - 50, 100)
    text("X", WIDTH - 50, 40)
    
    x = CurrentTouch.x
    y = CurrentTouch.y
    
    if CurrentTouch.state == BEGAN or CurrentTouch.state == MOVING then
        if x > (WIDTH / 2) - 100 and x < (WIDTH / 2) + 100 then
            -- easy
            if y > (HEIGHT / 2) + 50 and y < (HEIGHT / 2) + 150 then
                gameDifficulty = 1
                GameNew()
            end
            -- medium
            if y > (HEIGHT / 2) - 50 and y < (HEIGHT / 2) + 50 then
                gameDifficulty = 2
                GameNew()
            end
            -- hard
            if y > (HEIGHT / 2) - 150 and y < (HEIGHT / 2) - 50 then
                gameDifficulty = 3
                GameNew()
            end
            -- insane
            if y > (HEIGHT / 2) - 250 and y < (HEIGHT / 2) - 150 then
                gameDifficulty = 4
                GameNew()
            end
        end
        if x > WIDTH - 80 and x < WIDTH - 20 then
            -- help
            if y > 70 and y < 130 then
                HelpMenu()
            end
            -- exit
            if y > 10 and y < 70 then
                close()
            end
        end
    end

end

function HelpMenu()
    
    fill(0)
    stroke(255, 0, 0, 255)
    strokeWidth(5)
    rect((WIDTH / 2) - 150, (HEIGHT / 2) - 250, 300, 400)
    
    fill(255, 0, 0, 255)
    textWrapWidth(200)
    text("Swipe your finger to move.", (WIDTH / 2), (HEIGHT / 2) + 30)
    text("Dodge the incoming balls.", (WIDTH / 2), (HEIGHT / 2) - 130)
    
end

--# GameOver
GameOver = class()

function GameOver()
    
    font("Futura-CondensedMedium")
    fontSize(100)
    fill(255, 0, 0)
    textWrapWidth(0)
    text("Game Over", WIDTH / 2, (HEIGHT / 2) + 100)
    
    fontSize(60)
    text("Score:  " .. gameScore, WIDTH / 2, (HEIGHT / 2) - 50)
    
    if ElapsedTime > delay + 3 then
        currentScreen = "start"
    end
    
end

--# GameNew
GameNew = class()

function GameNew()
    
    currentScreen = "game"
    
    playerX = WIDTH / 2
    playerY = HEIGHT / 2
    
    gameScore = 0
    ballSize = ballSizeDefault
    ballSpeed = ballSpeedDefault
    
    for i = 1, ballCount do
        BallEntity:destroy(i)
    end
    
    if gameDifficulty > 0 then
        BallEntity:create("n")
    end
    if gameDifficulty > 1 then
        BallEntity:create("w")
    end
    if gameDifficulty > 2 then
        BallEntity:create("e")
    end
    if gameDifficulty > 3 then
        BallEntity:create("s")
    end
    
end

--# PlayerEntity
PlayerEntity = class()

function PlayerEntity:draw()
    
    fill(0, 0, 255)
    stroke(0)
    strokeWidth(playerSize / 20)
    rect(playerX - (playerSize / 2), playerY - (playerSize / 2), playerSize, playerSize)
    
end

function PlayerEntity:move()
    
    if CurrentTouch.state == MOVING then
        playerX = playerX + (CurrentTouch.deltaX * playerSpeed)
        playerY = playerY + (CurrentTouch.deltaY * playerSpeed)
    end
    
    if playerX < (playerSize / 2) then playerX = (playerSize / 2) end
    if playerX > WIDTH - (playerSize / 2) then playerX = WIDTH - (playerSize / 2) end
    if playerY < (playerSize / 2) then playerY = (playerSize / 2) end
    if playerY > HEIGHT - (playerSize / 2) then playerY = HEIGHT - (playerSize / 2) end
    
end

--# BallEntity
BallEntity = class()

function BallEntity:draw()
    
    for i = 1, ballCount do
        fill(255, 0, 0)
        stroke(0)
        strokeWidth(ballSize / 20)
        ellipse(ballX[i], ballY[i], ballSize)
    end
    
end

function BallEntity:move()
    
    for i = 1, ballCount do
        
        if ballType[i] == "n" then ballY[i] = ballY[i] - ballSpeed end
        if ballType[i] == "s" then ballY[i] = ballY[i] + ballSpeed end
        if ballType[i] == "w" then ballX[i] = ballX[i] + ballSpeed end
        if ballType[i] == "e" then ballX[i] = ballX[i] - ballSpeed end
        
        if ballX[i] < 0 - ballSize or ballX[i] > WIDTH + ballSize or ballY[i] < 0 - ballSize or ballY[i] > HEIGHT + ballSize then
            BallEntity:create(ballType[i])
            BallEntity:destroy(i)
        end
        
    end
    
end

function BallEntity:create(type)
    
    ballCount = ballCount + 1
    ballType[ballCount] = type
    
    if type == "n" then
        ballX[ballCount] = playerX
        ballY[ballCount] = HEIGHT + ballSize
    elseif type == "s" then
        ballX[ballCount] = playerX
        ballY[ballCount] = 0 - ballSize
    elseif type == "w" then
        ballX[ballCount] = 0 - ballSize
        ballY[ballCount] = playerY
    elseif type == "e" then
        ballX[ballCount] = WIDTH + ballSize
        ballY[ballCount] = playerY
    end
    
end

function BallEntity:destroy(index)
    
    for i = index, ballCount - 1 do
        ballX[i] = ballX[i + 1]
        ballY[i] = ballY[i + 1]
        ballType[i] = ballType[i + 1]
    end
    ballCount = ballCount - 1
    
    gameScore = gameScore + 10
    ballSize = ballSize + 1
    ballSpeed = ballSpeed + .4
end

--# HitDetect
HitDetect = class()

function HitDetect()
    
    playerHit = (playerSize / 2)
    ballHit = (ballSize / 2) - 5
    
    for i = 1, ballCount do
        if ballX[i] + ballHit > playerX - playerHit and ballX[i] - ballHit < playerX + playerHit and ballY[i] + ballHit > playerY - playerHit and ballY[i] - ballHit < playerY + playerHit then
            return true
        end
    end
    
    return false
    
end

Very nice! Thanks for sharing. Lauch the ball at random times for more difficulty when sevral balls.