Necessary spritesheet: https://opengameart.org/content/playing-cards-0
I’ve tried to code this decently well. I’m not totally happy: The Field.touched(touch) and Field.move functions took me over an hour… but it went pretty well. It looks a lot better than the first time i did this, I actually commented a lot of everything, it was done in less than 3 hours, and it will fit in your scratchpad
Although, I forgot to program the score hahaha
Oh! Another thing that i forgot to put in is instructions. Basically, you select a card, and then you move it to the left one or three indexes. And if suit or rank matches, that makes a valid move! sorry for not animating it, but I suppose I can work on that. I just got so excited and posted this as soon as it was playable
-- Created by Tommy
-- Second version of this game (first version is messier code, and took me two weeks. This took two and a half hours to make)
-- Note that this requires you to download a single spritesheet for the cards to play the game. i do not rememer where i got it from originally (\\_?_/) shrugs
-- I named it sheet, but you can rename it in the Field.draw() function, at line 63
displayMode( FULLSCREEN )
function setup()
Deck.setup()
Field.deal()
print(Field[1], "suit "..Field[1]/4, "rank ".. Field[1]/13)
end
function draw()
background(40,40,50,1)
Deck.draw()
Field.draw()
end
function touched(touch)
Deck.touched(touch)
Field.touched(touch)
end
-- Timezone GMT-10 Hawaii Time, Date format follows MM/DD/YYYY
-- Started 4:34 PM 09-02-2017 Saturday
-- Last Modified 7:16 PM 09-02-2017
Deck = {} --52 length array, holds cards in deck
Field = {} --16 length array, holds cards in play
Score = {0,0,0,0,0} --ten thousands, thousands, hundreds, tens, ones places
local numberOfRanks = 13
local numberOfSuits = 4
--[[ ^Cards are represented by number values of 1-52. Rank and suit are determined with modulus 13 and modulus 4, if you change the spritesheet, make sure to change these variables.
]]--
local width, height = 80,112 -- Card Width and Card Height. Note they use a 5:7 ratio, in real cards this would be 2.5" x 3.5"
-- used in Deck.draw, Deck.touched, Field.draw, Field.touched
--[[ If your device screen is too small or you simply don't like the way cards are draw, change the lookup table for positioning cards, has two rows. Feel free to modify
]]--
local position = {
vec2(50,668), vec2(150,668), vec2(250,668), vec2(350,668),
vec2(450,668), vec2(550,668), vec2(650,668), vec2(750,668),
vec2(50,338), vec2(150,338), vec2(250,338), vec2(350,338),
vec2(450,338), vec2(550,338), vec2(650,338), vec2(750,338) }
--positions the deck of cards
local deckposition = vec2(WIDTH-100, 168)
function Deck.setup() --called in setup()
for i = 1, 52 do
Deck[i] = i
end
local j
for i = 52, 2, -1 do
j = math.random(52)
Deck[i], Deck[j] = Deck[j], Deck[i]
end
end
function Field.draw() --called in draw()
local mesh=mesh()
for i,v in ipairs(Field) do --index determines position
local rank = (1/numberOfRanks)*(v % numberOfRanks)
local suit = (1/numberOfSuits)*(v % numberOfSuits)
mesh.texture = "Project:sheet"
mesh:addRect(position[i].x, position[i].y, width, height)
mesh:setRectTex(i, rank, suit, 1/numberOfRanks, 1/numberOfSuits)
end
mesh:draw()
end
function Field.deal() --called in Deck.touched(touch)
if #Field < 16 and #Deck > 0 then
Deck[#Deck], Field[#Field + 1] = nil, Deck[#Deck]
end
end
function Deck.draw() --called in draw()
local mesh = mesh()
mesh:addRect(deckposition.x, deckposition.y, 80, 112)
if #Deck > 0 then
mesh:setColors(0,168,0)
else
mesh:setColors(0)
end
mesh:draw()
end
function Deck.touched(touch) --called in touched(touch)
if touch.x > deckposition.x - width*.5 and touch.x < deckposition.x + width*.5
and touch.y > deckposition.y - height*.5 and touch.y < deckposition.y + height*.5
and touch.state == ENDED then
Field.deal() -- is this bad practice to handle field.deal inside deck.touched?
end
end
--[[ Field.move is the game rules, index1 must be 1 or 3 higher than ind2, and either ranks or suits must be equal. During a move, the card originally at index 2 is elimnated from the game
all other cards will move up
Here's a gamemode idea: Rank up, where cards change ranks based on your moves
]]--
function Field.move(index1, index2) --called in Field.touched(touch)
print "Starting move"
if index1 - index2 ~= 3 and index1 - index2 ~= 1 then
print "Illegal Move: difference between indexes must be equal to 1 or 3"
else
local rank1, suit1 = Field[index1]%numberOfRanks, Field[index1]%numberOfSuits
local rank2, suit2 = Field[index2]%numberOfRanks, Field[index2]%numberOfSuits
if rank1 ~= rank2 and suit1 ~= suit2 then
print "Illegal Move. Either the card suits or the card ranks must match."
else
Field[index2] = Field[index1]
table.remove(Field, index1)
end
end
end
function Field.touched(touch) --called in touched(touch)
-- This function took an hour. Once I threw out this code and started over, it was done in ten minutes
for i,v in ipairs(position) do
if touch.x > v.x - width*.5 and touch.x < v.x + width*.5
and touch.y > v.y - height*.5 and touch.y < v.y + height*.5 then
if touch.state == BEGAN then
index1 = i
break
elseif touch.state == ENDED then
index2 = i
if index1 and index2 and index1 ~= index2 then
Field.move(index1, index2)
end
print(index1, index2)
index1, index2 = nil, nil
break
end
end
end
end
Could someone give me sdvice on making an instructions ofr this game? I’ve never had to do that before.