Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@devilstower
Last active December 21, 2015 16:59
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 devilstower/6337642 to your computer and use it in GitHub Desktop.
Save devilstower/6337642 to your computer and use it in GitHub Desktop.
10 Second Puzzle -- a quick game in Codea
--# Main
-- 10-Second-Puzzle
--
function setup()
displayMode(FULLSCREEN)
iconBtn = {}
selectArray = {}
numSelected = 0
pos = 0
textTime = 10
for i=1, 5 do
iconBtn[i] = AnswerBox('x', math.random(500),
math.random(1000), 300, 70, nil, '')
end
p = {}
p[1] = Puzzle("Simple As", "A;B;C;D;E")
p[2] = Puzzle("Sing It", "Do;Re;Mi;Fa;So")
p[3] = Puzzle("Look Up", "Red;Orange;Yellow;Green;Blue")
p[4] = Puzzle("Ca-ching", "Penny;Nickel;Dime;Quarter;Half")
p[5] = Puzzle("En Espanol", "Uno;Dos;Tres;Quatro;Cinco")
p[6] = Puzzle("Lincolnish", "Four;Score;And;Seven;Years")
p[7] = Puzzle("Elemental", "Hydrogen;Helium;Lithium;Berylium;Boron")
p[8] = Puzzle("Line Up", "Line;Angle;Triangle;Square;Pentagon")
p[9] = Puzzle("Little Big", "milli;centi;deci;deca;kilo")
p[10] = Puzzle("Presidential", "Washington;Adams;Jefferson;Madison;Monroe")
p[11] = Puzzle("Systemic", "Mercury;Venus;Earth;Mars;Jupiter")
p[12] = Puzzle("Sie Deutsch", "ein;zwei;drei;vier;fünf")
p[13] = Puzzle("Long, Long Ago", "Phantom Menace;Attack of the Clones;Revenge of the Sith;A New Hope;Empire Strikes Back")
p[14] = Puzzle("Biblical", "Genesis;Exodus;Leviticus;Numbers;Dueteronomy")
p[15] = Puzzle("Testify", "Matthew;Mark;Luke;John;Acts")
p[16] = Puzzle("Nobly Go", "Star Trek;Next Generation;Deep Space Nine;Voyager;Enterprise")
p[17] = Puzzle("Potted", "Philosopher's Stone;Chamber of Secret;Prizoner of Azaban;Goblet of Fire;Order of the Phoenix")
p[18] = Puzzle("Good Grief", "Anger;Denial;Bargining;Depression;Acceptance")
p[19] = Puzzle("At the Movies", "Star Trek;Wrath of Khan;Search for Spock;Voyage Home;Final Frontier")
p[20] = Puzzle("Noble", "Helium;Neon;Argon;Krypton;Xenon")
p[21] = Puzzle("Kitchen", "Teaspoon;Tablespoon;Ounce;Cup;Pint")
p[22] = Puzzle("In Space...", "Mercury;Gemini;Apollo;Sky Lab;Space Shuttle")
p[23] = Puzzle("Geologic", "Cambrian;Ordivician;Silurian;Devonian;Mississipian")
p[24] = Puzzle("Storage", "Bit;Byte;Kilobyte;Megabyte;Terabyte")
p[25] = Puzzle("House of Mario", "NES;Super NES;Gamecube;Wii;Wii U")
p[26] = Puzzle("Biggest First", "Alaska;Texas;California;Montana;New Mexico")
p[27] = Puzzle("Smallest First", "Rhode Island;Delaware;Connecticut;New Jersey;New Hampshire")
p[28] = Puzzle("In the Navy",
"Ensign;Lieutenant;Commander;Captain;;Admiral")
p[29] = Puzzle("You're In the Army Now",
"Lieutenant;Captain;Major;;Colonel;General")
p[30] = Puzzle("Long and Winding Road",
"Meet the Beatles;Twist and Shout;Hard Day's Night;Help!;Rubber Soul;Revolver")
p[31] = Puzzle("Longest First",
"Nile;Amazon;Yangtze;Mississippi;Yenisel;Yellow")
p[32] = Puzzle("And Now, the News",
"Edwards;Cronkite;Rather;Schieffer;Couric")
-- hornblower?
-- states most populous
-- Biggest Countries
-- world's largest cities
maxPuzzle = 31
backAngle = 1
score = 0
currentPuzzle = 1
shufflePuzzles()
initPuzzle()
showTitle = true
titleTimer = ElapsedTime
titleIcons()
end
function titleIcons()
iconBtn[1] = AnswerBox('Touch', math.random(450), 400, 300, 70,
nil, '')
iconBtn[2] = AnswerBox('the', math.random(450), 200, 300, 70,
nil, '')
iconBtn[3] = AnswerBox('Answers', math.random(450), 300, 300, 70,
nil, '')
iconBtn[4] = AnswerBox('in', math.random(450), 100, 300, 70,
nil, '')
iconBtn[5] = AnswerBox('Order', math.random(450), 500, 300, 70,
nil, '')
for i = 1, 5 do
foundSpot = false
while not foundSpot do
x = math.random(450) + 2
y = math.random(5) * 100
foundSpot = true
for j = 1, i - 1 do
if math.abs(y - iconBtn[j].y) < 90 then
foundSpot = false
end
end
end
iconBtn[i].x = x
iconBtn[i].y = y
end
end
function titleScreen()
background()
fontSize(200)
text("10", WIDTH/2, HEIGHT-100)
fontSize(150)
text("Second", WIDTH/2, HEIGHT-250)
fontSize(125)
text("Quiz", WIDTH/2, HEIGHT-370)
i, r = math.modf(ElapsedTime - titleTimer)
if i > 0 and i < 6 then
iconBtn[i].background = color(130, 222, 49, 255)
end
if i > 6 then
for j = 1, 5 do
iconBtn[j].background = color(255, 255, 255, 255)
end
titleIcons()
titleTimer = ElapsedTime
end
for i = 1, 5 do
iconBtn[i]:draw()
end
end
function shufflePuzzles()
for i = 1, 50 do
fromRec = math.random(maxPuzzle - 1) + 1
toRec = math.random(maxPuzzle - 1) + 1
name = p[fromRec].name
txt = p[fromRec].text
p[fromRec] = Puzzle(p[toRec].name, p[toRec].text)
p[toRec] = Puzzle(name, txt)
end
end
function initPuzzle()
for i = 1, 5 do
iconBtn[i].text = p[currentPuzzle].itemText[i]
end
arrangeIcons()
numSelected = 1
roundStartTime = ElapsedTime
gameActive = false
end
function arrangeIcons()
for i = 1, 5 do
foundSpot = false
while not foundSpot do
x = math.random(450) + 2
y = math.random(750) + 100
foundSpot = true
for j = 1, i - 1 do
if math.abs(y - iconBtn[j].y) < 100 then
foundSpot = false
end
end
end
iconBtn[i].x = x
iconBtn[i].y = y
end
end
function clearIcons()
for i = 1, 5 do
iconBtn[i].background = color(255, 255, 255, 255)
iconBtn[i].bottomText = ""
numSelected = 1
sound(SOUND_PICKUP, 38483)
end
end
function drawReady()
textAlign(CENTER)
textMode(CENTER)
i, r = math.modf(ElapsedTime - roundStartTime)
if i >= 13 then
fill(47, 77, 221, 127)
fontSize(150)
text("Time's Up", WIDTH / 2, HEIGHT / 2)
fontSize(40)
fill(244, 215, 32, 255)
text("Touch here for new game", WIDTH/2, 40)
gameActive = false
return
elseif i < 1 then
pos = 1
s = "Ready"
fill(170, 22, 23, 255 - r * 250)
elseif i < 2 then
if pos == 1 then
sound(SOUND_PICKUP, 38483)
pos = 2
end
s = "Set"
fill(170, 168, 21, 255 - r * 250)
elseif i < 3 then
if pos == 2 then
sound(SOUND_PICKUP, 38483)
pos = 3
end
s = "Go"
fill(34, 170, 20, 255 - r * 250)
else
if not gameActive then
sound(SOUND_PICKUP, 24357)
end
gameActive = true
s = ""
end
if i < 4 then
fontSize(r * 300)
text(s, WIDTH / 2, HEIGHT / 2)
fill(255, 255, 255, 255)
fontSize(100)
text("10", WIDTH / 2, HEIGHT - 45)
return
else
fontSize(r * 80)
fill(212, 189, 207, 255)
text(p[currentPuzzle].name, WIDTH / 2, 60)
fill(255, 255, 255, 255)
fontSize(100)
text(13 - i, WIDTH / 2, HEIGHT - 45)
end
end
function draw()
background(255, 255, 255, 255)
pushMatrix()
spriteMode(CENTER)
if showTitle then
titleScreen()
return
end
--tint(math.random(255), math.random(255), math.random(255), 222)
backAngle = backAngle + 1
translate(WIDTH/2, HEIGHT/2)
rotate(backAngle)
if score < 5 then
s = "Cargo Bot:Starry Background"
tint(0, 0, 0, 127)
elseif score < 10 then s = "Cargo Bot:Background Fade"
else
s = "SpaceCute:Planet"
end
sprite(s, 0, 0, HEIGHT * 1.5)
popMatrix()
for i = 1,5 do
iconBtn[i]:draw()
end
drawReady()
fontSize(40)
fill(255, 0, 223, 255)
stroke(255, 255, 255, 255)
strokeWidth(1)
for i = 1, score do
rect(10, 10 + i * 5, 20, 4)
end
fill(228, 226, 213, 255)
fontSize(32)
text(score, 20, score * 5 + 32)
end
function touched(touch)
if showTitle and touch.state == ENDED then
showTitle = false
clearIcons()
initPuzzle()
end
if gameActive then
touchedAny = false
for i = 1,5 do
if iconBtn[i]:touched(touch) and touch.state == BEGAN then
if iconBtn[i].text ==
p[currentPuzzle].itemText[numSelected] then
iconBtn[i].selected = true
iconBtn[i].background = color(130, 222, 49, 255)
iconBtn[i].bottomText = i
numSelected = numSelected + 1
sound(SOUND_PICKUP, 18929)
if i == 5 then
score = score + 1
currentPuzzle = currentPuzzle + 1
clearIcons()
initPuzzle()
end
else
clearIcons()
end
touchedAny = true
end
end
if not touchedAny then
touchedArray = {}
numTouched = 0
end
elseif touch.y < 50 and touch.state == BEGAN then
score = 0
currentPuzzle = 1
shufflePuzzles()
clearIcons()
initPuzzle()
end
end
function keyboard(key)
if CCActiveTextBox then
CCActiveTextBox:acceptKey(key)
end
end
--# AnswerBox
AnswerBox = class()
function AnswerBox:init(s, x, y, w, h)
self.x = x
self.y = y
self.h = h
self.w = w
self.text = s
self.bottomText = bottomText
self.background = color(255, 255, 255, 255)
end
function AnswerBox:inset(dx, dy)
self.x = self.x + dx
self.y = self.y + dy
self.w = self.w - dx * 2
self.h = self.h - dy * 2
end
function AnswerBox:roundRect(radius)
pushStyle()
insetPos = vec2(self.x + radius, self.y + radius)
insetSize = vec2(self.w - 2 * radius, self.h - 2 * radius)
rectMode(CORNER)
rect(insetPos.x, insetPos.y, insetSize.x, insetSize.y)
if radius > 0 then
smooth()
lineCapMode(ROUND)
strokeWidth(radius * 2)
line(insetPos.x, insetPos.y,
insetPos.x + insetSize.x, insetPos.y)
line(insetPos.x, insetPos.y,
insetPos.x, insetPos.y + insetSize.y)
line(insetPos.x, insetPos.y + insetSize.y,
insetPos.x + insetSize.x, insetPos.y + insetSize.y)
line(insetPos.x + insetSize.x, insetPos.y,
insetPos.x + insetSize.x, insetPos.y + insetSize.y)
end
popStyle()
end
function AnswerBox:draw()
local h, w
w, h = textSize(self.text)
pushStyle()
fill(0, 0, 0, 255)
stroke(0, 0, 0, 255)
self:roundRect(4)
fill(self.background)
stroke(self.background)
self:inset(1,1)
self:roundRect(4)
self:inset(-1,-1)
font("HelveticaNeue-Light")
fontSize(18)
textMode(CENTER)
textAlign(CENTER)
smooth()
w, h = textSize(self.bottomText)
fill(0, 0, 0, 255)
if self.bottomText then
text(self.bottomText, self.x + self.w / 2,
self.y + h/2 + 4)
end
fontSize(32)
text(self.text, self.x + self.w / 2, self.y + self.h / 2 + 10)
popStyle()
end
function AnswerBox:touched(touch)
if self:ptIn(touch) then
return true
end
return false
end
function AnswerBox:ptIn(test)
if test.x >= self.x and test.x <= self.x + self.w then
if test.y >= self.y and
test.y <= self.y + self.h then
return true
end
end
return false
end
--# Puzzle
Puzzle = class()
function Puzzle:init(name, s)
self.name = name
self.text = s
self.itemText = {}
self:splitText()
end
function Puzzle:splitText()
-- allows multiple items to be passed to text using
-- semicolons as seperators
local i, k
i = 0
for k in string.gmatch(self.text,"([^;]+)") do
i = i + 1
self.itemText[i] = k
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment