예제 #1
0
        /// <summary>
        /// Helper setup start game state
        /// </summary>
        private void InitGame()
        {
            //show modeless box
            _HelpBox      = new frmInfos();
            this.Location = new Point(10, 10);
            _HelpBox.Show();

            //set helpbox location
            _HelpBox.Location = new Point(this.Location.X + this.Width + 10, 10);

            //preset flag
            _bGameStart = false;
            _bGameOver  = false;
            _bGamePause = false;
            _bWelcome   = true;

            //reset state
            _UserInfo  = new UserInfo();
            _gameLevel = eGameLevel.Easy;
            this.Focus();

            //add main ship
            MakeAShip(0, ClientSize);

            //add one rock
            Point     center  = new Point(_rnd.Next(ClientRectangle.Width), _rnd.Next(ClientRectangle.Height));
            ShapeRock newRock = new ShapeRock(center, (ShapeRock.RockSize)_rnd.Next(3));

            _lstRock.Add(newRock);

            //call make color function
            //make a random known color every second
            MakeColor(1000);

            //make rock based on selected level
            //add asteroids on canvas based on game level
            _gr = CreateGraphics();
            MakeAsteroid((int)_gameLevel, _gr.VisibleClipBounds.Size);

            //play opening sound
            _SoundFolder    = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "Sound"));
            _soundOpening   = _SoundFolder + "\\opening.wav";
            _soundExplosion = _SoundFolder + "\\explosion-012.wav";
            _soundGameOver  = _SoundFolder + "\\Yunas-Theme.wav";
            _soundGotHit    = _SoundFolder + "\\explosion.wav";
            _soundLazer     = _SoundFolder + "\\lazer.wav";

            PlaySound(_soundOpening);

            //load images
            //LoadImages();

            //play hot sound
            PlaySound(_soundOpening);

            // Helper to load existing high score
            LoadFile();
        }
예제 #2
0
        /// <summary>
        /// Helper to update score everytime bullet hit the rock
        /// </summary>
        /// <param name="rock">input rock</param>
        private void UpdateScore(ShapeRock rock)
        {
            //use rock instance property to update
            _UserInfo._CurrentScore += rock.RScore;

            //if current score mul by 10K -> award player a life
            if ((int)_UserInfo._CurrentScore / AWARD_LIFE > _UserInfo._AwardsTimes)
            {
                _UserInfo._AwardsTimes++;
                _UserInfo._Life++;
            }

            //increase level when user get 20k, and 100k
            if (_UserInfo._AwardsTimes == (int)_MediumScore / 10000)
            {
                _gameLevel = eGameLevel.Medium;
            }

            if (_UserInfo._AwardsTimes == (int)_HardScore / 10000)
            {
                _gameLevel = eGameLevel.Hard;
            }
        }
예제 #3
0
        /// <summary>
        /// Occur when user press key
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //set proper flags based on user key pressed
            if (e.KeyCode == System.Windows.Forms.Keys.Up)
            {
                _bUp = true;
            }

            if (e.KeyCode == System.Windows.Forms.Keys.Down)
            {
                _bDown = true;
            }

            if (e.KeyCode == System.Windows.Forms.Keys.Left)
            {
                _bLeft = true;
            }

            if (e.KeyCode == System.Windows.Forms.Keys.Right)
            {
                _bRight = true;
            }

            //set thurster while airship is alive
            if (e.KeyCode == System.Windows.Forms.Keys.Up && !_Airship.IsMarkedForDeath)
            {
                _Thruster.IsThruster = true;
            }

            //if user press SPACEBAR start game
            if (e.KeyCode == System.Windows.Forms.Keys.Space)
            {
                if (_bGameOver && !_bGameStart && !_bWelcome)
                {
                    _bGameStart = false;
                    _bWelcome   = true;
                    _bGameOver  = false;

                    InitGame();
                }


                else if (_bWelcome && !_bGameStart & !_bGameOver)
                {
                    _bGameOver  = false;
                    _bWelcome   = false;
                    _bGameStart = true;
                }

                timerRender.Start();
            }

            //ESC -> pause/un-pause game
            if (e.KeyCode == System.Windows.Forms.Keys.Escape)
            {
                if (_bGamePause)
                {
                    _bGamePause = false;
                    timerRender.Start();
                }
                else
                {
                    _bGamePause = true;
                    timerRender.Stop();
                }
            }

            //show help box
            if (e.KeyCode == System.Windows.Forms.Keys.F1)
            {
                _HelpBox.Show();
            }

            //set new game level
            if (e.KeyCode == System.Windows.Forms.Keys.D1)
            {
                _gameLevel = eGameLevel.Easy;

                //call make asteroid to update the change
                MakeAsteroid((int)_gameLevel, _gr.VisibleClipBounds.Size);
            }

            else if (e.KeyCode == System.Windows.Forms.Keys.D2)
            {
                _gameLevel = eGameLevel.Medium;

                //call make asteroid to update the change
                MakeAsteroid((int)_gameLevel, _gr.VisibleClipBounds.Size);
            }

            else if (e.KeyCode == System.Windows.Forms.Keys.D3)
            {
                _gameLevel = eGameLevel.Hard;

                //call make asteroid to update the change
                MakeAsteroid((int)_gameLevel, _gr.VisibleClipBounds.Size);
            }

            //if user press SPACEBAR, also add bullets in ALLOWED LIMIT
            if (e.KeyCode == System.Windows.Forms.Keys.Space && _lstBullet.Count < ShapeBullet.BULLET_ALLOW)
            {
                ShapeBullet newBullet = new ShapeBullet(_Airship.Location, _Airship);
                _lstBullet.Add(newBullet);
                PlaySound(_soundLazer);
            }
        }