comentsys.wordpress.com today features a Universal Windows Platform tutorial showing how to create a Chase Game where after a certain amount of time four squares will appear then within a countdown you must select all of them and shows how to create a simple game mechanic based on time.
There’s some int values for On and Off states plus a corresponding Color for each of these, there’s a int[,] for the game board plus the DispatcherTimer which will be used as the timer – the usual random number generator for a game is used along with variables for state information. There is the Shuffle method that uses the random number generator to generate a sequence of numbers but with “start”, “end” and “total” parameters and the Get method returns a Rectangle with a given Foreground colour, also there’s a Set method to set a Button to use that Get method. The Add method creates a Button with a Click event that will get the Row and Column and if it is “lit up” then it will set that to off otherwise it will count as a miss and the game will be over – controlled by lost variable being set to true.
The Layout method creates the initial layout for the game and Reset will clear the board so that all the Button elements will appear in their Off state, Choose uses the Shuffle method to randomly select which four Button elements will appear in the On state. The Timer method is what controls the flow of the game with the first part of this checking for an existing Timer and stopping and clearing it – by setting to null then a new DispatcherTimer is created with a one second interval then on the Tick event of the time which will occur each second there is a check to see if the game is “lost” then two countdowns for either a Waiting or Solve In countdown – the former will have a random time used which is set from the Reset method or a four second countdown to solve the puzzle by clicking on the buttons.
Behind the Scenes
There’s many a game based on this concept but this was just a simple way of having a timer based game – the concept of a game loop is common to games large and small and it was a good excuse to use this and show it off, the timer interval is probably too long but make it easy to see what’s going on, if want to increase the difficulty could reduce the tick interval to half or even a quarter of a second, that might make it more interesting as if you hit the black buttons you’ll lose which is more likely the faster you make it! The concept of using a timer to drive the game can be used to create any kind of game so this example was just a simple way of doing that without the game itself being too complex, although as-is the game is very easy to play, just need to make those modifications!