Exemple #1
0
        private void Play_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
            case Keys.Left:
                moveLeft = true;
                break;

            case Keys.Right:
                moveRight = true;
                break;

            case Keys.Space:
                if (!Globals.missileFired)
                {
                    // Get missile location at the moment of fire
                    missileX = (Globals.spaceships[Globals.selectedSpaceship].X + (Consts.PLAYER_WIDTH / 2) - (Consts.MISSILE_WIDTH / 2));
                    Globals.spaceships[Globals.selectedSpaceship].Shoot();
                    SoundEffects.fire.Play();
                }
                break;

            case Keys.Escape:
                Globals.message = "GAME OVER";
                Globals.allRecords.Add(new Record(score, Globals.spaceships[Globals.selectedSpaceship].Name));
                score           = 0;
                tmrPlay.Enabled = false;
                this.Hide();
                Leaderboards leaderboards = new Leaderboards();
                leaderboards.Show();
                break;
            }
        }
Exemple #2
0
        private void CheckIfGameOver()
        {
            // Execute if player won
            if (aliensLeft <= 0)
            {
                if (Globals.selectedLevel >= Globals.allLevels.Count - 1)
                {
                    Globals.message = "VICTORY!";
                    // Update scores
                    Globals.allRecords.Add(new Record(score, Globals.spaceships[Globals.selectedSpaceship].Name));
                    // Reset score for the next game
                    score = 0;

                    this.Hide();
                    Leaderboards leaderboards = new Leaderboards();
                    leaderboards.Show();
                    tmrPlay.Enabled = false;
                }
                else
                {
                    int milliseconds = 2000;
                    Thread.Sleep(milliseconds);
                    Globals.selectedLevel++;
                    GameReset();
                }
            }
            // Checking alien-player collision
            aliens.ForEach(a =>
            {
                Globals.spaceships[Globals.selectedSpaceship].RightEdge = Globals.spaceships[Globals.selectedSpaceship].X + Consts.PLAYER_WIDTH;

                if (a.Hp > 0)
                {
                    if (gameOver == false &&
                        Globals.spaceships[Globals.selectedSpaceship].X <= a.RightEdge &&
                        Globals.spaceships[Globals.selectedSpaceship].RightEdge >= a.X &&
                        Consts.PLAYER_Y <= a.BottomEdge &&
                        Consts.PLAYER_BOTTOM_EDGE >= a.Y)
                    {
                        gameOver = true;
                    }
                }
            });
            // Checking alien-bottom collision
            aliens.ForEach(a =>
            {
                if (a.BottomEdge >= Consts.PLAYER_BOTTOM_EDGE)
                {
                    gameOver = true;
                }
            });
            // Execute if player lost
            if (gameOver)
            {
                SoundEffects.destruction.Play();

                Globals.message = "GAME OVER";
                Globals.allRecords.Add(new Record(score, Globals.spaceships[Globals.selectedSpaceship].Name));
                score = 0;

                this.Hide();
                Leaderboards leaderboards = new Leaderboards();
                leaderboards.Show();
                tmrPlay.Enabled = false;
            }
        }