C#小游戏—钢铁侠VS太空侵略者

  身为漫威迷,最近又把《钢铁侠》和《复仇者联盟》系列又重温了一遍,真的是印证了那句话:“读书百遍,其意自现”。看电影一个道理,每看一遍,都有不懂的感受~ 不知道大伙是不是也有同样的感受,对于好的电影,真的是回味无穷!


     本篇博文也是因《复仇者联盟1》的启发,C#语言实现的一个小游戏,所以游戏命名就叫“钢铁侠VS太空侵略者》了!

     先上一个游戏原型图:

 

   Talk is Cheap,Show me the Code!

       代码方面没有太难的操作,主要依赖于Timer控件:

  

  

    分别用来监控游戏中Iron man 子弹移动,侵略者左右移动,往下移动,侵略者子弹移动,子弹碰撞,以及观察者监控(主要校验生命值),具体代码如下:

       侵略者界面生成:

private void CreateControl(Form p)
{
    PictureBox pb = new PictureBox();
    pb.Location = new Point(x, y);
    pb.Size = new Size(width, height);
    pb.BackgroundImage = Properties.Resources.invader;
    pb.BackgroundImageLayout = ImageLayout.Stretch;
    pb.Name = "Alien";
    p.Controls.Add(pb); 
}
public void CreateSprites(Form p)
{
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            CreateControl(p);
            x += width + space; 
        }
        y += height + space;
        x = 150; 
    }
}

   键盘事件绑定:

private void Pressed(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.A || e.KeyCode == Keys.Left)
    {
        moveLeft = true;
    }
    else if (e.KeyCode == Keys.D || e.KeyCode == Keys.Right)
    {
        moveRight = true;
    }
    else if (e.KeyCode == Keys.Space && game && !fired)
    {
        Missile();
        fired = true;
    }
}
private void Released(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.A || e.KeyCode == Keys.Left)
    {
        moveLeft = false;
    }
    else if (e.KeyCode == Keys.D || e.KeyCode == Keys.Right)
    {
        moveRight = false;
    }
    else if (e.KeyCode == Keys.Space)
    {
        fired = false;
    }
}

  Iron man 左右移动:

private void PlayerMove(object sender, EventArgs e)
  {
      if (moveLeft && Player.Location.X >= 0)
      {
          Player.Left--;
      }
      else if (moveRight && Player.Location.X <= limit)
      {
          Player.Left++;
      }
  }

   子弹发射:

 private void FireBullet(object sender, EventArgs e)
{
    foreach (Control c in this.Controls)
    {
        if (c is PictureBox && c.Name == "Bullet")
        {
            PictureBox bullet = (PictureBox)c;
            bullet.Top -= 5;

            if (bullet.Location.Y <= 0)
            {
                this.Controls.Remove(bullet); 
            }

            foreach(Control ct in this.Controls)
            {
                if (ct is PictureBox && ct.Name == "Laser")
                {
                    PictureBox laser = (PictureBox)ct;

                    if (bullet.Bounds.IntersectsWith(laser.Bounds))
                    {
                        this.Controls.Remove(bullet);
                        this.Controls.Remove(laser);
                        pts++;
                        Score(pts);
                    }
                }
            }

            foreach(Control ctrl in this.Controls)
            {
                if (ctrl is PictureBox && ctrl.Name == "Alien")
                {
                    PictureBox alien = (PictureBox)ctrl;

                    if (bullet.Bounds.IntersectsWith(alien.Bounds) && !Touched(alien))
                    {
                        this.Controls.Remove(bullet);
                        this.Controls.Remove(alien);
                        aliens.Remove(alien);
                        pts += 5;
                        Score(pts);
                        CheckForWinner();
                    }
                    else if (bullet.Bounds.IntersectsWith(alien.Bounds) && Touched(alien))
                    {
                        this.Controls.Remove(bullet);
                        this.Controls.Remove(alien);
                        delay.Add(alien);
                        pts += 5;
                        Score(pts);
                        CheckForWinner();
                    }
                }
            }
        }
    }
}

    子弹

private void Missile()
{
    PictureBox bullet = new PictureBox();
    bullet.Location = new Point(Player.Location.X + 
    Player.Width / 2, Player.Location.Y - 20);
    bullet.Size = new Size(5, 20);
    bullet.BackgroundImage = Properties.Resources.bullet;
    bullet.BackgroundImageLayout = ImageLayout.Stretch;
    bullet.Name = "Bullet";
    this.Controls.Add(bullet);
}

   侵略者移动:

private void AlienMove()
{            
    foreach(PictureBox alien in aliens)
    {
        alien.Location = new Point(alien.Location.X + left, 
        alien.Location.Y + top);
        SetDirection(alien);
        Collided(alien);                
    }
}
private void Collided(PictureBox a)
{
    if (a.Bounds.IntersectsWith(Player.Bounds))
    {
        gameOver();
    }
}

   子弹移动效果:

 private void Beam(PictureBox a)
{
    PictureBox laser = new PictureBox();
    laser.Location = new Point(a.Location.X + a.Width / 3,
     a.Location.Y + 20);
    laser.Size = new Size(5, 20);
    laser.BackgroundImage = Properties.Resources.laser;
    laser.BackgroundImageLayout = ImageLayout.Stretch;
    laser.Name = "Laser";
    this.Controls.Add(laser);
}
private void StrikeSpan(object sender, EventArgs e)
{
    Random r = new Random();
    int pick;

    if (aliens.Count > 0)
    {
        pick = r.Next(aliens.Count);
        Beam(aliens[pick]);
    }
}
private void DetectLaser(object sender, EventArgs e)
{
    foreach(Control c in this.Controls)
    {
        if (c is PictureBox && c.Name == "Laser")
        {
            PictureBox laser = (PictureBox)c;
            laser.Top += 5;

            if (laser.Location.Y >= limit)
            {
                this.Controls.Remove(laser); 
            }
            if (laser.Bounds.IntersectsWith(Player.Bounds))
            {
                this.Controls.Remove(laser); 
                LoseLife(); 
            }                    
        }
    }
}

  主要核心代码如上,下面看下运行效果图:

    何以解忧唯有撸码,欢迎有兴趣的朋友,联系我一起探讨。

    最后感谢您的耐心观看,赠人玫瑰,手留余香,觉得本文有些许意思和启发的,记得关注博主公众号(内附源码链接),您的支持,就是我写作莫大的动力!

原文地址:https://www.cnblogs.com/axing/p/12436355.html