× Main Menu

WPF C# Tutorial – Create Parallax Scrolling Endless Runner Game in Visual Studio

In this tutorial we will make an endless runner game similar to the T Rex endless runner game we have done before. This game will have all the similar functionalities except it will have its own Sprite changing function, parallax scrolling background and it will be made in WPF format. This is an exciting new tutorial and we worked really hard to make it work hopefully you enjoy making it too.

Lesson Objectives –

  • Create a parallax scrolling endless runner game in visual studio
  • Use Rectangle objects and assign sprites to them to make the game look amazing
  • Use two background objects to make a parallax scrolling background in the game
  • Use a custom-made sprite swapping function to make the main character animate in the game
  • Create a custom jump function
  • Check for collision between the ground, player and obstacle objects during runtime
  • Keep score in the game
  • Use an array to move the obstacle properties in the game
  • Reset the game with the default values to try again in the game

Video Tutorial for Endless Runner Game in WPF and C#

 

Images for this game

wpf c# parallax endless runner game tutorial sprite images

Download the images ZIP file here.

Make sure you extract the files, so you can import them to visual studio later on.

These are the images for this game, we have a main background image for the game and 8 different sprites for the player, each of the sprites are doing a specific animation for the player. The last sprite is the obstacle image. Each of the sprite is transparent. Since WPF supports transparency much better than windows form application, it’s much better to do more advanced game development programming for this game.

We will make a runSprite() function that will change the sprite as the game is running. This is why we have several different still images of the running image, once they are loaded in sequence, it will look like an animated image.

Written Tutorial – Starting the Project

Make a new project in visual studio, make sure it’s a WPF App (.NET Framework) template, in the next scene name the project Endless Runner Game.

wpf c# parallax endless runner game tutorial project selection window

Click on Create.

Below is the empty WPF form. This is the default view of the WPF .NET Framework template. The empty scene you see the design view of the app and the code in the bottom in the XAML code. We can add the elements we need for this game in the XAML code.

wpf c# parallax endless runner game tutorial project view window

This is the default XAML code view for this game. We can make some changes to this window.

 

We have made some changes to the XAML code below. Lets take a closer look.

 

Title tag has been changed to “Endless Runner by MOOICT” make sure those are inside a double quotation mark. This will show up on the title bar of the app. By default this is set to MainWindow so we want to customize it a little before we can start on the game.

Delete the <GRID> tags and add the <Canvas> tags for this game. Grids not suitable for this type of so we can use Canvas.

Inside the canvas tag Name the tag myCanvas, this name will be used in the C# script to identify and make changes to the properties. Focusable keyword means if we want the main focus for this app to be the canvas, when its set to true this canvas will get priority when the game loads. Key down and Key up keyword is adding the events for when the keys are pressed and released on the keyboard. Set background to light blue.

Inside the Canvas tag it has 5 rectangles and 1 label. 2 of those rectangles are for the background, since we are making a parallax scrolling background this game, we need two of those. 1 of the rectangles is for the ground. 1 rectangle is the player and last one is the obstacle.

There are names for each rectangle so we can identify and change them around from the C# code. First background has a name of background and the other has the name background. First one height is 400 pixels and width is set to 1262. Second one has the same except it has a canvas.left meaning the x position of the background set to 1262. So, where the first background ends this background2 object will begin. The first background fill colour is orange and second background fill colour is light blue.

The ground rectangle has a width of 800 so it covers the whole window and height of 32 pixels. The fill colour is black. Ground rectangle top position in the canvas is 387 and left position is set to 0. Top refers to the Y position and left refers to the X position of any object inside the canvas.

The player rectangle has the name player inside the double quotation marks and height is set to 99 and width is set to 67 pixels. These numbers are based on sprites we created for this game. Set the full colour to brown and top position to 151 and left position to 110.

In the obstacles rectangle set height set to 178, width set to 50, fill set to red and set top position to 320 and left position to 721.

The label we added last in this canvas is to show the score in the game when we successfully jump over an obstacle. Inside the label tag it has a name scoreText, font size is set to 22, font weight is set to bold and content meaning the texts of the label shows Score: 0 inside the quotation marks.

Now we are done with the design view of this game. Your design view above the XAML window should look like this.

wpf c# parallax endless runner game tutorial added components image

This window has been ZOOMED out slightly to show the whole thing, if you want to zoom out on yours then HOLD CTRL and use the mouse wheel to zoom out of the screen. If it doesn’t look like the screen shot above then double check the XAML code presented to make sure you are on the right track so far.

Click on the Page 2 to start adding images




3 responses to “WPF C# Tutorial – Create Parallax Scrolling Endless Runner Game in Visual Studio”

  1. Pascal F says:

    Very nice tutorial, I really appreciate it.

    But i got one question:

    How do i get a moving ground into the game? when i put an image with the same length as the background and also the nearly same code i fall through the ground.

    Pascal

  2. Anhar Ali says:

    Hi Pascal, glad you liked the tutorial, worked really hard on this one. For your question about moving the ground, you can use similar techniques as being used with the background scrolling animation , have two separate ground rectangles and move them the same way as the backgrounds are. Within the rectangles you can images as it’s texture so it looks like the ground is scrolling with the background.

    I think the reason your character is falling through it is because it’s not checking whether you have landed on the ground rectangle. See if that helps.

    Happy programming.

  3. kobubu says:

    Dude your tutorials get better.
    I love how you explain the things now.
    Thanks for everything you do to us!

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

%d bloggers like this: