C# 制作类似魔塔小游戏(菜鸟版)

        设计一个类似魔塔的小游戏游戏,一个 15*15 的地图,和一个显示区域。 地图上的每个格子:能通行或不能通行(有障碍或宝物,宝物被拾取后可通行) 有一个人物,可上下左右通行。 通行不同的区域,会使人物的属性产生变化,如:生命、魔力、金钱等。这些基本属性需要 在显示区域中显示出来。

       菜鸟成品效果预览:

C# 制作类似魔塔小游戏(菜鸟版)_第1张图片

C# 制作类似魔塔小游戏(菜鸟版)_第2张图片

C# 制作类似魔塔小游戏(菜鸟版)_第3张图片

        因为接触c#不久,所以做的有点菜鸟。
        先制作一个这样的窗口。左边用于放置地图,右边用于显示变化数据。
  C# 制作类似魔塔小游戏(菜鸟版)_第4张图片
                int[,] a = new int[15, 15];//15*15的地图
        int HP, MP, Gold, Level;
        int win = 0;
        int lose = 0;
        int bug = 0;
        int onex = 0, oney = 0;       //主角的各项数据
        public class monster
        {
            public int hp, mp, gold, level;
            public string name;

        }                         //设置怪兽的struct
        Bitmap bmp0 = new Bitmap("墙.jpg");
        Bitmap bmp1 = new Bitmap("主角.jpg");
        Bitmap bmp2 = new Bitmap("路.jpg");
        Bitmap bmp3 = new Bitmap("怪3.jpg");
        Bitmap bmp4 = new Bitmap("怪4.jpg");
        Bitmap bmp5 = new Bitmap("大魔王0.jpg");
        Bitmap bmp6 = new Bitmap("血瓶.jpg");
        Bitmap bmp7 = new Bitmap("蓝瓶.jpg");
        Bitmap bmp8 = new Bitmap("金币.jpg");
        Bitmap bmp9 = new Bitmap("公主.jpg");//加载图片
 
        monster[] gg = new monster[20];
        然后设置一个start()函数进行初始化。
       在start()函数中给地图各个坐标赋值,如将墙赋值为0;怪物赋值为3,4,5等。
       这个过程比较麻烦,作者自己就打了好久。。  

       然后在start()里面加上以下内容,使得主角的属性能够出现在右边的textbox里面。
            textBox1.Text = HP.ToString();
            textBox2.Text = MP.ToString();
            textBox3.Text = Gold.ToString();
            textBox4.Text = Level.ToString();
            textBox4.Text = (Level / 100 + 1).ToString() + "(" + (Level% 100).ToString() + "%)";//经验条。
       然后写一个怪物属性初始化函数
              void startgg()
        {
            int i;
            for (i = 0; i < 20; i++)
                gg[i] = new monster();
 
            gg[3].hp = 80; gg[3].mp = 0; gg[3].gold = 15; gg[3].level = 10; gg[3].name = "小恶魔";
            gg[4].hp = 400; gg[4].mp = 0; gg[4].gold = 100; gg[4].level = 20; gg[4].name = "幽灵";
            gg[5].hp = 1500; gg[5].mp = 0; gg[5].gold = 1000; gg[5].level = 40; gg[5].name = "大魔王";

        }//初始化怪怪

        画笔函数:
                private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;//画板
            int i;
            int j;
            Rectangle x = new Rectangle(580, 360, 200, 200);//右下角的事件框
            g.DrawRectangle(Pens.Black, x);
            Rectangle zz = new Rectangle(582, 363, 197, 197);
            g.FillRectangle(Brushes.White, zz);

           for (i = 0; i < 15; i++)
               for (j = 0; j < 15; j++)
                {
                    Rectangle f = new Rectangle(35 * i + 10, 35 * j + 10, 35, 35);
                    switch (a[i, j])
                    {
                        case 0:
                            {
                                Rectangle sr = new Rectangle(0, 0, 140, 133);//要截取的矩形区域
                                g.DrawImage(bmp0, f, sr, GraphicsUnit.Pixel);//图像截取,f为显示到foam的区域
                            }
                            break;
                            
                        case 1:
                            {
                                Rectangle sr = new Rectangle(0, 0, 350, 447);//要截取的矩形区域
                                g.DrawImage(bmp1, f, sr, GraphicsUnit.Pixel);//图像截取,f为显示到foam的区域
                            }
                            break;
       。。。。。。。。
      讲地图的各个部分以swich判断画出来。
     然后在public Form1中调用两个初始化函数和画笔函数
     public Form1()
        {
            InitializeComponent();
            start();
            startgg();
            this.Paint += new PaintEventHandler(Form1_Paint);
            MessageBox.Show("勇士!请你打败大魔王救出公主!");


        }
    运行即可看到初步的地图~
C# 制作类似魔塔小游戏(菜鸟版)_第5张图片
写一个MyWhite()以画出右下角的最近时间框
           private void MyWrite(string temp, object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Rectangle zz = new Rectangle(580, 360, 200, 200);
            g.FillRectangle(Brushes.White, zz);
            FontFamily ziti = new FontFamily("Arial");
            StringFormat zibuju = new StringFormat();
            zibuju.Alignment = StringAlignment.Center;
            Font zi = new Font(ziti, 9, FontStyle.Bold);
            g.DrawString(temp, zi, Brushes.Red, 590, 380);

        }
    然后是判断主角走到的位置后的操作
如:
      private void juge(int x, int y, object sender, EventArgs e)
        {
            if (a[x, y] == 6) { HP += 300; string s = "获得血瓶,HP+300!"; MyWrite(s, sender, e); }
            else if (a[x, y] == 7) { MP += 100; string s = "获得蓝瓶,MP+100!"; MyWrite(s, sender, e); }
            else if (a[x, y] == 8) { Gold += 300; string s = "获得金币,GOLD+300!"; MyWrite(s, sender, e); }
            else if (a[x, y] == 3 || a[x, y] == 4 || a[x, y] == 5)
            {
                textBox5.Text = gg[a[x,y]].hp.ToString();
                textBox6.Text = gg[a[x, y]].mp.ToString();
                textBox7.Text = gg[a[x, y]].level.ToString();
                int last = Level;
                if (gg[a[x, y]].hp > HP)
                {
                    lose= 1;
                    string s = "打不过" + gg[a[x, y]].name + "!"; MyWrite(s, sender, e);
                    System.Windows.Forms.Form f2 = new Form3();
                    f2.Show();
                    return;
                }
。。。。。。。。。
以及向个方向行走的函数
       //*****向右走
        private void button5_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            if (onex != 14)//判断是不是越界
            {
                if (a[onex + 1, oney] != 0)//判断是不是墙
                {

                    onex++;
                    juge(onex, oney, sender, e);
                    if (lose == 1)
                    {
                        onex--;
                        lose = 0;
                        return;
                    }
                    a[onex - 1, oney] = 2;
                    a[onex, oney] = 2;
                    Rectangle f = new Rectangle(35 * onex + 10, 35 * oney + 10, 35, 35);
                    Rectangle sr = new Rectangle(0, 0, 350, 447);//要截取的矩形区域
                    g.DrawImage(bmp1, f, sr, GraphicsUnit.Pixel);//图像截取,f为显示到foam的区域
                    f = new Rectangle(35 * (onex - 1) + 10, 35 * oney + 10, 35, 35);
                    sr = new Rectangle(0, 0, 300, 300);//要截取的矩形区域
                    g.DrawImage(bmp2, f, sr, GraphicsUnit.Pixel);//图像截取,f为显示到foam的区域
                }
            }
        }
        最后添加键盘映射
       private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {


            if (e.KeyChar == 's')
            {
                button4_Click(sender, e);
            }
            if (e.KeyChar == 'd')
            {
                button5_Click(sender, e);
            }
            if (e.KeyChar == 'a')
            {
                left_Click(sender, e);
            }
            if (e.KeyChar == 'w')
            {
                up_Click(sender, e);
            }
        }

如果你想要多张地图的话,也可以创建多个窗口,然后以传送门调用
           else if (a[x, y] == 9)
            {
                System.Windows.Forms.Form f1 = new Form2();
                f1.Show();
 
            
            }
这样的形式来显示第二张地图,或者
C# 制作类似魔塔小游戏(菜鸟版)_第6张图片
这样的弹窗。
然后你就可以体验自己的菜鸟版魔塔小游戏啦~你还可以更具自己的兴趣添加商店等更多功能

你可能感兴趣的:(个人)