Introduction To Programming For Games Development
Introduction To Programming For Games Development
The Module
You are going to:
Learn how to program in C++
Apply the language in the production of simple 2D
games
The rules
You agree to:
Attend all sessions
Stay awake and reasonably alert during sessions
Ask (sensible and relevant) questions when youre not sure of
something
What is C++
C++ is a high level program language
Enables you to write programs in a much nicer form than the computers
processor uses
Uses human-ish language (words) as opposed to raw binary (ones and
zeros)
Is converted into binary using a compiler and a linker to make an
executable (.exe) file
Its compiled code runs on bare metal, instead of using a virtual runtime
like Java, .Net, Python etc
Though technically silicon is a metalloid, not a metal!
This means your programs will be fast
Good for games!
It also means you get to know & control more of what goes on in the
machines memory, which is good for cybersecurity.
Programming with
Objects
Much modern programming involves the use of objects
Objects help us avoid getting bogged down in details
If you really had to code all the commands necessary to output
some text to a monitor, or get a response from the keyboard,
youd probably run into trouble
Objects
We can have objects to do all sorts of things
Store data
Interact with keyboard/monitor
Represent images on screen
Communicate with external devices
Simulate real life things like people, vehicles,
projectiles etc
Defining objects
Objects are defined according to common types
or classes.
For example, we may have more than one planet if
were simulating a solar system but they should all
have the same interface.
Symbol/method
name
How to code
cout.width(whole
number)
width
cout.precision(who
le number);
cout.setf(see
below);
setf
one of
(ios::left)
(ios::right)
(whole number,
ios::internal)
Example
The most common thing we want cout to do
is write something on the screen, e.g.
Writing a program
Two things to consider:
Where does it start and where does it stop?
We tell the computer where to start by defining
a function called main
For these purposes, a function is just some lines of
code grouped together.
int main()
{
//put code in here
}
Finishing a program
The computers operating system also
requires that we return a value to it,
which can be used to indicate whether
or not the program ran successfully
int main()
{
//put code in here
return 0;
}
#include <iostream>
using namespace std;
int main()
{
//put code in here
return 0;
}
EXAMPLE
#include <iostream>
using namespace std;
This prints
an end of
line
character
int main()
{
cout << Morning all! << endl;
return 0;
}
We can chain output
operators with cout. It
saves typing!
Task
Write a program to output a different
message of your choosing
Using a Method
couts methods can be used to format
output:
cout.width(whole number)
Sets minimum number of characters to be
output next time couts << operator is used
We put a whole number in the brackets
Also known as passing a value
This tells cout what the minimum number of
characters is
#include <iostream>
using namespace std;
Sets the
width to 32
int main()
{
cout.width(32);
cout << Morning all! << endl;
return 0;
}
int main()
{
Create a string
string s;
s = I am stored text;
cout << s << endl << Im about to be discarded!\n;
return 0;
}
The \n sequence is
another way of getting an
end of line character
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
s = Badger;
m = Mushroom;
cout << s << endl << s << endl << s << endl << s << endl;
cout << s << endl << s << endl << s << endl << s << endl;
cout << s << endl << s << endl << s << endl << s << endl;
cout << m << endl << m << endl;
return 0;
}
Strings
Some things
strings do
Symbol/Method
name
How to code
string s = value
here;
Get a substring
substr
starting at
position a and
finishing at
position b
(positions start at
0)
s.substr(0,4);
cout <<
myString.length()
;
Append one
string to the end
of another
string s,t;
s = Shoe;
t = horn;
append
Keyboard input
The object cin interacts with the keyboard for
us
Thing cin does
Operator/
Method
name
Example code
Input a value
>>
string s;
cin >> s;
skip characters in
input (can be used
to wait for user to
press enter)
ignore
cin.ignore(whole
number);
N.B. This can be fiddly sometimes you have to
use cin.ignore(2) to wait
for user to press enter
once!
#include <iostream>
#include <string>
using namespace std;
Store
user
input
Create a string
int main()
{
string s;
cout << Input a string: ;
cin >> s;
cout << You entered << s<<
endl ;
return 0;
}
Task:
Can you write a program to input a
string and output its length?
#include <iostream>
using namespace std;
int main()
{
Make a whole number
int
n;
Store
cout << Input a number: ;
value
entered cin >> n;
by user cout << You entered << n << endl ;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a number: ";
cin >> n;
Pass n to
cout.width(n);
width method
cout << "x" << endl;
return 0;
}
x will appear n
places from the left
Recap
we discovered...
Two ready made objects cin and cout
interact with the monitor/keyboard, respectively.
Lab Tasks
Write a program where the user inputs
a string s and a number x. The program
should output the string so that it is x
spaces from the left of the screen
Write a program which asks the user to
enter a number n and then outputs
rounded to n decimal places
There is more than one way of doing this