Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main.lua
- game = {}
- --[[ Index 1-52 contain deck. 101-116 contain player hand
- Cards are represented as integer between 1 and 52. Rank is
- known with modulus 13, suit by modulus 4. Index 201-252
- hold the text objects for each card. Index 301-317 contain
- positioning information for the 16 cards with 17 being the
- deck itself
- ]]
- if not love.graphics.getFont() then
- love.graphics.setFont(love.default_font, 12)
- end
- function newgame()
- for i = 1,52 do
- game[i] = i
- game[i+200] = love.graphics.newText(love.default_font, i)
- end
- for i = 101,116 do
- game[i] = nil
- end
- shuffle()
- deal()
- end
- function shuffle()
- for i = 52,2,-1 do
- local j = math.random(52)
- game[i], game[j] = game[j], game[i]
- end
- end
- function deal()
- if game[1] and not game[116] then
- local i = #game
- for j = 101, 116 do
- if not game[j] then
- game[i], game[j] = nil, game[i]
- break
- end
- end
- end
- end
- function move(a,b) --move a to b. Paramets should be index value
- local c, d = game[a], game[b]
- if a - b == 3 or a - b == 1 then -- compare index
- if c%4 == d%4 or c%13 == d%13 then -- compare value
- game[b] = game[a]
- table.remove(game, a)
- end
- end
- end
- function love.update(dt)
- setPositions(cardPositions)
- end
- local cardColors = { {100,0,0}, {0,100,0}, {0,0,100}, {100,100,100} }
- local cardPositions = { width = love.graphics.getWidth(),
- height = love.graphics.getHeight()
- }
- cardPositions.cw = cardPositions.width*0.125
- cardPositions.ch = cardPositions.width*1.4
- function setPositions(t)
- local width = love.graphics.getWidth()
- local height = love.graphics.getHeight()
- if t.width ~= width or t.height ~= height then --checks if change needs be made, else do nothing
- local v = {width = width, height = height}
- local cw = width*0.125 --card width
- local ch = cw*1.4 --card height
- local x, y
- v[17] = {1*cw, 2*ch} --deck position
- for a = 0,1 do
- y = a*ch
- for b = 1,8 do
- x = b*cw
- table.insert(v, {x,y})
- end
- end
- v.cw = cw
- v.ch = ch
- return v
- end
- end
- function love.draw()
- for i = 101, 116 do
- local c = game[i]%4+1 -- color
- local v = game[i] -- value
- local t = game[200+v] -- text
- local p = cardPositions[i-100] -- positions
- local x = p[1]
- local y = p[2]
- love.graphics.setColor(50,50,50)
- love.graphics.rectangle("fill", x, y, p.cw, p.ch) -- card front
- love.graphics.setColor(cardColors[c])
- love.graphics.print(t, x + p.cw*0.5, y + p.ch*0.5) -- card text
- end
- love.graphics.setColor(50,50,50)
- love.graphics.rectangle("fill", cardPositions[17][1], cardPositions[17][2], cardPositions.width,
- cardPositions.height)
- end
- function love.load()
- newgame()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement