Howdy, Stranger!

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

In this Discussion

Random cards....(Loops continiously)

edited March 2018 in Code Sharing Posts: 196
-- Random Card

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
   rectMode(CENTER)
   t=ElapsedTime+.7
   suit={"♠️","♣️","♥️","♦️"}
   u={" 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9","10"," J"," Q"," K"," A"}
   deck={}                        -- Table for the cards
   createDeck()                   -- Goto createDeck
   getCard=true                   -- Show the first card
   cc = 52                        -- Keep count of the Cards
end

function draw()
   -- Background color
   background(31, 34, 97, 255)
   if getCard then                --card is showing
       getCard=false              
       randomCard()               --Goto the random card fx
       c=v%13+1                   --The card value
       s=math.ceil(v/13)
   end

   -- This is the white card background
   fill(255)
   rect(WIDTH/2,HEIGHT/2,350,500)      --This is the WHT rectangle, location & Size

   -- Font & font size of the suit & value
   font("AmericanTypewriter-Bold") 
   fontSize(160) --size of suit font

    --This is the card suit being printed to the screen
   text(suit[s],WIDTH/2,HEIGHT/2) --Smack dab in the middle

    -- This is the different colors of the suits
    if s==1 then --spades
       fill(0, 0, 0, 255)  
        elseif 
    s==2 then --clubs
       fill(0, 0, 0, 255)  
        elseif 
    s==3 then --hearts
       fill(255, 0, 0, 255)  
        elseif 
    s==4 then --cdia onds
       fill(255, 0, 31, 255)  
   end
    -- This is the card value
   text(u[c],WIDTH/2-85,HEIGHT/2+170)  
   rotate(180)                 --The next number will be rotated 180 degrees
   text(u[c],WIDTH/2-1110,HEIGHT/2-600)  --When rotating the number has to be adjusted
   rotate(180)                 --Aftre the rtation, rotate back 180

    -- Plain text
    font("Baskerville-BoldItalic")
    fontSize(60)
    fill(255, 255, 255, 255)
    text(str, WIDTH/2,HEIGHT-60)  

    -- Card count, this is being constantly updated
    font("Baskerville-BoldItalic")
    fontSize(50)
    fill(255, 255, 255, 255)
    text(cc, WIDTH-875,HEIGHT/2)  

    -- Plain text
    font("Copperplate-Bold")
    fontSize(50)
    fill(255, 255, 255, 255)
    text("Cards left\n in Deck :", WIDTH-855,HEIGHT/2+80)   

  if ElapsedTime>t then
    sound("Game Sounds One:Bell 2")
    getCard=true
   t=ElapsedTime+.7
        if cc==1 then
            setup()
        end
  end
end 



function createDeck()
  str = "Cards"
   for z=1,52 do
       deck[z]=z
   end
end

