智力拼图.docx

上传人:b****7 文档编号:9633571 上传时间:2023-02-05 格式:DOCX 页数:13 大小:17.19KB
下载 相关 举报
智力拼图.docx_第1页
第1页 / 共13页
智力拼图.docx_第2页
第2页 / 共13页
智力拼图.docx_第3页
第3页 / 共13页
智力拼图.docx_第4页
第4页 / 共13页
智力拼图.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

智力拼图.docx

《智力拼图.docx》由会员分享,可在线阅读,更多相关《智力拼图.docx(13页珍藏版)》请在冰豆网上搜索。

智力拼图.docx

智力拼图

usingSystem;

usingSystem.Drawing;

usingSystem.Collections;

usingSystem.ComponentModel;

usingSystem.Windows.Forms;

namespaceNumPuzzle

{

///

///Form2的摘要说明。

///

publicclassForm1:

System.Windows.Forms.Form

{

///

///必需的设计器变量。

///

intGameSize;//布局大小

byte[]Position;//绝对地址

Button[]Buttons;//拼块按扭

constintMAP_WIDTH=260;//图片宽度

boolIsRun=false;//游戏状态

intClicks=0;//总移动数

privateSystem.Windows.Forms.StatusBarstatusBar1;

privateSystem.Windows.Forms.PictureBoxpictureBox1;

privateSystem.Windows.Forms.MainMenumainMenu1;

privateSystem.Windows.Forms.MenuItemmenuItem1;

privateSystem.Windows.Forms.MenuItemmenuItem2;

privateSystem.Windows.Forms.MenuItemmenuItem3;

privateSystem.Windows.Forms.MenuItemmenuItem4;

privateSystem.Windows.Forms.MenuItemmenuItem5;

privateSystem.ComponentModel.Containercomponents=null;

publicForm1()

{

InitializeComponent();

}

protectedoverridevoidDispose(booldisposing)

{

if(disposing)

{

if(components!

=null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}

#regionWindows窗体设计器生成的代码

///

///设计器支持所需的方法-不要使用代码编辑器修改

///此方法的内容。

///

privatevoidInitializeComponent()

{

System.Resources.ResourceManagerresources=newSystem.Resources.ResourceManager(typeof(Form1));

this.statusBar1=newSystem.Windows.Forms.StatusBar();

this.pictureBox1=newSystem.Windows.Forms.PictureBox();

this.mainMenu1=newSystem.Windows.Forms.MainMenu();

this.menuItem1=newSystem.Windows.Forms.MenuItem();

this.menuItem2=newSystem.Windows.Forms.MenuItem();

this.menuItem3=newSystem.Windows.Forms.MenuItem();

this.menuItem4=newSystem.Windows.Forms.MenuItem();

this.menuItem5=newSystem.Windows.Forms.MenuItem();

this.SuspendLayout();

//

//statusBar1

//

this.statusBar1.Location=newSystem.Drawing.Point(0,296);

this.statusBar1.Name="statusBar1";

this.statusBar1.Size=newSystem.Drawing.Size(608,22);

this.statusBar1.TabIndex=0;

this.statusBar1.Text="请选择难度";

//

//pictureBox1

//

this.pictureBox1.Image=((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));

this.pictureBox1.Location=newSystem.Drawing.Point(344,16);

this.pictureBox1.Name="pictureBox1";

this.pictureBox1.Size=newSystem.Drawing.Size(260,260);

this.pictureBox1.TabIndex=2;

this.pictureBox1.TabStop=false;

//

//mainMenu1

//

this.mainMenu1.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]{

this.menuItem1});

//

//menuItem1

//

this.menuItem1.Index=0;

this.menuItem1.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]{

this.menuItem2,

this.menuItem3,

this.menuItem4,

this.menuItem5});

this.menuItem1.Text="游戏";

//

//menuItem2

//

this.menuItem2.Index=0;

this.menuItem2.Text="2*2格";

this.menuItem2.Click+=newSystem.EventHandler(this.menuItem2_Click);

//

//menuItem3

//

this.menuItem3.Index=1;

this.menuItem3.Text="3*3格";

this.menuItem3.Click+=newSystem.EventHandler(this.menuItem3_Click);

//

//menuItem4

//

this.menuItem4.Index=2;

this.menuItem4.Text="4*4格";

this.menuItem4.Click+=newSystem.EventHandler(this.menuItem4_Click);

//

//menuItem5

//

this.menuItem5.Index=3;

this.menuItem5.Text="退出";

this.menuItem5.Click+=newSystem.EventHandler(this.menuItem5_Click);

//

//Form1

//

this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);

this.BackColor=System.Drawing.SystemColors.Desktop;

this.ClientSize=newSystem.Drawing.Size(608,318);

this.Controls.Add(this.pictureBox1);

this.Controls.Add(this.statusBar1);

this.Menu=this.mainMenu1;

this.Name="Form1";

this.Text="人物拼图游戏";

this.KeyUp+=newSystem.Windows.Forms.KeyEventHandler(this.Form2_KeyUp);

