Unity/Futile Pong Example (Part 4) – Creating the basic structure of our Pong game in Futile

By the end of this tutorial, you should have something that looks like this. You can download the Assets folder for this tutorial here.

Previously, we finally got our feet wet with some C# code for our Pong game. Let us now set up the various objects that are used in Pong. At the very minimum, we need two paddles and a ball.

Like any form of programming, there are many ways to go about designing our game. While this is a very simple game, I will be separating out all of the various classes into their own C# source files for clarity.

  • Create a new C# file in your Scripts folder and name it PDPaddle.cs
  • If you used the Unity editor to create this file, it will automatically create a class extending MonoBehaviour. Since we won’t be attaching this script to a Unity GameObject, there is no need for it to extend MonoBehaviour. Instead, I will extend FSprite to create the paddle.
  • Replace the contents of PDPaddle.cs with the source code below
using UnityEngine;
using System.Collections;

public class PDPaddle : FSprite {
	public string name; // Since paddles are essentially our players, we'll give them a name to display later.
	public int score; // Our player's current score
	
	public PDPaddle(string name) : base("paddle")  { // Constructor for our paddle
		this.name = name; // Set our _name variable to the value passed in the constructor
	}
}

This should be fairly straightforward. Since PDPaddle extends FSprite, and FSprite requires us to pass the name of our sprite as a parameter (you should recall typing paddle = new FSprite(“paddle”) in the last tutorial), we can use the base() keyword to push parameters to our base class.

Next, let’s create the ball class:

  • Create a new C# file in your Scripts folder and name it PDBall.cs
  • Open this file up, and replace the contents with the source code below
using UnityEngine;
using System.Collections;

public class PDBall : FSprite {	
	public PDBall() : base("ball") {
	}
}

This is the framework for our ball. I’m sure we’ll find a way to add some excitement to it later.

With our ball and our paddles defined, it might be a good idea to create a class that can manage our game. It needs to be able to create and control our ball and paddles, as well as handle any other game data.

  • Create a new C# file in your Scripts folder and name it PDGame.cs
  • Open this file up, and replace the contents with the source code below
using UnityEngine;
using System.Collections;

public class PDGame {
	public PDPaddle player1;
	public PDPaddle player2;
	public PDBall ball;
	
	public PDGame() {
		player1 = new PDPaddle("player1");	// Create player1
		player2 = new PDPaddle("player2");	// Create player2
		ResetPaddles();						// Reset the position of the paddles
		
		ball = new PDBall();				// Create our ball
		ResetBall();						// Reset the position of the ball to the center of the screen
		
		Futile.stage.AddChild(player1);		// Add our elements to the stage, making them visible
		Futile.stage.AddChild(player2);
		Futile.stage.AddChild(ball);
	}	
	
	public void ResetPaddles() {
		player1.x = -Futile.screen.halfWidth + player1.width/2;	// Make sure player1 to the left side of screen
		player1.y = 0;											// Recenter player1 vertically
		
		player2.x = Futile.screen.halfWidth - player2.width/2;	// Make sure player2 is on the right side of the screen
		player2.y = 0;											// Recenter player2 vertically
	}
	
	public void ResetBall() {
		ball.x = 0;	// Place ball in the center of the screen
		ball.y = 0;
	}
}

As with our other classes, right now they just contain the basics of getting our game scene set up. Note the use of Futile.screen.halfWidth. This is in points, not pixels, so it should scale well with any resolution. The same goes with the x and y coordinates of the FSprite class. Once we are ready to build an executable, you can test this by running the application and selecting different resolutions. All of the sprites will scale up and down, allowing you to develop for multiple devices with ease. This is one of the core concepts in Futile. Of course, you are free to design your game such that the graphics do not scale if you so desire.

Finally, it is time to instantiate our PDGame class and see how it looks. Replace the contents of your PongDemo.cs file with the code below.

using UnityEngine;
using System.Collections;

public class PongDemo : MonoBehaviour {
	public PDGame game;

	// Use this for initialization
	void Start () {
		FutileParams fparams = new FutileParams(true,true,true,true);
		
		fparams.AddResolutionLevel(480.0f, 1.0f, 1.0f, ""); // iPhone resolution
		
		fparams.origin = new Vector2(0.5f,0.5f);
		
		Futile.instance.Init (fparams); 
		
		Futile.atlasManager.LoadAtlas("Atlases/PongDemo"); 
		
		game = new PDGame(); // Create an instance of PDGame, which will create instances of our paddles and ball
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

Go ahead and run the code through the Unity editor, and you should see something similar to this:

pongdemo-scene-setup

Next up, we’ll be working with keyboard inputs to make our paddles move, as well as adding some basic ball functionality.

Take me to the next tutorial!

By the end of the tutorial you just read, you should have something that looks like this. You can download the Assets folder for this tutorial here.

About these ads

2 thoughts on “Unity/Futile Pong Example (Part 4) – Creating the basic structure of our Pong game in Futile

  1. Pingback: Adding and managing art assets in Futile for Unity | Game Development

  2. Pingback: Adding some ball movement and managing input for our Pong game in Futile | Game Development

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s