Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. --# Main.lua
  2.  
  3. game = {}
  4.  
  5. --[[ Index 1-52 contain deck. 101-116 contain player hand
  6. Cards are represented as integer between 1 and 52. Rank is
  7. known with modulus 13, suit by modulus 4. Index 201-252
  8. hold the text objects for each card. Index 301-317 contain
  9. positioning information for the 16 cards with 17 being the
  10. deck itself
  11. ]]
  12.  
  13. if not love.graphics.getFont() then
  14.    love.graphics.setFont(love.default_font, 12)
  15. end
  16.  
  17. function newgame()
  18.  
  19.     for i = 1,52 do
  20.         game[i] = i
  21.         game[i+200] = love.graphics.newText(love.default_font, i)
  22.     end
  23.  
  24.     for i = 101,116 do
  25.         game[i] = nil
  26.     end
  27.  
  28.     shuffle()
  29.  
  30.     deal()
  31. end
  32.  
  33. function shuffle()
  34.     for i = 52,2,-1 do
  35.         local j = math.random(52)
  36.         game[i], game[j] = game[j], game[i]
  37.     end
  38. end
  39.  
  40. function deal()
  41.     if game[1] and not game[116] then
  42.         local i = #game
  43.         for j = 101, 116 do
  44.             if not game[j] then
  45.                 game[i], game[j] = nil, game[i]
  46.                 break
  47.             end
  48.         end
  49.     end
  50. end
  51.  
  52. function move(a,b)  --move a to b. Paramets should be index value
  53.     local c, d = game[a], game[b]
  54.     if a - b == 3 or a - b == 1 then -- compare index
  55.         if c%4 == d%4 or c%13 == d%13 then -- compare value
  56.             game[b] = game[a]
  57.             table.remove(game, a)
  58.         end
  59.     end
  60. end
  61.  
  62. function love.update(dt)
  63.     setPositions(cardPositions)
  64. end
  65.  
  66. local cardColors = {    {100,0,0}, {0,100,0}, {0,0,100}, {100,100,100}  }
  67. local cardPositions = {  width = love.graphics.getWidth(),
  68.      height = love.graphics.getHeight()
  69. }
  70. cardPositions.cw = cardPositions.width*0.125
  71. cardPositions.ch = cardPositions.width*1.4
  72. function setPositions(t)
  73.     local width = love.graphics.getWidth()
  74.     local height = love.graphics.getHeight()
  75.     if t.width ~= width or t.height ~= height then --checks if change needs be made, else do nothing
  76.         local v = {width = width, height = height}
  77.         local cw = width*0.125  --card width
  78.         local ch =  cw*1.4      --card height
  79.         local x, y
  80.         v[17] = {1*cw, 2*ch}    --deck position
  81.  
  82.         for a = 0,1 do
  83.             y = a*ch
  84.             for b = 1,8 do
  85.                 x = b*cw
  86.                 table.insert(v, {x,y})
  87.             end
  88.         end
  89.         v.cw = cw
  90.         v.ch = ch
  91.         return v
  92.     end
  93. end
  94.  
  95. function love.draw()
  96.     for i = 101, 116 do
  97.         local c = game[i]%4+1   -- color
  98.         local v = game[i]       -- value
  99.         local t = game[200+v]           -- text
  100.         local p = cardPositions[i-100]  -- positions
  101.             local x = p[1]
  102.             local y = p[2]
  103.         love.graphics.setColor(50,50,50)
  104.         love.graphics.rectangle("fill", x, y, p.cw, p.ch)   -- card front
  105.         love.graphics.setColor(cardColors[c])
  106.         love.graphics.print(t, x + p.cw*0.5, y + p.ch*0.5)  -- card text
  107.     end
  108.     love.graphics.setColor(50,50,50)
  109.     love.graphics.rectangle("fill", cardPositions[17][1], cardPositions[17][2], cardPositions.width,
  110.         cardPositions.height)
  111. end
  112.  
  113. function love.load()
  114.     newgame()
  115. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement