基本信息
源码名称:C# 2048 小游戏源码
源码大小:0.24M
文件格式:.zip
开发语言:C#
更新时间:2018-06-26
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
c#2048游戏制作方法


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;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.InteropServices;

namespace Games
{
    public partial class Main : MyForm
    {
        private GameData data = GameData.Instance;
        private Point down_location;
        private bool isCanOperate;
        public Main()
        {
            InitializeComponent();
            data.OnReGood  = data_ReGood;
            data.OnReGrade  = data_ReGrade;
            data.OnChange  = data_OnChange;
            data.OnGameOver  = data_OnGameOver;
        }
        protected override void OnClosed(EventArgs e)
        {
            data.Save();
            base.OnClosed(e);
        }
        void data_OnGameOver()
        {
            new Game_Over(data.Grade, data.Good, () => { }, Shoot, this.Close).ShowDialog(this);
        }

        void data_OnChange()
        {
            using (Graphics g = this.CreateGraphics())
            {
                DrawBlock(g);
            }
        }

        void data_ReGrade()
        {
            using (Graphics g = this.CreateGraphics())
            {
                DrawGrade(g);
            }
        }

        void data_ReGood()
        {
            using (Graphics g = this.CreateGraphics())
            {
                DrawBest(g);
            }
        }
        private void Shoot()
        {
            using (Bitmap image = new Bitmap(this.Width, this.Height))
            {
                this.DrawToBitmap(image, new Rectangle(0, 0, this.Width, this.Height));
                String savePath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)   "/2048截图"   DateTime.Now.Ticks   ".png";
                image.Save(savePath);
                new _2048messageBox("保存成功", "保存在"   savePath).ShowDialog(this);
            }
        }
        /// <summary>
        /// 颜色生成使用原来楼主的颜色。
        /// </summary>
        /// <param name="i">需要获得颜色的方块中的数字</param>
        /// <returns>返回对应的颜色</returns>
        private Color GetBlockColor(int i)
        {
            switch (i)
            {
                case 0:
                    return Color.BurlyWood;
                case 2:
                    return Color.LightSalmon;
                case 4:
                    return Color.Peru;
                case 8:
                    return Color.Chocolate;
                case 16:
                    return Color.Gray;
                case 32:
                    return Color.DarkSeaGreen;
                case 64:
                    return Color.Gold;
                case 128:
                    return Color.HotPink;
                case 256:
                    return Color.DarkOrange;
                case 512:
                    return Color.LightPink;
                case 1024:
                    return Color.DarkRed;
                case 2048:
                    return Color.Red;
                default:
                    return Color.FromArgb(255, 20, 20, 20);
            }
        }
        /// <summary>
        /// 字体也是使用原来楼主的字体。
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        private Font GetWordFont(int i)
        {
            if (i < 100)
            {
                if (i < 10)
                {
                    return new Font("黑体", 40.5f, FontStyle.Bold);
                }
                return new Font("黑体", 35.5f, FontStyle.Bold);
            }
            if (i < 1000)
            {
                return new Font("黑体", 35.5f, FontStyle.Bold);
            }
            return new Font("黑体", 30.5f, FontStyle.Bold);
        }
        /// <summary>
        /// 画最高分。
        /// </summary>
        /// <param name="g"></param>
        private void DrawBest(Graphics g)
        {
            g.FillRectangle(Brushes.DarkGray, 316, 37, 100, 64);
            g.DrawString("BEST", new Font("微软雅黑", 9F, FontStyle.Bold, GraphicsUnit.Point), Brushes.White, 348, 42);
            String good = data.Good.ToString();
            Font drawFont = new Font("微软雅黑", 18F, FontStyle.Bold, GraphicsUnit.Point);
            SizeF drawSize = g.MeasureString(good, drawFont);
            g.DrawString(good, drawFont, Brushes.White, ((100 - (int)drawSize.Width) >> 1)   316, 60);
        }
        /// <summary>
        /// 画当前成绩。
        /// </summary>
        /// <param name="g"></param>
        private void DrawGrade(Graphics g)
        {
            g.FillRectangle(Brushes.DarkGray, 210, 37, 100, 64);
            g.DrawString("SCORE", new Font("微软雅黑", 9F, FontStyle.Bold, GraphicsUnit.Point), Brushes.White, 238, 42);
            String grade = data.Grade.ToString();
            Font drawFont = new Font("微软雅黑", 18F, FontStyle.Bold, GraphicsUnit.Point);
            SizeF drawSize = g.MeasureString(grade, drawFont);
            g.DrawString(grade, drawFont, Brushes.White, ((100 - (int)drawSize.Width) >> 1)   210, 60);
        }
        /// <summary>
        /// 画方块和数字。
        /// </summary>
        /// <param name="g"></param>
        private void DrawBlock(Graphics g)
        {
            for (int i = 0; i < data.Length; i  )
            {
                int x = (i - ((i >> 2) << 2)) * 100   26;
                int y = (i >> 2) * 100   153;
                g.FillRectangle(new SolidBrush(GetBlockColor(data[i])), x, y, 90, 90);
                if (data[i] != 0)
                {
                    Font drawFont = GetWordFont(data[i]);
                    string drawString = data[i].ToString();
                    SizeF drawSize = g.MeasureString(drawString, drawFont);
                    g.DrawString(drawString, drawFont, Brushes.White, ((90 - (int)drawSize.Width) / 2)   x, ((90 - (int)drawSize.Height) / 2)   y);
                }
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.DrawString("2048", new System.Drawing.Font("微软雅黑", 48F, FontStyle.Bold, GraphicsUnit.Point), Brushes.Gray, 15, 25);
            e.Graphics.DrawString("按F1打开帮助", new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, 315, 115);
            DrawGrade(e.Graphics);
            DrawBest(e.Graphics);
            DrawBlock(e.Graphics);
            base.OnPaint(e);
        }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    if (e.Shift)
                    {
                        this.Opacity  = 0.1;
                    }
                    else
                    {
                        data.Up();
                    }
                    break;
                case Keys.Down:
                    if (e.Shift)
                    {
                        if (this.Opacity > 0.2)
                        {
                            this.Opacity -= 0.1;
                        }
                    }
                    else
                    {
                        data.Down();
                    }
                    break;
                case Keys.Left:
                    data.Left();
                    break;
                case Keys.Right:
                    data.Right();
                    break;
                case Keys.F4:
                    this.TopMost = !this.TopMost;
                    break;
                case Keys.F5:
                    data.Reset();
                    break;
                case Keys.F6:
                    this.Shoot();
                    break;
                case Keys.F1:
                    new _2048messageBox("提示", "F4:窗口总在最前\r\nF5:重新开始\r\nF6:截图并保存\r\nShife   ↑↓:调整透明度\r\n↑↓←→:控制方块移动").ShowDialog(this);
                    break;
            }
            base.OnKeyDown(e);
        }
        /// <summary>
        /// 重写鼠标点击事件的触发方法,相当于鼠标点击事件。
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            // 判断点击的位置是不是在允许手势的位置。
            if (e.Y > 150)
            {
                isCanOperate = true;
                down_location = e.Location;
            }
            base.OnMouseDown(e);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            // 如果能操作手势。
            if (isCanOperate)
            {
                isCanOperate = false;
                int x = down_location.X - e.X;
                int y = down_location.Y - e.Y;
                // 获得点击到释放的最大位置动向如果动向大于50那么相应手势。
                if (x < -50 && x < (y < 0 ? y : -y))
                {
                    data.Right();
                }
                else if (x > 50 && x > (y > 0 ? y : -y))
                {
                    data.Left();
                }
                else if (y < -50 && y < (x < 0 ? x : -x))
                {
                    data.Down();
                }
                else if (y > 50 && y > (x > 0 ? x : -x))
                {
                    data.Up();
                }
            }
            base.OnMouseUp(e);
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            DialogResult res = MessageBox.Show("离开游戏?", "提示",
                               MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (res != DialogResult.OK)
            {
                e.Cancel = true;
            }
            base.OnClosing(e);
        }
    }
}