推箱子小游戏
来源:互联网 发布:macbook pro 知乎 编辑:程序博客网 时间:2024/05/02 06:12
- 功能较弱,只能对一种地图,还须改进!!
- 推箱子1。0 2008-11-25
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- namespace WindowsApplication1
- {
-
-
-
- public class Form1 : System.Windows.Forms.Form
- {
-
-
-
- private System.ComponentModel.Container components = null;
- public int[,] myArray;
- int x=0;
- private System.Windows.Forms.PictureBox pictureBox1;
- int y=2;
- private Panel panel1;
- bool flag=true;
-
- public Form1()
- {
-
-
-
- InitializeComponent();
-
-
-
- }
-
-
-
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
-
-
-
-
- private void InitializeComponent()
- {
- this.pictureBox1 = new System.Windows.Forms.PictureBox();
- this.panel1 = new System.Windows.Forms.Panel();
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
- this.SuspendLayout();
-
-
-
- this.pictureBox1.Location = new System.Drawing.Point(12, 30);
- this.pictureBox1.Name = "pictureBox1";
- this.pictureBox1.Size = new System.Drawing.Size(350, 350);
- this.pictureBox1.TabIndex = 2;
- this.pictureBox1.TabStop = false;
-
-
-
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(512, 565);
- this.Controls.Add(this.panel1);
- this.Controls.Add(this.pictureBox1);
- this.KeyPreview = true;
- this.Name = "Form1";
- this.Text = "Form1";
- this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
- this.Load += new System.EventHandler(this.Form1_Load);
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
- this.ResumeLayout(false);
- }
- #endregion
-
-
-
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- public void initdata()
- {
-
-
- myArray = new int[7,7] {{0,3,1,4,3,3,3}, {0,3,3,2,3,3,0},
- {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}};
- }
-
- private void Form1_Load(object sender, System.EventArgs e)
- {
- initdata();
- drawimage();
- }
-
-
- private void drawimage()
- {
- Bitmap bit = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
- Graphics g = Graphics.FromImage(bit);
-
- SolidBrush redBrush = new SolidBrush(Color.Red);
- System.Drawing.Image image=new Bitmap("2.jpg");
- for(int i =0;i<7;i++)
- {
-
- for(int j=0;j<7;j++)
- {
- if(myArray[i,j]==0)
- {
- image=new Bitmap("1.jpg");
- g.DrawImage(image,j*50,i*50,50,50);
- }
- if(myArray[i,j]==1)
- {
- image=new Bitmap("2.jpg");
- g.DrawImage(image,j*50,i*50,50,50);
- }
- if(myArray[i,j]==2)
- {
- image=new Bitmap("3.jpg");
- g.DrawImage(image,j*50,i*50,50,50);
- }
- if(myArray[i,j]==3)
- {
- image=new Bitmap("4.jpg");
- g.DrawImage(image,j*50,i*50,50,50);
- }
- if(myArray[i,j]==4)
- {
- image=new Bitmap("5.jpg");
- g.DrawImage(image,j*50,i*50,50,50);
- }
- }
- }
- this.pictureBox1.Image = bit;
- }
- private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
- {
-
-
-
- if(e.KeyCode==Keys.Up)
- {
-
- if(x>0)
- {
- if(myArray[x-1,y]==3)
- {
- if(flag)
- {
- myArray[x,y]=3;
- }
- else
- {
- myArray[x,y]=4;
-
- }
- x--;
- myArray[x,y]=1;
-
- }
- else if(myArray[x-1,y]==2)
- {
- if(x-1>0)
- {
- if(myArray[x-2,y]==3)
- {
- if(flag)
- {
- myArray[x,y]=3;
- }
- else
- {
- myArray[x,y]=4;
-
- }
- x--;
-
- myArray[x,y]=1;
-
-
- myArray[x-1,y]=2;
-
- }
- else if(myArray[x-2,y]==4)
- {
- if(flag)
- {
- myArray[x,y]=3;
- }
- else
- {
- myArray[x,y]=4;
-
- }
- x--;
- myArray[x,y]=1;
- myArray[x-1,y]=2;
-
- }
- }
- }
- else if(myArray[x-1,y]==4)
- {
- if(flag)
- {
- myArray[x,y]=3;
- }
- else
- {
- myArray[x,y]=4;
- flag=false;
- }
- x--;
- myArray[x,y]=1;
-
- }
-
- }
- }
- if(e.KeyCode==Keys.Down)
- {
-
-
- if(x<6)
- {
- if(myArray[x+1,y]==3)
- {
- myArray[x,y]=3;
- x++;
- myArray[x,y]=1;
-
- }
- else if(myArray[x+1,y]==2)
- {
- if(x+1<6)
- {
- if(myArray[x+2,y]==3)
- {
- myArray[x,y]=3;
- x++;
- myArray[x,y]=1;
- myArray[x+1,y]=2;
-
- }
- else if(myArray[x+2,y]==4)
- {
- myArray[x,y]=3;
- x++;
- myArray[x,y]=1;
- myArray[x+1,y]=2;
-
- }
- }
- }
- else if(myArray[x+1,y]==4)
- {
- if(flag)
- {
- myArray[x,y]=3;
- }
- else
- {
- myArray[x,y]=4;
- flag=false;
- }
- x++;
- myArray[x,y]=1;
-
- }
- }
- }
- if(e.KeyCode==Keys.Left)
- {
-
- if(y>0)
- {
- if(myArray[x,y-1]==3)
- {
- myArray[x,y]=3;
- y--;
- myArray[x,y]=1;
-
- }
- else if(myArray[x,y-1]==2)
- {
- if(y-1>0)
- {
- if(myArray[x,y-2]==3)
- {
- myArray[x,y]=3;
- y--;
- myArray[x,y]=1;
- myArray[x,y-1]=2;
-
- }
- else if(myArray[x,y-2]==4)
- {
- myArray[x,y]=3;
- y--;
- myArray[x,y]=1;
- myArray[x,y-1]=2;
-
- }
- }
- }
- else if(myArray[x,y-1]==4)
- {
- if(flag)
- {
- myArray[x,y]=3;
- }
- else
- {
- myArray[x,y]=4;
- flag=true;
- }
- y--;
- myArray[x,y]=1;
-
- }
- }
- }
- if(e.KeyCode==Keys.Right)
- {
-
-
-
- if(y<6)
- {
- if(myArray[x,y+1]==3)
- {
- myArray[x,y]=3;
- y++;
- myArray[x,y]=1;
-
- }
- else if(myArray[x,y+1]==2)
- {
- if(y+1<6)
- {
- if(myArray[x,y+2]==3)
- {
-
- myArray[x,y]=3;
- y++;
- myArray[x,y]=1;
- myArray[x,y+1]=2;
-
- }
- else if(myArray[x,y+2]==4)
- {
- myArray[x,y]=3;
- y++;
- myArray[x,y]=1;
- myArray[x,y+1]=2;
-
- }
- }
- }
- else if(myArray[x,y+1]==4)
- {
- if(flag)
- {
- myArray[x,y]=3;
- }
- else
- {
- myArray[x,y]=4;
- flag=true;
- }
- y++;
- myArray[x,y]=1;
-
- }
- }
-
- }
- check();
- drawimage();
-
- if(myArray[0,3]==2 && myArray[4,3]==2)
- {
- MessageBox.Show("恭喜你顺利过关");
- return;
- }
-
- }
- public void check()
- {
-
- if(myArray[0,3]==3)
- {
- myArray[0,3]=4;
- }
- if(myArray[4,3]==3)
- {
- myArray[4,3]=4;
- }
-
- }
- }
- }
-