65.9K
CodeProject 正在变化。 阅读更多。
Home

EmuEngine

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.51/5 (12投票s)

2006年11月16日

1分钟阅读

viewsIcon

64589

downloadIcon

610

使用EmuEngine,你可以用C#创建简单的2D游戏。

引言

使用EmuEngine,你可以用C#创建简单的2D游戏,它是一个小型库,包含以下类:

使用EmuEngine,你可以用C#创建简单的2D游戏,它是一个小型库,包含以下类:

  • DrawingArea:这是“主”类。它处理Picture对象、PictureWall对象和PixelWall对象。它还包含一个Collision函数。仅此而已,但对于像示例项目中的测试游戏这样的简单2D游戏来说已经足够了。
  • Picture:它代表一张图片。DrawingArea类有一个名为AddPicture的函数。使用该函数,你可以将图片添加到游戏中。
  • PictureWall:几乎与Picture类相同。你可以使用DrawingArea类的AddPictureWall函数将PictureWall对象添加到游戏中。
  • PixelWall:构造函数接受一个Color,这是一种向游戏中添加彩色墙壁的好方法。

项目中的测试应用程序的代码

  public partial class Form1 : Form
  {

    public MediaPlayer status;
    public MediaPlayer eat;
    public delegate void Walk(int speed);

    static int speed = 0;
    static int score = 0;

    Walk move;
    DrawingArea area;

    Picture player;
    Picture enemy;

    Picture gameOver;
    Picture gameWin;
    
    public Form1()
    {
      InitializeComponent();
      area = new DrawingArea(doubleBufferedPanel1);

      player = new Picture("pacmanUp.GIF", 300, 100, 30, 30);
      enemy = new Picture("enemy.jpg", 100, 100, 20, 23);

      area.AddPicture(new Picture("Food.gif", 200, 50, 10, 10));
      area.AddPicture(new Picture("Food.gif", 200, 50, 10, 10));
      area.AddPicture(new Picture("Food.gif", 100, 200, 10, 10));
      area.AddPicture(new Picture("Food.gif", 300, 300, 10, 10));
      area.AddPicture(new Picture("Food.gif", 400, 54, 10, 10));
      area.AddPicture(new Picture("Food.gif", 150, 200, 10, 10));
      area.AddPicture(new Picture("Food.gif", 500, 400, 10, 10));
      area.AddPicture(new Picture("Food.gif", 123, 400, 10, 10));
      area.AddPicture(new Picture("Food.gif", 345, 65, 10, 10));
      area.AddPicture(new Picture("Food.gif", 234, 40, 10, 10));
      area.AddPicture(new Picture("Food.gif", 46, 400, 10, 10));
      area.AddPicture(new Picture("Food.gif", 123, 300, 10, 10));
      area.AddPicture(new Picture("Food.gif", 321, 123, 10, 10));

      gameOver = new Picture("gameover.gif", 140, 100, 280, 281);
      gameWin = new Picture("gamewin.gif", 140, 100, 280, 281);

      area.AddPixelWall(new PixelWall(Color.LightBlue, 30,30, 5, 
           doubleBufferedPanel1.Height - 60));
      area.AddPixelWall(new PixelWall(Color.LightBlue, 30, 30, 
           doubleBufferedPanel1.Width - 60, 5));
      area.AddPixelWall(new PixelWall(Color.LightBlue, 
           doubleBufferedPanel1.Width - 30, 30, 5, 
           doubleBufferedPanel1.Height - 60));
      area.AddPixelWall(new PixelWall(Color.LightBlue, 30, 
           doubleBufferedPanel1.Height - 30, 
           doubleBufferedPanel1.Width - 55, 5));

      area.AddPicture(player);
      area.AddPicture(enemy);

      timer1.Enabled = true;
      move = new Walk(player.MoveUp);
      speed = 1;
      score = 0;
      eat = new MediaPlayer(doubleBufferedPanel1.Handle);
      eat.Open("pacchomp.wav");
      status = new MediaPlayer(doubleBufferedPanel1.Handle);
      status.Open(@"GAMEBEGINNING.wav");
      status.Play(false);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
      area.UpdateScreen();
      if (area.PixelWallCollision(player) != null)
      {
        status.Close();
        eat.Dispose();
        status.Open(@"killed.wav");
        status.Play();
        area.DeleteAllPictures();
        timer1.Enabled = false;
        area.AddPicture(gameOver);
      }
      if (area.Collision(player) != null)
      {
        eat.Stop();
        eat.Play();
        area.DeletePicture(area.Collision(player));
        if (area.Pictures.Count == 2)
        {
          area.DeleteAllPictures();
          area.AddPicture(gameWin);
          timer1.Enabled = false;
        }
        speed++;
        score++;
        label1.Text = score.ToString();
      }
      if (enemy.Catch(player, 2))
      {
        status.Close();
        eat.Dispose();
        status.Open(@"killed.wav");
        status.Play();
        area.DeleteAllPictures();
        timer1.Enabled = false;
        area.AddPicture(gameOver);
        doubleBufferedPanel1.Invalidate();
      }
      move(speed);
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Up)
      {
        player.Image = Image.FromFile("pacmanUp.gif");
        move = new Walk(player.MoveUp);
      }
      if (e.KeyCode == Keys.Down)
      {
        player.Image = Image.FromFile("pacmanDown.gif");
        move = new Walk(player.MoveDown);
      }
      if (e.KeyCode == Keys.Right)
      {
        player.Image = Image.FromFile("pacmanRight.gif");
        move = new Walk(player.MoveRight);
      }
      if (e.KeyCode == Keys.Left)
      {
        player.Image = Image.FromFile("pacmanLeft.gif");
        move = new Walk(player.MoveLeft);
      }
    }

  }

我认为这是一种编码游戏的好方法。你需要知道的全部就是你的Form应该有一个Timer来检查是否发生任何碰撞,然后调用UpdateScreen()(重新绘制场景)。

更新

Picture类获得了一个名为Catch(Picture target, int difficultLevel)的新函数。使用此函数,你可以轻松添加enemy,并且你的控件的Paint函数集成到绘图区域中,因此你只需要在TimerTick函数中调用DrawingArea.UpdateScreen()来绘制新场景。

© . All rights reserved.