function randomCard()
-- This plucks a random card then subtracts it from the Main deck
   if #deck>0 then     --if there are any cards left in the deck, then pluck
       d=math.random(#deck)     --pluck at random
       v=deck[d]
       table.remove(deck,d)      --after being plucked, remove it from the deck
   else
        str = "End of Deck"
   end
-- This keeps track of the amount of cards left in the deck
    if cc <= 52 then   --cc means card-count
        cc = cc - 1
    end
end
Tagged:

Comments

  • dave1707dave1707 Mod
    edited March 2018 Posts: 8,464

    Remove this code to stop picking a new card every second. You didn’t say how you want to select the next card or what you wanted to do by creating the loop of selecting cards. Just a suggestion, programs run exactly as you code them to run, not exactly as you want them to run.

      if ElapsedTime>t then
        sound("Game Sounds One:Bell 2")
        getCard=true
       t=ElapsedTime+.7
            if cc==1 then
                setup()
            end
      end
    
  • Posts: 196
    Theirs something else I want to know, when dealing cards automatically, is their a delay FX, in Fuze it's called sleep( ). It would add a nice delay...
  • dave1707dave1707 Mod
    Posts: 8,464

    There’s no delay because draw runs at 60 FPS. You delay things in draw by executing something every so many draw cycles. In the ElapsedTime if statement, increase the .7 to a larger value to slow the deal speed.

  • edited June 11 Posts: 196
    I have here a PGM you've written a while back. It works fine on the I-pad, but since then I started to use the I-phone 6s.the I-pad I had was a 4 or 5, an was slow. The screen resolution has changed. Is there some way I could readjust this ?

    ~~~
    --Deck to Screen
    displayMode(FULLSCREEN)
    supportedOrientations(LANDSCAPE_ANY)

    function setup()
    s={" ♥️"," ♠️"," ♦️"," ♣️"}
    v={" 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9","10"," J"," Q"," K"," A"}
    deck={}
    for z=0,51 do
    table.insert(deck,v[z%13+1]..s[z//13+1])
    end
    end

    function draw()
    background(10, 26, 223)
    fontSize(30)
    fill(255, 255, 255)
    text("Tap\nscreen \nto\nshuffle",WIDTH/2-270,HEIGHT-100)
    for z=0,51 do
    text(deck[z+1],155+(z//13)*150,50+(z%13)*50)
    end
    end

    function shuffle()
    for q=1,100 do
    a,b=math.random(52),math.random(52)
    deck[a],deck[b]=deck[b],deck[a]
    end
    end

    function touched(t)
    if t.state==BEGAN then
    shuffle()
    end
    end
    ~~~

    The other PGM"s all I had to do was move text & rect"s around. This is a little tougher, all I can see is 8's down to 2's....
  • Posts: 196
    Just a quick question ; what does the '%' sign stand for ? What does the '//' double slash stand for ?.....table.insert(deck,v[z%13+1]..s[z//13+1])......
  • dave1707dave1707 Mod
    Posts: 8,464

    @kendog400 See if this fits any better. If it doesn’t, then in the draw function change the values in the text statement. Change the 120, 80, 40, or 30 values to different values and see how they alter the display on your phone. As for the % and //, the % is a mod division. It give you just the remainder of a divide. The // is an integer division. It gives you the whole number of a division, dropping anything after the decimal point.

    --Deck to Screen
    displayMode(FULLSCREEN)
    
    function setup()
        s={" ♥️"," ♠️"," ♦️"," ♣️"}
        v={" 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9","10"," J"," Q"," K"," A"}
        deck={}
        for z=0,51 do
            table.insert(deck,v[z%13+1]..s[z//13+1])
        end
    end
    
    function draw()
        background(10, 26, 223)
        fontSize(30)
        fill(255, 255, 255)
        text("Tap screen to shuffle",WIDTH/2,HEIGHT-50)
        for z=0,51 do
            text(deck[z+1],WIDTH/2-120+(z//13)*80,40+(z%13)*30)
        end
    end
    
    function shuffle()
        for q=1,100 do
            a,b=math.random(52),math.random(52)
            deck[a],deck[b]=deck[b],deck[a]
        end
    end
    
    function touched(t)
        if t.state==BEGAN then
            shuffle()
        end
    end
    
  • Posts: 196
    This runs good, but I tried to put break at the end of the draw fx ( )..
    The PGM keeps running after the deck is exausted... I was trying to get the PGM to stop after the deck of cards run out....I think i"m using the word break, but that applies to other PGM languages, not codea. Does anyone know the trick ?...
  • dave1707dave1707 Mod
    Posts: 8,464

    The draw function runs at 60 frames per second. You’re not supposed to stop it. If you do, then nothing is drawn on the screen.

  • edited July 1 Posts: 196
    Is there a pause fx() ? Or some tutorial / dusscion on pauseing ?
  • dave1707dave1707 Mod
    Posts: 8,464

    You don’t pause the code. The only thing you should do is to write code so you don’t execute the code that update something. Here’s a simple example of pausing the count and sprite movement.

    displayMode(FULLSCREEN)
    
    function setup()
        fill(255)
        count=0
        xx=0
        x,y=200,200
    end
    
    function draw()
        background(0)
        text("tap screen to pause / unpause",WIDTH/2,HEIGHT-100)
        text("count = "..count,WIDTH/2,HEIGHT/2)
        sprite(asset.builtin.Planet_Cute.Character_Horn_Girl,x,y)
    
        -- this increments the count value and the sprites x,y position. 
        -- when you want to pause the count and sprite position,
        --      you just dont update their values. 
        if xx==0 then
            count=count+1
            x=(x+3)%WIDTH
            y=(y+3)%HEIGHT
        end
    end
    
    function touched(t)
        if t.state==BEGAN then
            if xx==1 then
                xx=0    -- set to 0 to update values
            else
                xx=1    -- set to 1 to skip the update code
            end
        end
    end
    
  • Posts: 196
    At the top of this post, is the PGM that deals 1 card at a time. But when it gets to 52, it keeps going and keeps plucking cards. I was wondering if I could put a while loop to make it stop after the deck runs out...I can't seem to post PGMs longer than 50 lines...because I'm using an i-ph 6s. If I had an i-pad I could post the PGM...but it's already posted and works fine, i would just like to make a couple of adjustments....thanks to anyone who could help...
  • dave1707dave1707 Mod
    Posts: 8,464

    Comment out the code I show below starting with if cc==1 then

        if ElapsedTime>t then
            sound("Game Sounds One:Bell 2")
            getCard=true
            t=ElapsedTime+.7
    
            --if cc==1 then
                --setup()
            --end
        end
    end
    

    Then change the code I show below starting with if cc> 0 then

    function randomCard()
        -- This plucks a random card then subtracts it from the Main deck
        if #deck>0 then     --if there are any cards left in the deck, then pluck
            d=math.random(#deck)     --pluck at random
            v=deck[d]
            table.remove(deck,d)      --after being plucked, remove it from the deck
        else
            str = "End of Deck"
        end
    
        -- This keeps track of the amount of cards left in the deck
        if cc>0 then
        --if cc <= 52 then   --cc means card-count
            cc = cc - 1
        end
    end
    
  • Posts: 196
    Thanks...
  • Posts: 196
    Everything works except 1 thing,
    Right here in this line in the PGM, keeps looping ::
    ~~
    if ElapsedTime>t then
    sound("Game Sounds One:Bell 2")
    getCard=true
    t=ElapsedTime+.7
    ~~
    I keep hearing the sound after the PGM stops......
    Should I put a for or a while loop so it would stop when the PGM stops ?
  • dave1707dave1707 Mod
    Posts: 8,464

    Make this change. Add cc>0 to the if statement.

        if ElapsedTime>t and cc>0 then
            sound("Game Sounds One:Bell 2")
            getCard=true
            t=ElapsedTime+.7
        end
    end
    
Sign In or Register to comment.