First Step CSharp

From SwinGame

This tutorial guides you through the process of really getting started with the SwinGame SDK. In this tutorial you will learn to open a window, draw images, draw sprites and move, play sounds, and have some interactivity within your game. At the end of the tutorial you should be much more comfortable using the SwinGame SDK and ready to start working on your own game.

OK so at this stage you have:

Now it is time for your first program in Swingame: "Hello World".

This page contains a Tutorial. Tutorials are designed to walk you through the development of a small game.

Hello World

Follow these steps to get a "Hello World" program working with SwinGame.

  1. Open Visual Studio, and click File > New > Project.
  2. Select the Visual C# Language and Click the SwinGame C# Project Template. Click OK.
  3. Enter "FirstProgram" as the name of the project, and choose the location it is to be saved and click OK.
  4. In the Solution Explorer on the right hand side of the screen (see picture below) double click on "GameLogic". You should be presented with the following code:
Note: The Project Name will be the name of the executable file you create for your game in the end as well.
Choose the C# Template
Solution Explorer


using System;
using System.Drawing;
 
using SwinGame;
using Graphics = SwinGame.Graphics;
using Bitmap = SwinGame.Bitmap;
using Font = SwinGame.Font;
using FontStyle = SwinGame.FontStyle;
using Event = SwinGame.Event;
using CollisionSide = SwinGame.CollisionSide;
using Sprite = SwinGame.Sprite;
 
using GameResources;
 
namespace FirstProject
{
    public static class GameLogic
    {
        public static void RunGame()
        {
            //Open a new Graphics Window
            Core.OpenGraphicsWindow("Game", 800, 600);
            //Open Audio Device
            Audio.OpenAudio();
            //Load Resources
            Resources.LoadResources();
 
            //Game Loop
            do
            {
                //Clears the Screen to Black
                Graphics.ClearScreen();
 
                Graphics.FillRectangle(Color.Red, 20, 200, 200, 100);
                Graphics.FillRectangle(Color.Green, 220, 200, 200, 100);
                Graphics.FillRectangle(Color.Blue, 420, 200, 200, 100);
 
                Text.DrawText("Hello World", Color.Red, Resources.GameFont("Courier"), 20, 310);
                Text.DrawText("Hello World", Color.Green, Resources.GameFont("Courier"), 220, 310);
                Text.DrawText("Hello World", Color.Blue, Resources.GameFont("Courier"), 420, 310);
 
                Text.DrawFramerate(0, 0, Resources.GameFont("Courier"));
                Text.DrawText("Hello World", Color.White, Resources.GameFont("ArialLarge"), 50, 50);
 
                //Refreshes the Screen and Processes Input Events
                Core.RefreshScreen();
                Core.ProcessEvents();
            } while (!Core.WindowCloseRequested());
 
            //Free Resources and Close Audio, to end the program.
            Resources.FreeResources();
            Audio.CloseAudio();
        }
    }
}

The purpose of this code is to create a starting point for your project. If you press the "Start Debugging" button at the top of the screen (looks like a green arrowImage:StartDebugging.PNG, F5 works too) you will see what it does. After you have had a look close the window.

You can basically read what it is doing
//comments are in green, and start with a two forward slashes at the start like this sentence 

See if you can find the parts of the code that are doing the following things:

  • Loading all the resources you need for the game
  • Opening up a blank screen 800 pixels wide (x axis) by 600 tall (y axis) - see picture below
  • Starting a Loop, inside which it does the following:
    • Clearing the window
    • Drawing 3 filled rectangles, setting their sizes and different colours width height and position
    • Putting the game's "Frame rate" up on the screen in the top right hand corner
    • Writing the words "Hello World" 3 times in different colours below the rectangles
    • Writing the words "Hello World" once at the top in large letters
  • Telling the loop to stop when someone closes the window
Screen Layout

Now you do this...

To help your understanding of what is going on in this code do the following:

  • Change the big "Hello World" to your name
  • Move the Red Rectangle so it is in the bottom right hand corner
  • Make the Red Rectangle twice as big

And that is it! You have completed your "Hello World" next we look at adding an image.