ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:17.19KB ,
资源ID:9633571      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9633571.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(智力拼图.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

智力拼图.docx

1、智力拼图using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;namespace NumPuzzle / / Form2 的摘要说明。 / public class Form1 : System.Windows.Forms.Form / / 必需的设计器变量。 / int GameSize; /布局大小 byte Position; /绝对地址 Button Buttons; /拼块按扭 const int MAP_WID

2、TH = 260; /图片宽度 bool IsRun = false; /游戏状态 int Clicks = 0; /总移动数 private System.Windows.Forms.StatusBar statusBar1; private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuI

3、tem menuItem2; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.MenuItem menuItem4; private System.Windows.Forms.MenuItem menuItem5; private System.ComponentModel.Container components = null; public Form1() InitializeComponent(); protected override void Dispose( bool dis

4、posing ) if( disposing ) if(components != null) components.Dispose(); base.Dispose( disposing ); #region Windows 窗体设计器生成的代码 / / 设计器支持所需的方法 - 不要使用代码编辑器修改 / 此方法的内容。 / private void InitializeComponent() System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1); thi

5、s.statusBar1 = new System.Windows.Forms.StatusBar(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System

6、.Windows.Forms.MenuItem(); this.menuItem4 = new System.Windows.Forms.MenuItem(); this.menuItem5 = new System.Windows.Forms.MenuItem(); this.SuspendLayout(); / / statusBar1 / this.statusBar1.Location = new System.Drawing.Point(0, 296); this.statusBar1.Name = statusBar1; this.statusBar1.Size = new Sys

7、tem.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 = new System.Drawing.Point(344, 16); this.pictureBox1.Name = pictureBox1; this.pi

8、ctureBox1.Size = new System.Drawing.Size(260, 260); this.pictureBox1.TabIndex = 2; this.pictureBox1.TabStop = false; / / mainMenu1 / this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem this.menuItem1); / / menuItem1 / this.menuItem1.Index = 0; this.menuItem1.MenuItems.AddRange(new Sy

9、stem.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 += new System.EventHandler(this.menuItem2_Click); / / menuItem3 / this.menuItem3.Index = 1

10、; this.menuItem3.Text = 3*3格; this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click); / / menuItem4 / this.menuItem4.Index = 2; this.menuItem4.Text = 4*4格; this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click); / / menuItem5 / this.menuItem5.Index = 3; this.menuItem5.T

11、ext = 退出; this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click); / / Form1 / this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.BackColor = System.Drawing.SystemColors.Desktop; this.ClientSize = new System.Drawing.Size(608, 318); this.Controls.Add(this.pictureBox1); this.C

12、ontrols.Add(this.statusBar1); this.Menu = this.mainMenu1; this.Name = Form1; this.Text = 人物拼图游戏; this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form2_KeyUp); this.ResumeLayout(false); #endregion / / 应用程序的主入口点。 / STAThread static void Main() Application.Run(new Form1(); /初始化游戏相关设置 privat

13、e void InitGame() /清除已有棋盘按扭 if(Buttons != null) for(int i=0;iButtons.Length;i+) Buttonsi.Dispose(); Buttons = new ButtonGameSize*GameSize; Position = new byteGameSize*GameSize; Position0 = 0; /空的位置 for(int i=1;iPosition.Length;i+) Positioni = (byte)i;/初始化数组 /随机打乱数组算法 byte key = new byteGameSize*Game

14、Size; Random Rnd1= new Random(); Rnd1.NextBytes(key); Array.Sort(key,Position); /动态生成按扭,其实可以用GDI画 int BWidth = MAP_WIDTH / GameSize; for(int i=0;iButtons.Length;i+) Buttonsi = new Button(); Buttonsi.Size = new Size(BWidth,BWidth); int j = i / GameSize; int k = i % GameSize; Buttonsi.Location = new P

15、oint(24+k*BWidth,16+j*BWidth); if(Positioni = 0) Buttonsi.Visible = false; Buttonsi.Text = Positioni.ToString(); /设置按钮背景图片* Buttonsi.BackgroundImage= create_image(Convert.ToInt16( Positioni.ToString(); Buttonsi.Enabled = false; this.Controls.Add(Buttonsi); IsRun = true; /设置游戏运行标志 this.Clicks = 0; pr

16、ivate void DoChange(Keys key) int offest = -1; int MoveIndex = -1; for(int i=0;iPosition.Length;i+)/寻找空位置 if(Positioni = 0) offest = i;/offest记录空位置 break; /判断玩家按键,根据空位置推算被移动的按钮 switch(key) case Keys.Up: MoveIndex = offest + GameSize; break; case Keys.Down: MoveIndex = offest - GameSize; break; case

17、Keys.Left: MoveIndex = offest + 1; if(offest % GameSize = GameSize - 1) return; break; case Keys.Right: MoveIndex = offest - 1; if(offest % GameSize = 0) return; break; default: break; /End Switch /判断有效范围,判断是否能移动 if(MoveIndex = Position.Length) return; Clicks+; this.statusBar1.Text = Clicks.ToString

18、()+ Move; PlaySound.Play(MOVE.WAV); byte temp; /交换数组中offest和MoveIndex位置 temp = Positionoffest; Positionoffest = PositionMoveIndex; PositionMoveIndex = temp; /更新游戏界面 UpDataUI(offest,MoveIndex); /检查游戏是否过关 CheckWin(); private void UpDataUI(int offest,int MoveIndex) if(this.IsRun = false) return; Button

19、soffest.Visible = true; Buttonsoffest.Text = Positionoffest.ToString(); Buttonsoffest.BackgroundImage= create_image(Convert.ToInt16( Positionoffest.ToString(); ButtonsMoveIndex.Visible = false; /检查是否胜利 private void CheckWin() for(int i=1;iPosition.Length;i+) if(Positioni != (byte)i) return; /不符合条件返回

20、 /显示去掉的拼块 Buttons0.Visible = true; Buttons0.Text = 0; Buttons0.BackgroundImage= create_image(0); /过关后播放相应音乐 PlaySound.Play(WIN.WAV); IsRun = false; this.statusBar1.Text+= 过关!; private void Form2_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) if(IsRun = false) return; switch (e.KeyCode) ca

21、se Keys.Up: case Keys.Down: case Keys.Right: case Keys.Left: DoChange(e.KeyCode); break; default: break; private Bitmap create_image(int n)/按标号n截图 int W = MAP_WIDTH /GameSize ; Bitmap bit = new Bitmap( W, W ); Graphics g = Graphics.FromImage( bit ); /截图 g.DrawImage( pictureBox1.Image, new Rectangle(

22、 0, 0, W,W ), new Rectangle( n%GameSize*W,n/GameSize* W, W,W )/*Copy W*W part from source image */, GraphicsUnit.Pixel ); return bit; private void menuItem2_Click(object sender, System.EventArgs e)/2*2格 GameSize = 2; InitGame(); private void menuItem3_Click(object sender, System.EventArgs e)/3*3格 GameSize = 3; InitGame(); private void menuItem4_Click(object sender, System.EventArgs e)/4*4格 GameSize = 4; InitGame(); private void menuItem5_Click(object sender, System.EventArgs e)/退出 Application.Exit();

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

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