• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#利用控件拖拽技术制作拼图游戏

C#利用控件拖拽技术制作拼图游戏

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c#拖拽,c#拖拽文件,android控件拖拽,拖拽控件,qq邮箱拖拽控件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

主要实现的功能:

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"></para



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#利用控件拖拽技术制作拼图游戏
  • C# 实现的图片盖章功能,支持拖拽、旋转、放缩、保存

相关文章

  • 2017-05-28Winform启动另一个项目传值的方法
  • 2017-05-28C#.Net ArrayList的使用方法
  • 2017-05-28C#最简单的字符串加密解密方法
  • 2017-05-28C#操作SQLite方法实例详解
  • 2017-05-28C#中计算时间差中的小数问题解决
  • 2017-05-28C#控制台程序使用Log4net日志组件详解
  • 2017-05-28解析C#彩色图像灰度化算法的实现代码详解
  • 2017-05-28C# 中如何利用lambda实现委托事件的挂接
  • 2017-05-28C#基础之泛型委托实例教程
  • 2017-05-28C#基础语法:Base关键字学习笔记

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C# 位运算符整理
    • 如何使用C#操作幻灯片
    • C#连接Oracle数据库的实例方法
    • C#中WinForm控件的拖动和缩放的实现代码
    • C#数据库操作小结
    • 深入c# Func委托的详解
    • C#采用OpenXml实现给word文档添加文字
    • Winform中进行MD5加密的实例
    • C#中FormClosing与FormClosed的区别详细解析
    • 在.net应用程序中运行其它EXE文件的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有