Howdy, Stranger!

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

text() to screen, not print() to compiler

Can anyone look at this and see whats wrong, trying to get the print out to the screen not the compiler.
when I change the print to text, I dont see anything. Why ?

--Press "h" for hit, "s" for stay, 
--and "a" to play again. 
--It's in portrait mode

supportedOrientations(PORTRAIT_ANY)

function setup()
    showKeyboard()
    output.clear()
    player,dealer={},{}
    for z=1,2 do
        table.insert(player,math.random(11))
        table.insert(dealer,math.random(11))
    end   
    showPlayer(1)
end

function draw()
    background(0)
    fill(255)
    if ans=="s" then
        output.clear()
        showDealer()
        showPlayer(2)
        showWinner()        
    elseif ans=="h" then
        output.clear()
        table.insert(player,math.random(11))
        showPlayer(1)
    elseif ans=="a" then
        setup()
    end
    ans=""        
end

function showDealer()
    fill(233, 233, 233, 255)   --added 
    fontSize(70)                    --added 
    font("Georgia-BoldItalic")--added 

    dealerSum=0
    for a=1,#dealer do
        dealerSum=dealerSum+dealer[a]
    end
    while dealerSum<17 do
        dealerSum=0
        table.insert(dealer,math.random(11))
        for a=1,#dealer do
            dealerSum=dealerSum+dealer[a]
        end
    end
    text("The dealer has a total of "..dealerSum)       --changed print to text
    text("from these cards "..table.concat(dealer," "))--changed print to text
end

function showPlayer(a)
    fill(233, 233, 233, 255)   --added 
    fontSize(70)                    --added 
    font("Georgia-BoldItalic")--added 

    playerSum=0
    for a=1,#player do
        playerSum=playerSum+player[a]
    end
    text("You have a total of "..playerSum)               --changed print to text
    text("from these cards "..table.concat(player," "))--changed print to text
    if playerSum>21 then
        showWinner()
    elseif a==1 then
        print("\n\nDo you want to stay(s) or hit(h)") 
    end   
end

function showWinner()
    fill(233, 233, 233, 255)  --added 
    fontSize(70)                    --added 
    font("Georgia-BoldItalic")--added 

    if playerSum>21 then
        text("Dealer wins!")  --changed print to text
    elseif dealerSum>21 then
        text("You win!")       --changed print to text
    elseif playerSum>dealerSum then
        text("You win!")       --changed print to text
    else
        text("Dealer wins!") --changed print to text
    end
    text("Play again?(a)")   --changed print to text
end

function keyboard(k)
    ans=k
end

Comments

  • I'm not at my iPad to check this, but I notice that you're concatenating the strings right in the text() statement. I don't know if that's it, but you might try concatenating them first and then putting them in the text() statement.

  • dave1707dave1707 Mod
    Posts: 8,456

    @kendog400 You’re not telling the text statement where on the screen to display. Anytime you have trouble with a statement, try looking at the built in reference to see how to use that statement. An easy way is to double tap on the word text and select reference from the popup.

  • dave1707dave1707 Mod
    edited October 2017 Posts: 8,456

    @kendog400 The code you show above is what I wrote for you back in August using print statements. Here’s the original post.

    https://codea.io/talk/discussion/8613/blackjack-the-card-game
    
  • Posts: 193

    Yes, dave1707, this was the code you orginally wrote.
    If I can get this rolling, it would be a Major step foward.
    Everything in the game works fine. I would like to get the cards on the screen and apply this knowledge to my own card games. Do you see anything wrong that I'm doing ? This Codea software is as only as good as the persons knowledge and ability to use it. Thanks in advance.

  • dave1707dave1707 Mod
    Posts: 8,456

    @kendog400 Here a version that uses text.

    displayMode(FULLSCREEN)
    supportedOrientations(LANDSCAPE_ANY)
    
    function setup()
        textMode(CORNER)
        showKeyboard()
        player,dealer={},{}
        table.insert(player,math.random(11))
        table.insert(player,math.random(11))
        dealerSum=0
        while dealerSum<17 do
            table.insert(dealer,math.random(11))
            dealerSum=0
            for a=1,#dealer do
                dealerSum=dealerSum+dealer[a]
            end
        end
        ans="" 
        font("Georgia-BoldItalic")
        fontSize(30)
    end
    
    function draw()
        background(0)
        fill(255)
        playerSum=0
        for a=1,#player do
            playerSum=playerSum+player[a]
        end
        if ans=="s" then
            showWinner()        
        elseif ans=="h" then
            table.insert(player,math.random(11))
            ans=""
        elseif ans=="a" then
            setup()
        end
        showDealer()
        showPlayer()
        showWinner()        
    end
    
    function showDealer()
        text("The dealer has a total of    "..dealerSum,100,HEIGHT-100)
        text("from these cards    "..table.concat(dealer," "),100,HEIGHT-130)
    end
    
    function showPlayer()
        text("You have a total of    "..playerSum,100,HEIGHT-200)
        text("from these cards    "..table.concat(player," "),100,HEIGHT-230)
        if playerSum<=dealerSum and dealerSum<21 then
            text("Do you want to stay(s) or hit(h)",100,HEIGHT-300) 
        end
    end
    
    function showWinner()
        ans=""
        if playerSum>21 or dealerSum==21 then
            text("Dealer wins!",100,HEIGHT-320)
            ans="p"
        elseif dealerSum>21 then
            text("You win!",100,HEIGHT-320)
            ans="p"
        elseif playerSum>dealerSum then
            text("You win!",100,HEIGHT-320)
            ans="p"
        elseif ans=="s" then
            text("Dealer wins!",100,HEIGHT-320)
            ans="p"
        end
        if ans=="p" then
            text("Play again?(a)",100,HEIGHT-370)
        end
    end
    
    function keyboard(k)
        ans=k
    end
    
  • em2em2
    Posts: 194

    Do you want prompts displayed as messages to the screen? If so, try creating a class that holds an array of messages you want to show. Tween them one by one to the screen by drawing them with text. You should try to figure it out yourself before anyone gives you the code.

  • Posts: 193

    Thank you everyone for the help...

Sign In or Register to comment.