Пример #1
0
        /// <summary>
        /// When the connect button is clicked, it calls this function.
        /// This function will check the texts boxes to determine if they are valid and then sends the information to
        /// the game controller to establish a connection.
        ///
        /// Once a connection has been established, the text boxes and connect button will gray out so they cannot be used again.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void connectButton_Click_1(object sender, EventArgs e)
        {
            if (serverAddress.Text == "")
            {
                MessageBox.Show("Please enter a server address");
                return;
            }
            if (nameBox.Text == "")
            {
                MessageBox.Show("Please enter a server address");
                return;
            }

            // Disable the controls and try to connect
            connectButton.Enabled = false;
            serverAddress.Enabled = false;
            nameBox.Enabled       = false;


            theWorld = new World();
            gc       = new GameController(theWorld);
            if (!gc.ConnectToServer(serverAddress.Text, nameBox.Text))
            {
                MessageBox.Show("Could not connect to server.\nInvalid server address entered or server connect timed out.");
                connectButton.Enabled = true;
                serverAddress.Enabled = true;
                nameBox.Enabled       = true;
                return;
            }


            while (theWorld.getWorldSize() == 0)
            {
            }                                        //world size is 0 until it the correct size is received from the server

            //once the player name and world size is receive from the server, the main game is created and drawn in drawingPanel
            // while the scoreboard is created and drawn to the right of themain game
            ClientSize            = new Size(theWorld.getWorldSize() + 200, theWorld.getWorldSize() + 25);
            drawingPanel          = new DrawingPanel(theWorld);
            drawingPanel.Location = new Point(0, 25);
            drawingPanel.Size     = new Size(theWorld.getWorldSize(), theWorld.getWorldSize());
            this.Controls.Add(drawingPanel);

            drawingPanel.BackColor = Color.Black;

            playerPanel          = new PlayerPanel(theWorld);
            playerPanel.Location = new Point(theWorld.getWorldSize(), 25);
            playerPanel.Size     = new Size(200, theWorld.getWorldSize());
            this.Controls.Add(playerPanel);

            playerPanel.BackColor = Color.White;
        }
Пример #2
0
        /// <summary>
        /// Initializes the form and contains the event listeners.
        /// </summary>
        /// <param name="ctl"></param>
        public Form1(GameController ctl)
        {
            // First initialize all the fields and components
            InitializeComponent();

            theController = ctl;
            theWorld      = theController.theWorld;
            labelDict     = new Dictionary <int, Label>();
            healthDict    = new Dictionary <int, Rectangle>();
            playerRanks   = new List <Ship>();
            shipSprites   = new List <Bitmap>();
            keys          = "";
            DISPLAY_SIZE  = new Size(20, 20);

            // Next set up all the event listeners.
            theController.RegisterServerUpdateHandler(UpdateWorld);
            this.KeyDown     += new KeyEventHandler(Form1_KeyDown);
            this.KeyUp       += new KeyEventHandler(Form1_KeyUp);
            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
            this.KeyPreview   = true;

            // Then draw the client and drawing panel at their default size
            ClientSize             = new Size(950, 800);
            drawingPanel           = new DrawingPanel(theWorld);
            drawingPanel.Location  = new Point(0, 50);
            drawingPanel.Size      = new Size(750, 750);
            drawingPanel.BackColor = Color.Black;
            this.Controls.Add(drawingPanel);

            // Finally, Initialize the sprite list and Mario ship
            string[] filePaths = Directory.GetFiles(@"..\..\..\Resource\Assets\", "*.*", SearchOption.TopDirectoryOnly);

            foreach (String fileName in filePaths)
            {
                if (fileName.Contains("coast"))
                {
                    Bitmap ship = new Bitmap(fileName);
                    ship = new Bitmap(ship, DISPLAY_SIZE);

                    shipSprites.Add(ship);
                }

                if (fileName.Contains("MarioScoreSprite"))
                {
                    Bitmap mShip = new Bitmap(fileName);
                    mShip = new Bitmap(mShip, DISPLAY_SIZE);

                    marioShip = mShip;
                }
            }
        }