基于C#弹幕类射击游戏的实现——(十)整合

先看实现的效果


剩下部分代码,首先是入口,MainForm

public partial class MainForm : Form  
    {  
        public MainForm()  
        {  
            //  
            // The InitializeComponent() call is required for Windows Forms designer support.  
            //  
            InitializeComponent();  
              
            //  
            // TODO: Add constructor code after the InitializeComponent() call.  
            //  
            this.ClientSize = new Size(Config.ScreenWidth, Config.ScreenHeight);  
            this.StartPosition = FormStartPosition.CenterScreen;  
              
            this.pictureBox1.Location = new Point(0, 0);  
            this.pictureBox1.ClientSize = new Size(Config.ScreenWidth, Config.ScreenHeight);  
            this.DoubleBuffered = true;  
        }  
        private Game.GameScene scene;  
        private void RenderOver(object sender)  
        {  
            this.pictureBox1.Image = scene.Surface;  
              
            this.Text = "FPS:" + scene.FPS.ToString();  
        }  
          
        void MainFormLoad(object sender, EventArgs e)  
        {  
            Resources.Load("Bullets.png");  
            Resources.Load("Background.bmp");  
            Resources.Load("Res.bmp");  
            Resources.Load("BulletsAll.png");  
            Resources.Load("Player.png");  
            Resources.Load("Bomb.png");  
            Resources.Load("Enemy.png");  
            Resources.Load("Boss1.png");  
            Resources.Load("PlayerPoint.png");  
            Resources.Load("Hp.png");  
              
            for ( int i = 0; i < Config.BulletTypeCount; i++ )  
            {  
                Resources.Load("Bullet" + i.ToString() + ".png");  
            }  
              
            Data.BulletSource = Resources.Get("Bullets");  
            Data.BackgroundSource = Resources.Get("Background");  
            Data.BulletAllSource = Resources.Get("BulletsAll");  
            Data.PlayerSource = Resources.Get("Player");  
            Data.PlayerPointSource =Resources.Get("PlayerPoint");  
            Data.BombSource = Resources.Get("Bomb");  
            Data.EnemySource = Resources.Get("Enemy");  
            Data.Boss1Source = Resources.Get("Boss1");  
            Data.HpSource = Resources.Get("Hp");  
              
            Data.BulletsSource = new Bitmap[Config.BulletTypeCount];  
            for ( int i = 0; i < Config.BulletTypeCount; i++ )  
            {  
                Data.BulletsSource[i] = Resources.Get("Bullet" + i.ToString());  
            }  
              
              
            Data.BulletAllSource.MakeTransparent(Color.FromArgb(255, 0, 255));  
            Data.PlayerSource.MakeTransparent(Color.FromArgb(255, 0, 255));  
            Data.BombSource.MakeTransparent(Color.FromArgb(255, 0, 255));  
            Data.EnemySource.MakeTransparent(Color.FromArgb(255, 0, 255));  
            Data.Boss1Source.MakeTransparent(Color.FromArgb(255, 0, 255));  
              
            scene = new STG.Game.GameScene();  
              
            scene.RenderOver += RenderOver;  
        }  
          
        void MainFormKeyDown(object sender, KeyEventArgs e)  
        {  
            Input.KeyDown(e.KeyCode);  
        }  
          
        void MainFormKeyUp(object sender, KeyEventArgs e)  
        {  
            Input.KeyUp(e.KeyCode);  
        }  
    }

这里面用到了Resource和Data。
Resource用来管理资源,其实最开始打算是这样的,后来。。。没写,就成下面这样了