this.ResumeLayout(false);

}

#endregion

///

///应用程序的主入口点。

///

[STAThread]

staticvoidMain()

{

Application.Run(newForm1());

}

 

//初始化游戏相关设置

privatevoidInitGame()

{

//清除已有棋盘按扭

if(Buttons!

=null)

{

for(inti=0;i

Buttons[i].Dispose();

}

Buttons=newButton[GameSize*GameSize];

Position=newbyte[GameSize*GameSize];

Position[0]=0;//空的位置

for(inti=1;i

{

Position[i]=(byte)i;//初始化数组

}

//随机打乱数组算法

byte[]key=newbyte[GameSize*GameSize];

RandomRnd1=newRandom();

Rnd1.NextBytes(key);

Array.Sort(key,Position);

//动态生成按扭,其实可以用GDI画

intBWidth=MAP_WIDTH/GameSize;

for(inti=0;i

{

Buttons[i]=newButton();

Buttons[i].Size=newSize(BWidth,BWidth);

intj=i/GameSize;

intk=i%GameSize;

Buttons[i].Location=newPoint(24+k*BWidth,16+j*BWidth);

if(Position[i]==0)

{

Buttons[i].Visible=false;

}

Buttons[i].Text=Position[i].ToString();

//设置按钮背景图片***************

Buttons[i].BackgroundImage=create_image(Convert.ToInt16(Position[i].ToString()));

Buttons[i].Enabled=false;

this.Controls.Add(Buttons[i]);

}

IsRun=true;//设置游戏运行标志

this.Clicks=0;

}

privatevoidDoChange(Keyskey)

{

intoffest=-1;

intMoveIndex=-1;

for(inti=0;i

{

if(Position[i]==0)

{

offest=i;//offest记录空位置

break;

}

}

//判断玩家按键,根据空位置推算被移动的按钮

switch(key)

{

caseKeys.Up:

MoveIndex=offest+GameSize;

break;

caseKeys.Down:

MoveIndex=offest-GameSize;

break;

caseKeys.Left:

MoveIndex=offest+1;

if(offest%GameSize==GameSize-1)

return;

break;

caseKeys.Right:

MoveIndex=offest-1;

if(offest%GameSize==0)

return;

break;

default:

break;

}//EndSwitch

//判断有效范围,判断是否能移动

if(MoveIndex<0||MoveIndex>=Position.Length)

return;

Clicks++;

this.statusBar1.Text=Clicks.ToString()+"Move";

PlaySound.Play("MOVE.WAV");

bytetemp;

//交换数组中offest和MoveIndex位置

temp=Position[offest];

Position[offest]=Position[MoveIndex];

Position[MoveIndex]=temp;

//更新游戏界面

UpDataUI(offest,MoveIndex);

//检查游戏是否过关

CheckWin();

}

privatevoidUpDataUI(intoffest,intMoveIndex)

{

if(this.IsRun==false)

return;

Buttons[offest].Visible=true;

Buttons[offest].Text=Position[offest].ToString();

Buttons[offest].BackgroundImage=create_image(Convert.ToInt16(Position[offest].ToString()));

Buttons[MoveIndex].Visible=false;

}

//检查是否胜利

privatevoidCheckWin()

{

for(inti=1;i

{

if(Position[i]!

=(byte)i)

{

return;//不符合条件返回

}

}

//显示去掉的拼块

Buttons[0].Visible=true;

Buttons[0].Text="0";

Buttons[0].BackgroundImage=create_image(0);

//过关后播放相应音乐

PlaySound.Play("WIN.WAV");

IsRun=false;

this.statusBar1.Text+="过关!

";

}

privatevoidForm2_KeyUp(objectsender,System.Windows.Forms.KeyEventArgse)

{

if(IsRun==false)

return;

switch(e.KeyCode)

{

caseKeys.Up:

caseKeys.Down:

caseKeys.Right:

caseKeys.Left:

DoChange(e.KeyCode);

break;

default:

break;

}

}

privateBitmapcreate_image(intn)//按标号n截图

{

intW=MAP_WIDTH/GameSize;

Bitmapbit=newBitmap(W,W);

Graphicsg=Graphics.FromImage(bit);

//截图

g.DrawImage(pictureBox1.Image,newRectangle(0,0,W,W),

newRectangle(n%GameSize*W,n/GameSize*W,W,W)/*CopyW*Wpartfromsourceimage*/,

GraphicsUnit.Pixel);

returnbit;

}

privatevoidmenuItem2_Click(objectsender,System.EventArgse)//2*2格

{

GameSize=2;

InitGame();

}

privatevoidmenuItem3_Click(objectsender,System.EventArgse)//3*3格

{

GameSize=3;

InitGame();

}

privatevoidmenuItem4_Click(objectsender,System.EventArgse)//4*4格

{

GameSize=4;

InitGame();

}

privatevoidmenuItem5_Click(objectsender,System.EventArgse)//退出

{

Application.Exit();

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1