最近两天写了个c#的拼图小游戏,支持用户用自己的图片,以下是代码
新手所以比较简陋,美化的相关还没学,以下是源代码,新手可以看一下
顺带学了下打包成安装包.msi 和setup.exe虽然中间也遇到了问题,但最后也都解决了
准备最近再练练算法的题,迷宫,acm什么的
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;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int succ = 0;
private void Form1_Paint(object sender, PaintEventArgs e) //初学gdi绘图的小代码
{
//Graphics g = e.Graphics;
//Pen p = new Pen(Color.Black, 1);
//g.DrawLine(p, 10, 10, 100, 100);
//g.DrawRectangle(p, 10, 10, 100, 100);
//g.DrawEllipse(p, 10, 10, 100, 100);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
if ((pictureBox1.Location.Y == pictureBox9.Location.Y && pictureBox1.Location.X == pictureBox9.Location.X + 100) || (pictureBox1.Location.Y == pictureBox9.Location.Y && pictureBox1.Location.X == pictureBox9.Location.X - 100) || (pictureBox1.Location.X
== pictureBox9.Location.X && pictureBox1.Location.Y == pictureBox9.Location.Y - 100) || (pictureBox1.Location.X == pictureBox9.Location.X && pictureBox1.Location.Y == pictureBox9.Location.Y + 100))
{
Point p = pictureBox9.Location;
pictureBox9.Location = pictureBox1.Location;
pictureBox1.Location = p;
}
}
private void pictureBox2_Click(object sender, EventArgs e)
{
if ((pictureBox2.Location.Y == pictureBox9.Location.Y && pictureBox2.Location.X == pictureBox9.Location.X + 100) || (pictureBox2.Location.Y == pictureBox9.Location.Y && pictureBox2.Location.X == pictureBox9.Location.X - 100) || (pictureBox2.Location.X
== pictureBox9.Location.X && pictureBox2.Location.Y == pictureBox9.Location.Y - 100) || (pictureBox2.Location.X == pictureBox9.Location.X && pictureBox2.Location.Y == pictureBox9.Location.Y + 100))
{
Point p = pictureBox9.Location;
pictureBox9.Location = pictureBox2.Location;
pictureBox2.Location = p;
}
}
private void pictureBox3_Click(object sender, EventArgs e)
{
if ((pictureBox3.Location.Y == pictureBox9.Location.Y && pictureBox3.Location.X == pictureBox9.Location.X + 100) || (pictureBox3.Location.Y == pictureBox9.Location.Y && pictureBox3.Location.X == pictureBox9.Location.X - 100) || (pictureBox3.Location.X
== pictureBox9.Location.X && pictureBox3.Location.Y == pictureBox9.Location.Y - 100) || (pictureBox3.Location.X == pictureBox9.Location.X && pictureBox3.Location.Y == pictureBox9.Location.Y + 100))
{
Point p = pictureBox9.Location;
pictureBox9.Location = pictureBox3.Location;
pictureBox3.Location = p;
}
}
private void pictureBox4_Click(object sender, EventArgs e)
{
if ((pictureBox4.Location.Y == pictureBox9.Location.Y && pictureBox4.Location.X == pictureBox9.Location.X + 100) || (pictureBox4.Location.Y == pictureBox9.Location.Y && pictureBox4.Location.X == pictureBox9.Location.X - 100) || (pictureBox4.Location.X
== pictureBox9.Location.X && pictureBox4.Location.Y == pictureBox9.Location.Y - 100) || (pictureBox4.Location.X == pictureBox9.Location.X && pictureBox4.Location.Y == pictureBox9.Location.Y + 100))
{
Point p = pictureBox9.Location;
pictureBox9.Location = pictureBox4.Location;
pictureBox4.Location = p;
}
}
private void pictureBox5_Click(object sender, EventArgs e)
{
if ((pictureBox5.Location.Y == pictureBox9.Location.Y && pictureBox5.Location.X == pictureBox9.Location.X + 100) || (pictureBox5.Location.Y == pictureBox9.Location.Y && pictureBox5.Location.X == pictureBox9.Location.X - 100) || (pictureBox5.Location.X
== pictureBox9.Location.X && pictureBox5.Location.Y == pictureBox9.Location.Y - 100) || (pictureBox5.Location.X == pictureBox9.Location.X && pictureBox5.Location.Y == pictureBox9.Location.Y + 100))
{
Point p = pictureBox9.Location;
pictureBox9.Location = pictureBox5.Location;
pictureBox5.Location = p;
}
}
private void pictureBox6_Click(object sender, EventArgs e)
{
if ((pictureBox6.Location.Y == pictureBox9.Location.Y && pictureBox6.Location.X == pictureBox9.Location.X + 100) || (pictureBox6.Location.Y == pictureBox9.Location.Y && pictureBox6.Location.X == pictureBox9.Location.X - 100) || (pictureBox6.Location.X
== pictureBox9.Location.X && pictureBox6.Location.Y == pictureBox9.Location.Y - 100) || (pictureBox6.Location.X == pictureBox9.Location.X && pictureBox6.Location.Y == pictureBox9.Location.Y + 100))
{
Point p = pictureBox9.Location;
pictureBox9.Location = pictureBox6.Location;
pictureBox6.Location = p;
}
if (succ == 0)
{
if (pictureBox1.Location == new Point(0, 0) && pictureBox2.Location == new Point(100, 0) && pictureBox3.Location == new Point(200, 0) && pictureBox4.Location == new Point(0, 100) && pictureBox5.Location == new Point(100, 100) && pictureBox6.Location
== new Point(200, 100) && pictureBox7.Location == new Point(0, 200) && pictureBox8.Location == new Point(100, 200))
{
MessageBox.Show("拼图成功,恭喜(~ ̄▽ ̄)~ ", "congratulation", MessageBoxButtons.OK);
succ = 1;
}
}
}
private void pictureBox7_Click(object sender, EventArgs e)
{
if ((pictureBox7.Location.Y == pictureBox9.Location.Y && pictureBox7.Location.X == pictureBox9.Location.X + 100) || (pictureBox7.Location.Y == pictureBox9.Location.Y && pictureBox7.Location.X == pictureBox9.Location.X - 100) || (pictureBox7.Location.X
== pictureBox9.Location.X && pictureBox7.Location.Y == pictureBox9.Location.Y - 100) || (pictureBox7.Location.X == pictureBox9.Location.X && pictureBox7.Location.Y == pictureBox9.Location.Y + 100))
{
Point p = pictureBox9.Location;
pictureBox9.Location = pictureBox7.Location;
pictureBox7.Location = p;
}
}
private void pictureBox8_Click(object sender, EventArgs e)
{
if ((pictureBox8.Location.Y == pictureBox9.Location.Y && pictureBox8.Location.X == pictureBox9.Location.X + 100) || (pictureBox8.Location.Y == pictureBox9.Location.Y && pictureBox8.Location.X == pictureBox9.Location.X - 100) || (pictureBox8.Location.X
== pictureBox9.Location.X && pictureBox8.Location.Y == pictureBox9.Location.Y - 100) || (pictureBox8.Location.X == pictureBox9.Location.X && pictureBox8.Location.Y == pictureBox9.Location.Y + 100))
{
Point p = pictureBox9.Location;
pictureBox9.Location = pictureBox8.Location;
pictureBox8.Location = p;
}
if (succ == 0)
{
if (pictureBox1.Location == new Point(0, 0) && pictureBox2.Location == new Point(100, 0) && pictureBox3.Location == new Point(200, 0) && pictureBox4.Location == new Point(0, 100) && pictureBox5.Location == new Point(100, 100) && pictureBox6.Location
== new Point(200, 100) && pictureBox7.Location == new Point(0, 200) && pictureBox8.Location == new Point(100, 200))
{
MessageBox.Show("拼图成功,恭喜(~ ̄▽ ̄)~ ", "congratulation", MessageBoxButtons.OK);
succ = 1;
}
}
}
private void pictureBox9_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e) //这一段是对图像的操作
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
string strf = System.IO.Path.GetExtension(ofd.FileName);
if (strf == ".jpg" || strf == ".png" || strf == ".bmp")
{
Image img1 = Image.FromFile(ofd.FileName);
if (img1.Width < 300 || img1.Height < 300)
MessageBox.Show("图片大小必须大于300*300", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
pictureBox10.Image = new System.Drawing.Bitmap(img1, 200, 200); //图片自适应
Image img2 = new System.Drawing.Bitmap(img1, 300, 300);
Bitmap bmp = new Bitmap(img2); //图像分割
pictureBox1.Image = bmp.Clone(new System.Drawing.Rectangle(0, 0, 100, 100), img2.PixelFormat);
pictureBox2.Image = bmp.Clone(new System.Drawing.Rectangle(100, 0, 100, 100), img2.PixelFormat);
pictureBox3.Image = bmp.Clone(new System.Drawing.Rectangle(200, 0, 100, 100), img2.PixelFormat);
pictureBox4.Image = bmp.Clone(new System.Drawing.Rectangle(0, 100, 100, 100), img2.PixelFormat);
pictureBox5.Image = bmp.Clone(new System.Drawing.Rectangle(100, 100, 100, 100), img2.PixelFormat);
pictureBox6.Image = bmp.Clone(new System.Drawing.Rectangle(200, 100, 100, 100), img2.PixelFormat);
pictureBox7.Image = bmp.Clone(new System.Drawing.Rectangle(0, 200, 100, 100), img2.PixelFormat);
pictureBox8.Image = bmp.Clone(new System.Drawing.Rectangle(100, 200, 100, 100), img2.PixelFormat);
Random r = new Random();
Point pit = new Point();
for (int i = 0; i <= r.Next(60, 120); i++) //打乱图片且保证肯定可以拼成功
{
int j = r.Next(4);
switch (j)
{
case 0:
{
if (pictureBox9.Location.X != 0)
{
pit = pictureBox9.Location;
pictureBox9.Location = new Point(pictureBox9.Location.X - 100, pictureBox9.Location.Y);
foreach (Control p in this.panel1.Controls)
{
if (p is PictureBox)
{
if (p.Location == pictureBox9.Location)
{
if (p != pictureBox9)
p.Location = pit;
}
}
}
}
} break;
case 1:
{
if (pictureBox9.Location.Y != 0)
{
pit = pictureBox9.Location;
pictureBox9.Location = new Point(pictureBox9.Location.X, pictureBox9.Location.Y - 100);
foreach (Control p in this.panel1.Controls)
{
if (p is PictureBox)
{
if (p.Location == pictureBox9.Location)
{
if (p != pictureBox9)
p.Location = pit;
}
}
}
}
} break;
case 2:
{
if (pictureBox9.Location.X != 200)
{
pit = pictureBox9.Location;
pictureBox9.Location = new Point(pictureBox9.Location.X + 100, pictureBox9.Location.Y);
foreach (Control p in this.panel1.Controls)
{
if (p is PictureBox)
{
if (p.Location == pictureBox9.Location)
{
if (p != pictureBox9)
p.Location = pit;
}
}
}
}
} break;
case 3:
{
if (pictureBox9.Location.Y != 200)
{
pit = pictureBox9.Location;
pictureBox9.Location = new Point(pictureBox9.Location.X, pictureBox9.Location.Y + 100);
foreach (Control p in this.panel1.Controls)
{
if (p is PictureBox)
{
if (p.Location == pictureBox9.Location)
{
if (p != pictureBox9)
p.Location = pit;
}
}
}
}
} break;
}
}
}
}
else
{
if(ofd.ShowDialog()==DialogResult.Cancel)
MessageBox.Show("您未选择文件", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
MessageBox.Show("仅支持.bmp或.png或.bmp文件","error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void Form1_Load(object sender, EventArgs e)
{
succ = 0;
Random r = new Random();
Point pit = new Point();
for (int i = 0; i <= r.Next(60, 120); i++)
{
int j = r.Next(4);
switch (j)
{
case 0:
{
if (pictureBox9.Location.X != 0)
{
pit = pictureBox9.Location;
pictureBox9.Location = new Point(pictureBox9.Location.X - 100, pictureBox9.Location.Y);
foreach (Control p in this.panel1.Controls)
{
if (p is PictureBox)
{
if (p.Location == pictureBox9.Location)
{
if (p != pictureBox9)
p.Location = pit;
}
}
}
}
}break;
case 1:
{
if (pictureBox9.Location.Y != 0)
{
pit = pictureBox9.Location;
pictureBox9.Location = new Point(pictureBox9.Location.X, pictureBox9.Location.Y - 100);
foreach (Control p in this.panel1.Controls)
{
if (p is PictureBox)
{
if (p.Location == pictureBox9.Location)
{
if (p != pictureBox9)
p.Location = pit;
}
}
}
}
} break;
case 2:
{
if (pictureBox9.Location.X != 200)
{
pit = pictureBox9.Location;
pictureBox9.Location = new Point(pictureBox9.Location.X + 100, pictureBox9.Location.Y);
foreach (Control p in this.panel1.Controls)
{
if (p is PictureBox)
{
if (p.Location == pictureBox9.Location)
{
if (p != pictureBox9)
p.Location = pit;
}
}
}
}
} break;
case 3:
{
if (pictureBox9.Location.Y != 200)
{
pit = pictureBox9.Location;
pictureBox9.Location = new Point(pictureBox9.Location.X, pictureBox9.Location.Y + 100);
foreach (Control p in this.panel1.Controls)
{
if (p is PictureBox)
{
if (p.Location == pictureBox9.Location)
{
if (p != pictureBox9)
p.Location = pit;
}
}
}
}
} break;
}
}
}
}
}