Skip to content

Instantly share code, notes, and snippets.

/Game.lua
Created Nov 24, 2011

Embed
What would you like to do?
Shoot Babbles! game
color1 = {255, 255, 255, 255}
color2 = {255, 0, 255, 255}
color3 = {0, 255, 255, 255}
color4 = {200, 255, 200, 255}
color5 = {255, 255, 0, 255}
colors ={color1, color2, color3, color4, color5}
SCORE = 0
good ="GOOD!"
function setup()
    print("SHOOT BABBLES!")
    bab = List()
    addsome(1)
    watch("SCORE")
end
function addsome(start)
    if SCORE > 7 then start = 5 end
   for i = start, 5 do
        j = 1 + math.floor(5 * math.random())
        s = Babbles(colors[j],
        50 + 600 * math.random(), 
        50 + 600 * math.random(),
        30 + 90 * math.random(),
        5 - 10 * math.random(),
        5 - 10 * math.random())
        if i == 1 or start > 1 then h = bab:insertAtStart(s) else h = bab:insertAfter(h, s) end
    end 
end
function shoot()
    if CurrentTouch.state ~= BEGAN then return end
    xt = CurrentTouch.x
    yt = CurrentTouch.y
    h = bab.first
    while h do
        if (h.data.centerX - xt) * (h.data.centerX - xt) + 
        (h.data.centerY - yt) * (h.data.centerY - yt) < 120 then
            sound(SOUND_EXPLODE, 100)
            SCORE = SCORE + 1
            bab:delete(h)
            if SCORE < 20 then addsome(2) end
            if SCORE % 10 == 0 then
                print(good)
                good = "VERY " .. good
            end
        end
        h = h.next 
    end   
end
function draw()
    background(0, 0, 0, 255)
    fill(101, 179, 48, 255)
    rect(100, 100, WIDTH - 200, HEIGHT - 200)
    h = bab.first
    while h do
        h.data:draw() 
        h = h.next 
    end
    shoot()
end
Babbles = class()
function Babbles:init(strokeColor, centerX, centerY, r, vx, vy)
    self.strokeColor = strokeColor
    self.centerX = centerX
    self.centerY = centerY
    self.alpha = centerX % 360
    self.r = r
    self.vx = vx
    self.vy = vy
end
function Babbles:draw()
    pushMatrix()
    pushStyle()
    xx = self.centerX + self.vx 
    if xx + self.r > WIDTH or xx - self.r < 0 then self.vx = - self.vx end
    yy = self.centerY + self.vy
    if yy + self.r > HEIGHT or yy - self.r < 0 then self.vy = - self.vy end
    self.centerX = self.centerX + self.vx
    self.centerY = self.centerY + self.vy
    stroke(unpack(self.strokeColor))
    strokeWidth(1)
    fill(0, 0, 0, 255)
    ellipseMode(CENTER)
    translate(self.centerX, self.centerY)
    rotate(self.alpha)
    noSmooth()
    ellipse(0, 0, self.r)
    ellipse(0, 0, self.r * 0.9)
    ellipse(0, 0, self.r * 0.7)
    stroke(0, 0, 0, 255)
    ellipse(0, 0, self.r - 2, self.r * 0.7 - 2)
    self.alpha = self.alpha + 1 
    if self.alpha > 360 then self.alpha = 0 end
    popStyle()
    popMatrix()
end
-- Beginning Lua with World of Warcraft addons. 2009. Paul Emmerich
List = class()
function List:init()
    return {first = nil, last = nil}
end
function List:insertAtStart(data)
    local new = {prev = nil, next = self.first, data = data}
    if not self.first then
        self.first = new
        self.last = new
    else
        self.first.prev = new
        self.first = new
    end
    return new
end 
function List:insertAfter(node, data)
    local new = {prev = node, next = node.next, data = data}
    node.next = new
    if node == self.last then
        self.last = new
    else
        new.next.prew = new
    end
    return new
end
function List:delete(node)
    if node == self.first then
        self.first = node.next
    else
        node.prev.next = node.next
    end
    if node == self.last then
        self.last = node.prev
    else
        node.next.prev = node.prev
    end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.