Jump to content

Tutorial-the basics of C++ and game making


Recommended Posts

I know, I know, I have done it before... two times... but that was before I knew how to make games... o.k. o.k. ascii games.

This time I have a goal. After you will finish this class, you will be able to create ascii games, like the ascii snake you can download from the attachment (better play it in fullscreen).

You can download Turbo C++ from here. Don't use Visual C++ or anyother windows based C++ compiler, because it won't support the console functions and objects.

 

Feel free to ask questions.

SNAKE.zip

Link to comment
Share on other sites

  • Replies 68
  • Created
  • Last Reply

Top Posters In This Topic

In C++, you can add comments in two ways:

code // single line comment
code

code /*multiline
comment */ code

 

Every C++ look like this:

//Headers
//Declerations
void main()
{
 //The programs code
}

 

Headers: collections of functions (commands)

Declerations: your own functions, global variables and classes. I will get in to this later, since this is more advanced stuff.

The programs code: The commands you want the program to execute when it starts.

 

So what this program does? well, nothing actualy. We need to put some commands so this program will work.

 

Type this in your compiler:

#include<iostream.h>
void main()
{
 cout<<"Hello world";
}

Now press Ctrl+F9.

If the program report that it's "Unable to open include file 'iostream.h', press options/directories and change the "include directories" to the right path. you can allso change the "library directories" if you want.

 

The screen go black and... nothing appens. Actualy, something did appened, but it was so fast you couldn't see it. Just press Alt+F5 to see the screen.

 

Now I will explain:

#inlucde<iostream.h>

This command tells the program that you need the library "iostream.h". This library contain's the basic input and output commands.

 

cout<<"Hello world";

this command shows the text "Hello world" on the screen. Text in C++ must be inside inverted commas. The ; in the end is the end of the command.

You can put more than one pice of text in one command like this:

cout<<"Hello"<<"world";

This will display "Helloworld", since there is no space.

 

You must have noticed that some of the code is not starting in the begining of the line. This is not necessary for the program to work, but it makes the program easier to read, so I recommand it.

 

That will be all for today. Class dismissed

Edited by someboddy
Link to comment
Share on other sites

If you tried to sell the "Hello world" program, you probebly didn't earn alot. Every proram that worth something uses variables. Variables are places for you to put values : numbers, text, world domination plans ect.

The simplest kind of variable is the integer. You define (make) an integer like this:

int a;

Now a is an integer. You can define more then one interger in the same command like this:

int a,b;

You can define how many variables you want anywhere in the program. There are few rules:

1. A variables name can contain only english letters, nubmers, and _s (I don't know how this line called in english).

2. A variables name can not begin with a number.

3. A variable can not have a name already taken by other variable, or preserved words (like void, int). This is not allowed:

int a,a;
int int;

However, C++ is cse sensetive, so this is allowed:

int a,A;
int iNt;

4. A variable can not be used before it is defined.

 

Now there are some commands for variables. If you want to put a value in a variable, you do it like this:

a=7;

now the value of a is 7. You can print it to the screen like this:

cout<<a;

Note that the a is not inside inverted commas. a is not a text. Of course you can print other stuff with your a:

cout<<"a="<<a<<",got it?";

you can use +,-,* and / with variables and numbers like this:

int a,b,c;
a=a+1;
b=c-a;
c=1/a;
a=b*c;

You can get input into a variable like this:

cin>>a;
cin>>b>>c;

For output use <<. For input use >>. If you are having hard time to remember, use this trick:

Output: you give the text to the screen, so the arrows are in the direction of the screen (cout is the screen).

Input: You take the text from the keyboard, so the arrows are in the direction of the variable (cin is the screen).

 

Now lets combine everything we learned:

#include<iostream.h>
void main()
{
 int a,b,c;
 cout<<"give me two numbers";
 cin>>a>>b;
 c=a+b;
 cout<<a<<"+"<<b<<"="<<c;
}

 

That will be all for today. Class dismissed

Link to comment
Share on other sites

I know, I know, I have done it before... two times... but that was  before I knew how to make games... o.k. o.k. ascii games.

Feel free to ask questions.

;) wait you've never made a graphics game?

 

not even in SDL?

I have done few with game makers. I am trying to learn C++, but I stopped after my hard disc got broken.

Link to comment
Share on other sites

Anyways, lets go on.

One of the most important tools in every programing language is the if sentence. The if allows you to change the course of program. How do you do it? like this:

if(/*expression*/)
 /*command*/

or

if(/*expression*/)
 /*command*/
else
 /*alternative command*/

The expression is the condition for the command to be executed. If the expression is true, then the command will be performed. Otherwize, the alternative command will be performed, or nothing, in the case of the first form.

 

So how do you make expression's? Very simple.

a>b: a is larger then b.

a<b: a is smaller then b.

a==b: a is equal to b (remember: two equal signs).

a!=b: a is not equal to b.

a>=b: a is larger or equal then b.

a<=b: a is smaller or equal then b.

 

so:

if(a>b)
 cout<<"a";
else
 cout<<"b";

Prints "a" if a is bigger, or "b" is be is bigger or equal to a

 

