📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace 拼图游戏
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int _Width;//每张图的宽度
int _Height;//每张图的高度
PictureBox[] _pics=null;
public Image Pic1
{
set
{
pic1.Image = value;
}
get
{
return pic1.Image;
}
}
private void Form1_Load(object sender, EventArgs e)
{
string[] stName = Directory.GetFiles("img","*.*");
foreach (string st in stName)
{
comBox.Items.Add(st);
}
}
/// <summary>
/// 选中的文本发生改变时候
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comBox_SelectedIndexChanged(object sender, EventArgs e)
{
Pic1 = Image.FromFile(comBox.Text);
pic1.SizeMode = PictureBoxSizeMode.StretchImage;
}
/// <summary>
/// 游戏开始
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void butStart_Click(object sender, EventArgs e)
{
this.Run();
}
public void Run()
{
_Height = pan.Height / Game._map.GetLength(0);
_Width = pan.Width / Game._map.GetLength(1);
if (_pics != null)
{
pan.Controls.Clear();
for (int x = 0; x < _pics.Length; x++)
{
_pics[x].Dispose();
}
}
_pics = GetPics();
for (int x = 0; x < Game._map.GetLength(0); x++)
{
for (int y = 0; y < Game._map.GetLength(1); y++)
{
if (Game._map[x, y] == 8)
{
continue;
}
_pics[Game._map[x, y]].Show();
pan.Controls.Add(_pics[Game._map[x, y]]);
}
}
refashMap();
}
/// <summary>
/// 刷新地图
/// </summary>
public void refashMap()
{
for (int x = 0; x < Game._map.GetLength(0); x++)
{
for (int y = 0; y < Game._map.GetLength(1); y++)
{
if (Game._map[x, y] == 8)
{
continue;
}
_pics[Game._map[x, y]].Top = x * _Height;
_pics[Game._map[x, y]].Left = y * _Width;
}
}
}
/// <summary>
/// 获得图片数组
/// </summary>
/// <returns></returns>
public PictureBox[] GetPics()
{
int index=0;
PictureBox[] pi=new PictureBox[Game._map.GetLength(0)*Game._map.GetLength(1)];
for (int x = 0; x < Game._map.GetLength(0); x++)
{
for (int y = 0; y < Game._map.GetLength(1); y++)
{
PictureBox pi1 = new PictureBox();
pi1.Width =_Width;
pi1.Height = _Height;
pi1.BorderStyle = BorderStyle.FixedSingle;
pi1.Image = this.GetBitmap(x,y);
pi1.SizeMode = PictureBoxSizeMode.StretchImage;
pi[index] = pi1;
index++;
}
}
return pi;
}
public Bitmap GetBitmap(int x,int y)
{
Bitmap bp = new Bitmap(_Width,_Height);
Bitmap bc = (Bitmap)Pic1;
for (int b=0,j = x *_Height; j < x *_Height + _Height;b++, j++)
{
for (int a=0, z = y * _Width; z < y * _Width + _Width; z++,a++)
{
bp.SetPixel(a,b,bc.GetPixel(z,j));
}
}
return bp;
}
/// <summary>
/// 键盘按下实践
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.A: Game.Move(Way.Left); break;
case Keys.D: Game.Move(Way.Right); break;
case Keys.W: Game.Move(Way.Up); break;
case Keys.S: Game.Move(Way.Lower); break;
}
refashMap();
if (Game.isWin())
{
MessageBox.Show("胜利咯哦");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -