欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 拼图魔方小游戏

程序员文章站 2022-06-21 08:44:27
工作闲暇之余去逛了逛codeproject,刚好现有项目主要用到就是winform,浏览了下照片,找到上周带着蛋挞打疫苗回家的照片,于是新生一记,如何把这些图片玩起来~80后应该都有印象,小时候有种玩...

工作闲暇之余去逛了逛codeproject,刚好现有项目主要用到就是winform,浏览了下照片,找到上周带着蛋挞打疫苗回家的照片,于是新生一记,如何把这些图片玩起来~

80后应该都有印象,小时候有种玩具,叫做拼图魔方,90后00后的世界这种玩具应该早已灭绝了。一个塑料小板,上面分隔了很多小图框,通过移动这些小图框,最后拼接成完整的图片

话不多说开始吧~ 先上一张原图

C# 拼图魔方小游戏

代码也很简单,主要就是通过bitmap分隔现有(后面有时间可以优化下,让玩家自动上传图片,应该会更有意思)图片,然后random随机打乱分割后图片的顺序,通过点击小方格来完成图片的拼图,为了更方便玩家,每个小方格添加了序号,玩家也可以不参考原图,按照小方格上的序号进行拼图

C# 拼图魔方小游戏

序号功能实现主要是类mybutton集成父类button实现:

public class mybutton : button
  {
    private int number; 
    public int number
    {
      get
      {
        return this.number;
      }
      set
      {
        this.text = value.tostring();
        this.number = value;
      }
    } 
    public mybutton()
    {
    }
  }

随机分隔

random r = new random();
      int[] a = new int[24];
      int i = 0;
      int b;
      bool exist;
      while (i != a.length)
      {
        exist = false;
        b = (r.next(24) + 1);
        for (int j = 0; j < a.length; j++)
          if (a[j] == b) exist = true;
        if (!exist) a[i++] = b;
      }
      for (int j = 0; j < a.length; j++)
        buttonarray[j].number = a[j];
      // set picture pieces as the background image
      int number;
      int row, column;
      for (int k = 0; k < 5; k++)
      {
        for (int j = 0; j < 5; j++)
        {
          if (k == 4)
            if (j == 4) break;
          number = buttonarray[k * 5 + j].number; //get the number of button
          row = (number - 1) / 5;
          column = (number - 1) - (row * 5);
          buttonarray[k * 5 + j].image = currentbitmapimage.clone(new rectangle(new point(column * 75, row * 75), new size(75, 75)), system.drawing.imaging.pixelformat.dontcare);
        }
      }

点击小方格,通过改变当前点击的小方格x,y坐标来更新小方格的位置

private void mybutton_locationchanged(object sender, eventargs e)
    {
      mybutton a = sender as mybutton;
      youwin = true;
      int buttonnumber;
      this.numberofmoves++;
      if (buttonarray == null)
      {
        this.frmmain_load(sender, e);
      }
      for (int i = 0; i < 5; i++)
      {
        if (youwin == false)
          break;
        else for (int j = 0; j < 5; j++)
          {
            buttonnumber = i * 5 + j;
            if (i == 4 && j == 4)
              break;
            else if (getnumber(buttonarray[buttonnumber].location.x, buttonarray[buttonnumber].location.y) == buttonarray[buttonnumber].number)
              continue;
            else
            {
              youwin = false;
              break;
            }
          }
      }
      if (youwin)
      {

        if (messagebox.show("you win this game in " + this.numberofmoves.tostring() + " moves\n\rdo you want to play another game ?", "congratulation", messageboxbuttons.yesno) == dialogresult.yes)
          this.loadnewgame();
        else
          this.close();
      }
    }
private void mybutton_locationchanged(object sender, eventargs e)
    {
      mybutton a = sender as mybutton;
      youwin = true;
      int buttonnumber;
      this.numberofmoves++;
      if (buttonarray == null)
      {
        this.frmmain_load(sender, e);
      }
      for (int i = 0; i < 5; i++)
      {
        if (youwin == false)
          break;
        else for (int j = 0; j < 5; j++)
          {
            buttonnumber = i * 5 + j;
            if (i == 4 && j == 4)
              break;
            else if (getnumber(buttonarray[buttonnumber].location.x, buttonarray[buttonnumber].location.y) == buttonarray[buttonnumber].number)
              continue;
            else
            {
              youwin = false;
              break;
            }
          }
      }
      if (youwin)
      {

        if (messagebox.show("you win this game in " + this.numberofmoves.tostring() + " moves\n\rdo you want to play another game ?", "congratulation", messageboxbuttons.yesno) == dialogresult.yes)
          this.loadnewgame();
        else
          this.close();
      }
    }

具体效果如下:

C# 拼图魔方小游戏

代码有很多已知的可以优化的地方,后面有闲暇时间会处理,如果大家有更好的建议,不妨在下方评论区告知,在此感谢~

点击下载源码

到此这篇关于c# 拼图魔方小游戏的文章就介绍到这了,更多相关c# 拼图魔方内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!