Howdy, Stranger!

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

How's your reflexes.

dave1707dave1707 Mod
edited June 2013 in Code Sharing Posts: 7,874

Here is a simple program to check your eye/hand coordination. The object is to tap the green circle before it disappears. Each time you tap the circle, the size and time to tap the next one will be reduced by 1. The 10 high scores will be kept. There is an instruction screen when the program starts.


-- popup supportedOrientations(PORTRAIT) displayMode(FULLSCREEN) function setup()     --clearLocalData()    -- uncomment to clear scores, then comment back.     fontSize(40)     init()     intro=true     highScore=false     start=false end function init()     w2=WIDTH/2     h2=HEIGHT/2     mainLoop=0     showLimit=100     limit=101     total=0 end function draw()     background(40, 40, 50)     if intro then         help()         return     end          if highScore then         showHighScore()     end          if start then         if mainLoop==0 then             limit=limit-1             xc=math.random(limit,WIDTH-limit)             yc=math.random(limit,HEIGHT-limit)             radius=limit             showLimit=limit              mainLoop=100         else             mainLoop=mainLoop-1             if  showLimit<=1 then                 if xc~=-100 then                     highScore=true                     start=false                 end             else                 showLimit=showLimit-1                 stroke(0,255,0)                 strokeWidth(2)                 noFill()                 ellipse(xc,yc,limit*2,limit*2)                 fill(0,255,0)                 strokeWidth(0)                 ellipse(xc,yc,showLimit*2,showLimit*2)                 end         end            end          fill(255)     str=string.format("Circle size = %d",limit)     text(str,WIDTH/2,980)                     str=string.format("Score = %d",total)     text(str,WIDTH/2,900) end function help()     fill(255,0,0)     local h2=900     text("Instructions",w2,h2+50)     fill(0,0,255)     text("Tap the green circle before",w2,h2-50)     text("it disappears.",w2,h2-100)     text("Each time you tap the circle,",w2,h2-150)     text("the size will be reduced by 1",w2,h2-200)     text("and the amount of time that you",w2,h2-250)     text("have to tap the next one is also",w2,h2-300)     text("reduced by 1.",w2,h2-350)     text("If you don't tap the green circle",w2,h2-400)     text("fast enough, the program will end",w2,h2-450)     text("and show you the 10 highest scores.",w2,h2-500)     text("Your score will be added to the list",w2,h2-550)     text("if it is high enough.",w2,h2-600)     text("A new circle will appear",w2,h2-650)     text("about every 1.5 seconds.",w2,h2-700)     fill(0,255,0)     text("Tap the screen to continue",w2,h2-800) end function touched(t)     if t.state==BEGAN then         if intro then             intro=false             highScore=true             return         end         if highScore then             if t.tapCount==2 then                 init()                 highScore=false                 start=true             end             return         end         if (t.y-yc)^2/limit^2+(t.x-xc)^2/limit^2 <= 1 then             total=total+limit             xc=-100             yc=-100         end     end end function showHighScore()     fill(0,0,255)     text("Double tap the screen to start",WIDTH/2,HEIGHT/2-160)             pushStyle()     fill(255)     font("Courier")     t=total     for x=1,10 do         str=string.format("popup%d",x)         h=readLocalData(str)         if h==nil then             saveLocalData(str,0)             if t>0 then                 saveLocalData(str,t)                 t=0             end         elseif t==tonumber(h) then                 break         elseif t>tonumber(h) then                 saveLocalData(str,t)                 t=tonumber(h)         end     end         for x=1,10 do         str=string.format("popup%d",x)         h=readLocalData(str)          str=string.format("%2d) %4d",x,h)         text(str,WIDTH/2,800-x*35)     end     popStyle() end

Comments

  • Posts: 743

    Nice.

    Your supportedOrientations call needs to go before setup though as it can still be played in landscape mode and some of e text disappears off the top of the screen in this case.

  • dave1707dave1707 Mod
    edited November 2012 Posts: 7,874

    Thanks @West. I didn't realize that putting it before setup made any difference. I thought that if you started in portrait mode, it would stay in that mode if you rotated the screen, but if you started in landscape mode it wouldn't lock into portrait mode until you turned the screen. It's nice to know that I can now start in portrait mode no matter which way the screen is turned. That's what I like about this forum, you always learn something new. Are there any other commands that should be placed before setup.

  • 4230 here, don't know if its good or bad '-'

  • dave1707dave1707 Mod
    Posts: 7,874

    I made it to 4522, circle size 32. But then I've played it alot as I was writing it and I think I got better the more I did it.

  • Posts: 743

    .@dave1707 not aware of any more commands that need to go pre-setup. I got 4104 so guess I'm getting old :-/

  • Posts: 2,161

    I sometimes put displayMode pre-setup to ensure that WIDTH. and HEIGHT are right.

  • Posts: 563

    The other thing to look out for pre_setup() wise is that orientationChanged(newOrientation) gets called before setup(). This seems to be a particular issue if you are using the runtime.

  • Posts: 743

    An observation about the scoring system. As you progress the circles get smaller and therefore the max available points is less. Less reward for a harder task. How about points based on the percentage of the original size instead?

  • Posts: 2,161

    .@ReefWing I'd forgotten that but it's bitten me a few times as I pass that change on to various objects which aren't defined until inside setup. So I set it up so that my custom orientationChanged is only effected at the end of setup.

  • 4522 first try!

  • dave1707dave1707 Mod
    edited November 2012 Posts: 7,874

    @West, I was going to do the score as 101 - the circle size, so the first circle was 1 point, second 2 points, etc. But then I thought that if 2 people were playing against each other, the higher their scores got, the smaller the difference would be between them and maybe they would try a little harder to beat the other one. But no matter which way the scoring, the high score is still the high score. @chunno. You tied my score the first time. That means you'll probably pass it after a few more times playing. Thanks to the others for pointing out the commands that should go before setup, I didn't know it made a difference.

Sign In or Register to comment.