Kingdaro

DDR Demo thing

Feb 6th, 2013
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 KB | None | 0 0
  1. local w,h = term.getSize()
  2.  
  3. local running
  4.  
  5. local notes = {}
  6. local receptor = {
  7.     x = 3,
  8.     y = 2,
  9.     blink = 4
  10. }
  11.  
  12. local keymap = {
  13.     left=1,
  14.     down=2,
  15.     up=3,
  16.     right=4
  17. }
  18. local notemap = {'<', 'V', '^', '>'}
  19.  
  20. local utime = 0.1
  21. local utimer
  22.  
  23. local judgement
  24. local judgetime = 0
  25.  
  26. local score = 0
  27. local bgcolor = colors.black
  28.  
  29. local function doJudge(num)
  30.     judgement = num
  31.     judgetime = 0.5 + utime
  32. end
  33.  
  34. local function update(dt)
  35.     for i=#notes, 1, -1 do
  36.         local note = notes[i]
  37.         if note.y > -receptor.y then
  38.             note.y = note.y - 1
  39.         else
  40.             table.remove(notes, i)
  41.             doJudge(0)
  42.             score = score - 5
  43.         end
  44.     end
  45.    
  46.     if receptor.blink > 0 then
  47.         receptor.blink = receptor.blink - 1
  48.     else
  49.         receptor.blink = 4
  50.        
  51.         -- for testing
  52.         table.insert(notes, {
  53.             col = math.random(4);
  54.             y = 19;
  55.         })
  56.     end
  57.    
  58.     judgetime = judgetime - dt
  59.    
  60.     if utime == 0 then
  61.         bgcolor = 2^math.random(15)
  62.     end
  63. end
  64.  
  65. local function keypressed(k)
  66.     if keymap[k] then
  67.         for i,note in pairs(notes) do
  68.             if note.col == keymap[k] then
  69.                 local dist = math.abs(note.y)
  70.                 if dist == 0 then
  71.                     doJudge(1) -- marvelous
  72.                     score = score + 10
  73.                 elseif dist <= 1 then
  74.                     doJudge(2) -- perfect
  75.                     score = score + 5
  76.                 elseif dist <= 2 then
  77.                     doJudge(3) -- great
  78.                     score = score + 1
  79.                 end
  80.                
  81.                 if dist <= 2 then
  82.                     table.remove(notes, i)
  83.                 end
  84.                 break
  85.             end
  86.         end
  87.     end
  88.    
  89.     if k == 'backspace' then
  90.         running = false
  91.     end
  92. end
  93.  
  94. local function draw()
  95.     local t = term
  96.     local w,h = term.getSize()
  97.    
  98.     t.setBackgroundColor(bgcolor)
  99.     t.clear()
  100.    
  101.    
  102.     -- draw the receptor
  103.     t.setTextColor(
  104.         receptor.blink == 0 and
  105.         colors.white or
  106.         colors.lightGray
  107.     )
  108.     t.setCursorPos(receptor.x, receptor.y)
  109.     t.write('>    <')
  110.    
  111.     -- draw the notes
  112.     local notecolors = {
  113.         colors.red,
  114.         colors.yellow,
  115.         colors.lime,
  116.         colors.lightBlue
  117.     }
  118.    
  119.     for _, note in pairs(notes) do
  120.         t.setCursorPos(note.col + receptor.x, note.y + receptor.y)
  121.         t.setTextColor(notecolors[note.col])
  122.         t.write(notemap[note.col])
  123.     end
  124.    
  125.     -- judgement
  126.     local words = {
  127.         [0] = {'miss', colors.red};
  128.         {'Amazing!', colors.lightBlue};
  129.         {'Perfect!', colors.yellow};
  130.         {'Nice!', colors.lime};
  131.     }
  132.    
  133.     if judgement and judgetime > 0 then
  134.         local item = words[judgement]
  135.         local word, color = item[math.random(#item - 1)], item[#item]
  136.        
  137.         t.setCursorPos(receptor.x + 8, receptor.y)
  138.         t.setTextColor(judgetime >= 0.5 and colors.white or color)
  139.         t.write(word)
  140.     end
  141.    
  142.     -- score
  143.     local scorestr = ('0'):rep(4 - #tostring(score)) ..score
  144.     t.setCursorPos(receptor.x + 8, receptor.y + 2)
  145.     t.setTextColor(colors.yellow)
  146.     t.write(scorestr)
  147. end
  148.  
  149. local function selectMode()
  150.     local modes = {'Easy','Normal','Hard','Satan',"You're high."}
  151.     local times = {0.2, 0.15, 0.1, 0.05, 0}
  152.     local selection = 1
  153.    
  154.     local t = term
  155.    
  156.    
  157.     while true do
  158.         t.setTextColor(colors.white)
  159.         t.setBackgroundColor(colors.black)
  160.         t.clear()
  161.        
  162.         t.setCursorPos(3, 2)
  163.         t.write 'Select Difficulty'
  164.         for i,v in pairs(modes) do
  165.             t.setTextColor(colors.white)
  166.             if selection == i then
  167.                 t.setTextColor(colors.lime)
  168.                 t.setCursorPos(2, i + 3)
  169.                 t.write '>'
  170.             end
  171.             t.setCursorPos(3, i + 3)
  172.             t.write(v)
  173.         end
  174.        
  175.         local _, k = os.pullEvent('key')
  176.         if k == keys.up then
  177.             selection =
  178.                 selection > 1 and
  179.                 selection - 1 or
  180.             #modes
  181.         elseif k == keys.down then
  182.             selection =
  183.                 selection < #modes and
  184.                 selection + 1 or
  185.             1
  186.         elseif k == keys.enter then
  187.             return times[selection]
  188.         end
  189.     end
  190. end
  191.  
  192. term.setBackgroundColor(colors.black)
  193. term.clear()
  194.  
  195. utime = selectMode()
  196. utimer = os.startTimer(utime)
  197.  
  198. running = true
  199. while running do
  200.     draw()
  201.    
  202.     local ev, p1 = os.pullEvent()
  203.    
  204.     if ev == 'timer' and p1 == utimer then
  205.         update(utime)
  206.         utimer = os.startTimer(utime)
  207.     elseif ev == 'key' then
  208.         keypressed(keys.getName(p1))
  209.     end
  210. end
  211.  
  212. term.setBackgroundColor(colors.black)
  213. term.clear()
  214. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment