Calculator Game

This is a game my friends and I play, but we use our hands. I decided to try to recreate it on codea, as it is a very simple game.

If you’ve never played it there are instructions in app.

There are probably a lot of things I could have done to shorten up the code, but this is my first game I’ve ever made on Codea.


--# Main
-- Calculator game
function setup()
    game = true
    p1h1= 1
    p1h2 = 1
    p2h1=1
    p2h2=1
    playerturn = math.random(1,3)
    number= 0
    p1h1rec = false
    p1h2rec = false
    p2h1rec = false
    p1h1a = true
    p1h2a = true
    p2h1a = true
    p2h2a = true
    back1 = 0
    back2 = 255
    backchange = 0
   displayMode(FULLSCREEN)
--    music("Game Music One:Happy Song")
end
function draw()
    if playerturn == 3 then
        playerturn = 1
    end
    stroke(back1, back2, 255, 255)
   if back1 <= 255 and backchange == 0 then
     back1 = back1 + 1
    end
if back1 >= 255 then
    backchange = 1
    end
    if back1 <= 255 and backchange == 1 then
        back1 = back1 - 1
    end
       if back2 <= 255 and backchange == 0 then
     back2 = back2 - 1
    end
if back2 >= 255 then
    backchange = 0
    end
    if back2 <= 255 and backchange == 1 then
        back2 = back2 + 1
    end
    background(back1, back2, 255, 255)
    strokeWidth(5)
 if game == true then
     fill(0, 0, 0, 255)
      text(p1h1,WIDTH/2-400,HEIGHT/2-230)
        text(p1h2,WIDTH/2+400,HEIGHT/2-230)
       text(p2h1,WIDTH/2-400,HEIGHT/2+230)
        text(p2h2,WIDTH/2+400,HEIGHT/2+230)
    if playerturn == 1 and CurrentTouch.y >= 100 and CurrentTouch.y <= 200 and CurrentTouch.x >= 90 and CurrentTouch.x <= 170 and p1h1a == true then
        p1h1rec = true
    end
  if p1h1rec == true and p1h1a == true then     
            p1h2rec = false
        rect(60,100,100,100)
        fill(255, 255, 255, 255)
        text(p1h1,WIDTH/2-400,HEIGHT/2-230)
        fill(0, 0, 0, 255)
        text(p1h2,WIDTH/2+400,HEIGHT/2-230)
    end
    if p1h2rec == true and p1h2a == true then
          p1h1rec = false
        rect(860,100,100,100)
        fill(255, 255, 255, 255)
        text(p1h2,WIDTH/2+400,HEIGHT/2-230)
        fill(0, 0, 0, 255)
        text(p1h1,WIDTH/2-400,HEIGHT/2-230)
    end
    if p1h1rec == true then
        if CurrentTouch.x >= 90 and CurrentTouch.x <= 170 and CurrentTouch.y >= 600 and CurrentTouch.y <= 700 and p1h1a == true and p2h1a == true then
            p2h1=p2h1+p1h1
            p1h1rec = false
            playerturn=2
            alert1 =0
        end
         if CurrentTouch.x >= 860 and CurrentTouch.x <= 960 and CurrentTouch.y >= 600 and CurrentTouch.y <= 700 and p1h1a == true and p2h2a == true then
            p2h2=p2h2+p1h1
            p1h1rec = false
            playerturn = 2
            alert1 =0
        end
        end
    if p1h2rec == true then
         if CurrentTouch.x >= 90 and CurrentTouch.x <= 170 and CurrentTouch.y >= 600 and CurrentTouch.y <= 700 and p1h2a == true and p2h1a == true then
            p2h1=p2h1+p1h2
            p1h2rec = false
            playerturn =2
            alert1 =0
        end
         if CurrentTouch.x >= 860 and CurrentTouch.x <= 960 and CurrentTouch.y >= 600 and CurrentTouch.y <= 700 and p1h2a == true and p2h2a == true then
            p2h2=p2h2+p1h2
            p1h2rec = false
            playerturn = 2
            alert1 =0
        end
    end
      if playerturn == 1 and CurrentTouch.x >= 850 and CurrentTouch.x <= 950 and CurrentTouch.y >= 100 and CurrentTouch.y <= 200 and p1h2a == true then
            p1h2rec = true        
    end
    if playerturn == 1 and CurrentTouch.x >= 850 and CurrentTouch.x <= 950 and CurrentTouch.y >= 100 and CurrentTouch.y <= 200 and p1h1rec == true then
         p1h2rec = true
        p1h1rec = false       
    end
    if playerturn == 2 then
         if CurrentTouch.x >= 90 and CurrentTouch.x <= 170 and CurrentTouch.y >= 600 and CurrentTouch.y <= 700 and p2h1a == true then
            p2h1rec = true
            p2h2rec = false
        end
    if CurrentTouch.x >= 860 and CurrentTouch.x <= 960 and CurrentTouch.y >= 600 and CurrentTouch.y <= 700 and p2h2a == true then
            p2h2rec = true
            p2h1rec = false
        end
        end
    if p2h1rec == true and p2h1a == true then
        rect(60,565,100,100)
        fill(255, 255, 255, 255)
        text(p2h1,WIDTH/2-400,HEIGHT/2+230)
    end
    if p2h2rec == true and p2h2a == true then
        rect(860,565,100,100)
        fill(255, 255, 255, 255)
        text(p2h2,WIDTH/2+400,HEIGHT/2+230)    
    end
    if p2h1rec== true and CurrentTouch.y >= 100 and CurrentTouch.y <= 200 and CurrentTouch.x >= 90 and CurrentTouch.x <= 170 and p1h1a == true and p2h1a == true then
        p1h1=p1h1+p2h1
        playerturn= 1
        p2h1rec = false
        p2h2rec = false
        alert1 =0
    end   
    if p2h1rec == true and CurrentTouch.x >= 850 and CurrentTouch.x <= 950 and CurrentTouch.y >= 100 and CurrentTouch.y <= 200 and p2h1a == true and p1h2a == true and p2h2rec == false then
        p1h2=p2h1+p1h2
        playerturn=1
        p2h1rec=false
        p2h2rec = false
        alert1 =0
    end
       if p2h2rec== true and CurrentTouch.y >= 100 and CurrentTouch.y <= 200 and CurrentTouch.x >= 90 and CurrentTouch.x <= 170 and p2h2a == true and p1h1a == true then
        p1h1=p1h1+p2h2
        playerturn= 1
        p2h2rec = false
        p2h1rec = false
        alert1 =0
    end   
    if p2h2rec == true and CurrentTouch.x >= 850 and CurrentTouch.x <= 950 and CurrentTouch.y >= 100 and CurrentTouch.y <= 200 and p2h2a == true and p1h2a == true then
        p1h2=p2h2+p1h2
        playerturn=1
        p2h2rec=false
        p2h1rec = false
        alert1 =0
    end
    if p1h1 >= 5 then
        fill(back1, back2, 255, 255)
        rect(60,100,100,100)
        p1h1a = false
    end
    if p1h2 >= 5 then
        fill(back1, back2, 255, 255)
        rect(860,100,100,100) 
        p1h2a = false 
    end
    if p2h1 >= 5 then
        fill(back1, back2, 255, 255)
        rect(60,565,100,100)
        p2h1a = false
    end
    if p2h2 >= 5 then
        fill(back1, back2, 255, 255)
       rect(860,565,100,100)
        p2h2a = false
    end
        end
        fill(0, 0, 0, 255)
    text("Help",WIDTH/2+460,HEIGHT/2-360)
    if p1h1a == false and p1h2a == false then
        text("PLAYER 2 WINS, DOUBLE TAP TO PLAY AGAIN",WIDTH/2,HEIGHT/2)
    end
    if p2h1a == false and p2h2a == false then
        text("PLAYER 1 WINS, DOUBLE TAP TO PLAY AGAIN",WIDTH/2,HEIGHT/2)
    end
    if p2h1a == false and p2h2a == false and CurrentTouch.tapCount == 2 or p1h1a == false and p1h2a == false and CurrentTouch.tapCount == 2 then
         game = true
    p1h1= 1
    p1h2 = 1
    p2h1=1
    p2h2=1
    playerturn = 1
    number= 0
    p1h1rec = false
    p1h2rec = false
    p2h1rec = false
    p1h1a = true
    p1h2a = true
    p2h1a = true
    p2h2a = true
    back1 = 0
    back2 = 255
    backchange = 0
    end
     if playerturn == 1 and game == true then
        fill(255, 0, 0, 255)
        rect(470,20,100,100)
        elseif playerturn == 2 and game == true then
            fill(255,0,0,255)
        rect(470,600,100,100)
    end
    if CurrentTouch.x >= 930 and CurrentTouch.y <= 50 then
        game = false   
    end
    if game == false then
        fill(0, 0, 0, 255)
        text("The player with the red square goes first, they have to click a number and then",WIDTH/2,HEIGHT/2+30)
        text("click a number on the row of the other player, those numbers will add together on the other players side",WIDTH/2,HEIGHT/2)
        text("If your number adds up to five or over, then you loose that number. Last player standing wins",WIDTH/     2,HEIGHT/2-30)
        text("Double Tap to exit",WIDTH/2,HEIGHT/2-120)
        if CurrentTouch.tapCount == 2 then
            game = true
        end
    end
    end

Great work on your first game, @Mason

Nice work!

I suggest you learn about tables, that will help make your code much smaller and easier for you to work with.