PRU211m - Slot 1 - Module 1 - Starting To Program
PRU211m - Slot 1 - Module 1 - Starting To Program
STARTING TO PROGRAM
1
Lecturer:
Bùi Thị Thùy-
thuybt26
Mobile (Zalo): 0915.868.769
Email: thuybt26@fe.edu.vn
2
Learning Objectives
• Use C# and Unity documentation to effectively utilize C# and Unity
engine code.
• Use Write and WriteLine methods to display output in console
applications.
• Use the print method to display output in the Unity Console window.
3
Content
• Writing and Running a C# Program
• What Does a C# Program Look Like?
• Using Other Namespaces and Classes
• Namespace/ Comments/ Class/ Identifiers
• The Main method
• Variables and Constants
• Console Output
• Exercise 1
• A Word About Curly Braces
• A Matter of Style
4
Content
• Solving Problems One Step at a Time
• Sequence Control Structure
• Testing Sequence Control Structures
• Putting It All Together
• Adding a Unity Script
• Exercise 2
5
Writing and Running a C# Program
• Installing:
• IDE: Visual Studio Community 2019
• Unity
• Refer to “Appendix: Setting Up Your Development Environment ” (the pdf
file).
• Create one Console Application with Visual Studio:
• Name: PrintRainMessage
• Code: in the next slide.
• Build
6
Writing and Running a C# Program
7
Writing and Running a C# Program
• Output
8
What Does a C# Program Look Like?
• C# is an object-oriented language.
• Classes are critical part of C# programs.
• The Program.cs file contains the application class to run the program.
9
What Does a C# Program Look Like?
10
What Does a C# Program Look Like?
• Items in italics (e.g., class documentation comment) means the
programmer decide what goes there.
• Words without italics (such as namespace, class, static, and void),
need to appear EXACTLY as written.
11
Using Other Namespaces and Classes
• Namespaces and classes are collections of useful C# code that someone
else has already written, tested, and debugged.
• To access the class of a namespace, we need to use namespace with
using directive.
• Looking at the documentation to find out namespaces and classes:
• Select Help > View Help in Visual Studio to look for information about a specific
class or namespace or access
https://docs.microsoft.com/en-us/documentation/
• Type Random Class into the Search box, and press <Enter>
• Unity gives more namespaces to use for developing games.
12
Namespace/ Comments/ Class/ Identifiers
• Namespaces in C# are used to organize too many classes so that it can be easy to handle
the application.
• Comments that start with three slashes, ///, are called documentation comments; XML
tags are used within the documentation comments.
• Line comments start with a double slash, //, followed by whatever descriptive text you
choose to add after the double slash.
• Comment span multiple lines by starting with /*, then ending it with */.
13
Namespace/ Comments/ Class/ Identifiers
• An identifier is used to name something in a program.
• Identifiers can contain any number of letters, numbers, and underscores.
• They can't start with a number, and they can't contain any spaces.
• Identifiers can't be keywords (remember, those special C# words like using, etc.)
• Note: C# is case sensitive, which means that the identifier playerName is NOT the
same as the identifier playername.
14
The Main Method
• The Main method is the main entry point for the application.
• When we run our program, it will simply do the things we told it to do in
the Main method.
• The syntax description (which the IDE provides in the default template):
15
Variables and Constants
• Einstein's work:
• Energy and mass are the variables in the equation
• c (for the speed of light) is a constant in the equation
• C# lets us declare the required variables and constants as follows:
16
Console Output
• To do our output, we’ll use the Console.Write and Console.WriteLine
methods, which are contained in the Console class.
• The Console.Write method outputs whatever we tell it to print on a line on the
screen and leaves the cursor on that line so we can print more on that line.
• The Console.WriteLine method moves the cursor to the next line after printing
what we tell it to.
17
Console Output
• Console Class
Document
18
Console Output
• Console
WriteLine
Area
19
Exercise 1: Writing Your First Console
Application
• Problem 1 – Output your name
• Create a new C# Console Application named Exercise1.
• Add a line of code that will display your name to the user.
• Problem 2 – Output your best friend's or your nemesis' name
• Add code to output the name of your best friend or your nemesis.
20
A Word About Curly Braces
• The default Unity scripts don't follow the curly brace placement
convention we want to us.
• The default line endings in the script template are for Unix (LF only) but
Windows uses different line endings (CR and LF).
• Follow guide lines in page 25 and 26 to change the content of 81-C#
Script-NewBehaviourScript.cs.txt file.
21
A Matter of Style
• Using reasonable, consistent style guidelines will help you develop
better code
• Variable names
• Class names
• Indentation
22
Solving Problems One Step at a Time
• How to go from a problem description to the code that solves the
problem:
1. Understand the Problem
2. Design a Solution
3. Write Test Cases
4. Write the Code
5. Test the Code
• Problem solving is an iterative process no matter what set of steps we use.
23
Solving Problems One Step at a Time
• Understand the Problem
• Understanding the problem requires that you understand WHAT is required.
• Design a Solution
• To figure out HOW you're going to solve the problem.
• Designing your solution is one of the hardest problem-solving steps.
• This step results in a set of classes, a set of objects, and a set of methods designed to
solve the problem.
• Write Test Cases
• Does your solution do what it's supposed to?
• One way to try to make sure a program does what it's supposed to do is with testing.
• To accomplish testing, we'll write a set of test cases you run against your code to make
sure it's working properly.
• Black box test vs While box test, Unit test vs Functional test. 24
Solving Problems One Step at a Time
• Write the Code
• This step is where you take your design and implement that design in a working
C# program.
• The hard part of this step is doing the detailed problem solving to figure out the
precise steps you need to implement in your code to solve the problem.
• Algorithms
• Test the Code
• You run your test cases to make sure the program actually behaves the way you
expected it to (and the way it's supposed to).
• Debug – to find and fix the problems you discover as you test your code.
Debugging is a part of every programmer’s life.
25
Sequence Control Structure
• The simplest problem solutions just start at the beginning and go to the
end, one step at a time.
• Example: Write an algorithm that will read in the name of a band, then
print that name out.
• Algorithm:
26
Testing Sequence Control Structures
• Testing the code for the problem in the previous example.
27
Putting It All Together
• The print rain message program description:
Print the lines:
Hello, world
Chinese Democracy is done and it's November
Is it raining?
to the screen.
• Understand the Problem
• easy to understand.
• Design a Solution
• using an application class that prints out each line in the message.
• Application class: Program
• A single method: Main 28
Putting It All Together
• Write Test Cases
• No inputs, only output
29
Putting It All Together
• Write the Code
30
Putting It All Together
• Test the Code
31
Adding a Unity Script
• Implement a Unity script that outputs the same message.
• Refer to 2.16 in the textbook.
32
Exercise 2: Writing Your First Unity Script
• Problem – Output your favorite three games
• Create a new 2D Unity project named NoFullnameEx2.
• Add a scenes folder and save the current scene as scene0.
• Add a scripts folder and add a new C# script named PrintFavoriteGames.
Open the script, fill in the comment near the top of the script, and delete the
Update method.
• Next, add code to the Start method to output the names of your three
favorite games.
• Attach the script to the Main Camera in the scene and run the game to see
the output in the Console window.
33
Summary
• Unity documentation.
• Write and WriteLine methods.
• Display output in the Unity Console window.
34