Saturday, 29 March 2008

Pacman Silverlight Updae

Having been left with the previous application we have been able to get a moving sprite going around a game board and boundaries within the board to stop Mr Pacman walking through walls. Now I've decided to start adding some "dots" onto the board which will get eaten as pacman goes past.
There have been 2 approaches that I've taken, the first is a bit more clunky but and time consuming used to create the bigdot. With this method being so time consuming i looked into a more benificial way of doing it (especially with the ammount of smaller dots).

In this section of code the big dots are being created exclusivly in the C# code, first a Rect is created so that we can check to see if Pacman intercepts the dots, the width and fill of the big dot is then configured and finally the positoning is specified using the Ellipse.setValue() method.

These rects and dots have then been added to an array so that the rect in the rect array corresponds to the ellipse in the bigdots array so that when an intersect happens we can get rid of the dots.

The bigdots array is then added to the canvas, so that they can be seen on screen.

The second method that has been used to add the smaller dots to the board is much nicer and uses a combination of Expression Blend 2.5 to create and place the dots within the canvas and then using C# code to apply a rect to each of the dots.

Straight away you can see that this method reduces the ammount of code needed, and creates 158 dots compared to the 4 big dots.

The code loops through each of the ellipses on the canvas using canvasname.children.oftype which returns all ellipses in the specified canvas. For each of these ellipses we must then add them to a littledots array and then create rects based on the values of the ellipses using the Rect constructor and add that to a corresponding littlerects array.

A small section of code is then also generated so that Pacman can eat up the dots, this uses a very similar approach to checking the boundarys by creating a rect in the direction pacman is moving and then see if it intersects the dot.

One the dots have been intersepted the visibility of the ellipse is set to collapsed so that it cant be seen anymore. And the player score is then incremented and displayed.

A problem that did occour is that originally when the ellipse disapeared the rect was still there in the background and would rack up the score each time pacman intersected with the rect, and high scores of a couple of thousand were attainable in a second.

This was solved by creating a new Rect in place of the one which had just been intersected. Im sure there must be a nicer way of doing this, however there dosnt seem to be a destructor for Rect, ~Rect() doestnt want to work for me.

The current version of the game can be found at the bottom of the main page of this blog. Go test it out and give some feed back.

No comments: