c#期末项目——连连看

本文介绍了一个使用C#编程实现的连连看游戏。游戏中,通过随机算法生成游戏地图,玩家通过选择两个相同图案的方块进行消除。文章详细展示了游戏的初始化、重置、图像绘制和游戏逻辑等功能的实现代码,包括判断两个方块是否能连通的算法。随着关卡的递增,难度也会相应提升。
摘要由CSDN通过智能技术生成
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
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值