0% found this document useful (0 votes)
32 views30 pages

Introduction To Programming For Games Development

This document provides an introduction to the CO4625 Introduction to Programming for Games Development module. It outlines that students will learn C++ and apply it to create simple 2D games, and will later learn 3D graphics. Assessment includes weekly tasks, programming assignments worth 63% of the grade, and a final exam worth 37%. The document reviews rules for students and the instructor and provides an overview of C++, including that it is a powerful, high-level language that can be compiled to binary. It introduces common programming concepts like objects, classes, and defining objects to represent things like user input and output. Examples are provided to demonstrate basic C++ programs and using objects like cout and cin.

Uploaded by

Ben Denny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views30 pages

Introduction To Programming For Games Development

This document provides an introduction to the CO4625 Introduction to Programming for Games Development module. It outlines that students will learn C++ and apply it to create simple 2D games, and will later learn 3D graphics. Assessment includes weekly tasks, programming assignments worth 63% of the grade, and a final exam worth 37%. The document reviews rules for students and the instructor and provides an overview of C++, including that it is a powerful, high-level language that can be compiled to binary. It introduces common programming concepts like objects, classes, and defining objects to represent things like user input and output. Examples are provided to demonstrate basic C++ programs and using objects like cout and cin.

Uploaded by

Ben Denny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

CO4625

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

Next year youll learn 3D graphics, which is hard


So youll miss 2D games one day
The assessment:
Weekly tasks for formative feedback
Programming project assignments (63%)
An examination at the end of the year (37%)

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

Complete all tasks by the session in the following week


I agree to
Attend all sessions
Stay awake and reasonably alert during sessions
Attempt to answer sensible and relevant questions to the best of my
ability

Give you feedback on your progress


If you need to contact me, try [email protected]

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

C++ is a very powerful language

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.

It supports most major programming paradigms


Object oriented, generic, procedural

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

so in C++ we have objects which deal with that for us.

We ask objects to perform tasks for us


Instead of writing complex code
They do some of the difficult bits so we dont have to
Leaving us to focus on designing good games

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

Depending on the objects role, it will have a


particular interface we can use
In simple terms this is a list of things it can do, along
with details of any information it requires to do them

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.

So these lists of capabilities are defined for the class to


which the object belongs:

but we will deal with classes later


Lets introduce our first object. It is called cout
cout means console out
It interacts with the monitor for us

Thing cout does

Symbol/method
name

How to code

Print something on <<


the console

cout << thing you


need to output;

Set the minimum


number of
characters to be
printed to the
console

cout.width(whole
number)

width

Set the precision


precision
(number of digits)
for outputting
fractional numbers

cout.precision(who
le number);

Set the alignment


(right, left or
centre) amongst
other things!

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.

cout << Can you read this?;

will print the words Can you read this? on


the screen.

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.

The main function is often called the entry point for


the program

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;
}

Getting objects to use


Finally, were going to be using cout in our program,
so we need to tell the compiler where to find it.
The compiler is the software that converts C++ into
something the computer can understand

#include <iostream>
using namespace std;
int main()
{
//put code in here
return 0;
}

cout lives here


and is part of
this!

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;
}

Literals and variables


We can store a section of text using a string object
Useful for storing user input
#include <iostream>
#include <string>
using namespace std;

include string library

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

Give the string a


value

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);

Find the length of length


a string (number
of characters)

cout <<
myString.length()
;

Append one
string to the end
of another

string s,t;
s = Shoe;
t = horn;

append

What will this do?


#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
s = Badger;
cout << s.substr(0,5)<< endl;
return 0;
}

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?

Primitive data types


In addition to objects, all programming
languages have more straightforward
data types
To represent numbers (whole and fractional)
and characters
Creating one of these is very much like
creating an object:

#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;
}

Primitive data types


Advantage for using number types
we can do calculations with them
or pass them to methods that accept
numbers as an argument (like cout.width())
for example.

We have types which represent


numbers, characters and Boolean
(true/false) values.
Numbers may be whole or fractional

#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

Heres a program that enters a fractional number and a whole


number and formats the fractional number to the number of
decimal places specified by the whole number:
#include <iostream>
using namespace std;
int main()
{
int n;
Make a fractional
double f; number
cout << Input a fractional value: ;
cin >> f;
cout << Now input the number of digits you want to round
it to: ;
cin >> n;
cout.precision(n); Set precision to n
cout << f;
digits
return 0;
}

Recap
we discovered...
Two ready made objects cin and cout
interact with the monitor/keyboard, respectively.

A type (or class) of objects called string.


We can create objects of type string and use them to store
and manipulate text the user has entered.

We looked at some handy methods for all of these:


We looked at primitive data types
whole numbers (int)
fractional numbers (double)
we tried passing them to objects methods.

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

You might also like