Advertisement
electronic_steve

cc scripts | graphics system

Mar 12th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.50 KB | None | 0 0
  1.  
  2.    tiles = {{0,0,0,0,0,0,0,0,0,0,0},
  3.           {0,0,0,0,0,0,0,0,0,0,0},
  4.           {0,0,0,0,0,0,0,0,0,0,0},
  5.           {0,0,0,0,0,0,0,0,0,0,0},
  6.           {0,0,0,0,0,0,0,0,0,0,0},
  7.           {0,0,0,0,0,0,0,0,0,0,0},
  8.           {0,0,0,0,0,0,0,0,0,0,0},
  9.           {0,0,0,0,0,0,0,0,0,0,0},
  10.           {0,0,0,0,0,0,0,0,0,0,0}
  11. }
  12.  
  13.    timer = 1
  14.    speed = 0
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. r={}
  23. fps={}
  24. fps.old=os.time()
  25. fps.enable=true
  26. c={1} --таблица цветов. просто для упрощения жизни.
  27. for i=1,15 do
  28. c[#c+1]=2^i
  29.  end
  30.  mouse={}
  31.  mouse.x=0
  32.  mouse.y=0
  33. r.s={} -- таблица для рендера.
  34. r.sx,r.sy=term.getSize() -- размер экрана
  35. r.d={} -- таблица для сравнения рендеров.
  36. r.se={} -- таблица несовпадений рендеров.
  37. r.to=os.time()
  38. r.renderspeed=0 -- чем больше число тем больше  сошибок делает рендер но fps будет выше. если поставить 0 то fps упадет до 10 до не будет глюков рендера.
  39. gh={}
  40. angle=0
  41. defbackcolor=16 -- цвет заднего фона.
  42. keypress=0
  43.  lcolor=1
  44. function limit(n,n1) -- 1<n<n1
  45. local x=math.min(n,n1)
  46. local nx=math.max(1,x)
  47. return nx
  48. end
  49.  
  50. function fps.update()
  51. local delta=(os.time()-fps.old)*1000*0.05
  52. fps.old=os.time()
  53. return  math.floor(1/delta)
  54. end
  55.  
  56. for y=1,r.sy do
  57. r.s[y]={}
  58. for x=1,r.sx do
  59. r.s[y][x]={16,16," "}
  60. end end
  61. for y=1,r.sy do
  62. r.d[y]={}
  63. for x=1,r.sx do
  64. r.d[y][x]={16,16,"X"}
  65. end end
  66. function all(t,v)
  67.    local a = true
  68.    for i,b in ipairs(t) do
  69.       if b~=v then
  70.          a = false
  71.       end
  72.    end
  73.    return a
  74. end
  75.  
  76. function update()
  77.  
  78.  
  79.    -- code from  https://www.love2d.org/forums/viewtopic.php?f=3&t=77616&start=10
  80.       local falling = false
  81.       for j=#tiles,1,-1 do
  82.          for i,v in ipairs(tiles[j]) do
  83.        
  84.             if v > 0 then
  85.                if j~=#tiles then
  86.                   if tiles[j+1][i]~=1 then
  87.                      tiles[j+1][i] = tiles[j][i]
  88.                      tiles[j][i] = 0
  89.                      falling = true
  90.                   else
  91.                      tiles[j][i] = 1
  92.                   end
  93.                else
  94.                   tiles[j][i] = 1
  95.                end
  96.             end
  97.          end
  98.          if all(tiles[j],1) then
  99.             tiles[j] = {0,0,0,0,0,0,0,0,0,0,0}
  100.          end
  101.       end
  102.       if falling == false then
  103.          tiles[1] = {0,0,0,0,0,0,0,0,0,0,0}
  104.          tiles[1][math.random(#tiles[1])] = 2
  105.       end
  106.    
  107.    
  108.  
  109. end
  110. function gh.rectangle(x,y,x2,y2,mode,color,altcolor,text)
  111. local func={}
  112. func["line"]= function(x,y,x2,y2,color,altcolor,text)
  113. for nx=1,x2 do
  114. for ny=1,y2 do
  115. if nx==1 or nx==x2 or ny==1 or ny==y2 then
  116. gh.point(x+nx,y+ny,color,altcolor,text) end
  117.  
  118.  
  119. end end
  120.  end
  121. func["fill"]= function(x,y,x2,y2,color,altcolor,text)
  122. for nx=1,x2 do
  123. for ny=1,y2 do
  124. gh.point(x+nx,y+ny,color,altcolor,text)
  125.  
  126.  
  127. end end
  128.  end
  129.  
  130.  func[mode](x,y,x2,y2,color,altcolor,text)
  131.  
  132. end
  133. function gh.point(x,y,color,altcolor,text)
  134.  
  135. r.s[y][x]={color,altcolor,text}
  136. end
  137. function gh.text(x,y,color,altcolor,text)
  138. text2= text..""
  139. local i=0
  140. for w in string.gmatch(text2, ".") do
  141.  
  142.  
  143. gh.point(x+i,y,color,altcolor,w)
  144. i=i+1
  145.  
  146.  
  147. end
  148.  
  149.  
  150.  
  151. end
  152. function gh.line(x2,y2,x3,y3,color,altcolor,text)
  153. local x=x3-x2
  154. local y=y3-y2
  155. local nx=x2
  156. local ny=y2
  157. local h=math.sqrt(x^2+y^2)
  158. local sina= y/h
  159. local cosa= x/h
  160.  
  161. for i=1,h do
  162. gh.point(math.floor(nx),math.floor(ny),color,altcolor,text)
  163.  
  164. nx=nx+cosa
  165. ny=ny+sina
  166.  
  167.  end
  168.  
  169. end
  170.  
  171. function draw()
  172.  
  173. for j,v in ipairs(tiles) do
  174. for i,t in ipairs(v) do
  175. if t > 0 then  gh.point(math.floor((r.sx-#tiles)/2+i) ,math.floor((r.sy-#tiles[1])/2+j),1,16,"#") end
  176.  
  177. end end
  178. gh.rectangle((r.sx-#tiles)/2-1,(r.sy-#tiles[1])/2-1,#tiles[1]+2,#tiles+2,"line",1,16,"O")
  179. gh.text(2,2,1,16,"fps:"..fps.update())
  180.  
  181.  
  182.  
  183.  
  184.  
  185. -- пререндер
  186.  
  187.  
  188.  
  189.  
  190. for y=1,r.sy do
  191. for x=1,r.sx do
  192. for i=1,3 do
  193. if  r.s[y][x][i]==r.d[y][x][i] then else
  194. r.se[#r.se+1]={y,x,r.s[y][x][1],r.s[y][x][2],r.s[y][x][3]}
  195. r.d[y][x][i]=r.s[y][x][i]
  196. r.s[y][x]={16,16," "}
  197. end
  198. end
  199. end
  200.  
  201.  
  202.  
  203.  
  204. -- рендер
  205. if #r.se>0 then
  206. for i=#r.se,1,-1 do
  207.  
  208. term.setCursorPos(r.se[i][2],r.se[i][1])
  209. term.setTextColor(c[r.se[i][3] ])
  210. term.setBackgroundColor(c[r.se[i][4] ])
  211.  
  212. write(r.se[i][5])
  213.  
  214. r.se[i]=nil
  215.  
  216.  
  217.  end
  218.  
  219.  
  220.  end
  221.  
  222. end
  223.  for y=1,r.sy do
  224. for x=1,r.sx do
  225. r.s[y][x]={16,16," "}
  226. end end
  227. end
  228.  
  229.  
  230. while true do
  231.  
  232. update()
  233. draw()
  234. sleep(0)
  235. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement