Tuesday, 22 April 2014

C# Source Code Post #1

Hey guys, sorry this post is kinda late. Internet's been broken lately, hoping to get it fixed but to do so I need to buy a new ethernet adapter PCIe card. It's relatively cheap, only around £8 ($13.44), but sadly that's a lot to save up when my average daily income is around $0.02 from ads on this blog :/

Anyway, you don't want to hear my cry about First world problems, you want me to post some big, fat, juicy code :3

So, as some of you may have guessed, I will be posting AwesomeRPG source code.
"But why would we want to see that?" You ask, recalling that the source is readily available on the SourceForge page.

It's quite simple, in these source code posts, I will not only be posting the source code, but heavily commenting it and explaining, in detail, what everything does.

This is TitleScreen.cs, basically the main menu code.
I'm not going to bother posting Program.cs, the class that runs when you run the program, because all that does is run this class xD

##File Start##
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//Default using statements for windows forms classes.

namespace Game
//Imaginative Namespace
{
    public partial class TitleScreen : Form
    //We haven't explored partial classes or extentions yet
    // but this is a partial class that extends the windows class "Form"
    {    
        //Constructor-ey goodness
        public TitleScreen()
        {

            //The InitLoad.PreLoading() method attempts to load all the startup content
            //If it fails it returns false, at which point the game closes
            InitializeComponent();
            if (!InitLoad.PreLoading()) GameExit();
        }
        //If you click the exit button.. Run the "GameExit" method (Guess what that does...)
        private void btn_Exit_Click(object sender, EventArgs e)
        {
            GameExit();
        }
        //If you click the options button the game will troll you.
        private void btn_Options_Click(object sender, EventArgs e)
        {
            MessageBox.Show("The options menu is not currently activated.\nPlease enable this feature in the options menu.", "Feature disabled");
            //Yes, it just told you to enable the Options menu in the Options menu
            //This is because I haven't made the options menu yet, and I like to troll people
        }
        //If you click the NewGame Button, you will be taken to (surprise!) the New game screen
        private void btn_New_Click(object sender, EventArgs e)
        {
            //Hide the main menu
            this.Hide();
            //Find if there is already a hidden instance of the NewGame screen running
            foreach (Form frm in Application.OpenForms)
            {
                if (frm is NewGame)
                {
                    //If there is, un-hide it
                    frm.Show();
                    return;
                }
            }
            //Otherwise, make one and show it
            new NewGame().Show();
        }
        //If you click the LoadGame Button, you will be taken to (surprise!) the Load game screen
        private void btn_Load_Click(object sender, EventArgs e)
        {
            //Hide the main menu
            this.Hide();
            //Find if there is already a hidden instance of the LoadGame screen running
            foreach (Form frm in Application.OpenForms)
            {
                if (frm is LoadGame)
                {
                    //If there is, un-hide it
                    frm.Show();
                    return;
                }
            }
            //Otherwise, make one and show it
            new LoadGame().Show();
        }
        //When this method is run, surprisingly it exits the game
        public void GameExit()
        {
            //Betcha didn't guess that!
            Application.Exit();
        }
    }
}


Woah boy!
There are a few things in that class you might not understand.

InitializeComponent();

Dafuq does this do!?
Well, it does exactly what it says, "Initialize Component". It's actually a default method in the constructor of all windows forms classes.

if (!InitLoad.PreLoading()) GameExit();

This, believe it or not, is a simple if statement.
If InitLoad.PreLoading (The function PreLoading in the static class InitLoad) returns false, run GameExit() (Exit the game)

The code for the InitLoad class will have to wait 6 days I'm afraid >:D

private void btn_Exit_Click(object sender, EventArgs e)
{
    GameExit();
}

This method runs when you click the exit button, and runs GameExit()

        private void btn_Options_Click(object sender, EventArgs e)
        {
            MessageBox.Show("The options menu is not currently activated.\nPlease enable this feature in the options menu.", "Feature disabled");
            //Yes, it just told you to enable the Options menu in the Options menu
            //This is because I haven't made the options menu yet, and I like to troll people
        }

This method is when you click the options button, and it pops up a message saying "The options menu is not currently activated." and on the second line "Please enable this feature in the options menu." The title of the message box is "Feature disabled"

private void btn_New_Click(object sender, EventArgs e)
{
    //Hide the main menu
    this.Hide();
    //Find if there is already a hidden instance of the NewGame screen running
    foreach (Form frm in Application.OpenForms)
    {
        if (frm is NewGame)
        {
            //If there is, un-hide it
            frm.Show();
            return;
        }
    }
    //Otherwise, make one and show it
    new NewGame().Show();
}

Getting tricky now.
This method runs when the New Game button is clicked.
Firstly

this.Hide();

runs the hide method within the instance of TitleScreen.. but wait, there's no Hide method. Remember, this is a partial class and extends the form class, the Hide method can be found in Form.cs

Next, it loops through every Form in "Application.OpenForms" which is pretty self explanatory.
With every form it finds in "Application.OpenForms" if checks if it is the form "NewGame". If it is, it un-hides it, if none of them are, it creates a new one and shows it.

The next method does the same, however it is activated on clicking Load Game, and it opens the LoadGame form, rather than the NewGame form.

And finally, GameExit() which exits the game when triggered.

And thus ends the explanation of the source code for the Title Screen of AwesomeRPG.

Next Wednesday we will discuss Static classes, and Friday will be Partial classes and extending other classes.

For those only interested in these source code posts, the next one will be next Sunday, and it will be the InitLoad class.


No comments:

Post a Comment