gamedev

CSharp – for Statement

CSharp – for Statement


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 5;

            for(int i = 0; i < number; i++)
                Console.WriteLine(i);

            // the console will close if you press Return on the keyboard
            Console.ReadLine();
        }
    }
}

The result is:
0
1
2
3
4

By |CSharp, Video Games Development|Commenti disabilitati su CSharp – for Statement

CSharp – while Statement

CSharp – while Statement


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 0;

            while(number < 5)
            {
                Console.WriteLine(number);
                number = number + 1;
            }

	    // the console will close if you press Return on the keyboard
            Console.ReadLine();
        }
    }
}

The result is:
0
1
2
3
4

By |CSharp, Video Games Development|Commenti disabilitati su CSharp – while Statement

CSharp – Switch Statement

CSharp – Switch Statement

The switch statement is like a set of if statements. It’s a list of possibilities, with an action for each possibility, and an optional default action, in case nothing else evaluates to true.

switch case


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int caseSwitch = 1;
            switch (caseSwitch)
            {
                case 1:
                    Console.WriteLine("Case 1");
                    break;
                case 2:
                    Console.WriteLine("Case 2");
                    break;
                // if case 1 and case 2 are false:
                default:
                    Console.WriteLine("Default case");
                    break;
            }
        } // End Main()
    }
}

switch case – while

How to create a loop:

...
case 4:
    while (true)
        Console.WriteLine("Endless looping. . . .");
...
By |CSharp, Video Games Development|Commenti disabilitati su CSharp – Switch Statement

CSharp – Variables and Data Types

CSharp – Variables and Data Types

Data types

You are required to inform the compiler about which data types you wish to use every time you declare a variable.

byte —> 0 .. 255

sbyte —> -128 .. 127

short —> -32,768 .. 32,767

ushort —> 0 .. 65,535

int —> -2,147,483,648 .. 2,147,483,647

uint —> 0 .. 4,294,967,295

long —> -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807

ulong —> 0 .. 18,446,744,073,709,551,615

float —> -3.402823e38 .. 3.402823e38

double —> -1.79769313486232e308 .. 1.79769313486232e308

decimal —> -79228162514264337593543950335 .. 79228162514264337593543950335

char —> a Unicode character.

string —> a string of Unicode characters.

bool —> true or false.

object —> an object.

Example with strings:


// Working with strings

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // <datatype> <name>
            string firstName = "John";
            string lastName = "Doe";

            Console.WriteLine("Name: " + firstName + " " + lastName);

            Console.WriteLine("Please enter a new first name:");
            // get new value from console 
            firstName = Console.ReadLine();

            Console.WriteLine("New name: " + firstName + " " + lastName);

            Console.ReadLine();
        }
    }
}

Example with math:


// Working with math

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            // integer numbers
            int number1, number2;

            Console.WriteLine("Please enter a number:");
            // It reads a string and converts it into an integer. 
            number1 = int.Parse(Console.ReadLine());

            Console.WriteLine("Thank you. One more:");
            number2 = int.Parse(Console.ReadLine());

            Console.WriteLine("Adding the two numbers: " + (number1 + number2));

            Console.ReadLine();
        }
    }
}

Constants

The constants refer to fixed values that the program may not alter during its execution.

The syntax is:

const <data_type> <constant_name> = value;

Example:


using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            const double pi = 3.14159; // constant declaration 
            double r;
            Console.WriteLine("Enter Radius: ");
            r = Convert.ToDouble(Console.ReadLine());
            double areaCircle = pi * r * r;
            Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle);
            Console.ReadLine();
        }
    }
}

By |CSharp, Video Games Development|Commenti disabilitati su CSharp – Variables and Data Types

C++ Classes

C++ Classes

Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members.


// Syntax
class class_name {
  access_specifier_1:
    member1;
  access_specifier_2:
    member2;
  ...
} object_names;

Working example:


// classes example
#include <iostream>
using namespace std;

class Rectangle {
    int width, height;
  public:
    void set_values (int,int);
    int area() {return width*height;}
};

void Rectangle::set_values (int x, int y) {
  width = x;
  height = y;
}

int main () {
  Rectangle rect;
  rect.set_values (3,4);
  cout << "area: " << rect.area();
  return 0;
}

The result is:
area: 12

For italian people: come funziona?

1. Viene per prima eseguita la funzione main ()

2. Rectangle rect; -> da qui quando scriverò rect farò riferimento a Rectangle

3. rect.set_values (3,4); -> è come se scrivessi Rectangle.set_values (3,4);

4. void Rectangle::set_values -> la classe Rectangle::funzione set_values riceve 3,4

5. quindi assegna alle variabili width = 3; height = 4;

6. tornando all’interno di main() -> rect.area(); -> la classe Rectangle, funzione area, ritorna width*height = 3*4 = 12

By |C++, Video Games Development|Commenti disabilitati su C++ Classes