using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace 连连看
{
public partial class Form1 : Form
{
private Bitmap Source;
private int W = 50;
private int GameSize=10;
private bool Select_first = false;
private int x1, y1;
private int x2, y2;
Point z1, z2;
private int m_nCol = 10;
private int m_nRow = 10;
private int[] m_map = new int[10*10];
public enum LinkType {LineType,OneCornerType,TwoCornerType};
LinkType LType;
// public int LType;
private int level = 1;
private int length = 20;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Source = (Bitmap)Image.FromFile("D:\\MyProject\\C#\\WindowsFormsApplication4\\WindowsFormsApplication4\\animal.bmp");
this.pictureBox1.Height = W * (m_nRow+2);
this.pictureBox1.Width = W * (m_nCol+2);
this.pictureBox1.Top = 23;
this.pictureBox1.Left = 0;
int d = (this.Height - this.ClientRectangle.Height-this.menuStrip1.Height);
this.Height = this.pictureBox1.Height + this.pictureBox1.Top+d;
this.Width = this.pictureBox1.Width + this.pictureBox1.Left;
StartNewGame();
Init_Graphic();
}
private void StartNewGame()
{
length = 500;
for(int i=0;i<(m_nCol*m_nRow);i++)
{
m_map[i]=-1;
}
Random r = new Random();
ArrayList tmp=new ArrayList ();
for(int i=0;i<(m_nCol*m_nRow)/4;i++)
for(int j=0;j<4;j++)
tmp.Add(i);
for (int i = 0; i < m_nRow * m_nCol; i++)
{
int n = r.Next() % tmp.Count;
m_map[i] = (int)tmp[n];
tmp.RemoveAt(n);
}
}
private void RebuildGame()
{
Random
r = new Random();
ArrayList tmp = new ArrayList();
int count = 0;
for (int i = 0; i < (m_nCol * m_nRow); i++)
{
if (m_map[i] != -1)
count++;
}
if (count % 4 == 0)
{
for (int i = 0; i < count / 4; i++)
for (int j = 0; j < 4; j++)
tmp.Add(i);
for (int i = 0; i < m_nRow * m_nCol; i++)
{
if (tmp.Count != 0)
{
int n = r.Next() % tmp.Count;
if (m_map[i] != -1)
{
m_map[i] = (int)tmp[n];
tmp.RemoveAt(n);
}
}
}
}
else
{
for (int i = 0; i < 2; i++)
{
if (m_map[i] != -1) m_map[i] = 25;
}
int temp=0;
for (int i = 0; i < (count - 2) / 4; i++)
{ temp++;
for (int j = 0; j < 4; j++)
tmp.Add(i);
}
for (int m = 0; m < m_nRow * m_nCol; m++)
{
if (tmp.Count == 0)
break;
int n = r.Next() % tmp.Count;
if (m_map[m] != -1)
{
m_map[m] = (int)tmp[n];
tmp.RemoveAt(n);
}
}
}
}
private void Init_Graphic()
{
Graphics g = get_Graphic();
g.DrawString("第" + level + "关", new Font("宋体", 10, FontStyle.Regular), Brushes.White, (float)10, (float)20);
// g.DrawRectangle(new Pen(Color.Green, 2), 20, 20, length, 20);
for (int i = 0; i< 10 * 10; i++)
{
g.DrawImage(create_image(m_map[i]), W * (i % GameSize)+W, W * (i / GameSize)+W, W, W);
}
}
private Graphics get_Graphic()
{
//if (pictureBox1.Image == null
c#期末项目——连连看
最新推荐文章于 2023-12-23 15:09:31 发布
本文介绍了一个使用C#编程实现的连连看游戏。游戏中,通过随机算法生成游戏地图,玩家通过选择两个相同图案的方块进行消除。文章详细展示了游戏的初始化、重置、图像绘制和游戏逻辑等功能的实现代码,包括判断两个方块是否能连通的算法。随着关卡的递增,难度也会相应提升。
摘要由CSDN通过智能技术生成