Пример #1
0
        public Form1()
        {
            InitializeComponent();

            controller = new ClientController();
            controller.InputArrived += DisplayInput;
            controller.error        += ErrorEvent;

            theWorld = controller.getWorld();
            panel    = new DrawingPanel(theWorld, controller);
            //panel.Location = new Point(0, menuSize);
            panel.Location = new Point(0, 0);
            panel.Size     = new Size(viewSize, viewSize);
            this.Controls.Add(panel);


            FormClosed += OnExit;

            this.KeyDown      += HandleKeyDown;
            this.KeyUp        += HandleKeyUp;
            panel.MouseDown   += HandleMouseDown;
            panel.MouseUp     += HandleMouseUp;
            panel.MouseMove   += HandleMouseMove;
            nameBox.Text       = "yo";
            serverAddress.Text = "localhost";
        }
Пример #2
0
        public Form1()
        {
            InitializeComponent();
            // Set the desired size and background color
            ClientSize     = new Size(Constants.VIEWSIZE, Constants.VIEWSIZE + Constants.MENUSIZE);
            this.BackColor = Color.Black;

            // Make a new controller
            controller = new GameController.GameController();

            // register handlers for the controller's events
            controller.newInformation += UpdateView;
            controller.Error          += ShowError;
            controller.Connected      += HandleConnected;
            controller.AllowInput     += StartGameFunctionality;

            // Place and add the drawing panel
            drawingPanel          = new DrawingPanel(controller);
            drawingPanel.Location = new Point(0, Constants.MENUSIZE);
            drawingPanel.Size     = new Size(Constants.VIEWSIZE, Constants.VIEWSIZE);
            this.Controls.Add(drawingPanel);

            //register key handlers
            this.KeyDown += HandleKeyDown;
            this.KeyUp   += HandleKeyUp;
        }
Пример #3
0
        /// <summary>
        /// Initializes the Game after connection to server
        /// </summary>
        /// <param name="WorldSize"></param>
        private void IntilializeGame(int WorldSize)
        {
            MethodInvoker m = new MethodInvoker(() =>
            {
                //create the drawingPanel
                drawingPanel           = new DrawingPanel(WorldSize, Controller.theWorld);
                drawingPanel.Location  = new Point(0, 30);
                drawingPanel.Size      = new Size(WorldSize, WorldSize);
                this.Size              = new Size(WorldSize + 200, WorldSize + 70);
                drawingPanel.BackColor = Color.Black;
                this.Controls.Add(drawingPanel);
                drawingPanel.Focus();

                //create the scoreboard
                scoreBoard           = new ScoreBoard(Controller.theWorld);
                scoreBoard.Location  = new Point(WorldSize, 30);
                scoreBoard.Size      = new Size(200, WorldSize);
                scoreBoard.BackColor = Color.White;

                this.Controls.Add(scoreBoard);

                Controller.WorldUpdated += UpdateWorld;

                this.KeyDown += Form1_KeyDown;
                this.KeyUp   += Form1_KeyUp;
                this.Invalidate(true);
            });

            this.Invoke(m);
        }
Пример #4
0
        /// <summary>
        /// Receives information about the world and adds the DrawingPanel
        /// so that the world can begin being drawn.
        /// </summary>
        /// <param name="state"></param>
        private void ReceiveStartup(SocketState state)
        {
            String[] response = Regex.Split(state.Builder.ToString(), @"(?<=[\n])");
            theWorld = new World(int.Parse(response[1]));
            // Change the client size based on the received worldSize; constant number is so the DrawingPanel
            // is not drawn where the buttons are, and to provide space for the scoreboard.
            this.Invoke(new MethodInvoker(() => ClientSize           = new Size(theWorld.WorldSize + 250, theWorld.WorldSize + 30)));
            this.Invoke(new MethodInvoker(() => this.FormBorderStyle = FormBorderStyle.FixedSingle));
            drawingPanel           = new DrawingPanel(theWorld);
            drawingPanel.Location  = new Point(0, 30);
            drawingPanel.Size      = new Size(theWorld.WorldSize, theWorld.WorldSize);
            drawingPanel.BackColor = Color.Black;

            // Add scoreboard
            scorePanel          = new ScorePanel(theWorld);
            scorePanel.Location = new Point(theWorld.WorldSize, 30);
            scorePanel.Size     = new Size(ClientSize.Width - theWorld.WorldSize, theWorld.WorldSize);

            // Add the drawingPanel and invalidate the client to have it redrawn
            this.Invoke(new MethodInvoker(() => this.Controls.Add(drawingPanel)));
            this.Invoke(new MethodInvoker(() => this.Controls.Add(scorePanel)));
            this.Invoke(new MethodInvoker(() => this.Invalidate()));
            this.Invoke(new MethodInvoker(() => this.redrawTimer.Enabled = true));
            this.Invoke(new MethodInvoker(() => this.redrawTimer.Start()));

            keyCapture = new Thread(KeyCapturer);
            keyCapture.SetApartmentState(ApartmentState.STA);
            keyCapture.Start();

            state.Builder.Clear();
            state.CallMe = ReceiveWorld;
            Network.GetData(state);
        }
