示例#1
0
        private void keyisdown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                goleft = true;
            }

            if (e.KeyCode == Keys.Right)
            {
                goright = true;
            }
            if (e.KeyCode == Keys.Space && !isPressed)
            {
                isPressed = true;
            }
            if (e.KeyCode == Keys.Enter && (Ready_Go_Label.Visible || GameOverLabel.Visible))
            {
                Ready_Go_Label.Hide();
                GameOverLabel.Hide();
                setGame();
                timer1.Enabled = true;
            }
        }
示例#2
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (game.isGameOver())
            {
                GameOverLabel.Show();
                Ready_Go_Label.Show();
                if (game.GetEnemies().Count == 0)
                {
                    GameOverLabel.Text = "YOU WIN";
                }
                else
                {
                    GameOverLabel.Text = "GAME OVER";
                }
                GameOverLabel.Left  = (this.Width - GameOverLabel.Width) / 2;
                Ready_Go_Label.Text = "Press Enter to Restart";
                Ready_Go_Label.Left = (this.Width - Ready_Go_Label.Width) / 2;
                return;
            }
            game.Update(goleft, goright, isPressed);
            List <Enemy>  en     = game.GetEnemies();
            List <Bullet> bull   = game.GetBullets();
            int           Ecount = 0;
            int           Bcount = 0;

            foreach (Control c in this.Controls)
            {
                if (c is PictureBox && (string)c.Tag == "player")
                {
                    int x, y;
                    game.GetPlayerPos(out x, out y);
                    ((PictureBox)c).Top  = y; //((PictureBox)c).Height * y;
                    ((PictureBox)c).Left = x; // ((PictureBox)c).Width * x;
                }

                if (c is PictureBox && (string)c.Tag == "invader")
                {
                    if (Ecount >= en.Count)
                    {
                        //remove from form
                        this.Controls.Remove(c);
                        //release memory by disposing
                        c.Dispose();
                        break;
                    }
                    int x, y;
                    en[Ecount].GetPos(out x, out y);
                    ((PictureBox)c).Top  = y; // ((PictureBox)c).Height * y;
                    ((PictureBox)c).Left = x; // ((PictureBox)c).Width * x;
                    Ecount++;
                }

                if (c is PictureBox && (string)c.Tag == "bullet")
                {
                    if (bull.Count > Bcount)
                    {
                        int x, y;
                        bull[Bcount].GetPos(out x, out y);
                        ((PictureBox)c).Top  = y; // ((PictureBox)c).Height * y;
                        ((PictureBox)c).Left = x; // ((PictureBox)c).Width * x;
                        Bcount++;
                    }
                    else
                    {
                        ((PictureBox)c).Top  = -((PictureBox)c).Height;
                        ((PictureBox)c).Left = -((PictureBox)c).Width;
                    }
                }
            }

            while (bull.Count > Bcount)
            {
                int x, y;
                bull[Bcount].GetPos(out x, out y);
                createBullet(Bcount, x, y);
                Bcount++;
            }

            labelLives.Text = "lives: " + game.getLives().ToString();
        }