Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

A deck of standard playing cards.

As a newbie I've looked around to find sprites/images for a standard deck of playing cards. No success not even a sprite for a single 'heart' , 'club', 'diamond' or 'spade' . There are so many uses for playing cards I can not understand why this should be.
Is there something that I am missing ?

«1

Comments

  • Not only is this doable within Codea, the amazing Ignatz already did a tutorial on it ... https://coolcodea.wordpress.com/2013/04/22/39-working-with-playing-cards/

  • edited February 2015 Posts: 1,595

    Edit: Ninja'd by epicurus. Check out opengameart for sprites as that's all free.

  • A simple google search for "Playing Card Sprite Sheet" will do the trick; here is a
    Good one http://jgauthie.imgd.ca/IMG-250/memory_game/images/deck.png

  • Jmv38Jmv38 Mod
    Posts: 3,295

    you can use these unicode characters and sprite them with size and position you want 

  • Posts: 61

    Thanks chaps. Epicurus has given me the link I wanted. It looks as though Ignatz is going to take playing cards a bit further. As a newbie there is a lot to go at with what he has done so far.
    It points to another issue with regard to source of knowledge.
    As a newbie I tend to stick within the Codea App in my search for info. I must say that CoolCodea seems to be an excellent site and I must remember to search it with regard to any question I have.
    Thanks again. - Steep.

  • If you deny yourself coolcodea, you really would be missing out. If I've done anything to change your mind it's a good day's work! Codea's impossible to learn for coding newbies just from the reference.

  • Jmv38Jmv38 Mod
    edited February 2015 Posts: 3,295

    Codea is Codea AND this forum! That is what makes it so special.

  • Posts: 1,976

    You can actually use Emojis and text() to draw icons for cards. Here's the ones you would need: ♠️(spade) ♥️ (heart) ♣️ (club) and ♦️ (diamond), they're found under "symbols"

  • dave1707dave1707 Mod
    Posts: 7,807

    The ♠️♥️♣️♦️ symbols are also on the Codea keyboard. Just touch the smiley face on the lower left of the keyboard. Then press the !?# key at the bottom right. Scroll to the right and you'll see the symbols.

  • IgnatzIgnatz Mod
    edited February 2015 Posts: 5,396

    I used emojis for the suit symbols and also the king, queen and jack, which avoided the need to import images. My post above explains how to do this.

  • I have a playing deck class if you'd like it

  • Posts: 61

    Dave and SkyTheCoder - Interested to find that the card suits (♠️♣️♦️♥️) are on the Codea keyboard. Wonder why and how they are used ?
    Although they were useful in this post.
    It would seem that Codea smiley's are automatically switched to Codea Talk keyboard.
    Invad - What can you do with your 'playing card class' ?
    Steep

  • dave1707dave1707 Mod
    Posts: 7,807

    @Steep To use them, just do

    print("♥️")
    
    text("♦️",200,200)
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    Finding suit icons is only one of several challenges in creating a deck of realistic playing cards.

    Laying out the numbers and icons on the cards in the right patterns, and mirroring them top and bottom, and finding suitable pictures for royalty, took me quite some time.

  • Posts: 61

    Ignatz - That is appreciated. Hoping that Invad's 'playing card class' has done all the work and presents each card in a way that makes it easier to manipulate. I suspect that the next part of your CoolCodea 'working-with-playing-cards' may well do that.
    Dave - ♠️♥️♣️♦️ seem to be accessible from the basic iPad keyboard.

  • Posts: 61

    I've got myself a single playing card (Jack of spades) cut from a set of 3 images that cover the whole deck. All 52 cards could be cut from these three free images but it would be a bit of a fiddle for a newbie. In any event having done the work could the individual sprites be distributed to the Codea community ? and it begs the further question why hasn't it been done before ?
    Playing card sprites is something that is sadly missing from Codea.

  • dave1707dave1707 Mod
    Posts: 7,807

    @Steep An easier way is to go to a web site that shows decks of cards. Some allow you to view each individual card. While viewing each card, you can save the image to the photo apps file. From Codea, you can go to the Dropbox or Documents folder and do an "add from photos" for each card image. Once the image is in one of those folders, you can use them in Codea with the sprite command.

  • Posts: 61

    Ok Dave, I'm with you on that. The images I have were taken from the web, but they were a collection of cards, 3 images to cover the full deck and I used a programme of yours to cut one or two individual picture cards. I haven't seen a site where you could download a full set of individual cards.
    The question remains where is such a set of cards available to Codea users ? one sprite/card (52 in all) that users can use to create relatively simple card games that can be distributed to Codea users.

  • dave1707dave1707 Mod
    Posts: 7,807

    @Steep Try this site. Scroll down to the Ace of clubs. Tap on it then tap on the "see all 157 photos". As you look at each card, long press on it, you'll get an option to save the image. It will go into the photo app file. From the Codea Dropbox or Documents folder, you can add each image to the folder. It can then be used with the sprite command.

    http://juggergnost.hubpages.com/hub/playing-cards-clip-art#
    
  • Posts: 61

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

  • Posts: 61

    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

  • IgnatzIgnatz Mod
    Posts: 5,396

    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? :)

  • Posts: 61

    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.

  • IgnatzIgnatz Mod
    Posts: 5,396

    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.

  • Posts: 61

    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.

  • IgnatzIgnatz Mod
    Posts: 5,396

    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.

  • dave1707dave1707 Mod
    Posts: 7,807

    @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\n%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
  • IgnatzIgnatz Mod
    Posts: 5,396

    @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

  • dave1707dave1707 Mod
    Posts: 7,807

    @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.

  • dave1707dave1707 Mod
    edited February 2015 Posts: 7,807

    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\n%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
  • IgnatzIgnatz Mod
    Posts: 5,396

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

  • Posts: 61

    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.

  • Posts: 61

    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.

  • Posts: 61

    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.

  • edited February 2015 Posts: 61

    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
    
  • dave1707dave1707 Mod
    Posts: 7,807

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

  • dave1707dave1707 Mod
    edited February 2015 Posts: 7,807

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

  • dave1707dave1707 Mod
    edited February 2015 Posts: 7,807

    @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
  • IgnatzIgnatz Mod
    Posts: 5,396

    or like this

    cards={}
    for i=1,52 do cards[i]={i,math.random()} end
    table.sort(cards, function(a,b) return a[2]<=b[2] end)
    for i=1,52 do print(cards[i][1]) end
    
  • dave1707dave1707 Mod
    Posts: 7,807

    @Ignatz Nice use of table sort.

  • dave1707dave1707 Mod
    Posts: 7,807

    Or you can do this.

        tab={} for z=1,52 do tab[z]=z end
        while #tab>0 do n=math.random(#tab) print(tab[n]) table.remove(tab,n) end
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    You realise we're going to get into trouble with our resident mathematician for not using the sort method with three mathematicians' names attached? ;)

  • Posts: 61

    Thanks chaps it's back to the drawing board. It's hard to find the alternatives that you suggest by reading the 'manual' . Most patience games need a shuffled pack to start with.

  • dave1707dave1707 Mod
    Posts: 7,807

    @Steep Here's something for you to look over. Calling the function shuffle(52) creates a table with the numbers 1 to 52 in it. Calling shuffle(0) returns a random number from the table and the number of cards left in the deck. When the number left is 0, then the deck is reshuffled. Tap the screen to show a random number from the deck.


    function setup() fontSize(40) shuffle(52) card="" end function draw() background(0) fill(255) text("Tap screen for next card",WIDTH/2,HEIGHT/2+250) text(card,WIDTH/2,HEIGHT/2) text(shuf,WIDTH/2,HEIGHT/2-200) end function touched(t) if t.state==BEGAN then card,left=shuffle(0) -- card number and remaining card count if left==0 then -- no cards left, reshuffle deck shuffle(52) end end end function shuffle(size) if size>0 then -- size >0, create table of 52 numbers cardTab={} for z=1,size do cardTab[z]=z end shuf="shuffled deck" elseif #cardTab>0 then -- get a random number from table shuf="" n=math.random(#cardTab) -- create random number local card=cardTab[n] -- get card number table.remove(cardTab,n) -- remove it from table return card,#cardTab -- return card number and number of remaining cards end end
  • Posts: 61

    Enjoyed the code Dave. Added to my learning curve.
    I have your simple cards in my Assets:documents, so I changed the 'card' value from string to 0 (zero) in the setup function. I then added the two lines :-
    str=string.format("Documents:card%02d",card)
    sprite(str,450,400)
    to the Draw function. This gives number/card relationship.
    Next step is to put face down pack and touch pack to deal 4 cards top of screen.
    these cards will be removed under set conditions, when move is complete 4 more cards are dealt.

  • Posts: 61

    This code will produce a display of the numbers 1 to 52 in random and in in four columns. I have simple cards as sprites in my Assets/Documents store thanks to Dave. They are called(card01 to card52). However when I try to swap the numbers with the sprites the randomness breaks down. I have no idea why.
    Any help please ?

    -- Code to display 52 random numbers.
    backingMode(RETAINED)
    
    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 = 280
      y = 700
      fontSize(48)
     fill(256)
    end
    
    function draw()  
        tmr=tmr+1 
        if tmr>40 and x<=580 and nos>0 then
            tmr=0
            local n=math.random(nos)        
            text((tab[n]),x,y).                                          -- } I have replaced this line with
         -- str=string.format("Documents:card%02d",n)  } this line and the line below.
         -- sprite(str,x,y)                                                   }The randomness breaks down
            table.remove(tab,n)      
            nos=nos-1
            x=x+100
              if x>580 then 
              y=y-50
              x=280
              end
         end        
    end
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    I think it should be

    str=string.format("Documents:card%02d",tab[n])
    

    but it doesn't make much sense to keep loading all 52 images from disk, 60 times a second.

    I would load them all into a table of sprites in setup, and show the relevant sprite in draw, eg

    sprite(TableOfSprites[n],x,y)
    
  • Posts: 61

    Thanks Ignatz. It's the way you think. Putting tables and the Draw function together is something I find a bit tricky. Not sure where I will go when I investigate touch to initiate the deal. It just seemed to me that having got the numbers to do what I want and sprites where I wanted them, then I should be able to make the switch and check that things were ok up to this point.
    My way of thinking is not always the best way for Lua, Codea and the iPad.

  • IgnatzIgnatz Mod
    Posts: 5,396

    You'll get used to it, like I did, just stick at it.

    You should also try to avoid hard coding anything. So if you want your pack to be dealt starting at (say) (50, HEIGHT-50), with a gap of (50,75) pixels between cards, then I might write it like this (untested)

    --in setup
    DealStartPos=vec2(50, HEIGHT-50)
    DealSpacing=vec2(50,75)
    
    --write this function to give you the screen position for a card
    function DealPos(col,row)
        local x=DealStartPos.x+(col-1)*DealSpacing.x
        local y=DealStartPos.y+(row-1)*DealSpacing.y
        return x,y
    end
    
    --then in draw
    --we want to draw card `n` in col `c`, row `r`
    local x,y=DealPos(c,r)
    sprite(TableOfSprites[n],x,y)
    

    Using this approach, you can define your playing surface and card positions, and if you ever have to change anything, it is only in one place, in setup.

Sign In or Register to comment.