Test contest

Entry

Winner

This was glorious. Everyone loves ansi color codes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env lua

-- Smariot's horrible abuse of sine waves and ansi escape codes.
-- Note: Run this on a proper terminal emulator. You'll get garbage otherwise.

-- Tip: For bonus points, rewrite this to do a zoom of the mandelbrot set.

local t = 0
local delta = 0.0001
local last_time = os.time()
local time_mode = 0
local counter = 0

local floor = math.floor
local sin = math.sin
local abs = math.abs
local sub = string.sub

local pal = {"\27[31m@", "\27[32mO", "\27[33mo", "\27[34m.", " ", "\27[35m.", "\27[36mo", "\27[37mO"}
local pal_size = #pal

local function char(x, y)
  if y == floor(sin(t)*5)+7 and x >= 5 and x < 8 then
    return "\27[91m"..(sub("Hello,", (x-4.5)*2, (x-4.5)*2))
  end
  
  if y == floor(sin(t*1.1+1)*5)+8 and x >= 6 and x < 9 then
    return "\27[93m"..(sub("World!", (x-5.5)*2, (x-5.5)*2))
  end
  
  y = y + x*0.5 + t*15
  x = x - t*30 + y* 0.5
  return pal[floor((sin(0.05340707511102649*x)+
                    sin(0.003141592653589793*y)+
                    sin(0.01570796326794897*x+0.0471238898038469*y))%1*pal_size)+1] or "?"
end

while true do
  for y = 1, 15 do
    for x = 1, 30 do
      io.write(char(x*.5, y))
    end
    
    io.write("\n")
  end
  
  -- We don't have access to a precision timer, so we fudge it
  -- by figuring out how many frames we can draw in a second.
  -- and using that to guess how much time elapses each frame
  -- from then on.
  local now = os.time()
  if now ~= last_time then
    last_time = now
    if time_mode == 0 then
      time_mode = 1
      counter = 0
    elseif time_mode == 1 then
      io.write("    \27[97m--==[ ", counter, " FPS ]==--    ")
      delta = 1/counter
      counter = 0
    end
  end
  
  counter = counter + 1
  t = t + delta
  
  -- A busy loop, to reduce the chances of seeing the cursor
  -- in the middle of the image.
  for i = 1,80000 do io.write() end
  
  io.write("\27[15A\r") -- To the top of the screen!
end

Facts

Date created
20 May 2009
Updated
20 May 2009

Contestant