You can put more then one command like this:

if(a>b)
{
 a=a+1;
 b=b+1;
}

 

So lets combine what we learned:

#include<iostream.h>
void main()
{
 int age;
 cout<<"Whats you age?";
 cin>>age;
 if(age<18)
   cout<<"you are not old enough to drink";
 else
   cout<<"you are old enough to drink, but I cheap so I wont buy you beer";
}

 

That will be all for today. Class dismissed

Link to comment
Share on other sites

There are more things you need to know about conditions.

First, condition expressions return numbers, 1 is true and 0 is false, so you can store it in integers. The if expression will treat to every non zero value as true, so

if(4)
 cout<<"4";
if(-14)
 cout<<"-14";

will print "4-14".

 

There are few logical operators to combine expressions.

&&: the and operaor. a&&b will return true only if both a and b are true.

||: the or operator. a||b will return true if none of the values a or b are false.

!: the not operator. !a will return true if the value of a is false.

 

So

(1<2)&&(2<70)
(4>2)||(1<0)
!(1>2)

are all true, and

(4<2)&&(7<10)
(3>8)||(6<4)
!(5>1)

are all false.

 

So lets combine what we learned:

#include<iostream.h>
void main()
{
 int a,b1,b2;
 cout<<"enter the first number";
 cin>>a;
 b1=(a<100)&&(a>0);
 cout<<"enter the second number";
 cin>>a;
 b2=(a<100)&&(a>0);
 if(b1||b2)
   cout<<"at least one of the numbers is between zero and 100";
}

 

That will be all for today. Class dismissed

Link to comment
Share on other sites

Next, are the loops. The most simple loop looks like this:

while(/*expression*/)
{
 /*code*/
}

You don't have to use {} if you only put one command inside the loop.

Explenation: the code will be executed as long as the expression is true. So:

int i;
i=0;
while(i<10)
 i=i+1;

The code "i=i+1" will be executed 10 times i=0,i=1,i=2,i=3,i=4,i=5,i=0,6=1,i=7,i=8,i=9. When i=10 "i<10"="10<10" is false.

 

So lets make a simple program:

#include<iostream.h>
void main()
{
 int sum,count,num;
 sum=0;
 count=0;
 while(sum<20)
  {
   cout<<"Enter number ";
   cin>>num;
   sum=sum+num;
   count=count+1;
  }
 cout<<"You entered "<<count<<" numbers before their sum reached to 20";
}

 

That will be all for today. Class dismissed

Link to comment
Share on other sites

The next loop is the do...while loop. it goes like this:

do
{
 /*code*/
}
while(/*expression*/);

The only diffrent between while and do...while is the do...while's code will be executed at least once. so the output of:

do
{
 cout<<"1";
}
while(2<1);

will be "1", and the output of:

while(2<1)
{
 cout<<"1";
}

will be "".

 

You can use it to check values you don't know yet, like:

#include<iostream.h>
void main()
{
 int num;
 do
  {
   cout<<"enter a number ";
   cin>>num;
  }
 while(num!=1);
 cout<<"you finaly entered 1";
}

The value of num is unknown at the begining, but it doesn't metter, because the loop will be executed once anyways.

Link to comment
Share on other sites

Now will be a good time for some stuff to small for their own topic.

 

You can set the value of variable when you declare it like this:

int a=4;
int b=1,c,d=5;

This is done alot in C++.

 

There are few short ways to change the value of variable:

"a++" is the same as "a=a+1"

"a--" is the same as "a=a-1"

"a+=b" is the same as "a=a+b"

"a-=b" is the same as "a=a-b"

"a*=b" is the same as "a=a*b"

"a/=b" is the same as "a=a/b"

"a%=b" is the same as "a=a%b"

 

What is % you ask? % is the reminder you get when dividing. So:

5%2=1

4%2=0

3%4=3

ect.

 

iif you want to go down one line in the output (the screen), you do it like this:

cout<<"first line"<<endl<<"second line";

or

cout<<"first line\nsecond line";

\something can do many things inside text. For example: '\a' beeps. \number is the sign assigned the the number in ascii code. If you want just \, type '\\'.

 

That will be all for today. Class dismissed

Link to comment
Share on other sites

The for loop is usualy used if you know how many times your loop is going to loop. When I say "you know", I don't mean you know the number when you write the program. You can allso have the number inside a variable.

 

for(/*reset code*/;/*expression*/;/*advancing code*/)
{
 /*code*/
}

reset code: will be executed once, before the loop is started.

expression: the loop will continue as long as the expression is true.

advancing code: will be executed at the end of every cycle of the loop, right befor the expression is checked.

 

So if you want a loop to loop 4 times, you write:

for(i=0;i<4;i++)
{
 /*code*/
}

i is usualy used with for loops. If you need to make one loop inside another, use j fo the second loop.

 

i allso contain's the number of the cycle, so we can use it.

 

#include<iostream.h>
void main()
{
 for(int i=0;i<10;i++)
   cout<<i;
}

The output will be "0123456789"

I decalred i inside the loop. This is usualy done in C++, since we don't want variables without values.

 

That will be all for today. Class dismissed.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...