A deck of standard playing cards.

That site is good one, thanks Dave. It’s my bed time so I’ll take a serious look at it tomorrow.

Thanks again Dave I now have a few card sprites among my ASSETS and more. light has been cast upon my iPad in general and Codea in particular. Still begs the question why Codea hasn’t got a deck of cards pre loaded.
See you around - Steep

It’s not enough to just have a pack of cards. You need a set of functions that do things like shuffling, dealing, stacking (for games like patience), handling screen touches, etc.

As I found when I explored this, it gets hard, and I never finished building a card library. (But if you’d like to volunteer…)

As to why it isn’t included, how much do you expect for $10? :slight_smile:

Ignatz- Here again I find your comments a bit abrasive.
I’ve downloaded your poker hand demo., it’s great, way above my head of course but I keep going back to it. It’s not far off being a game in itself and some of its functions are specIfic lessons in card handling. I have also downloaded the two card games that you have produced as one load of coding, but it seems to have picked up a bug in the course of transfer. Copying and pasting from CoolCodea seems to be a bit hit and miss.
I choose to play around with playing cards as a basis for learning to code because a deck of cards has been a gaming tool for ever and a day. There is a particular soliaire game that my wife likes called Kings High that her mother used to play. I got it to work on her Windows lap top, using BBC Basic for Window and my aim is to set it up on the iPad. She can then have this iPad and I can move up to the latest version.

I wasn’t trying in any way to be abrasive, but you did ask why Codea doesn’t have a pack of cards. The answer is a very limited budget - and I think they did amazing things with that budget.

I wouldn’t normally recommend a card game for learning to code, because it’s harder than it looks, as I found. Creating the pack is just the first challenge. (However, I understand your motivation. I would like the latest iPad as well…)

I’m sorry about the code errors. I do provide a link to a full set of code, at the end of the second post about card playing, and that is what I would download. Wordpress tends to mangle the code I embed in the post itself.

Ok Ignatz I much appreciate that what you do here is within the realms of Codea which as you say is a remarkable App. I’ll try again with the download of the 21 and patience games but it is a lot of code. I’m sure that much of that code will go a long way to help my understanding of what goes on between the iPad, Lua and Codea.

It might be easier for you if you make a simplified set of your own cards, just rectangles with a number or letter and suit printed on them. The fancy cards can come later.

Also, I did that when I was fairly new at Codea. I could probably improve the code now.

@Steep Maybe you could use this until you get a better deck of cards.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    rectMode(CENTER)
    suit={"??","??","??","??"}
    val={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}
    cards={}    -- table for cards
    for z=1,4 do    -- suits
        for y=1,13 do   -- values
            str=string.format(" %s\
%s",val[y],suit[z]) -- create value and suit
            table.insert(cards,str) -- insert value and suit into the table
        end
    end
end

function draw()
    background(0)
    stroke(255, 95, 0, 255)
    strokeWidth(5)
    count=0
    -- draw cards 
    for z=1,4 do
        for y=1,13 do
            count=count+1
            fill(255)
            rect(y*75,z*100+200,60,80)
            fill(0,0,0)
            if z==2 or z==4 then
                fill(255,0,0)
            end
            text(cards[count],y*75,z*100+200)
        end
    end
end

@dave1707 - whichever way I did it, I would start by creating a table of sprites, rather than building them from scratch in each draw cycle

@Ignatz I’m just giving suggestions. I don’t think any card program needs to run so fast that drawing the cards each time slows it down that much. If I was after a deck of cards, I would get them from the link I show farther up in this discussion by copying the 52 card images.

Here’s some code that will create 52 sprites in the Documents folder. They are simple images for a deck of cards that are shown in the program above. The sprite names are card01 thru card52. Once the setup function is run, there isn’t any need for it anymore.

EDIT: It takes a few seconds to create the sprites.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    rectMode(CENTER)
    img=image(60,80)
    suit={"??","??","??","??"}
    val={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}
    count=0
    
    -- this only needs to be run once
    -- it creates 52 images in the Documents folder
    for z=1,4 do    -- suits
        for y=1,13 do   -- values
            str=string.format(" %s\
%s",val[y],suit[z]) -- create value and suit
            setContext(img)
            stroke(255, 95, 0, 255)
            strokeWidth(5)
            fill(255)
            rect(30,40,60,80)
            fill(0,0,0)
            if z==2 or z==4 then
                fill(255,0,0)
            end
            text(str,30,40)
            setContext()
            count=count+1
            str1=string.format("%s%02d","Documents:card",count)
            saveImage(str1,img)
        end
    end
end

function draw()
    background(0)
    count=0
    -- show the 52 cards 
    for z=1,4 do
        for y=1,13 do
            count=count+1
            str=string.format("Documents:card%02d",count)
            sprite(str,y*75,z*100+150)
        end
    end
end

@dave1707 - now write some card handling functions! :smiley:

Ok chaps that’s enough for now. Those snips of card creating code will keep me going for a while. My wife’s patience game and my new iPad is a long way off.

Incidentally, the Knuth-Fisher-Yates algorithm for shuffling an array is generally acknowledged as the most efficient method. It’s quite straightforward to implement in lua.

Dave - The two progs. that you provide above are a tutorial in themselves. Not too many lines of code and a functional playground that any novice who has played around with ‘basic’ coding can have fun with. All done within the realms of Codea.
LoopSpace I found a reference to Knuth shuffle here in TalkCodea I’ll have a go at stitching it into Dave’s code.

Any card game requires that you pick out a single card. It becomes very difficult to get the suits and colours right if you are drawing that card each time. With the sprites of course no problem. A sprite is a sprite for all that.

I have loaded a Table with the numbers 1 to 52 and withdrawn them in a random fashion. Could someone please tell me what has to be done to stop the screen flickering.

function setup()
Tab = {}
    for no = 1,52 do
        table.insert(Tab,no)
        no = no+1
    end
  tmr = 0   -- initiate timer
  nos = 52 -- number of numbers in Table (Tab)
  x = 80
  y=700
end

function draw()   
tmr=tmr+1
    if tmr>40 and x<=380 and nos>0 then
        n=math.random(nos)
        p = Tab[n]
        print (p)
        fontSize(48)
        fill(256)
        text ((p),x,y)
        table.remove(Tab,n)
        nos=nos-1
        x=x+100
        if x>380 then 
        y=y-50
        x=80
     end
tmr=0
end    
end

@Steep you need to add background(0) at the start of draw()

@Steep If you don’t like that, then put backingMode(RETAINED) before setup(). Remove the background(0) if you do.

@Steep You can also get random card numbers this way.


backingMode(RETAINED)
supportedOrientations(PORTRAIT_ANY)

function setup() 
    tab={}
    for z=1,52 do
        tab[z]=z
    end
end

function draw()
    if #tab>0 then
        n=math.random(#tab)
        text(tab[n],200,#tab*18+50)
        table.remove(tab,n)
    end
end