C# (Shrape)
C# (Shrape)
JOEL 1226
INTRODUCTION C#
Welcome to the exciting world of programming with C#, your gateway to creating
interactive games! This introduction will equip you with the basic building blocks to
start scripting your own game ideas.
What is C#?
C# is a versatile programming language widely used for game development,
especially with the popular game engine Unity. It's known for its:
Readability: Clear syntax and logical structure make it easier to learn and
understand.
Power: Handles complex game logic and calculations efficiently.
Popularity: Supported by a large community and vast resources for learning
and assistance.
LET'S DIVE INTO THE FUNDAMENTALS:
1. Variables:
Think of them as boxes that store information you use in your game. You can have numbers (e.g., player
health), text (e.g., character names), or even true/false values (e.g., is the player jumping?).
Variables:
Storage containers for data: Think of them as boxes with labels for holding different types of
information.
Naming: Use meaningful names that describe what the variable stores (e.g., playerHealth,
enemyCount).
Declaring variables: Use keywords like int, string, float, bool to specify the data type.
Example: int playerScore = 0; declares an integer variable named playerScore and initializes it with 0.
LET'S DIVE INTO THE FUNDAMENTALS:
Data Types:
Each variable holds a specific kind of data:
Integer (int): Whole numbers (e.g., score,
lives).
Float: Numbers with decimals (e.g., health
points, speed).
String: Text (e.g., dialogue, item names).
Boolean (bool): True or false values
(e.g., game over, is attacking).
LET'S DIVE INTO THE FUNDAMENTALS:
Operators:
Perform operations on data:
Arithmetic operators: + (addition), -
(subtraction), * (multiplication), / (division),
% (modulo).
Comparison operators: == (equal to), !=
(not equal to), < (less than), > (greater
than), <= (less than or equal to), >=
(greater than or equal to).
Logical operators: && (and), || (or), ! (not).
LET'S DIVE INTO THE FUNDAMENTALS:
Control Flow:
Directs the order of code execution:
If statements: Make decisions based on conditions.
Example: if (playerHealth <= 0) { gameOver(); }
Loops: Repeat code blocks until a condition is met.
for loops: Iterate a specific number of times.
while loops: Repeat as long as a condition is
true.
LET'S DIVE INTO THE FUNDAMENTALS:
Functions:
Reusable blocks of code, like tiny programs within your game:
Define a function: Specify its name, what it does, and
any information it needs (parameters).
Call a function: Tell it to run at specific points in your
game, with needed data.
FUNCTIONS AND METHODS:
Reusable blocks of code: Like mini-programs that
perform specific tasks.
Make code more organized, readable, and reusable.
Key terms:
Definition: Creating the function, specifying its
name, parameters, and code block.
Calling: Using the function in other parts of the
code to execute its actions.
Parameters: Inputs passed to the function to
customize its behavior.
DEFINING A FUNCTION:
// Access modifier (e.g., public) Example:
// Return type (e.g., void for no return value) public void MovePlayer(float distance)
// Function name {
// (Parameters, if any) playerPosition += distance;
{ }
// Code to be executed
}
CALLING A FUNCTION:
Calling a Function:
Use the function's name and provide any required parameters:
C#-------
Using Parameters:
Allow functions to be flexible and adaptable:
C#
public void AttackEnemy(int damage)
{
enemyHealth -= damage;
}
EXTRA POINTS
Examples in Game Development:
Player movement: Functions for moving forward, jumping, attacking.
Enemy AI: Functions for chasing the player, choosing actions.
Scorekeeping: Function for adding points to the score.
Level loading: Function for loading a new level.
Menu interactions: Functions for handling button presses and selections.
Benefits:
Improved code organization and readability.
Reduced code duplication.
Easier testing and debugging.
Enhanced code sharing and collaboration.
OOP: BUILDING GAMES WITH BLUEPRINTS
Imagine OOP as a way to create reusable blueprints for game elements. Instead of building everything
from scratch, you design blueprints (classes) that define how objects behave and interact.
Key Concepts:
Classes: The blueprints for creating objects. They define properties (data) and methods (actions)
that objects of that class will have.
Objects: Instances of classes, like individual game elements built from a blueprint.
Inheritance: Allows new classes to inherit properties and methods from existing ones, creating a
hierarchy.
Polymorphism: Enables objects of different classes to be treated as the same type, making code
more flexible.
LET'S BUILD A GAME WORLD USING OOP:
Create a Player class:
Properties: name, health, position
Methods: Move(), Attack(), TakeDamage()