示例#1
0
        // **********************************************************************************************************************************************************************************
        // Form and Controller Initializations
        // **********************************************************************************************************************************************************************************
        public asteroidarena()
        {
            InitializeComponent();         // Initialize form

            spc  = new SpriteController(); // Initialize Sprite Controller
            sndc = new SoundController();  // Initialize Sound Controller

            // Initialize Background controller and set the background image
            bg = new BackGroundController(@"Graphics\nebulastartscreen.png", pictureMain.Width, pictureMain.Height);
            bg.SetWindow(0, 0, pictureMain.Width, pictureMain.Height);

            this.pictureMain.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureMain_Down); // Handle mouse clicks

            ScoreBoardDefinitions();                                                                         // Intialize Scoreboard
            SpriteDefinitions();                                                                             // Intialize Sprites
            SoundDefinitions();                                                                              // Intialize Sound

            new Task(EventLoop).Start();                                                                     // Start Game Event Loop
        }
示例#2
0
        // **********************************************************************************************************************************************************************************
        // Game Event Loop
        // **********************************************************************************************************************************************************************************
        private void EventLoop()
        {
            long screenrefresh_timer   = 0;
            long keyboardrefresh_timer = 0;
            long fps_timer             = 0;
            long seconds_left;                    // How long the match lasts
            long ms = Stopwatch.Frequency / 1000; // Precalculate timers for better speed
            long scntm;
            long keytm = ms * 50;                 // Check Keyboard every 50ms
            long fpstm = ms * 1000;               // Show Scoreboard every second

            if (frameLock)                        // Framelock?
            {
                scntm = ms * 1000 / 100;          // Framelock 100fps
            }
            else
            {
                scntm = 0;
            }

            sw = Stopwatch.StartNew(); // Start high resolution timer

            // Show Startup and countdown screens
            Sprite.SetSpritesEnabled(false);    // Show only background to start

            pictureMain.Invalidate();           // Show the opening screen

            while (waiting)                     // Wait for spacebar press
            {
                tm = sw.ElapsedTicks;           // Get the current tick
                if (keyboardrefresh_timer < tm) // Keyboard Check
                {
                    CheckKeyboard();
                    keyboardrefresh_timer = tm + keytm;
                }
            }

            scoreboardLocation = new RectangleF(pictureMain.Width / 2 - 50, pictureMain.Height / 2 - 100, 250, 250); // Location for countdown text
            scoreboardFont     = new Font("Lucida Sans", 100);                                                       // Font for Scoreboard text
            scoreboardBrushes  = System.Drawing.Brushes.Red;                                                         // Brush Color for scoreboard text
            waiting            = true;
            seconds_left       = 5;                                                                                  // 5 second count down

            while (waiting)                                                                                          // Wait for count down
            {
                tm = sw.ElapsedTicks;                                                                                // Get the current tick

                if (fps_timer < tm)                                                                                  // Update scoreboard
                {
                    scoreboard = seconds_left.ToString();                                                            // Scoreboard text
                    pictureMain.Invalidate();                                                                        // Render text

                    if (seconds_left < 1)
                    {
                        waiting = false;                   // Check countdown finished
                    }
                    seconds_left--;

                    fps_timer = tm + fpstm;
                }
            }

            seconds_left = 120;                                                              // 2 minute round

            Sprite.SetSpritesEnabled(true);                                                  // Show the sprites to start the game

            bg.SetBackground("Graphics\\nebula.png", pictureMain.Width, pictureMain.Height); // Change to game screen background
            bg.SetWindow(0, 0, pictureMain.Width, pictureMain.Height);

            scoreboardLocation = new RectangleF(pictureMain.Width / 2 - 150, 20, 300, 50);                                                                  // Location for scoreboard text
            scoreboardFont     = new Font("Tahoma", 14);                                                                                                    // Font for Scoreboard text
            scoreboardBrushes  = System.Drawing.Brushes.Chartreuse;                                                                                         // Brush Color for scoreboard text
            scoreboard         = "P1: " + player1_points + "  P2: " + player2_points + "   SL:" + seconds_left.ToString() + "   " + fps.ToString() + "fps"; // Scoreboard text

            // Main Game Loop
            while (running)
            {
                tm = sw.ElapsedTicks;         // Get the current tick

                if (screenrefresh_timer < tm) // Screen Refresh
                {
                    pictureMain.Invalidate();
                    screenrefresh_timer = tm + scntm;
                }

                if (keyboardrefresh_timer < tm) // Keyboard Check
                {
                    CheckKeyboard();
                    keyboardrefresh_timer = tm + keytm;
                }

                if (fps_timer < tm) // Update scoreboard and FPS
                {
                    fps        = frames;
                    scoreboard = "P1: " + player1_points + "  P2: " + player2_points + "   SL:" + seconds_left.ToString() + "   " + fps.ToString() + "fps"; // Scoreboard text

                    if (seconds_left < 0)
                    {
                        running = false;                   // Check if Game Over
                    }
                    seconds_left--;

                    frames    = 0;
                    fps_timer = tm + fpstm;
                }
            }

            // End the game!
            scoreboard = "P1: " + player1_points + "  P2: " + player2_points + " GAME OVER!"; // Scoreboard text
            pictureMain.Invalidate();

            sw.Stop();                           // Stop the timer

            System.Threading.Thread.Sleep(5000); // Allow Game Over screen for 5 sec.

            Application.Restart();               // Restart the game
            Environment.Exit(0);                 // Exit old application
        }