Пример #5
0
        //constructor, initlize the event handlers
        public SpaceWars()
        {
            InitializeComponent();
            gc = new GController();

            gc.RegisterTick(redraw);
            gc.RegisterKey(HandleKeyEvent);
            gc.RegisterStep(getStep);
            gc.RegisterResize(resize);
            panel = new DrawingPanel(gc.getWorld());
            score = new ScorePanel(gc.getWorld());

            gc.RegisterShip(score.addShips);
            gc.RegisterDie(score.DiedShips);
            panel.Location  = new System.Drawing.Point(0, 40);
            panel.Size      = new Size(750, 750);
            this.Size       = new Size(1000, 835);
            panel.BackColor = Color.Black;



            this.Controls.Add(panel);
            isLeft   = false;
            isRight  = false;
            isThrust = false;
            isFire   = false;
        }
Пример #6
0
        /// <summary>
        /// The client's last part of the 3-way handshake. Change the callMe delegate to its final
        /// incarnation, to continue receiving and processing the World from the Server. If something went wrong
        /// in the NetworkingLibrary, the SocketState will have an error, and the user will have
        /// another chance to connect.
        /// </summary>
        /// <param name="state"></param>
        private void ReceiveStartup(SocketState state)
        {
            if (state.hasError)
            {
                NetworkError();
            }

            if (state.sBuilder == null)
            {
                return;
            }
            // get the player ID and world size out of state.sBuilder
            string info = state.sBuilder.ToString();

            string[] infoArray = info.Split('\n');

            if (infoArray.Length < 2)
            {
                return;
            }
            if (!(Int32.TryParse(infoArray[0], out playerID) && Int32.TryParse(infoArray[1], out worldSize)))
            {
                return;
            }

            // recreate the World
            theWorld = new World(worldSize);

            // create the drawingPanel, based on the new World.
            drawingPanel          = new DrawingPanel(theWorld);
            drawingPanel.Location = new Point(0, menuStrip.Height);
            drawingPanel.Size     = new Size(worldSize, worldSize);

            // create the scoreboardPanel, based on the new World.
            scoreboardPanel          = new ScoreboardPanel(theWorld);
            scoreboardPanel.Location = new Point(worldSize, menuStrip.Height);

            // Ask the form to Invoke Methods to add the drawingPanel and resize the form
            MethodInvoker addDrawingPanel  = new MethodInvoker(() => this.Controls.Add(drawingPanel));
            MethodInvoker addScorePanel    = new MethodInvoker(() => this.Controls.Add(scoreboardPanel));
            MethodInvoker resizeClientForm = new MethodInvoker(() => ClientSize = new Size(worldSize + scoreboardPanel.Width, worldSize + menuStrip.Height));

            try
            {
                this.Invoke(resizeClientForm);
                this.Invoke(addDrawingPanel);
                this.Invoke(addScorePanel);
            }
            catch (Exception)
            {
            }

            state.sBuilder.Clear();

            // Update the action to take when network events happen
            state.callMe = ReceiveWorld;

            // Start waiting for data
            Networking.GetData(state);
        }
Пример #7
0
        /// <summary>
        /// The constructor of Form1 that also watches for Mouse Moved Events
        /// </summary>
        /// <param name="control"></param>
        public Form1(Controller control)
        {
            InitializeComponent();
            mainCtrl = control;
            theWorld = mainCtrl.getWorld();
            mainCtrl.registerUpdateHandler(OnFrame);

            ClientSize            = new Size(800, 800);
            drawingPanel          = new DrawingPanel(theWorld);
            drawingPanel.Location = new Point(0, 0);
            drawingPanel.Size     = new Size(this.ClientSize.Width, this.ClientSize.Height);
            this.Controls.Add(drawingPanel);
            this.drawingPanel.MouseMove  += new MouseEventHandler(this.drawingPanel_MouseMove);
            this.drawingPanel.MouseClick += new MouseEventHandler(this.drawingPanel_MouseClick);
        }
Пример #8
0
        /// <summary>
        /// Represents a SpaceWars form, creating all the controls necessary for the game to function.
        /// </summary>
        public SpaceWars()
        {
            InitializeComponent();
            theWorld        = new World();
            inputController = new InputController();

            drawPanel           = new DrawingPanel();
            drawPanel.Location  = new Point(0, 58);
            drawPanel.BackColor = Color.Black;
            drawPanel.Size      = new Size(750, 750);

            scoreBoard           = new Scoreboard(theWorld);
            scoreBoard.Location  = new Point(750, 58);
            scoreBoard.Size      = new Size(230, 750);
            scoreBoard.BackColor = Color.LightGray;

            this.Controls.Add(drawPanel);
            this.Controls.Add(scoreBoard);

            this.AcceptButton     = connectButton;
            connectButton.Enabled = false;
        }