× Main Menu

WPF C# Tutorial – Create Snipe the dummies, a sniper scope shooter game in visual studio

Start adding code to the events –

Lets add some code inside of the ShootDummy event. This event will trigger when we click somewhere on the canvas.

Beginning of this function we check where the click landed. So we are checking using the IF(e.OriginalSource is Rectangle) meaning if the click landed on a rectangle then we create a local version of that rectangle and call it activeRec this will be directly linked to the rectangle we just clicked on. Once we have identified it then we can remove it from the canvas. This means we have shot a dummy image so we can increase the score 1. There are few other things we need to check here. We need to check if the dummy images we clicked on belongs to the top or bottom location. So, we can then see if the activeRec has a tag because each dummy image that’s created in this game either has a tag called top or bottom. So, if either of these tags are present, we take away 1 from either top counter or bottom counter integer.

After that part we are getting to creating a new rectangle and calling it ghost rec. This rectangle will hold the ghost image so inside the rectangle we give its properties for example width 60 pixels, height 100 pixels and remember we created that image brush called ghostSprite this is where it will work. We are linking the ghostSprite to this ghost rec as its main Fill and this rectangle will have a tag called ghost. After that we are setting and left and position of the ghostRec object and lastly adding to the canvas to display.

Mouse Move Event

This event is responsible for when the mouse enters and moves inside the canvas.

This is a simple event, first we are creating a new point called position and it capturing the mouse position. Then we have created two double variables called pX and pY. pX is capturing the mouse horizontal or x position. pY is capturing the mouse vertical or y position. With both of them set up we are setting score image left and top to the px and pY minus the height and width of the image so it looks like the its in the centre of the mouse pointer.

Ghost Animation Event

This event will trigger every 20 milliseconds as we have set it when we declared the timer and its properties.

This is a very simple event. First two lines are setting up the link between the two labels we have the canvas and their content. So, this timer works very fast this is why we are giving it the responsibility to update the score and misses when the game is running.

After that we have a foreach loop to check all of the rectangles present in this canvas. Inside the loop we get a bit more specific, we want to find only the rectangles that has a tag called ghost inside of it. When we find it then we can animate it by using the Canvas.SetTop(x, Canvas.GetTop(x) – 5); What this line means is that first we access the set top function of x and inside it we get the current position of x which is the ghost and then we deduct 5 pixels from it every frame, so it will look like its floating to the top.

The ghost rectangle only serves the purpose of telling the user that they shot the dummy image and scored a point, once it floats out of display, we have no need for it. Inside the second fi statement we are checking if the ghost rectangle made it past -180 pixels from the top, if it has then we add that rectangle x into the remove this list.

Bottom of the function you can see the other for each loop, this one is looking for rectangles saved inside of the remove this list. Any rectangles found inside will be remove from my canvas display. This method is important because if we leave the objects spawning and leaving the scene without removing them properly, we can have memory overload and crash the game. If you have any objects in your game that doesn’t need to be there make sure you remove it.

Go to Page 4 for Dummy Move Tick Event Programming

Comment on this tutorial and let us know how you got on -

%d bloggers like this: