Bug Splat

Here is a little game I made. You can re-use any of the code if you want to. Enjoy:

--# Main

-- Main
displayMode(FULLSCREEN)
function setup()
    if not readLocalData("highScore") then 
        saveLocalData("highScore",0) 
    end
    GAME,MENU=1,2
    STATE=MENU
    
    createBugSprite()
    setupSound()
    
    newHighScore=false
    
    gamefont="Futura-CondensedExtraBold"
end

function draw()
    background(0)
    if STATE==GAME then
        if not music.name then music("A Hero's Quest:Battle") end
        spriteMode(CORNER)
        sprite("SpaceCute:Background",0,0,WIDTH,HEIGHT)
        n:draw()
        b:draw(health)
        t:draw()
        s:draw(points)
        if health<=0 then 
            if readLocalData("highScore") WIDTH+10 then 
            health = health - self.width/10
            self.alive=false
            sound(loseLifeSound)
        end
    end
end

function Bug:touched(touch)
    if touch.x > self.x-self.hitRad and touch.x < self.x + self.width + self.hitRad and 
       touch.y > self.y-self.hitRad and touch.y < self.y + self.height  + self.hitRad
    and not self.splated then
        
        points = points + math.floor( self.speed * 10 +0.5)
        self.splated=true
        sound(bugSplatSounds[math.random(1,3)])
        return true
    end
    return false
end

--# Nest
Nest = class()

function Nest:init()
    self.timelimit = 50
    self.timer=self.timelimit  
    self.bugspeed=2
    self.bugs = {}
    self.combo=0
end

function Nest:draw()
    for i=1,#self.bugs do
        self.bugs[i]:draw()
    end
    self:spawn()
    self:cullBugs()
end

function Nest:spawn()
    self.timer = self.timer + 1
    if self.timer > self.timelimit then 
        self.bugspeed = self.bugspeed *1.001
        self.timer = self.timer - self.timelimit 
        self.timelimit = self.timelimit * 0.999
        table.insert(self.bugs, Bug(-100,math.random(100,HEIGHT-150),self.bugspeed) )
    end
end

function Nest:cullBugs()
    local i = 1
    while i < #self.bugs do
        if not self.bugs[i].alive then
            table.remove(self.bugs,i)
            i = i - 1
        end
        i = i + 1
    end
end

function Nest:touched(touch)
    self.combo = 0
    if touch.state == BEGAN then
        for i = 1, #self.bugs do
            if self.bugs[i]:touched(touch) then
                self.combo = self.combo + 1
            end
        end
        if self.combo > 1 then
            local bonus = 100*self.combo
            t:spawnText(touch.x,touch.y,
            self.combo.."\
 combo! \
 +"..bonus,
            color(255, 255, 0, 255)  ,40)
            points = points + bonus
            sound(comboSound)
        end
    end
end

--# Text
Text = class()

function Text:init(x,y,text,col,size)
    self.x=x
    self.y=y
    self.text=text
    self.col=col
    self.size=size
    self.alive=true
end

function Text:draw()
    self.col.a = self.col.a - 5
    self.y = self.y + 1
    if self.col.a <= 0 then self.alive = false end
    pushStyle()
    fill(self.col)
    font(gamefont)
    fontSize(self.size)
    textAlign(CENTER)
    text(self.text,self.x,self.y)
    popStyle()
end

TextManager=class()

function TextManager:init()
    self.t={}
end

function TextManager:draw()
    for i = 1, #self.t do
        self.t[i]:draw()
    end
    self:cull()
end

function TextManager:spawnText(x,y,text,col,size)
    table.insert(self.t,Text(x,y,text,col,size))
end

function TextManager:cull()
    local i =1
    while i < #self.t do
        if not self.t[i].alive then
            table.remove(self.t,i)
            i = i - 1
        end
        i = i + 1
    end
end

--# HealthBar
HealthBar = class()

function HealthBar:init(x,y,w,v,s)
    self.x = x
    self.y = y
    self.width = w
    self.health = v
    self.max = v
    self.green = self.health / (self.max/255)
    self.red = 255- self.health / (self.max/255)
    self.strokewidth = s or 5
end

function HealthBar:draw(v)
    self.health = v
    
    self.green = self.health / (self.max/255)
    self.red = 255- self.green
    
    pushStyle()
    
    lineCapMode(SQUARE)
    
    stroke(self.red,self.green,0)
    
    strokeWidth(self.strokewidth)
    if self.health > 0 then
        line(self.x,self.y,self.x+(self.width/self.max*self.health),self.y)
    end
    popStyle()
end

--# Score
Score = class()

function Score:init(x,y,s,c)
    self.x = x
    self.y = y
    self.size=s
    self.col = c
end

function Score:draw(value)
    pushStyle()
    font(gamefont)
    fill(self.col)
    textAlign(LEFT)
    textMode(CORNER)
    fontSize(self.size)
    text(value,self.x,self.y)
    popStyle()
end


```

@Coder I added a high score that gets saved in global data and made the game restart. Replace the setup(), draw(), and touched() routines with the ones below. See the commented lines of code for the changes I added. You can take those lines and rewrite them if you want.

EDIT: Nice game. Easy to play and interesting.


function setup()
    hScore=readGlobalData("bugSplat")   -- get high score
    if hScore==nil then -- first time is nil
        hScore=0    -- make it 0
    end  
    fontSize(40)    -- set font size for hi
    
    health=100
    points=0

    createBugSprite()
    setupSound()

    n=Nest()
    b=HealthBar(50,HEIGHT-50,WIDTH-100,health,50)
    t=TextManager()
    s=Score(10,0,100,color(255))    

    music("A Hero's Quest:Battle")
    play=true   -- set play mode
end

function draw()
    if not music.name then music("A Hero's Quest:Battle") end
    background(127, 127, 127, 255)
    spriteMode(CORNER)
    sprite("SpaceCute:Background",0,0,WIDTH,HEIGHT)
    n:draw()
    b:draw(health)
    t:draw()
    s:draw(points)
    
    if health<=0 then 
        play=false  -- set play mode to false
        saveGlobalData("bugSplat",math.max(points,hScore))  -- save high score
        restart()   -- restart game
    end
    fill(255,0,0)   -- color for high score
    text("High Score  "..hScore,WIDTH/2,HEIGHT-100) -- show high score
end

function touched(touch)
    if play then    -- only allow touch while game in play mode
        n:touched(touch)
    end
end

Fun game, starts off really easy and then pretty soon it’s oh my god there’s too many I’m gonna lose.

@Coder I noticed that the game crashes when it restarts and someone is still touching the screen. I added code to the touched() routine in the above changes to prevent that.

nice job @coder

Nice game! Easy at first, then they start FLOODING in! Maybe increase the touch size for the bugs? The smalls ones are very hard to touch and I missed a lot.

Thanks everyone, I will make the smaller bugs larger so they are easier to hit

@SkyTheCoder change the line

self.hitRad=10

```

In the bug class
To a higher number for a larger collision area