推箱子小游戏

来源:互联网 发布:macbook pro 知乎 编辑:程序博客网 时间:2024/05/02 06:12
  1. 功能较弱,只能对一种地图,还须改进!!
  2. 推箱子1。0    2008-11-25
  3. using System;
  4. using System.Drawing;
  5. using System.Collections;
  6. using System.ComponentModel;
  7. using System.Windows.Forms;
  8. using System.Data;
  9. namespace WindowsApplication1
  10. {
  11.  /// <summary>
  12.  /// Form1 的摘要说明。
  13.  /// </summary>
  14.  public class Form1 : System.Windows.Forms.Form
  15.  {
  16.   /// <summary>
  17.   /// 必需的设计器变量。
  18.   /// </summary>
  19.         private System.ComponentModel.Container components = null;
  20.   public int[,]   myArray; 
  21.   int x=0;
  22.         private System.Windows.Forms.PictureBox pictureBox1;
  23.   int y=2;
  24.         private Panel panel1;
  25.   bool flag=true;
  26.   
  27.   public Form1()
  28.   {
  29.    //
  30.    // Windows 窗体设计器支持所必需的
  31.    //
  32.    InitializeComponent();
  33.    //
  34.    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  35.    //
  36.   }
  37.   /// <summary>
  38.   /// 清理所有正在使用的资源。
  39.   /// </summary>
  40.   protected override void Dispose( bool disposing )
  41.   {
  42.    if( disposing )
  43.    {
  44.     if (components != null
  45.     {
  46.      components.Dispose();
  47.     }
  48.    }
  49.    base.Dispose( disposing );
  50.   }
  51.   #region Windows 窗体设计器生成的代码
  52.   /// <summary>
  53.   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  54.   /// 此方法的内容。
  55.   /// </summary>
  56.   private void InitializeComponent()
  57.   {
  58.             this.pictureBox1 = new System.Windows.Forms.PictureBox();
  59.             this.panel1 = new System.Windows.Forms.Panel();
  60.             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  61.             this.SuspendLayout();
  62.             // 
  63.             // pictureBox1
  64.             // 
  65.             this.pictureBox1.Location = new System.Drawing.Point(12, 30);
  66.             this.pictureBox1.Name = "pictureBox1";
  67.             this.pictureBox1.Size = new System.Drawing.Size(350, 350);
  68.             this.pictureBox1.TabIndex = 2;
  69.             this.pictureBox1.TabStop = false;
  70.             // 
  71.             // Form1
  72.             // 
  73.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  74.             this.ClientSize = new System.Drawing.Size(512, 565);
  75.             this.Controls.Add(this.panel1);
  76.             this.Controls.Add(this.pictureBox1);
  77.             this.KeyPreview = true;
  78.             this.Name = "Form1";
  79.             this.Text = "Form1";
  80.             this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
  81.             this.Load += new System.EventHandler(this.Form1_Load);
  82.             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  83.             this.ResumeLayout(false);
  84.   }
  85.   #endregion
  86.   /// <summary>
  87.   /// 应用程序的主入口点。
  88.   /// </summary>
  89.   [STAThread] 
  90.   static void Main() 
  91.   {
  92.    Application.Run(new Form1());
  93.   }
  94.   public void initdata()
  95.   {
  96.    //myArray   =   new   int[,]{{0,3,1,3,3,3,3}, {0,3,3,2,3,3,0}, {0,0,3,0,4,3,0},
  97.                 //{3,3,2,3,0,0,0},{3,3,3,3,4,0,0},{0,0,3,3,3,0,0},{0,0,0,0,0,0,0}}; 
  98.       myArray   =   new   int[7,7]   {{0,3,1,4,3,3,3},   {0,3,3,2,3,3,0},  
  99.             {0,0,3,0,3,3,0},{3,3,2,3,0,0,0},{3,3,3,4,3,0,0},{0,0,3,3,3,3,0},{0,0,0,0,0,0,0}}; 
  100.   }
  101.       
  102.   private void Form1_Load(object sender, System.EventArgs e)
  103.   {
  104.             initdata();
  105.             drawimage();
  106.   }
  107.       //画的方法
  108.    // 1代表墙,2代表人,3代表箱子,4代表路,5代表目的地
  109.   private void drawimage()
  110.   {
  111.    Bitmap   bit   =   new   Bitmap(this.pictureBox1.Width,   this.pictureBox1.Height);   
  112.    Graphics   g   =   Graphics.FromImage(bit); 
  113.    //Graphics g=this.panel1.CreateGraphics();
  114.    SolidBrush redBrush = new SolidBrush(Color.Red);
  115.    System.Drawing.Image image=new Bitmap("2.jpg");
  116.    for(int i =0;i<7;i++)
  117.    {
  118.     
  119.     for(int j=0;j<7;j++)
  120.     {
  121.      if(myArray[i,j]==0)
  122.      {
  123.       image=new Bitmap("1.jpg");
  124.       g.DrawImage(image,j*50,i*50,50,50);
  125.      }
  126.      if(myArray[i,j]==1)
  127.      {
  128.       image=new Bitmap("2.jpg");
  129.       g.DrawImage(image,j*50,i*50,50,50);
  130.      }
  131.      if(myArray[i,j]==2)
  132.      {
  133.       image=new Bitmap("3.jpg");
  134.       g.DrawImage(image,j*50,i*50,50,50);
  135.      }
  136.      if(myArray[i,j]==3)
  137.      {
  138.       image=new Bitmap("4.jpg");
  139.       g.DrawImage(image,j*50,i*50,50,50);
  140.      }
  141.      if(myArray[i,j]==4)
  142.      {
  143.       image=new Bitmap("5.jpg");
  144.       g.DrawImage(image,j*50,i*50,50,50);
  145.      }
  146.     }
  147.    }
  148.    this.pictureBox1.Image   =   bit;  
  149.   }
  150.   private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  151.   {
  152.            // 1.jpg代表墙,2.jpg代表人,3.jpg代表箱子,4.jpg代表路,5.jpg代表目的地
  153.    //0代表墙,1代表人,2代表箱子,3代表路,4代表目的地
  154.    //myArray[0,3],myArray[4,3]..
  155.        if(e.KeyCode==Keys.Up)
  156.    {
  157.      //X大于0时,才执行--操作
  158.      if(x>0)
  159.      {
  160.       if(myArray[x-1,y]==3)
  161.       { 
  162.        if(flag)
  163.        { 
  164.          myArray[x,y]=3;
  165.        } 
  166.      else
  167.        {
  168.                          myArray[x,y]=4;
  169.       // flag=false;
  170.        }
  171.        x--;
  172.        myArray[x,y]=1;
  173.       // drawimage();
  174.       }
  175.       else if(myArray[x-1,y]==2)
  176.       {
  177.        if(x-1>0)
  178.        {
  179.         if(myArray[x-2,y]==3)
  180.         { 
  181.          if(flag)
  182.          { 
  183.          myArray[x,y]=3;
  184.          }
  185.          else
  186.          {
  187.           myArray[x,y]=4;
  188.         //   flag=false;
  189.          }
  190.          x--;
  191.         
  192.           myArray[x,y]=1;
  193.          
  194.         
  195.          myArray[x-1,y]=2;
  196.        //  drawimage();
  197.         }
  198.         else if(myArray[x-2,y]==4)
  199.         {
  200.          if(flag)
  201.          { 
  202.           myArray[x,y]=3;
  203.          }  
  204.          else
  205.          {
  206.           myArray[x,y]=4;
  207.         //  flag=false;
  208.          }
  209.          x--;
  210.         myArray[x,y]=1;
  211.          myArray[x-1,y]=2;
  212.        //  drawimage();
  213.         }
  214.        }
  215.       }
  216.       else if(myArray[x-1,y]==4)
  217.       { 
  218.        if(flag)
  219.        { 
  220.          myArray[x,y]=3;
  221.        } 
  222.        else
  223.        {
  224.         myArray[x,y]=4;
  225.         flag=false;
  226.        }
  227.        x--;
  228.        myArray[x,y]=1;
  229.       // drawimage();
  230.       }
  231.     
  232.      }
  233.    }
  234.    if(e.KeyCode==Keys.Down)
  235.    {
  236.     //X小于6时,才执行++操作
  237.     //因为数组不能超界
  238.     if(x<6)
  239.     {
  240.      if(myArray[x+1,y]==3)
  241.      {
  242.       myArray[x,y]=3;
  243.       x++;
  244.       myArray[x,y]=1;
  245.       //drawimage();
  246.      }
  247.      else if(myArray[x+1,y]==2)
  248.      {
  249.       if(x+1<6)
  250.       {
  251.        if(myArray[x+2,y]==3)
  252.        {
  253.         myArray[x,y]=3;
  254.         x++;
  255.         myArray[x,y]=1;
  256.         myArray[x+1,y]=2;
  257.         //drawimage();
  258.        }
  259.        else if(myArray[x+2,y]==4)
  260.        {
  261.         myArray[x,y]=3;
  262.         x++;
  263.         myArray[x,y]=1;
  264.         myArray[x+1,y]=2;
  265.         //drawimage();
  266.        }
  267.       }
  268.      }
  269.      else if(myArray[x+1,y]==4)
  270.      { 
  271.       if(flag)
  272.       { 
  273.        myArray[x,y]=3;
  274.       } 
  275.       else
  276.       {
  277.        myArray[x,y]=4;
  278.        flag=false;
  279.       }
  280.       x++;
  281.       myArray[x,y]=1;
  282.       //drawimage();
  283.      }
  284.     }
  285.    }
  286.    if(e.KeyCode==Keys.Left)
  287.    {
  288.     //Y大于0时,才执行--操作
  289.     if(y>0)
  290.     {
  291.      if(myArray[x,y-1]==3)
  292.      {
  293.       myArray[x,y]=3;
  294.       y--;
  295.       myArray[x,y]=1;
  296.       //drawimage();
  297.      }
  298.      else if(myArray[x,y-1]==2)
  299.      {
  300.       if(y-1>0)
  301.       {
  302.        if(myArray[x,y-2]==3)
  303.        {
  304.         myArray[x,y]=3;
  305.         y--;
  306.         myArray[x,y]=1;
  307.         myArray[x,y-1]=2;
  308.         //drawimage();
  309.        }
  310.        else if(myArray[x,y-2]==4)
  311.        {
  312.         myArray[x,y]=3;
  313.         y--;
  314.         myArray[x,y]=1;
  315.         myArray[x,y-1]=2;
  316.         //drawimage();
  317.        }
  318.       }
  319.      }
  320.      else if(myArray[x,y-1]==4)
  321.      {
  322.       if(flag)
  323.       { 
  324.        myArray[x,y]=3;
  325.       } 
  326.       else
  327.       {
  328.        myArray[x,y]=4;
  329.        flag=true;
  330.       }
  331.       y--;
  332.       myArray[x,y]=1;
  333.       //drawimage();
  334.      }
  335.      }
  336.    }
  337.    if(e.KeyCode==Keys.Right)
  338.    {
  339.     //X小于6时,才执行++操作
  340.     //因为数组不能超界
  341.     //MessageBox.Show("右");
  342.     if(y<6)
  343.     {
  344.      if(myArray[x,y+1]==3)
  345.      {
  346.          myArray[x,y]=3;
  347.       y++;
  348.       myArray[x,y]=1;
  349.       //drawimage();
  350.      }
  351.      else if(myArray[x,y+1]==2)
  352.      {
  353.       if(y+1<6)
  354.       {
  355.        if(myArray[x,y+2]==3)
  356.        {
  357.         
  358.         myArray[x,y]=3;
  359.         y++;
  360.         myArray[x,y]=1;
  361.         myArray[x,y+1]=2;
  362.         //drawimage();
  363.        }
  364.        else if(myArray[x,y+2]==4)
  365.        {
  366.         myArray[x,y]=3;
  367.         y++;
  368.         myArray[x,y]=1;
  369.         myArray[x,y+1]=2;
  370.         //drawimage();
  371.        }
  372.       }
  373.      }
  374.      else if(myArray[x,y+1]==4)
  375.      {
  376.       if(flag)
  377.       { 
  378.        myArray[x,y]=3;
  379.       } 
  380.       else
  381.       {
  382.        myArray[x,y]=4;
  383.        flag=true;
  384.       }
  385.       y++;
  386.       myArray[x,y]=1;
  387.       //drawimage();
  388.      }
  389.     }
  390.     
  391.    }
  392.    check();
  393.    drawimage();
  394.    //这里要验证是否过关
  395.    if(myArray[0,3]==2 && myArray[4,3]==2)
  396.    {
  397.     MessageBox.Show("恭喜你顺利过关");
  398.     return;
  399.    }
  400.   
  401.   }
  402.   public void check()
  403.   {
  404.    
  405.    if(myArray[0,3]==3)
  406.    {
  407.     myArray[0,3]=4;
  408.    }
  409.    if(myArray[4,3]==3)
  410.    {
  411.     myArray[4,3]=4;
  412.    }   
  413.     
  414.   }
  415.  }
  416. }
  417.   
原创粉丝点击