⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 creator.cs

📁 《3d游戏编程入门经典》中范例小游戏。使用C#+DirectX开发。
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Configuration;
using System.IO;

namespace LevelCreator
{
    /// <summary>
    /// Color possibilities of the blocks
    /// </summary>
    public enum BlockColor : byte
    {
        Red = 0,
        Blue = 1,
        Green = 2,
        Yellow = 3,
        SandyBrown = 4,
        WhiteSmoke = 5,
        DarkGoldenRod = 6,
        CornflowerBlue = 7,
        Pink = 8
    }

    /// <summary>
	/// Summary description for Form1.
	/// </summary>
	public unsafe class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>

        private static readonly Color[] BoxColors = {Color.Black, Color.Red , Color.Blue, Color.Green, 
                                                        Color.Yellow, Color.SandyBrown, Color.WhiteSmoke,
                                                        Color.DarkGoldenrod, Color.CornflowerBlue, Color.Pink
                                                    };

        
        private System.ComponentModel.Container components = null;
        private static readonly string MediaPath = ConfigurationSettings.AppSettings.Get("MediaPath");
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.CheckBox chkWrap;

        private System.Windows.Forms.Button[] levelButtons;
        private byte[] levelIndices;
        private System.Windows.Forms.Button[] mainButtons;
        private int[] mainColors;

        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.TextBox txtTime;
        private System.Windows.Forms.TextBox txtMoves;
        private System.Windows.Forms.NumericUpDown levelNumber;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.Button btnSave;

        int playerIndex = 0;

        private const int SquareSize = 13;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

            // Create the main buttons
            mainButtons = new Button[4];
            mainColors = new int[4];

            for (int i = 0; i < 4; i++)
            {
                mainButtons[i] = new Button();
                mainButtons[i].Tag = i; 
                mainButtons[i].BackColor = System.Drawing.Color.Black;
                mainButtons[i].Location = new System.Drawing.Point(16 + (72 * i), 40);
                mainButtons[i].Size = new System.Drawing.Size(24, 24);
                mainButtons[i].TabStop = false;
                mainButtons[i].Click +=new EventHandler(OnMainButtonClick);
                mainColors[i] = 0;

                this.Controls.Add(mainButtons[i]);
            }

        
            levelButtons = new Button[SquareSize * SquareSize];
            levelIndices = new byte[levelButtons.Length];

            int index = 0;
            this.SuspendLayout();
            Font f = new Font(this.Font.Name, 10.0f);
            for (int j = 0; j < SquareSize; j++)
            {
                for (int i = 0; i < SquareSize; i++)
                {
                    levelButtons[index] = new Button();
                    levelButtons[index].Tag = index; 
                    levelButtons[index].Font = f; 
                    levelButtons[index].BackColor = System.Drawing.Color.Black;
                    levelButtons[index].Location = new System.Drawing.Point(2 + (22 * i), 80 + (22 * j));
                    levelButtons[index].Size = new System.Drawing.Size(20, 20);
                    levelButtons[index].TabStop = false;
                    levelButtons[index].Click +=new EventHandler(OnLevelButtonClick);
                    levelIndices[index] = 0;

                    this.Controls.Add(levelButtons[index]);
                    index++;
                }
            }
            this.ResumeLayout(true);
        }

        private void UpdateBoxColors()
        {
            for(int i = 0; i < levelButtons.Length; i++)
            {
                if (levelIndices[i] < 4)
                {
                    levelButtons[i].BackColor = mainButtons[levelIndices[i]].BackColor;
                }
                else
                {
                    levelButtons[i].BackColor = System.Drawing.Color.Black;
                }
                if (playerIndex == i)
                {
                    if (levelButtons[i].BackColor == System.Drawing.Color.Black)
                    {
                        playerIndex++;
                    }
                    else
                    {
                        levelButtons[i].Text = "*";
                    }
                }
                else
                {
                    levelButtons[i].Text = string.Empty;
                }
            }
        }
        private void OnSaveLevel(object sender, System.EventArgs e)
        {

            FileStream stm = File.Create(MediaPath + string.Format("Level{0}.lvl", (int)levelNumber.Value));

            float maxTime = float.Parse(txtTime.Text);
            int maxMoves = int.Parse(txtMoves.Text);

            stm.Write(BitConverter.GetBytes(maxTime), 0, sizeof(float));
            stm.Write(BitConverter.GetBytes(maxMoves), 0, sizeof(int));

            int numColors = 0;
            for (int i = 0; i < 4; i++)
            {
                if (mainColors[i] > 0)
                    numColors++;
                else
                    break;
            }
            if (numColors < 2)
                throw new InvalidOperationException("You need more colors defined.");

            BlockColor[] colors = new BlockColor[numColors];

            int numberColors = colors.Length;
            stm.Write(BitConverter.GetBytes(numberColors), 0, sizeof(int));

            for (int i = 0; i < colors.Length; i++)
            {
                colors[i] = (BlockColor)(mainColors[i] - 1);
                stm.WriteByte((byte)colors[i]);
            }


            BlockColor final = colors[colors.Length - 1];

            stm.WriteByte((byte)final);

            stm.Write(BitConverter.GetBytes(chkWrap.Checked), 0, sizeof(bool));

            int blockIndex = 0;
            for (int j = 0; j < SquareSize; j++)
            {
                for (int i = 0; i < SquareSize; i++)
                {
                    if (levelIndices[blockIndex] < numColors)
                    {
                        stm.WriteByte(levelIndices[blockIndex]);
                    }
                    else
                    {
                        stm.WriteByte((byte)0xff);
                    }
                    blockIndex++;
                }
            }

            stm.Write(BitConverter.GetBytes(playerIndex), 0, 4);

            stm.Close();
        }
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -