Advertisement
guitarplayer616

CardGame

Jul 6th, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.83 KB | None | 0 0
  1. local w,h = term.getSize()
  2. local buffer = {}
  3. local defcol = colors.white
  4. local defback = colors.black
  5. local selcol = defcol
  6. local selback = defback
  7.  
  8. local deck = {}
  9. local cardPos = {}
  10. cardPos[1] = {3,2}
  11. cardPos[2] = {7,2}
  12. cardPos[3] = {11,2}
  13. cardPos[4] = {15,2}
  14. cardPos[5] = {3,12}
  15. cardPos[6] = {7,12}
  16. cardPos[7] = {11,12}
  17. cardPos[8] = {15,12}
  18. cardPos[9] = {7,7} --center left
  19. cardPos[10] = {11,7} -- center right
  20.  
  21. local p1deck = {20,2}
  22. local p2deck = {9,17}
  23.  
  24. local player1 = {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}
  25. local player2 = {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}
  26.  
  27.  
  28. local cards = {}
  29.  
  30. for i=1,13 do
  31.     cards[i] = function(x,y)
  32.         local card = {}
  33.         card.value = i
  34.         card.window = {x,y,x+2,y+2}
  35.         card.graphic = function(x,y)
  36.             x = card.window[1]
  37.             y = card.window[2]
  38.             local len = #tostring(i)
  39.             draw(tostring(i)..string.rep(" ",3-len),x,y,colors.black,colors.white)
  40.             draw("   ",x,y+1,colors.black,colors.white)
  41.             draw(string.rep(" ",3-len)..tostring(i),x,y+2,colors.black,colors.white)
  42.         end
  43.         card.back = function(x,y)
  44.             x = card.window[1]
  45.             y = card.window[2]
  46.             draw(" ",x,y,colors.black,colors.red)
  47.             draw(" ",x+1,y,colors.black,colors.white)
  48.             draw(" ",x+2,y,_,colors.red)
  49.             draw(" ",x,y+1,_,colors.white)
  50.             draw(" ",x+1,y+1,_,colors.red)
  51.             draw(" ",x+2,y+1,_,colors.white)
  52.             for p = 0,2,2 do
  53.                 draw(" ",x+p,y+2,_,colors.red)
  54.             end
  55.             draw(" ",x+1,y+2,_,colors.white)
  56.         end
  57.     return card
  58.     end
  59. end
  60.  
  61.  
  62.  
  63. --buffer[1][1] = {char "s", int txtcol, int backcol}
  64.  
  65. function createBuffer()
  66.     for x=1,w do
  67.         buffer[x] = {}
  68.         for y=1,h do
  69.             buffer[x][y] = nil
  70.         end
  71.     end
  72. end
  73.  
  74. function draw(words,x,y,txt,back)
  75.         txt = txt or defcol
  76.         back = back or defback
  77.         for i = 0,#words-1 do
  78.                 if not (x + i > w) then
  79.                         buffer[x + i][y] = {words:sub(i+1,i+1),txt,back}
  80.                 end
  81.         end
  82. end
  83.  
  84. function drawBuffer(a,b,c,d)
  85.     if not (a and b and c and d) then
  86.         a,b,c,d = 1,1,w,h
  87.     end
  88.     for y = b,d do
  89.         for x = a,c do
  90.             term.setCursorPos(x,y)
  91.             if selcol ~= buffer[x][y][2] then
  92.                 selcol = buffer[x][y][2]
  93.                 term.setTextColor(selcol)
  94.             end
  95.             if selback ~= buffer[x][y][3] then
  96.                 selback = buffer[x][y][3]
  97.                 term.setBackgroundColor(selback)
  98.             end
  99.             write(buffer[x][y][1])
  100.         end
  101.     end
  102. end
  103.  
  104. function drawCards(deck)
  105.     for i=1,#deck do
  106.         deck[i].graphic()
  107.     end
  108.     cards[11](p1deck[1],p1deck[2]).back()
  109.     cards[12](p2deck[1],p2deck[2]).back()
  110. end
  111.  
  112. function startup()
  113.     createBuffer()
  114.     for i=1,w do
  115.         for k = 1,h do
  116.             draw(" ",i,k,_,colors.green)
  117.         end
  118.     end
  119.     for i,v in ipairs(cardPos) do
  120.         deck[#deck+1] = cards[i](v[1],v[2])
  121.     end
  122.     drawCards(deck)
  123. end
  124.  
  125.  
  126. function loop()
  127.     drawBuffer()   
  128.     sleep(1)
  129. end
  130.  
  131.  
  132. startup()
  133. while true do
  134.     loop()
  135. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement