Skip to content

Instantly share code, notes, and snippets.

@Bri-G
Created July 9, 2012 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bri-G/3075538 to your computer and use it in GitHub Desktop.
Save Bri-G/3075538 to your computer and use it in GitHub Desktop.
Flood-it version 3 from veeeralp
--veeralp Flood-it
-- version 3 with win/lose messages in
if require ~= nil then
require("loveCodea")
end
function setup()
displayMode(FULLSCREEN_NO_BUTTONS)
supportedOrientations(LANDSCAPE_ANY)
grid = {}
turnsStart = 25
turnsUsed = 0
turnsLeft = turnsStart- turnsUsed
totCells = 15*17
maxCells = 0
local r,c
for r = 1,15 do
grid[r] = {}
for c = 1,17 do
grid[r][c] = math.random(6)
end
end
colors = {}
colors[1] = color(255, 0, 0, 255)
colors[2] = color(0, 0, 255, 255)
colors[3] = color(0, 255, 0, 255)
colors[4] = color(255, 255, 0, 255)
colors[5] = color(255, 0, 255, 255)
colors[6] = color(117, 38, 137, 0)
end
function colourStats()
local r,c, l
for r = 1, cellNo do
for c = 1, cellNo do
cellVal = grid[r][c]
colNumber[cellVal] = colNumber[cellVal] + 1
end
end
maxCells = colNumber[1]
for l = 2,6 do
if maxCells < colNumber[l] then
maxCells = colNumber[l]
end
end
end
function draw()
background(135,206,249,255)
pushStyle()
noSmooth()
local r,c,col
for r, row in ipairs(grid) do
for c, cellVal in ipairs(row) do
col = colors[cellVal]
fill(col.r, col.g, col.b)
rect(((c - 1) * 50) + 164, ((r - 1) * 50) + 9, 50, 50)
end
end
popStyle()
pushStyle()
strokeWidth(4)
stroke(255, 255, 255, 255)
for c, col in ipairs(colors) do
fill(col.r, col.g, col.b)
rect(16, ((c-1) * 120) + 20, 130,130)
end
popStyle()
pushStyle()
font("ArialMT")
fontSize(43)
fill(255, 255, 255, 146)
text(turnsLeft, 505, HEIGHT - 27)
popStyle()
pushStyle()
font("ArialMT")
fontSize(43)
fill(255, 255, 255, 146)
text("turns left", 618, HEIGHT - 27)
popStyle()
pushStyle()
font("ArialMT")
fontSize(140)
fill(255, 255, 255, 85)
text("FLOOD IT", 585, HEIGHT - 388)
popStyle()
pushStyle()
font("ArialMT")
fontSize(55)
fill(255, 255, 255, 0)
text("VEERAL PATEL", 593, HEIGHT - 740)
timeLeft = os.difftime(91 - ElapsedTime)
popStyle()
font("ArialMT")
fontSize(43)
fill(255, 255, 255, 146)
text(timeLeft, 589, HEIGHT - 740)
if timeLeft == 0 then
messageLose()
end
if maxCells == totCells then
messageWin()
elseif turnsLeft == 0 then
messageLose()
end
end
function lineDist(x1, y1, x2, y2)
return math.sqrt(((x2 - x1)^2) + ((y2 - y1)^2))
end
function floodFill(grid, row, col, target, new)
if row < 1 or col < 1 or row > #grid or col > #grid[row] then return end
if grid[row][col] == target then
grid[row][col] = new
floodFill(grid, row + 1, col, target, new)
floodFill(grid, row - 1, col, target, new)
floodFill(grid, row, col + 1, target, new)
floodFill(grid, row, col - 1, target, new)
end
end
function touched(touch)
if touch.state == ENDED and turnsLeft > 0 then
local x,y,c
x = 81
for c in ipairs(colors) do
y = ((c - 1) * 120) + 85
if lineDist(x, y, touch.x, touch.y) <= 50 then
floodFill(grid, 15, 1, grid[15][1], c)
turnsLeft = turnsLeft - 1
end
end
end
end
function messageWin()
pushStyle()
font("ArialMT")
fontSize(54)
fill(30, 125, 36, 255)
text("CONGRATULATIONS, YOU WIN!", 585, HEIGHT - 263)
popStyle()
end
function messageLose()
pushStyle()
font("ArialMT")
fontSize(54)
fill(183, 31, 31, 255)
text("SORRY, YOU LOSE!", 585, HEIGHT - 263)
popStyle()
end
@Bri-G
Copy link
Author

Bri-G commented Jul 9, 2012

Hi All,

This is veeeralp's third version of Floodit, posted here to help with code transfers - not sure but it may help veeeralp update and post branches etc.

Any comments - please address to veeeralp on the Codea forum.

http://twolivesleft.com/Codea/Talk/discussion/1296/flood-it#Item_16

Bri_G

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment