📄 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.Media;
using System.Runtime.InteropServices;
using System.Threading;
using System.Resources;
using System.Reflection;
//=======================================================================
//本程序由本人独立完成,允许修改,但请保留本处.
//联系方式:blessyou312@163.com
//blog:blog.csdn.net/blessyou312 hi.baidu.com/blessyou312
//=======================================================================
namespace LLK
{
public partial class Form1 : Form
{
#region//API
[DllImport("Winmm.dll")]
public static extern long PlaySound(string name,long module,long flag);
#endregion
public Form1()
{
InitializeComponent();
IniteBmp(MAXPICS);
}
Music music = new Music();
SoundPlayer selectplayer = new SoundPlayer("Sounds\\select.wav");
SoundPlayer eraseplayer = new SoundPlayer("Sounds\\erase.wav");
SoundPlayer refreshplayer = new SoundPlayer("Sounds\\refresh.wav");
SoundPlayer bombplayer = new SoundPlayer("Sounds\\bomb.wav");
int[,] gmap = new int[MAPWIDTH,MAPHEIGHT];//实际的图片矩阵为19*11
Graphics g_g = null;//全局画布
const int PBMAX = 100;//processbar的最大值
static int pbvalue=PBMAX;//proecssbar现在的值
static int reducestep = 1;//第次减少的步数
static long score = 0;//游戏得分
//图片的宽和高
private const int PICWIDTH = 31;
private const int PICHEIGHT = 34;
private const int MAPWIDTH = 19;
private const int MAPHEIGHT = 11;
private const int MAXPICS = 39;
//本局中图片的数量
private int picnum = 18;
private int multipic = 4;//一张图片重复出来的次数,一定为偶数
//加载到内存中的图片
Image[] img = new Image[39];
Image[] bombimg = new Image[6];
Kernal AI=null;
//游戏开始
bool bStart = false;
//加载图
private void IniteBmp(int maxnum)
{
g_g = this.CreateGraphics();
for (int i = 0; i < MAPWIDTH; i++)
for (int j = 0; j < MAPHEIGHT; j++)
gmap[i, j] = 0;
IniteRandomMap(ref gmap, maxnum);
AI = new Kernal(ref gmap);
for (int i = 0; i < maxnum; i++)
{
ResourceManager rm = new ResourceManager("LLK.data", Assembly.GetExecutingAssembly());
img[i]= (Image)rm.GetObject(i.ToString()+".bmp");
//img[i] = (Image)Bitmap.FromFile("Images\\"+(i+1).ToString()+".bmp");
}
for (int i = 0; i < 6; i++)
{
//bombimg[i] = (Image)Bitmap.FromFile("Images\\B"+(i+1).ToString()+".bmp");
}
}
private bool CheckWin(ref int[,] map)
{
bool Win = true;
for (int i = 0; i < MAPWIDTH; i++)
for (int j = 0; j < MAPHEIGHT; j++)
if (map[i, j] != 0)
Win = false;
return Win;
}
private void IniteRandomMap(ref int[,] map,int num)
{
Random r=new Random();
while (num > 0)
{
for (int i = 0; i < multipic; i++)
{
int xrandom = r.Next(19) ;
int yrandom = r.Next(11) ;
if (map[xrandom, yrandom] == 0)
{
map[xrandom, yrandom] = num;
}
else
i--;
}
num--;
}
}
private void FreshMap(ref int[,] map)
{
Random r = new Random();
for(int i=0;i<MAPWIDTH;i++)
for (int j = 0; j < MAPHEIGHT; j++)
{
if (gmap[i, j] != 0)
{
int x = r.Next(19);
int y = r.Next(11);
int temp = gmap[x, y];
gmap[x, y] = gmap[i, j];
gmap[i, j] = temp;
}
}
TransportMap(ref gmap);
}
private void TransportMap(ref int[,] map)
{
for(int i=0;i<MAPWIDTH;i++)
for (int j = 0; j < MAPHEIGHT; j++)
{
AI.GiveMapValue(i, j, map[i, j]);
}
}
//在指定位置画指定图
private void Draw(Graphics g, Image scrImg, int PicX,int PicY)
{
g.DrawImage(scrImg, new Point(PicX, PicY));
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
g_g.DrawLine(new Pen(new SolidBrush(Color.DeepSkyBlue), 5), 0, 11 * 34 + 5, 19 * 34, 11 * 34 + 5);
if (bStart)
{
for (int i = 0; i < MAPWIDTH; i++)
for (int j = 0; j < MAPHEIGHT; j++)
{
if (gmap[i, j] != 0)
{
Draw(g_g, img[gmap[i, j] - 1], i * PICWIDTH, j * PICHEIGHT);
}
}
}
}
int two = 0;//用来计算点击块的次数,最大为2
Point fpoint;//记录第一次点击的位置
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
int curx = e.X / 31;
int cury = e.Y/ 34;
if (curx > 18 || cury > 10)
return;
if (gmap[curx, cury] != 0)
{
selectplayer.Play();
Pen pen = new Pen(new SolidBrush(this.BackColor));
g_g.DrawRectangle(pen, new Rectangle(curx * 31, cury * 34, 31, 34));
two++;
switch (two)
{
case 1:
fpoint = new Point(curx, cury);
break;
case 2:
if (AI.IsLink(fpoint.X, fpoint.Y, curx, cury))
{
two = 0;
eraseplayer.Play();
//获得拐点数目和位置,做画线处理
ProcessCorner(fpoint,new Point(curx,cury));
if (CheckWin(ref gmap))
{
starttimer = false;
MessageBox.Show("恭喜过关!");
bStart = false;
picnum++;//种类加1
if (picnum>38)
{
picnum = 39;
multipic += 2;
if (picnum * multipic > 209)
{
MessageBox.Show("你太强悍了,请联系尚占锋,请为你提高难度!哈哈!");
return;
}
}
button1_Click_1(sender, e);
}
}
else
{
two = 1;
fpoint = new Point(curx, cury);
}
break;
default:
break;
}
}
}
//此方法用做处理画线和消去,写的比较烂
private void ProcessCorner(Point p1,Point p2)
{
Point[] corner = new Point[3];
corner = AI.GetPoints();
Pen pen = new Pen(new SolidBrush(Color.Red), 5);//线画笔
Pen bkpen = new Pen(new SolidBrush(this.BackColor), 5);//擦去画笔
pbvalue += 4;
// MoveUp(p1.X, p1.Y);
//MoveUp(p2.X, p2.Y);
switch (corner[2].X)
{
case 1:
score += 20;//一个拐点加20;
g_g.DrawLine(pen, new Point(p1.X*31+15,p1.Y*34+17), new Point(corner[0].X*31+15,corner[0].Y*34+17));
g_g.DrawLine(pen, new Point(p2.X * 31+15, p2.Y * 34+17), new Point(corner[0].X * 31+15, corner[0].Y * 34+17));
Thread.Sleep(100);
EraseBlock(g_g, p1, p2);
g_g.DrawLine(bkpen, new Point(p1.X * 31 + 15, p1.Y * 34 + 17), new Point(corner[0].X * 31 + 15, corner[0].Y * 34 + 17));
g_g.DrawLine(bkpen, new Point(p2.X * 31 + 15, p2.Y * 34 + 17), new Point(corner[0].X * 31 + 15, corner[0].Y * 34 + 17));
break;
case 2:
score += 40;//两个拐点加40
Point[] ps ={ new Point(p1.X*31+15,p1.Y*34+17),new Point( corner[1].X*31+15,corner[1].Y*34+17),new Point(corner[0].X*31+15,corner[0].Y*34+17),new Point(p2.X*31+15,p2.Y*34+17)};
g_g.DrawLines(pen, ps);
Thread.Sleep(100);
EraseBlock(g_g, p1, p2);
g_g.DrawLines(bkpen,ps);
//foreach (Point mp in ps)
//{
//MessageBox.Show("("+mp.X.ToString()+","+mp.Y.ToString()+")");
//}
break;
case 0:
score += 10;//直连加10
g_g.DrawLine(pen, new Point(p1.X * 31 + 15, p1.Y * 34 + 17), new Point(p2.X * 31 + 15, p2.Y * 34 + 17));
Thread.Sleep(100);
EraseBlock(g_g, p1, p2);
g_g.DrawLine(bkpen, new Point(p1.X * 31 + 15, p1.Y * 34 + 17), new Point(p2.X * 31 + 15, p2.Y * 34 + 17));
break;
default: break;
}
//RefreshMap(ref gmap);
label5.Text = score.ToString();
}
private void EraseBlock(Graphics g, Point p1, Point p2)
{
g.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(p1.X * 31, p1.Y * 34, 31, 34));
g.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(p2.X * 31, p2.Y * 34, 31, 34));
gmap[p1.X, p1.Y] = 0;
gmap[p2.X, p2.Y] = 0;
AI.GiveMapValue(p1.X, p1.Y, 0);
AI.GiveMapValue(p2.X, p2.Y, 0);
}
bool starttimer = false;
private void button1_Click_1(object sender, EventArgs e)
{
//处理Processbar
if (!starttimer)
{
progressBar1.Value = PBMAX;
pbtimer.Interval = 500;
pbtimer.Start();
starttimer = true;
}
//处理分数
score = 0;
picnum = Convert.ToInt16(textBox1.Text);
multipic = Convert.ToInt16(textBox2.Text);
if (picnum * multipic > 209)
{
MessageBox.Show("游戏区域内最多只有209个空,您选的数据太多!请重新选 !");
textBox1.Text = "18";
textBox2.Text = "4";
return;
}
IniteBmp(picnum);
if (bStart)
{
MessageBox.Show("游戏已在运行!");
return;
}
else
{
bStart = true;
this.Invalidate();
music.Play("Sounds\\bg-03.mid");
}
}
//将块向上移
private void MoveUp(int x, int y)
{
if (y < 1)
return;
for (int i = y; i < MAPHEIGHT-1;i++ )
{
gmap[x, i] = gmap[x, i + 1];
}
}
private void RefreshMap(ref int[,] map)
{
if (bStart)
{
for (int i = 0; i < MAPWIDTH; i++)
for (int j = 0; j < MAPHEIGHT; j++)
{
if (gmap[i, j] != 0)
{
Draw(g_g, img[gmap[i, j] - 1], i * PICWIDTH, j * PICHEIGHT);
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
refreshplayer.Play();
//Thread.Sleep(100);
//bombplayer.Play();
FreshMap(ref gmap);
this.Invalidate();
}
/// <summary>
/// 以下为爆炸处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
}
private void pbtimer_Tick(object sender, EventArgs e)
{
pbvalue = pbvalue - reducestep;
if (pbvalue > 100)
pbvalue = 100;
if (pbvalue == 0&&starttimer)
{
starttimer = false;
pbtimer.Stop();
MessageBox.Show("Failed!");
return;
}
else
progressBar1.Value = pbvalue;
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
MessageBox.Show("你好顾银晓");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -