第十三次会议
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第十三次会议相关的知识,希望对你有一定的参考价值。
通过测试和改查,我们制作的小游戏可以正常运行,制作部分完成,当然也发现了游戏的不足之处,我们会进一步改善。
附部分代码:
using System; using System.Collections; using System.Drawing; using System.Windows.Forms; using System.IO;
namespace 拼图 { public partial class Form1 : Form { private int FirstBlock; private int GameSize; //布局大小 private int MAP_WIDTH = 320; //图片宽度 public PictureBox[] PicBlock; private int[] Position; //存放图片序号的数组 private int SecondBlock; private Bitmap Source; //原图像320*320 private bool flag; //是否交换 private PictureBox pb; private Form gameForm; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { comboBox1.Items.Clear(); comboBox1.FlatStyle = FlatStyle.Standard; comboBox1.Items.Add("卡通"); comboBox1.Items.Add("T-ara"); comboBox1.Items.Add("风景"); comboBox1.Items.Add("建筑"); comboBox1.Items.Add("美女"); comboBox1.Items.Add("火影");
comboBox1.SelectedIndex = 0;//默认选取第一个 comboBox2.Items.Add("9"); comboBox2.Items.Add("16"); comboBox2.Items.Add("25"); comboBox2.Text = "9"; comboBox2.SelectedIndex = 0;//默认选取第一个 MAP_WIDTH = 320; Source = new Bitmap(MAP_WIDTH, MAP_WIDTH); //原图像 } private void ShowThePicture() { pb=new PictureBox(); Source = new Bitmap(Application.StartupPath + "\\\\pic\\\\" + comboBox1.SelectedItem.ToString() + ".jpg"); pb.Image = Source; pb.SizeMode = PictureBoxSizeMode.StretchImage; pb.Width = Source.Width; pb.Height = Source.Height; pb.Location =new Point(Source.Width+1,0); } private void ShowTheGame() { gameForm = new Form(); gameForm.Width = MAP_WIDTH * 2 + 1; gameForm.Height = MAP_WIDTH+40; gameForm.MaximizeBox = false; gameForm.FormBorderStyle = FormBorderStyle.Fixed3D; gameForm.Controls.Add(pb); gameForm.Text = comboBox1.SelectedItem.ToString() + "拼图"; init(GameSize); //卸载上次的图片块 //重新加载图片块 // PicBlock = new PictureBox[GameSize * GameSize]; int i = 0; int BWidth = 0; BWidth = MAP_WIDTH / GameSize; for (i = 0; i <= GameSize * GameSize - 1; i++) { PicBlock[i] = new PictureBox(); gameForm.Controls.Add(PicBlock[i]); PicBlock[i].Width = BWidth; PicBlock[i].Height = BWidth; PicBlock[i].Location = new Point(BWidth * (i % GameSize), BWidth * (i / GameSize)); PicBlock[i].Name = "PicBlock" + i.ToString(); PicBlock[i].Tag = i; PicBlock[i].Image = create_image(Position[i]); PicBlock[i].BorderStyle = BorderStyle.Fixed3D; (PicBlock[i]).MouseClick += swap; //(PicBlock[i]).MouseHover+= 鼠标停放在按钮上面 //(PicBlock[i]).MouseClick } gameForm.Show(); } private void button1_Click(object sender, EventArgs e) { ShowThePicture(); ShowTheGame(); } private Bitmap create_image(int n) //按标号n截图 { int W = 0; W = MAP_WIDTH / GameSize; Bitmap bit = new Bitmap(W, W); Graphics g = Graphics.FromImage(bit); //生成Graphics对象 Rectangle a = new Rectangle(0, 0, W, W); Rectangle b = new Rectangle((n % GameSize) * W, n / GameSize * W, W, W); g.DrawImage(Source, a, b, GraphicsUnit.Pixel); //截图 Copy W*W part from source image Image.FromFile("temp.bmp") return bit; } private bool CheckWin() //判断是否成功 { int t = 0; for (t = 0; t <= Position.Length - 1; t++) { if (Position[t] != t) { return false; } } return true; } private void init(int n) { Random rdm; ArrayList al = new ArrayList(); int t = 0; rdm = new Random(); t = 0; while (al.Count < n * n) { t = rdm.Next(0, n * n); if ((!al.Contains(t))) { al.Add(t); } } //清除已有图片框控件数组中的控件 if (PicBlock != null) { for (int i = 0; i < PicBlock.Length; i++) if (PicBlock[i] != null) PicBlock[i].Dispose(); } PicBlock = new PictureBox[n * n]; Position = new int[n * n]; for (t = 0; t <= al.Count - 1; t++) { Position[t] = Convert.ToInt16(al[t]); } }
private void swap(object sender, EventArgs e) { //这里处理公共事件,根据单击交换数组元素; PictureBox bClick = (PictureBox)sender; int i = 0; Image temp; //将被点击的控件赋给bClick变量 if (flag == false) { flag = true; FirstBlock = Convert.ToInt16(bClick.Tag); } else //交换 { Text = ""; SecondBlock = Convert.ToInt16(bClick.Tag); temp = PicBlock[SecondBlock].Image; PicBlock[SecondBlock].Image = PicBlock[FirstBlock].Image; PicBlock[FirstBlock].Image = temp; flag = false; i = Position[SecondBlock]; Position[SecondBlock] = Position[FirstBlock]; Position[FirstBlock] = i; foreach (int s in Position) { Text = Text + Position[s].ToString(); } if (CheckWin()) //过关 { MessageBox.Show("成功了", "提示"); } }
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { Source = new Bitmap(Application.StartupPath + "\\\\pic\\\\" + comboBox1.SelectedItem.ToString() + ".jpg"); } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { GameSize = (int)Math.Sqrt(Convert.ToInt16(comboBox2.Text)); init(GameSize); } } }
以上是关于第十三次会议的主要内容,如果未能解决你的问题,请参考以下文章