Curl Global Community
Page 5: Application... A “Push the Button in 10s” Game - Printable Version

+- Curl Global Community (https://communities.curl.com)
+-- Forum: Tutorials (https://communities.curl.com/forumdisplay.php?fid=3)
+--- Forum: Public Training (https://communities.curl.com/forumdisplay.php?fid=4)
+---- Forum: Curl IDE Made Easy (https://communities.curl.com/forumdisplay.php?fid=6)
+----- Forum: Try 2: Making a Simple Action Game (https://communities.curl.com/forumdisplay.php?fid=10)
+----- Thread: Page 5: Application... A “Push the Button in 10s” Game (/showthread.php?tid=51)



Page 5: Application... A “Push the Button in 10s” Game - ashimo - 06-16-2011

Application... A “Push the Button in 10s” Game

Let’s create a simple game that combines events and control. In this game, we have to press a “start” button and then, when we think that ten seconds have passed, press a “stop” button. The closer we are to exactly 10s, the higher is our score.

Create the 'Try 2-3' Project

Close the Try 2-2 project and then, from the IDE "File" menu, select "New Project". In the "New Project" dialog box, select "Applet Project”"(1), input “Try2-3” (2), specify c:\Curl\lesson\Try2\03_game in the "Directory" field (3), set the "API Version" to "6.0" (4), and then click "OK" (5).


Inputting the Program

Next, we’ll input the following program code. Or, if you prefer, you can copy the program from c:\Curl\Try2\03_game\start.curl and then paste it into the editor.

Code:
{value
    let start-time:#DateTime
    let start-btn:CommandButton = {CommandButton label = Start}
    let stop-btn:CommandButton = {CommandButton label = Stop}
    
    {start-btn.add-event-handler
        {on Action do
            set start-time = {DateTime}
        }
    }
    
    {stop-btn.add-event-handler
        {on Action do
            let elapsed-time:Time = {start-time.elapsed}
            let time-double:double = elapsed-time / 1s
            let score:int = {switch time-double using <=
                             case 10 do (time-double * 10) asa int
                             case 20 do {abs ((20 - time-double) * 10) asa int}
                             else 0
                            }
            
            {popup-message {VBox
                               {center font-size = 20pt, You have {value score} points!!},
                               {center {value {start-time.elapsed}}}
                           }
            }
        }
    }
    
    {VBox
        margin = 10pt,
        spacing = 10pt,
        halign = center,
        {bold Try to stop in 10 seconds},
        start-btn,
        stop-btn
    }
}


Save the File and Execute the Program

After inputting the code, save the file, and then execute the program. The following result will appear in your browser.