C#應用控件拖拽技巧制造拼圖游戲。本站提示廣大學習愛好者:(C#應用控件拖拽技巧制造拼圖游戲)文章只能為提供參考,不一定能成為您想要的結果。以下是C#應用控件拖拽技巧制造拼圖游戲正文
重要完成的功效:
1.法式附帶多張拼圖隨機拼圖。
2.可手動添加拼圖。
3.游戲勝利斷定。
4.30秒超時斷定。
Puzzle.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Puzzle
{
public partial class Puzzle : Form
{
//圖片列表
PictureBox[] pictureList = null;
//圖片地位字典
SortedDictionary<string, Bitmap> pictureLocationDict = new SortedDictionary<string, Bitmap>();
//Location List
Point[] pointList = null;
//圖片控件字典
SortedDictionary<string, PictureBox> pictureBoxLocationDict = new SortedDictionary<string, PictureBox>();
//拼圖時光
int second = 0;
//所拖拽的圖片
PictureBox currentPictureBox = null;
//自願須要挪動的圖片
PictureBox haveToPictureBox = null;
//原地位
Point oldLocation = Point.Empty;
//新地位
Point newLocation = Point.Empty;
//鼠標按下坐標(control控件的絕對坐標)
Point mouseDownPoint = Point.Empty;
//顯示拖動後果的矩形
Rectangle rect = Rectangle.Empty;
//能否正在拖拽
bool isDrag = false;
public Puzzle()
{
InitializeComponent();
InitGame();
}
/// <summary>
/// 初始化游戲資本
/// </summary>
public void InitGame()
{
pictureList = new PictureBox[9] { pictureBox1, pictureBox2, pictureBox3, pictureBox4, pictureBox5, pictureBox6, pictureBox7, pictureBox8, pictureBox9 };
pointList = new Point[9] { new Point(0, 0), new Point(100, 0), new Point(200, 0), new Point(0, 100), new Point(100, 100), new Point(200, 100), new Point(0, 200), new Point(100, 200), new Point(200, 200) };
if (!Directory.Exists(Application.StartupPath.ToString() + "\\Picture"))
{
Directory.CreateDirectory(Application.StartupPath.ToString() + "\\Picture");
Properties.Resources.默許.Save(Application.StartupPath.ToString() + "\\Picture\\1.jpg");
Properties.Resources._1.Save(Application.StartupPath.ToString() + "\\Picture\\2.jpg");
Properties.Resources._2.Save(Application.StartupPath.ToString() + "\\Picture\\3.jpg");
Properties.Resources._3.Save(Application.StartupPath.ToString() + "\\Picture\\4.jpg");
Properties.Resources._4.Save(Application.StartupPath.ToString() + "\\Picture\\5.jpg");
Properties.Resources.勝利.Save(Application.StartupPath.ToString() + "\\Picture\\6.jpg");
Properties.Resources.喝彩.Save(Application.StartupPath.ToString() + "\\Picture\\7.jpg");
}
Random r = new Random();
int i = r.Next(7);
Flow(Application.StartupPath.ToString() + "\\Picture\\"+i.ToString()+".jpg");
}
private void Puzzle_Paint(object sender, PaintEventArgs e)
{
if (rect != Rectangle.Empty)
{
if (isDrag)
{
e.Graphics.DrawRectangle(Pens.White, rect);
}
else
{
e.Graphics.DrawRectangle(new Pen(this.BackColor), rect);
}
}
}
/// <summary>
/// 欠好用
/// </summary>
/// <returns></returns>
public PictureBox GetPictureBoxByLocation()
{
PictureBox pic = null;
if (this.ActiveControl.Name.Contains("pictureBox"))
{
pic = (PictureBox)this.ActiveControl;
}
return pic;
}
public PictureBox GetPictureBoxByLocation(MouseEventArgs e)
{
PictureBox pic = null;
foreach (PictureBox item in pictureList)
{
if (e.Location.X > item.Location.X && e.Location.Y > item.Location.Y && item.Location.X + 100 > e.Location.X && item.Location.Y + 100 > e.Location.X)
{
pic = item;
}
}
return pic;
}
public PictureBox GetPictureBoxByLocation(int x,int y)
{
PictureBox pic = null;
foreach (PictureBox item in pictureList)
{
if (x> item.Location.X && y > item.Location.Y && item.Location.X + 100 > x && item.Location.Y + 100 > y)
{
pic = item;
}
}
return pic;
}
/// <summary>
/// 經由過程hashcode獲得picture,用mouseeventargs以後獲得絕對於picture的坐標不是絕對窗體
/// </summary>
/// <param name="hascode"></param>
/// <returns></returns>
public PictureBox GetPictureBoxByHashCode(string hascode)
{
PictureBox pic = null;
foreach (PictureBox item in pictureList)
{
if (hascode == item.GetHashCode().ToString())
{
pic = item;
}
}
return pic;
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
oldLocation = new Point(e.X, e.Y);
currentPictureBox = GetPictureBoxByHashCode(sender.GetHashCode().ToString());
MoseDown(currentPictureBox, sender, e);
}
public void MoseDown(PictureBox pic, object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
oldLocation = e.Location;
rect = pic.Bounds;
}
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrag = true;
rect.Location = getPointToForm(new Point(e.Location.X - oldLocation.X, e.Location.Y - oldLocation.Y));
this.Refresh();
}
}
private void reset()
{
mouseDownPoint = Point.Empty;
rect = Rectangle.Empty;
isDrag = false;
}
private Point getPointToForm(Point p)
{
return this.PointToClient(pictureBox1.PointToScreen(p));
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
oldLocation = new Point(currentPictureBox.Location.X, currentPictureBox.Location.Y);
if (oldLocation.X + e.X > 300 || oldLocation.Y + e.Y > 300||oldLocation.X + e.X < 0 || oldLocation.Y + e.Y < 0)
{
return;
}
haveToPictureBox = GetPictureBoxByLocation(oldLocation.X + e.X, oldLocation.Y + e.Y);
newLocation = new Point(haveToPictureBox.Location.X, haveToPictureBox.Location.Y);
haveToPictureBox.Location = oldLocation;
PictureMouseUp(currentPictureBox, sender, e);
if ( Judge())
{
lab_result.Text = "勝利!";
//MessageBox.Show("祝賀拼圖勝利");
}
}
public void PictureMouseUp(PictureBox pic, object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (isDrag)
{
isDrag = false;
pic.Location = newLocation;
this.Refresh();
}
reset();
}
}
public void ExchangePictureBox(MouseEventArgs e)
{ }
private void btn_sta_Click(object sender, EventArgs e)
{
MessageBox.Show(this.ActiveControl.Name);
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="path"></param>
public void Flow(string path)
{
Image bm = CutPicture.Resize(path, 300, 300);
CutPicture.BitMapList = new List<Bitmap>();
for (int y = 0; y < 300; y += 100)
{
for (int x = 0; x < 300; x += 100)
{
//string key = x + "-" + y;
Bitmap temp = CutPicture.Cut(bm, x, y, 100, 100);
//pictureLocationDict.Add(key, temp);
CutPicture.BitMapList.Add(temp);
}
}
ImportBitMap();
}
/// <summary>
/// 打亂數據
/// </summary>
/// <param name="pictureArray"></param>
/// <returns></returns>
public PictureBox[] DisOrderArray(PictureBox[] pictureArray)
{
PictureBox[] tempArray = pictureArray;
for (int i = tempArray.Length - 1; i > 0; i--)
{
Random rand = new Random();
int p = rand.Next(i);
PictureBox temp = tempArray[p];
tempArray[p] = tempArray[i];
tempArray[i] = temp;
}
return tempArray;
}
/// <summary>
/// 斷定能否拼圖勝利
/// </summary>
/// <returns></returns>
public bool Judge()
{
bool result = true;
int i = 0;
foreach (PictureBox item in pictureList)
{
if (item.Location != pointList[i])
{
result = false;
}
i++;
}
return result;
}
private void btn_import_Click(object sender, EventArgs e)
{
lab_result.Text = "";
ofd_picture.ShowDialog();
CutPicture.PicturePath = ofd_picture.FileName;
Flow(CutPicture.PicturePath);
CountTime();
}
/// <summary>
/// 計時
/// </summary>
public void CountTime()
{
lab_time.Text = "0";
timer1.Start();
}
/// <summary>
/// 給piturebox賦值
/// </summary>
public void ImportBitMap()
{
try
{
int i = 0;// DisOrderArray(pictureList)
foreach (PictureBox item in pictureList)
{
Bitmap temp = CutPicture.BitMapList[i];
item.Image = temp;
i++;
}
ResetPictureLocation();
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
}
/// <summary>
/// 打亂地位列表
/// </summary>
/// <returns></returns>
public Point[] DisOrderLocation()
{
Point[] tempArray = (Point[])pointList.Clone();
for (int i = tempArray.Length - 1; i > 0; i--)
{
Random rand = new Random();
int p = rand.Next(i);
Point temp = tempArray[p];
tempArray[p] = tempArray[i];
tempArray[i] = temp;
}
return tempArray;
}
/// <summary>
/// 從新設置圖片地位
/// </summary>
public void ResetPictureLocation()
{
Point[] temp = DisOrderLocation();
int i = 0;
foreach (PictureBox item in pictureList)
{
item.Location = temp[i];
i++;
}
}
/// <summary>
/// 計時,跨越30秒停滯計時
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
second++;
lab_time.Text = second.ToString();
if (second == 30)
{
timer1.Stop();
lab_result.Text = "掉敗!";
}
}
private void btn_sta_Click_1(object sender, EventArgs e)
{
lab_result.Text = "";
timer1.Start();
}
}
}
CutPicture.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace Puzzle
{
class CutPicture
{
public static string PicturePath = "";
public static List<Bitmap> BitMapList = null;
/// <summary>
/// 剪切圖片
/// </summary>
/// <param name="b">圖片</param>
/// <param name="StartX">X坐標</param>
/// <param name="StartY">Y坐標</param>
/// <param name="iWidth">寬</param>
/// <param name="iHeight">高</param>
/// <returns></returns>
public static Bitmap Cut(Image b, int StartX, int StartY, int iWidth, int iHeight)
{
if (b == null)
{
return null;
}
int w = b.Width;
int h = b.Height;
if (StartX >= w || StartY >= h)
{
return null;
}
if (StartX + iWidth > w)
{
iWidth = w - StartX;
}
if (StartY + iHeight > h)
{
iHeight = h - StartY;
}
try
{
Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}
/// <summary>
/// 保留圖片到根目次的Pictures文件夾下
/// </summary>
/// <param name="path">文件途徑</param>
/// <param name="iWidth">調劑的寬</param>
/// <param name="iHeignt">調劑的高</param>
/// <returns></returns>
public static Image Resize(string path, int iWidth, int iHeignt)
{
Image thumbnail = null;
try
{
var img = Image.FromFile(path);
thumbnail = img.GetThumbnailImage(iWidth, iHeignt, null, IntPtr.Zero);
thumbnail.Save(Application.StartupPath.ToString() + "\\Picture\\img.jpeg");
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
return thumbnail;
}
}
}
mouse_down
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
oldLocation = new Point(e.X, e.Y);
currentPictureBox = GetPictureBoxByHashCode(sender.GetHashCode().ToString());
MoseDown(currentPictureBox, sender, e);
}
public void MoseDown(PictureBox pic, object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
oldLocation = e.Location;
rect = pic.Bounds;
}
}
mouse_move
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrag = true;
rect.Location = getPointToForm(new Point(e.Location.X - oldLocation.X, e.Location.Y - oldLocation.Y));
this.Refresh();
}
}
mouse_up
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
oldLocation = new Point(currentPictureBox.Location.X, currentPictureBox.Location.Y);
if (oldLocation.X + e.X > 300 || oldLocation.Y + e.Y > 300||oldLocation.X + e.X < 0 || oldLocation.Y + e.Y < 0)
{
return;
}
haveToPictureBox = GetPictureBoxByLocation(oldLocation.X + e.X, oldLocation.Y + e.Y);
newLocation = new Point(haveToPictureBox.Location.X, haveToPictureBox.Location.Y);
haveToPictureBox.Location = oldLocation;
PictureMouseUp(currentPictureBox, sender, e);
if ( Judge())
{
lab_result.Text = "勝利!";
//MessageBox.Show("祝賀拼圖勝利");
}
}
public void PictureMouseUp(PictureBox pic, object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (isDrag)
{
isDrag = false;
pic.Location = newLocation;
this.Refresh();
}
reset();
}
}
reset
private void reset()
{
mouseDownPoint = Point.Empty;
rect = Rectangle.Empty;
isDrag = false;
}
以上所述就是本文的全體內容了,願望年夜家可以或許愛好。