Game Design
Class 02: Coding PrimerTopics
- Coding Primer
- Platformer (win and lose) Lab Demonstration
- Character Sprite Assignment
I am not a crook!
Coding Primer
C-Sharp (C#)
C-Sharp will be the programming language used in Unity to code games.
C-Sharp was developed as an alternative to to JavaScript, to which it is quite similar. It is a more modern object-oriented language in the vein of C++. You will see it utilized to develop everything from web applications, to windows programs, to video games. Good for a beginner with depth for an advanced user.
Language:
Spoken languages can broadly be broken into two foundational elements. Programming languages are the same:
- Syntax: The specific way the code needs to be written for the computer to understand.
- Semantics: The meaning of what you are writing. You must memorize or have readily available the “vocabulary” of your language.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movSpd = 2.5f;
public float jmpStr = 50f;
public bool ground = false;
public bool doubleJump = false;
private Rigidbody2D pRB2D;
private Vector2 startPos;
void Awake()
{
pRB2D = GetComponent<Rigidbody2D>();
}
// Start is called before the first frame update
void Start()
{
startPos = transform.position;
}
// Update is called once per frame
void Update()
{
pRB2D.velocity = new Vector2(Input.GetAxis("Horizontal") * movSpd, pRB2D.velocity.y);
if(Input.GetButtonDown("Jump"))
{
if(ground)
{
pRB2D.AddForce(Vector2.up * jmpStr);
ground = false;
doubleJump = true;
}
else
{
if(doubleJump)
{
pRB2D.AddForce(Vector2.up * jmpStr);
doubleJump = false;
}
}
}
if(transform.position.y <= -10)
{
transform.position = startPos;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.CompareTag("Ground"))
ground = true;
}
}
Script Break Down:
To explain the elements of a script we will break down a simple script.
(the player controller script we made last class)
Commands/Statements
- A Command or statement is a single line of code that contains instructions.
- Most statements are either math operations, comparative statements, function call, or an assignment.
- All elements are case-sensitive (Rigidbody != rigidbody)
- Each statement ends with a semicolon “;”
- Blocks of statements are contained in “{“ and “}” (tab in your blocks)
example statement
int playerScore = 10;
statement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movSpd = 2.5f;
public float jmpStr = 50f;
public bool ground = false;
public bool doubleJump = false;
private Rigidbody2D pRB2D;
private Vector2 startPos;
void Awake()
{
pRB2D = GetComponent<Rigidbody2D>();
}
// Start is called before the first frame update
void Start()
{
startPos = transform.position;
}
// Update is called once per frame
void Update()
{
pRB2D.velocity = new Vector2(Input.GetAxis("Horizontal") * movSpd, pRB2D.velocity.y);
if(Input.GetButtonDown("Jump"))
{
if(ground)
{
pRB2D.AddForce(Vector2.up * jmpStr);
ground = false;
doubleJump = true;
}
else
{
if(doubleJump)
{
pRB2D.AddForce(Vector2.up * jmpStr);
doubleJump = false;
}
}
}
if(transform.position.y <= -10)
{
transform.position = startPos;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.CompareTag("Ground"))
ground = true;
}
}
Variables:
Variables may store, update, or replace data.
- Four Properties
-
- Type:
- The kind of data it can hold (int, Number, String, etc.).
- Name:
- The name allows you to call the information whenever you need it.
- Value:
- The data that is stored in the variable. This is not necessary to declare this initially.
- Rules
- Name cannot begin with a number.
- Name cannot include spaces.
- Name cannot use anything but letters, numbers, or _.
- Name cannot use a reserve or keyword.
-
- Camel Case:
- In order to differentiate words without spaces you can start with lowercase and then uppercase for each subsequent word. (ex. finalScore).
- youCanActuallyReadAWholeSentenceWithoutSpaces
- muchharderifcamelcaseisnotusedEVENWORSEINALLCAPS
variable assignment example
float playerJumpStrength = 0.6f;
float playerJumpStrenght = 0.6f;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movSpd = 2.5f;
public float jmpStr = 50f;
public bool ground = false;
public bool doubleJump = false;
private Rigidbody2D pRB2D;
private Vector2 startPos;
void Awake()
{
pRB2D = GetComponent<Rigidbody2D>();
}
// Start is called before the first frame update
void Start()
{
startPos = transform.position;
}
// Update is called once per frame
void Update()
{
pRB2D.velocity = new Vector2(Input.GetAxis("Horizontal") * movSpd, pRB2D.velocity.y);
if(Input.GetButtonDown("Jump"))
{
if(ground)
{
pRB2D.AddForce(Vector2.up * jmpStr);
ground = false;
doubleJump = true;
}
else
{
if(doubleJump)
{
pRB2D.AddForce(Vector2.up * jmpStr);
doubleJump = false;
}
}
}
if(transform.position.y <= -10)
{
transform.position = startPos;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.CompareTag("Ground"))
ground = true;
}
}
Variable Assignment:
When you apply a value to a variable it is called an assignment.
C# Data Types:
- int: whole numbers
- float: decimal numbers
- String: text (notice cap “S”)
- boolean: true/false
- Vector3: xyz vector (notice cap “V”) use Vector2 for 2D (xy)
- array: list[‘white’,’black’,’red’]
- and more…
- Common Unity Data Types:
- GameObject, KeyCode, Text, Collider, Rigidbody, etc.
- Basically defines the various components of given object
- Assignment Statement (=):
Scope:
- Local (private)
- The variable only exists inside the function it is declared in
- Global (public)
- The variable exists outside a specific function and may be used anywhere in the code. It will also show up in the Unity Inspector panel.
Comments
Comments are used to explain what is happening in your code and are not read by the computer.
- //
- Used to comment out a single line.
- /* at start and then */ at the end
- Used to comment out multiple lines of code. As much as you’d like.
single line comment example
//this comment is a note not read by the computer
// this comment is a note not read by the computer
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movSpd = 2.5f;
public float jmpStr = 50f;
public bool ground = false;
public bool doubleJump = false;
private Rigidbody2D pRB2D;
private Vector2 startPos;
void Awake()
{
pRB2D = GetComponent<Rigidbody2D>();
}
// Start is called before the first frame update
void Start()
{
startPos = transform.position;
}
// Update is called once per frame
void Update()
{
pRB2D.velocity = new Vector2(Input.GetAxis("Horizontal") * movSpd, pRB2D.velocity.y);
if(Input.GetButtonDown("Jump"))
{
if(ground)
{
pRB2D.AddForce(Vector2.up * jmpStr);
ground = false;
doubleJump = true;
}
else
{
if(doubleJump)
{
pRB2D.AddForce(Vector2.up * jmpStr);
doubleJump = false;
}
}
}
if(transform.position.y <= -10)
{
transform.position = startPos;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.CompareTag("Ground"))
ground = true;
}
}
Operators:
Expressions: Are assignments, math, or concatenation that evaluates into a single value.
Expressions, Conditionals, and loops use operators.
Common Operators (math):
- + addition (concatenation strings)
- – subtraction
- * multiplication
- / division
- % modulo (remainder)
Postfix Operators (increment/decrement):
- ++ adds one
- — subtracts one
Logical Operators (increment/decrement):
- && and
- || or
- ! not
Assignment Operators:
- += total += 1 is same as total = total + 1
- -= total -= 1 is same as total = total – 1
- *= total *= 1 is same as total = total * 1
- /= total /= 1 is same as total = total / 1
- %= total %= 1 is same as total = total % 1
Comparison Operators (for conditionals):
- > greater than
- >= greater than or equal to
- < less than
- <= less than or equal to
- == equal to
- != not equal to
Conditions & Loops:
Conditions and loops test for a certain condition and if met run a block of code once or many times.
If Statement: tests the condition and then runs if true.
For Loop: set a variable to count the times code runs and stops when that is reached.
{
Do this
}
Else
{
Do this
}
int score = 65;
if (score >= 75)
{
Debug.Log("Pass");
}
else
{
Debug.Log("Loser");
}
{
Do this
}
for (int i = 0; i < 10; i++)
{
Debug.Log(i);
}
While Loop: runs a loop while specified condition is true.
Do While Loop: runs without conditional then test afterwards to run again.
{
Do this
}
int score = 0;
while (score != 100 )
{
score += 1;
Debug.Log(score);
}
{
This
}
Repeat (if this is true)
int score = 75;
do
{
Debug.Log(score);
}
while (score != 100);
Functions/Methods:
A function (method when inside an object) completes a specific task with a block of commands grouped together so that they may be called and reused.
Function Syntax:
- void
- the keyword that let’s the computer know that this a function.
- ()
- the parameters are data that is passed to the function to process.
- {}
- the code block contains all the statements the function runs.
- return
- the return is the information sent back from a function call.
- function call
- this is how you execute a function. You may also send a function data via arguments.
function example
void FindArea(int width, int height)
{
int area = width * height;
return area;
}
int shapeArea = FindArea(5, 7);
Debug.Log(shapeArea);
void FindArea (int width, int height)
{
int area = width * height;
return area;
}
int shapeArea = FindArea(5, 7);
Debug.Log(shapeArea);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movSpd = 2.5f;
public float jmpStr = 50f;
public bool ground = false;
public bool doubleJump = false;
private Rigidbody2D pRB2D;
private Vector2 startPos;
void Awake()
{
pRB2D = GetComponent<Rigidbody2D>();
}
// Start is called before the first frame update
void Start()
{
startPos = transform.position;
}
// Update is called once per frame
void Update()
{
pRB2D.velocity = new Vector2(Input.GetAxis("Horizontal") * movSpd, pRB2D.velocity.y);
if(Input.GetButtonDown("Jump"))
{
if(ground)
{
pRB2D.AddForce(Vector2.up * jmpStr);
ground = false;
doubleJump = true;
}
else
{
if(doubleJump)
{
pRB2D.AddForce(Vector2.up * jmpStr);
doubleJump = false;
}
}
}
if(transform.position.y <= -10)
{
transform.position = startPos;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.CompareTag("Ground"))
ground = true;
}
}
Objects:
Objects are data models that may contain variables, functions, and events. Variables become properties. Functions become methods.
Each component contains various methods and properties.
Almost like a file/folder structure you can use a dot in order to access children.
Object Components:
- Properties
- properties are variables nested inside of objects.
- properties come in pairs, key and value.
- Methods
- methods are functions nested inside of objects
- methods contain blocks of code that complete a given task
- Events
- events describes the response of the object to an input.
- events respond to things such as Start, Update, OnTriggerEnter, etc.
Typical Order of Operations:
- event triggers a method
- method completes a series of instructions
- method retrieves/updates object properties
object example
playerMove.transform.position.x = 10;
playerMove . transform . position . x = 10;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movSpd = 2.5f;
public float jmpStr = 50f;
public bool ground = false;
public bool doubleJump = false;
private Rigidbody2D pRB2D;
private Vector2 startPos;
void Awake()
{
pRB2D = GetComponent<Rigidbody2D>();
}
// Start is called before the first frame update
void Start()
{
startPos = transform.position;
}
// Update is called once per frame
void Update()
{
pRB2D.velocity = new Vector2(Input.GetAxis("Horizontal") * movSpd, pRB2D.velocity.y);
if(Input.GetButtonDown("Jump"))
{
if(ground)
{
pRB2D.AddForce(Vector2.up * jmpStr);
ground = false;
doubleJump = true;
}
else
{
if(doubleJump)
{
pRB2D.AddForce(Vector2.up * jmpStr);
doubleJump = false;
}
}
}
if(transform.position.y <= -10)
{
transform.position = startPos;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.CompareTag("Ground"))
ground = true;
}
}
Classes:
Put simply classes are templates for objects.
Every script you write is a class. Notice that you may write a script but it does not become an object that can do anything until you apply it to something in the game.
Every time you add a component like a Sprite Renderer, Collider2D, or Rigidbody2D you are producing objects from classes. (your scripts are the same as components).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movSpd = 2.5f;
public float jmpStr = 50f;
public bool ground = false;
public bool doubleJump = false;
private Rigidbody2D pRB2D;
private Vector2 startPos;
void Awake()
{
pRB2D = GetComponent<Rigidbody2D>();
}
// Start is called before the first frame update
void Start()
{
startPos = transform.position;
}
// Update is called once per frame
void Update()
{
pRB2D.velocity = new Vector2(Input.GetAxis("Horizontal") * movSpd, pRB2D.velocity.y);
if(Input.GetButtonDown("Jump"))
{
if(ground)
{
pRB2D.AddForce(Vector2.up * jmpStr);
ground = false;
doubleJump = true;
}
else
{
if(doubleJump)
{
pRB2D.AddForce(Vector2.up * jmpStr);
doubleJump = false;
}
}
}
if(transform.position.y <= -10)
{
transform.position = startPos;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.CompareTag("Ground"))
ground = true;
}
}
Platformer (win and lose) Lab Demonstration
Assets
- Environment
-
- Midground
- The imagery that the player character can actually contact, ground, platforms, walls, etc.
- Characters
-
- Player
- The protagonist controlled by the player
- Other(pickups, UI, etc.)
-
- FallBox
- An empty group that “kills” the player when he falls below the platforms
- Door
- A sprite that loads the next level when the player enters it.
Scripts
- New Scripts
-
- DeathboxScript
- Runs the death function when player enters
- VictoryboxScript
- Loads next level when player enters
- Updated Scripts
-
- PlayerControllerScript
- Add a death function to reset the player
- Old Scripts
-
- GroundCheckScript
- Checks that the player character is on the ground
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movSpd = 2.5f;
public float jmpStr = 50f;
public bool ground = false;
public bool doubleJump = false;
private Rigidbody2D pRB2D;
private Vector2 startPos;
void Awake()
{
pRB2D = GetComponent<Rigidbody2D>();
}
// Start is called before the first frame update
void Start()
{
startPos = transform.position;
}
// Update is called once per frame
void Update()
{
pRB2D.velocity = new Vector2(Input.GetAxis("Horizontal") * movSpd, pRB2D.velocity.y);
if(Input.GetButtonDown("Jump"))
{
if(ground)
{
pRB2D.AddForce(Vector2.up * jmpStr);
ground = false;
doubleJump = true;
}
else
{
if(doubleJump)
{
pRB2D.AddForce(Vector2.up * jmpStr);
doubleJump = false;
}
}
}
if(transform.position.y <= -10)
{
transform.position = startPos;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.CompareTag("Ground"))
ground = true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform pTransform;
public Vector3 offset;
void Update()
{
transform.position = pTransform.position + offset;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Victory : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.CompareTag("Player"))
{
Debug.Log("you won!");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class StartMenuScript : MonoBehaviour
{
public void StartGame()
{
SceneManager.LoadScene("Level01");
}
public void QuitGame()
{
Application.Quit();
}
}
Character Sprite Assignment
Character Sprite:
When you first start prototyping a game you most often use stand-in images. This is what we have done thus far. Obviously this isn’t appropriate for your final game. In this assignment you will create your player character sprite. You only need to create one still image (the animation will be a later assignment) but it should be final product quality. Once completed you should submit a png image at the appropriate resolution for your game.
You will be graded on the following:
- Lab Requirements
-
Techniques and processes covered in the instructional material is followed and implemented.
-
- Creativity & Craftsmanship
-
Excellent design choices, novel & appealing, and solid clean caliber work.
-
Resources:
- Assignment Video Tutorials
- You may watch the tutorial videos below to help you complete your assignment.
Assignment Video Tutorials
Wait! Before you go!
Did you remember to?
- Read through this webpage
- Submit Character Sprite Assignment on Blackboard