public static class Resources  
    {  
        private static Hashtable mRes;  
          
        static Resources()  
        {  
            mRes = new Hashtable();  
        }  
          
        public static void Load(string path)  
        {  
            Bitmap res = new Bitmap(System.Windows.Forms.Application.StartupPath + @"\Resources\" + path);  
              
            if ( res == null )  
            {  
                Log.Error("Can't load : " + System.Windows.Forms.Application.StartupPath + @"\Resources\" + path);  
                return;  
            }  
              
            string name = string.Empty;  
              
            int start = path.LastIndexOf('\\');  
            int end = path.LastIndexOf('.');  
              
            if ( start == -1 )  
            {  
                start = 0;  
            }  
              
            name = path.Substring(start, end);  
              
            mRes.Add(name, res);  
        }  
          
        public static Bitmap Get(string name)  
        {  
            return mRes[name] as Bitmap;  
        }  
    }

完全是多此一举的感觉啊。。哈哈
然后是Data,保存一些大家共用的图片啊,数据啊

public static class Data  
    {  
        public static Bitmap BulletSource = null;  
        public static Bitmap BackgroundSource = null;  
        public static Bitmap BulletAllSource = null;  
        public static Bitmap PlayerSource = null;  
        public static Bitmap PlayerPointSource = null;  
        public static Bitmap HpSource = null;  
        public static Bitmap BombSource = null;  
        public static Bitmap EnemySource = null;  
          
        public static Bitmap Boss1Source = null;  
          
        public static Bitmap [] BulletsSource = null;  
          
        public static Font NormalFont = new Font("Arial", 12);  
          
        public static Random Rnd = new Random(DateTime.Now.Millisecond);  
          
        /// <summary>  
        /// 2 * PI  
        /// </summary>  
        public static float TwoPI = (float)Math.PI * 2.0f;  
        /// <summary>  
        /// 弧度单位  
        /// </summary>  
        public static float Radian = (float)Math.PI / 180.0f;  
          
        /// <summary>  
        /// 不同类型的敌人位图在图库中的位置  
        /// </summary>  
        public static Rectangle[] EnemySourceRectangle = new Rectangle[]  
        {  
            new Rectangle(0, 0, 34, 31), // (1, 1) Enemy 第1排  第1个  
            new Rectangle(35, 0, 31, 33), // (1, 2)  
            new Rectangle(0, 33, 33, 27), // (2, 1)  
            new Rectangle(67, 0, 36, 32), // (1, 3)  
            new Rectangle(104, 0, 30, 36), // (1, 4)  
            new Rectangle(133, 54, 38, 22), // (2, 4)  
            new Rectangle(39, 34, 42, 39), // (2, 2)  
            new Rectangle(82, 39, 48, 34), // (2, 3)  
            new Rectangle(135, 0, 64, 53) // (1, 5)  
        };  
          
        /// <summary>  
        /// 不同类型的敌人的碰撞盒子(X,Y相对于位图中心点的便宜,W,H大小)  
        /// </summary>  
        public static Rectangle[] EnemyBoxColliderRectangle = new Rectangle[]  
        {  
            new Rectangle(0, 0, 34, 31), // (1, 1) Enemy 第1排  第1个  
            new Rectangle(0, 0, 31, 33), // (1, 2)  
            new Rectangle(0, 0, 33, 27), // (2, 1)  
            new Rectangle(0, 0, 36, 32), // (1, 3)  
            new Rectangle(0, 0, 30, 36), // (1, 4)  
            new Rectangle(0, 0, 38, 22), // (2, 4)  
            new Rectangle(0, 0, 23, 39), // (2, 2)  
            new Rectangle(0, -5, 48, 24), // (2, 3)  
            new Rectangle(0, -10, 64, 37) // (1, 5)  
        };  
          
        /// <summary>  
        /// 不同子弹类型的大小  
        /// </summary>  
        public static Size[] BulletsSize = new Size[]  
        {  
            new Size(10, 10),  
            new Size(16, 14),  
            new Size(18, 18),  
            new Size(18, 18),  
            new Size(9, 17),  
            new Size(9, 17),  
            new Size(7, 44)  
        };  
          
        public enum MouseButton  
        {  
            Left,  
            Right,  
            Middle  
        }  
  
        /// <summary>  
        /// 方向  
        /// </summary>  
        public enum Direction  
        {  
            None,  
            Up,  
            Down,  
            Left,  
            Right,  
            LeftUp,  
            LeftDown,  
            RightUp,  
            RightDown  
        }  
    }  
      
    /// <summary>  
    /// Bottom : 0  
    /// Background : 10  
    /// Floor : 20  
    /// Map : 30  
    /// Object : 40  
    /// Effect : 50  
    /// Sky : 80  
    /// UI : 90  
    /// Top : 100  
    /// </summary>  
    public static class DepthLayer  
    {  
        public const int Bottom = 0;  
          
        public const int Background = 10; // 背景层  
        public const int Map = 20; // 地表层  
        public const int Bullet = 30; // 子弹层  
        public const int Object = 40; // 单位层  
        public const int Effect = 50; // 特效层  
        public const int UI = 90; // UI层  
          
        public const int Top = 100;  
    }

然后是播放声音,网上找的一个类

public class GameSound  
    {  
        protected const int SND_SYNC  = 0x0;  
        protected const int SND_ASYNC  = 0x1;  
        protected const int SND_NODEFAULT  = 0x2;  
        protected const int SND_MEMORY  = 0x4;  
        protected const int SND_LOOP  = 0x8;  
        protected const int SND_NOSTOP  = 0x10;  
        protected const int SND_NOWAIT  = 0x2000;  
        protected const int SND_ALIAS  = 0x10000;  
        protected const int SND_ALIAS_ID  = 0x110000;  
        protected const int SND_FILENAME  = 0x20000;  
        protected const int SND_RESOURCE  = 0x40004;  
        protected const int SND_PURGE  = 0x40;  
        protected const int SND_APPLICATION  = 0x80;  
   
        [DllImport("Winmm.dll", CharSet=CharSet.Auto)]  
        protected extern static bool PlaySound(string strFile, IntPtr hMod, int flag );  
   
        //播放声音函数  
        //strSoundFile   --- 声音文件  
        //bSynch         --- 是否同步,如果为True,则播放声音完毕再执行后面的操作,为False,则播放声音的同时继续执行后面的操作  
        public static bool PlaySoundFile(string strSoundFile, bool bSynch)  
        {  
            if(!System.IO.File.Exists(strSoundFile))  
                return false;  
            int Flags;  
            if(bSynch)  
                Flags = SND_FILENAME | SND_SYNC;  
            else  
                Flags = SND_FILENAME | SND_ASYNC;  
   
            return PlaySound(strSoundFile, IntPtr.Zero, Flags);  
        }

剩下的一些Log啊,GPoint啊~~都是无关紧要的,自己实现或者不要都行
整个游戏就这么完成了。勉强算是半成品
对了,差点忘了,载入地图资源的函数没给出

private void LoadMapInfo(string path)  
        {  
            try  
            {  
                StreamReader fp = new StreamReader(path, Encoding.Default);  
                int timeIndex = 0;  
                  
                while ( fp.EndOfStream == false )  
                {  
                    string line = fp.ReadLine();  
                      
                    if ( line.StartsWith("[") && line.EndsWith("]") )  
                    {  
                        timeIndex = int.Parse(line.Substring(1, line.Length - 2));  
                        continue;  
                    }  
                      
                    if ( timeIndex >= Config.OneMapTime )  
                    {  
                        Log.Error("索引超出地图最大限制");  
                        fp.Close();  
                        return;  
                    }  
                      
                    mMapInfo[timeIndex].Add(line);  
                }  
                  
                fp.Close();  
            }  
            catch(Exception ce)  
            {  
                Log.Error(ce.Message);  
            }  
        }  
          
        private void DealMapInfo(int index)  
        {  
            if ( index < 0 || index >= Config.OneMapTime )  
            {  
                return;  
            }  
              
            if ( mMapInfo[index].Count == 0 )  
            {  
                return;  
            }  
              
            foreach ( string line in mMapInfo[index] )  
            {  
                string [] param = line.Split(',');  
                Vector2 pos = new Vector2(int.Parse(param[1]), int.Parse(param[2]));  
                  
                if ( pos.X == -1 )  
                {  
                    pos.X = Config.ScreenWidth / 2;  
                }  
                if ( pos.Y == -1 )  
                {  
                    pos.Y = Config.ScreenHeight / 2;  
                }  
                  
                switch ( param[0] )  
                {  
                    case "Enemy1":  
                        AddEnemy(new Enemy1(pos));  
                        break;  
                    case "Enemy2":  
                        AddEnemy(new Enemy2(pos));  
                        break;  
                    case "Enemy3":  
                        AddEnemy(new Enemy3(pos));  
                        break;  
                    case "Enemy4":  
                        AddEnemy(new Enemy4(pos));  
                        break;  
                    case "Enemy5":  
                        AddEnemy(new Enemy5(pos));  
                        break;  
                    case "Enemy6":  
                        AddEnemy(new Enemy6(pos));  
                        break;  
                    case "Enemy7":  
                        AddEnemy(new Enemy7(pos));  
                        break;  
                    case "Enemy8":  
                        AddEnemy(new Enemy8(pos));  
                        break;  
                    case "Enemy9":  
                        AddEnemy(new Enemy9(pos));  
                        break;  
                    case "Boss1":  
                        AddBoss(new Boss1(mBarrage, pos));  
                        break;  
                    default:  
                        break;  
                }  
            }  
        }  
          
        public bool InScreen(Box box)  
        {  
            if ( box.Left > Config.ScreenWidth )  
            {  
                return false;  
            }  
            if ( box.Right < 0 )  
            {  
                return false;  
            }  
            if ( box.Top > Config.ScreenHeight )  
            {  
                return false;  
            }  
            if ( box.Bottom < 0 )  
            {  
                return false;  
            }  
            return true;  
        } 

这一部分用于从文件中载入配置,动态的生成敌人
好了,半成品完成了~~貌似我没讲什么啊。。。哎,不太会写博客啊,下面一个游戏学着多讲一些吧

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容