项目01【回合制06游戏】Visual C#

 

双击Program.cs

修改这里

修改为

拖拽PictureBox到对话控制框里

拉伸

 

将以下代码放在Form_Obj.cs中

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace vs_Timerbutton
{
    public partial class Form_Obj : Form
    {
        Player[] player = new Player[3];
        //地图数组
        Map[] map = new Map[2];
        //多媒体播放器
        WMPLib.WindowsMediaPlayer music_player = new WMPLib.WindowsMediaPlayer();
        Npc[] npc = new Npc[3];


        //计数器
        public int animation_ctrl = 0;

        public Form_Obj()
        {
            InitializeComponent();
        }

        private void Form_Obj_KeyDown(object sender, KeyEventArgs e)
        {
            Player.key_ctrl(player, map, npc, e);
            Map.drawMap(map, player, npc, pictureBox1.CreateGraphics(),
                new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));


        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            for (int i = 0; i < npc.Length; i++)
            {
                if (npc[i] == null)
                {
                    continue;
                }
                if (npc[i].map != Map.current_map)
                {
                    continue;
                }
                npc[i].timer_logic(map);
            }
            Map.drawMap(map, player, npc, pictureBox1.CreateGraphics(),
              new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
        }


        private void Form_Obj_Load(object sender, EventArgs e)
        {
            player[0] = new Player();
            player[0].bitmap = new Bitmap(@"r1.png");
            player[0].bitmap.SetResolution(100, 100);
            player[0].is_active = 1;

            player[1] = new Player();
            player[1].bitmap = new Bitmap(@"r2.png");
            player[1].bitmap.SetResolution(100, 100);
            player[1].is_active = 1;

            player[2] = new Player();
            player[2].bitmap = new Bitmap(@"r3.png");
            player[2].bitmap.SetResolution(100, 100);
            player[2].is_active = 1;


            map[0] = new Map();
            map[0].bitmap_path = "map1.png";
            map[0].shade_path = "map1_shade.png";
            map[0].block_path = "map1_block.png";
            map[0].back_path = "map1_back.png";
            map[0].music = "3.mp3";

            map[1] = new Map();
            map[1].bitmap_path = "map2.png";
            map[1].shade_path = "map2_shade.png";
            map[1].block_path = "map2_block.png";
            map[1].back_path = "map1_back.png";
            map[1].music = "1.mp3";

            npc[0] = new Npc();
            npc[0].map = 0;
            npc[0].x = 150;
            npc[0].y = 600;
            npc[0].bitmap_path = "npc2.png";

            npc[1] = new Npc();
            npc[1].map = 0;
            npc[1].x = 150;
            npc[1].y = 700;
            npc[1].bitmap_path = "npc3.png";


            npc[2] = new Npc();
            npc[2].map = 0;
            npc[2].x = 170;
            npc[2].y = 350;
            npc[2].bitmap_path = "npc4.png";
            npc[2].Collision_type = Npc.Collosion_type.KEY;
            npc[2].npc_type = Npc.Npc_type.CHARACTER;
            npc[2].idle_walk_direction = Comm.Direction.LEFT;
            npc[2].idle_walk_time = 20;


            Map.change_map(map, player, npc, 0, 100, 450, 1, music_player);

        }

        private void Form_Obj_KeyUp(object sender, KeyEventArgs e)
        {
            Player.key_ctrl_up(player, e);
            Map.drawMap(map, player, npc, pictureBox1.CreateGraphics(),
                   new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
        }
    }
}

将以下代码放在Player.cs中

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace vs_Timerbutton
{
    class Player
    {
        public int x = 0;
        public int y = 0;
        public int face = 1;
        public Bitmap bitmap;

        //当前角色
        public static int current_player = 0;
        //是否在队伍中
        public int is_active = 0;

        public int x_offset = -120;
        public int y_offset = -220;

        public int anm_frame = 0;
        public long last_walk_time = 0;
        public long walk_interval = 100;
        public int speed = 20;

        //碰撞检测线距离
        public int collision_ray = 50;


        public Player()
        {
            bitmap = new Bitmap(@"r1.png");
            bitmap.SetResolution(100, 100);
        }

        public static int get_pos_x(Player[] player)
        {
            return player[current_player].x;
        }

        public static int get_pos_y(Player[] player)
        {
            return player[current_player].y;
        }

        public static int get_pos_f(Player[] player)
        {
            return player[current_player].face;
        }


        public static void key_change_player(Player[] player)
        {
            for (int i = current_player + 1; i < player.Length; i++)
            {
                if (player[i].is_active == 1)
                {
                    set_player(player, current_player, i);
                    return;
                }
            }
            for (int i = 0; i < current_player; i++)
            {
                if (player[i].is_active == 1)
                {
                    set_player(player, current_player, i);
                    return;
                }
            }

        }

        public static void set_player(Player[] player, int oldindex, int newindex)
        {
            current_player = newindex;
            player[newindex].x = player[oldindex].x;
            player[newindex].y = player[oldindex].y;
            player[newindex].face = player[oldindex].face;
        }


        //键盘控制
        public static void key_ctrl(Player[] player, Map[] map, Npc[] npc, KeyEventArgs e)
        {
            Player p = player[current_player];
            if (e.KeyCode == Keys.Tab)
            {
                key_change_player(player);
            }

            //通过键位的判断,实现角色的位移
            switch (e.KeyCode)
            {
                case Keys.Left://左
                    walk(player, map, Comm.Direction.LEFT);
                    break;
                case Keys.Up://上
                    walk(player, map, Comm.Direction.UP);
                    break;
                case Keys.Right://右
                    walk(player, map, Comm.Direction.RIGHT);
                    break;
                case Keys.Down://下
                    walk(player, map, Comm.Direction.DOWN);
                    break;
                default:
                    break;
            }
            //npc碰撞
            npc_collision(player, map, npc, e);
            /*
                        p.anm_frame = p.anm_frame + 1;
                        if (p.anm_frame >= int.MaxValue)
                        {
                            p.anm_frame = 0;
                        }
                        p.last_walk_time = Comm.Time();*/


        }

        public static void key_ctrl_up(Player[] player, KeyEventArgs e)
        {
            Player p = player[current_player];
            p.anm_frame = 0;
            p.last_walk_time = 0;
        }

        //行走方法
        public static void walk(Player[] player, Map[] map, Comm.Direction direction)
        {
            Player p = player[current_player];
            //方向
            p.face = (int)direction;
            //间隔判定
            if (Comm.Time() - p.last_walk_time <= p.walk_interval)
            {
                return;
            }
            //处理上下左右四个方向的移动
            //人物向上移动
            if (direction == Comm.Direction.UP && Map.can_through(map, p.x, p.y - p.speed))
            {
                p.y = p.y - p.speed;
            }
            //向下移动
            else if (direction == Comm.Direction.DOWN && Map.can_through(map, p.x, p.y + p.speed))
            {
                p.y = p.y + p.speed;
            }
            //向右移动
            else if (direction == Comm.Direction.RIGHT && Map.can_through(map, p.x + p.speed, p.y))
            {
                p.x = p.x + p.speed;
            }
            //向左移动
            else if (direction == Comm.Direction.LEFT && Map.can_through(map, p.x - p.speed, p.y))
            {
                p.x = p.x - p.speed;
            }

            //动画帧
            p.anm_frame = p.anm_frame + 1;
            if (p.anm_frame >= int.MaxValue)
            {
                p.anm_frame = 0;

            }
            p.last_walk_time = Comm.Time();

        }


        public static void set_pos(Player[] player, int x, int y, int face)
        {
            player[current_player].x = x;
            player[current_player].y = y;
            player[current_player].face = face;
        }


        public static Point get_collision_point(Player[] player)
        {
            Player p = player[current_player];
            int collision_x = 0;
            int collision_y = 0;

            if (p.face == (int)Comm.Direction.UP)
            {
                collision_x = p.x;
                collision_y = p.y - p.collision_ray;
            }

            if (p.face == (int)Comm.Direction.DOWN)
            {
                collision_x = p.x;
                collision_y = p.y + p.collision_ray;
            }

            if (p.face == (int)Comm.Direction.LEFT)
            {
                collision_x = p.x - p.collision_ray;
                collision_y = p.y;
            }

            if (p.face == (int)Comm.Direction.RIGHT)
            {
                collision_x = p.x + p.collision_ray;
                collision_y = p.y;
            }

            return new Point(collision_x, collision_y);
        }


        public static void npc_collision(Player[] player, Map[] map, Npc[] npc, KeyEventArgs e)
        {
            Player p = player[current_player];
            Point p1 = new Point(p.x, p.y);
            Point p2 = get_collision_point(player);

            for (int i = 0; i < npc.Length; i++)
            {
                if (npc[i] == null)
                {
                    continue;
                }

                if (npc[i].map != Map.current_map)
                {
                    continue;
                }

                if (npc[i].is_line_collision(p1, p2))
                {
                    if (npc[i].Collision_type == Npc.Collosion_type.ENTER)
                    {
                        //触发故事操作
                        Task.story(i);
                        break;
                    }
                    else if (npc[i].Collision_type == Npc.Collosion_type.KEY)
                    {
                        if (e.KeyCode == Keys.Space || e.KeyCode == Keys.Enter)
                        {
                            //触发故事操作
                            Task.story(i);
                            break;
                        }
                    }
                }
            }

        }

        //绘制角色
        //按钮的点击事件触发程序
        public static void DrawHero(Player[] player, Graphics g, int map_sx, int map_sy)
        {

            Player p = player[current_player];
            //0 =x   y=bitmap1.Height / 4 * (face - 1)
            //width= bitmap1.Width / 4  height=bitmap1.Height / 4
            Rectangle rl = new Rectangle(p.bitmap.Width / 4 * (p.anm_frame % 4), p.bitmap.Height / 4 * (p.face - 1),
                p.bitmap.Width / 4, p.bitmap.Height / 4);
            Bitmap bitmapRec = p.bitmap.Clone(rl, p.bitmap.PixelFormat);

            g.DrawImage(bitmapRec, map_sx + p.x + p.x_offset, map_sy + p.y + p.y_offset, 100, 100);


        }


    }
}

将以下代码放在Map.cs中

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static vs_Timerbutton.Layer_Sort_comparer;

namespace vs_Timerbutton
{
    class Map
    {
        //地图编号
        public static int current_map = 0;
        //图片路径
        public string bitmap_path;
        public Bitmap bitmap;

        public string shade_path;
        public Bitmap shade;

        public string block_path;
        public Bitmap block;

        //天空背景路径
        public string back_path;
        public Bitmap back;
        //背景音乐
        public string music;

        public Map()
        {
            bitmap_path = "m0.png";
        }

        //判断能够通行
        public static bool can_through(Map[] map, int x, int y)
        {
            Map m = map[current_map];
            if (x < 0)
            {
                return false;
            }
            else if (x >= m.block.Width)
            {
                return false;
            }
            else if (y < 0)
            {
                return false;
            }
            else if (y >= m.block.Height)
            {
                return false;
            }

            if (m.block.GetPixel(x, y).B == 0)
            {
                return false;
            }
            else
            {
                return true;
            }


        }

        public static void drawMap(Map[] map, Player[] player, Npc[] npc, Graphics g, Rectangle stage)
        {
            Map m = map[current_map];

            int map_sx = get_map_sx(map, player, stage);
            int map_sy = get_map_sy(map, player, stage);

            int p_x = Player.get_pos_x(player);
            int p_y = Player.get_pos_y(player);

            int map_w = m.bitmap.Width;
            int map_h = m.bitmap.Height;

            if (p_x <= stage.Width / 2)
            {
                map_sx = 0;
            }
            else if (p_x >= map_w - stage.Width / 2)
            {
                map_sx = stage.Width - map_w;
            }
            else
            {
                map_sx = stage.Width / 2 - p_x;
            }


            if (p_y <= stage.Height / 2)
            {
                map_sy = 0;
            }
            else if (p_y >= map_h - stage.Height / 2)
            {
                map_sy = stage.Height - map_h;
            }
            else
            {
                map_sy = stage.Height / 2 - p_y;
            }

            BufferedGraphicsContext bgc = BufferedGraphicsManager.Current;
            //缓冲区图形对象
            BufferedGraphics myBuffer = bgc.Allocate(g, stage);
            //缓冲区操作图形
            Graphics g2 = myBuffer.Graphics;
            //绘制 背景层
            if (m.back != null)
            {
                g2.DrawImage(m.back, 0, 0);
            }
            //画地图
            g2.DrawImage(m.bitmap, map_sx, map_sy);
            //画人物
            // Player.DrawHero(player, g2, map_sx,map_sy);
            draw_player_npc(map, player, npc, g2, map_sx, map_sy);
            //绘制覆盖图层
            g2.DrawImage(m.shade, map_sx, map_sy);
            for (int i = 0; i < npc.Length; i++)
            {
                if (npc[i] == null)
                {
                    continue;
                }
                if (npc[i].map != current_map)
                {
                    continue;
                }
                npc[i].drawNPC(g2, map_sx, map_sy);
            }
            myBuffer.Render();
            myBuffer.Dispose();

        }

        public static int get_map_sx(Map[] map, Player[] player, Rectangle stage)
        {
            Map m = map[current_map];
            if (m.bitmap == null)
            {
                return 0;
            }
            int map_sx = 0;
            int p_x = Player.get_pos_x(player);
            int map_w = m.bitmap.Width;
            if (p_x <= stage.Width / 2)
            {
                map_sx = 0;
            }
            else if (p_x >= map_w - stage.Width / 2)
            {
                map_sx = stage.Width - map_w;
            }
            else
            {
                map_sx = stage.Width / 2 - p_x;
            }
            return map_sx;
        }

        public static int get_map_sy(Map[] map, Player[] player, Rectangle stage)
        {
            Map m = map[current_map];
            if (m.bitmap == null)
            {
                return 0;
            }
            int map_sy = 0;
            int p_y = Player.get_pos_y(player);
            int map_h = m.bitmap.Height;
            if (p_y <= stage.Height / 2)
            {
                map_sy = 0;
            }
            else if (p_y >= map_h - stage.Width / 2)
            {
                map_sy = stage.Height - map_h;
            }
            else
            {
                map_sy = stage.Height / 2 - p_y;
            }
            return map_sy;
        }

        public static void draw_player_npc(Map[] map, Player[] player, Npc[] npc, Graphics g, int map_sx, int map_sy)
        {
            Layer_sort[] layer_sort = new Layer_sort[npc.Length + 1];
            for (int i = 0; i < npc.Length; i++)
            {
                if (npc[i] != null)
                {
                    layer_sort[i].y = npc[i].y;
                    layer_sort[i].index = i;
                    layer_sort[i].type = 1;
                }
                else
                {
                    layer_sort[i].y = int.MaxValue;
                    layer_sort[i].index = i;
                    layer_sort[i].type = 1;
                }
            }

            layer_sort[npc.Length].y = Player.get_pos_y(player);
            layer_sort[npc.Length].index = 0;
            layer_sort[npc.Length].type = 0;

            System.Array.Sort(layer_sort, new Layer_Sort_comparer());
            for (int i = 0; i < layer_sort.Length; i++)
            {
                if (layer_sort[i].type == 0)
                {
                    Player.DrawHero(player, g, map_sx, map_sy);
                }
                else if (layer_sort[i].type == 1)
                {
                    int index = layer_sort[i].index;
                    if (npc[index] == null)
                    {
                        continue;
                    }
                    if (npc[index].map != current_map)
                    {
                        continue;
                    }
                    npc[index].drawNPC(g, map_sx, map_sy);
                }
            }

        }

        public static void change_map(Map[] map, Player[] player, Npc[] npc, int newindex, int x, int y, int face, WMPLib.WindowsMediaPlayer music_player)
        {
            if (map[current_map].bitmap != null)
            {
                map[current_map].bitmap = null;
            }
            //判断是否有遮挡图
            if (map[current_map].shade != null)
            {
                map[current_map].shade = null;
            }

            if (map[current_map].block != null)
            {
                map[current_map].block = null;
            }


            if (map[current_map].back != null)
            {
                map[current_map].back = null;
            }

            if (map[newindex].bitmap_path != null && map[newindex].bitmap_path != "")
            {
                map[newindex].bitmap = new Bitmap(map[newindex].bitmap_path);
                map[newindex].bitmap.SetResolution(100, 100);
            }

            if (map[newindex].shade_path != null && map[newindex].shade_path != "")
            {
                map[newindex].shade = new Bitmap(map[newindex].shade_path);
                map[newindex].shade.SetResolution(100, 100);
            }


            if (map[newindex].block_path != null && map[newindex].block_path != "")
            {
                map[newindex].block = new Bitmap(map[newindex].block_path);
                map[newindex].block.SetResolution(100, 100);
            }

            if (map[newindex].back_path != null && map[newindex].back_path != "")
            {
                map[newindex].back = new Bitmap(map[newindex].back_path);
                map[newindex].back.SetResolution(100, 100);
            }


            //加载NPC
            for (int i = 0; i < npc.Length; i++)
            {
                //如果npc数组中某个位置没有npc数据,执行下次
                if (npc[i] == null)
                {
                    continue;
                }

                if (npc[i].map == current_map)
                {
                    npc[i].unload();
                }
                if (npc[i].map == newindex)
                {
                    npc[i].load();
                }
            }


            //设置音乐播放器的文件路径
            music_player.URL = map[newindex].music;

            current_map = newindex;
            Player.set_pos(player, x, y, face);
        }

    }
}

 

 将以下代码放在Comm.cs中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace vs_Timerbutton
{
    class Comm
    {
        public static long Time()
        {
            DateTime dt1 = new DateTime(1970, 1, 1);
            TimeSpan ts = DateTime.Now - dt1;
            return (long)ts.TotalMilliseconds;
        }


        public enum Direction
        {
            UP = 4,
            DOWN = 1,
            RIGHT = 3,
            LEFT = 2,
        }
    }
}

将以下代码放在Npc.cs中

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace vs_Timerbutton
{
    class Npc
    {
        //npc所属地图
        public int map = -1;
        //NPC坐标
        public int x = 0;
        public int y = 0;
        //NPC在地图上绘图偏移量
        public int x_offset = -120;
        public int y_offset = -220;
        //npc图片资源路径
        public string bitmap_path = "";
        public Bitmap bitmap;
        //NPC可见性
        public bool visible = true;

        //碰撞区域
        public int region_x = 60;
        public int region_y = 60;

        public enum Collosion_type
        {
            KEY = 1,
            ENTER = 2,
        }

        public enum Npc_type
        {
            NORMAL = 0,
            CHARACTER = 1,
        }

        public Npc_type npc_type = Npc_type.NORMAL;


        public Comm.Direction face = Comm.Direction.DOWN;
        public int walk_frame = 0;
        public long last_walk_time = 0;
        public long walk_interval = 80;
        public int speed = 40;
        public Comm.Direction idle_walk_direction = Comm.Direction.DOWN;
        public int idle_walk_time = 0;
        public int idle_walk_time_now = 0;


        public Collosion_type Collision_type = Collosion_type.KEY;

        //加载NPC
        public void load()
        {
            if (bitmap_path != "")
            {
                //NPC的位图对象
                bitmap = new Bitmap(bitmap_path);
                //设置NPC的分辨率
                bitmap.SetResolution(100, 100);
            }
        }
        //卸载NPC
        public void unload()
        {
            if (bitmap != null)
            {
                bitmap = null;
            }
        }
        //绘制NPC
        public void drawNPC(Graphics g, int map_sx, int map_sy)
        {
            if (npc_type == Npc_type.NORMAL)
            {
                if (visible != true)
                {
                    return;
                }
                if (bitmap != null)
                {
                    g.DrawImage(bitmap, map_sx + x + x_offset, map_sy + y + y_offset, 100, 100);
                }
            }
            else if (npc_type == Npc_type.CHARACTER)
            {
                draw_character(g, map_sx, map_sy);
            }

        }

        //绘制能够移动的NPC
        public void draw_character(Graphics g, int map_sx, int map_sy)
        {
            Rectangle rl = new Rectangle(bitmap.Width / 4 * (walk_frame % 4), bitmap.Height / 4 * ((int)face - 1),
             bitmap.Width / 4, bitmap.Height / 4);
            Bitmap bitmapRec = bitmap.Clone(rl, bitmap.PixelFormat);

            g.DrawImage(bitmapRec, map_sx + x + x_offset, map_sy + y + y_offset, 100, 100);
        }


        //NPC行走
        public void walk(Map[] map, Comm.Direction direction, bool isblock)
        {
            //方向
            face = direction;

            if (Comm.Time() - last_walk_time <= walk_interval)
            {
                return;
            }

            //up
            if (direction == Comm.Direction.UP && (!isblock || Map.can_through(map, x, y - speed)))
            {
                y -= speed;
            }

            //down
            if (direction == Comm.Direction.DOWN && (!isblock || Map.can_through(map, x, y + speed)))
            {
                y += speed;
            }

            //right
            if (direction == Comm.Direction.RIGHT && (!isblock || Map.can_through(map, x + speed, y)))
            {
                x += speed;
            }

            //left
            if (direction == Comm.Direction.LEFT && (!isblock || Map.can_through(map, x - speed, y)))
            {
                x -= speed;
            }

            walk_frame = walk_frame + 1;
            if (walk_frame >= int.MaxValue)
            {
                walk_frame = 0;
            }
            last_walk_time = Comm.Time();

        }

        //停止移动
        public void stop_walk()
        {
            walk_frame = 0;
            last_walk_time = 0;
        }

        //时间逻辑
        public void timer_logic(Map[] map)
        {
            if (idle_walk_time != 0)
            {
                walk(map, Comm.Direction.DOWN, true);
            }
        }

        //碰撞检测
        public bool is_collision(int collision_x, int collision_y)
        {
            Rectangle rect = new Rectangle(x - region_x / 2, y - region_y / 2, region_x, region_y);
            return rect.Contains(new Point(collision_x, collision_y));

        }

        //线碰撞检测
        public bool is_line_collision(Point p1, Point p2)
        {
            if (is_collision(p2.X, p2.Y))
            {
                return true;
            }

            int px, py;
            px = p1.X + (p2.X - p1.X) / 2;
            py = p1.Y + (p2.Y - p1.Y) / 2;
            if (is_collision(px, py))
            {
                return true;
            }

            px = p2.X - (p2.X - p1.X) / 4;
            py = p2.Y - (p2.Y - p1.Y) / 4;

            if (is_collision(px, py))
            {
                return true;
            }

            return false;
        }


    }
}

将以下代码放在Layer_Sort_comparer.cs中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace vs_Timerbutton
{
    class Layer_Sort_comparer : System.Collections.IComparer
    {
        public struct Layer_sort
        {
            public int y;
            public int index;
            public int type;
        }
        public int Compare(object s1, object s2)
        {
            return ((Layer_sort)s1).y - ((Layer_sort)s2).y;
            throw new NotImplementedException();
        }
    }
}

 将以下代码放在Task.cs中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace vs_Timerbutton
{
    class Task
    {
        public static void story(int i)
        {
            DialogResult r1;
            if (i == 0)
            {
                r1 = MessageBox.Show("我是NPC1");
            }
            if (i == 1)
            {
                r1 = MessageBox.Show("我是NPC2");
            }
        }
    }
}

 

将以下代码放在Button.cs中

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace vs_Timerbutton
{
    class Button
    {

        public int x = 0;
        public int y = 0;
        public int w = 0;
        public int h = 0;

        //按钮普通外观图片
        public string b_normal_path;
        //选中
        public string b_select_path;
        //按压
        public string b_press_path;


        public Bitmap b_nornal;
        public Bitmap b_select;
        public Bitmap b_press;

        public enum Status
        {
            NORMAL = 1,
            SELECT = 2,
            PRESS = 3,
        }

        public Status status = Status.NORMAL;

        public class Key_ctrl
        {
            public int up = -1;
            public int down = -1;
            public int left = -1;
            public int right = -1;

        }
        public Key_ctrl key_ctrl = new Key_ctrl();

        public void set(int x0, int y0, int w0, int h0,
            string normal_path, string select_path, string press_path,
            int key_up, int key_down, int key_left, int key_right)
        {

            x = x0;
            y = y0;
            w = w0;
            h = h0;
            b_normal_path = normal_path;
            b_select_path = select_path;
            b_press_path = press_path;
            key_ctrl.up = key_up;
            key_ctrl.down = key_down;
            key_ctrl.left = key_left;
            key_ctrl.right = key_right;

        }

        public void load()
        {
            if (b_normal_path != null && b_normal_path != "")
            {
                b_nornal = new Bitmap(b_normal_path);
                b_nornal.SetResolution(100, 100);

                if (w <= 0)
                {
                    w = b_nornal.Width;
                }

                if (h <= 0)
                {
                    h = b_nornal.Height;
                }

            }
            if (b_select_path != null && b_select_path != "")
            {
                b_select = new Bitmap(b_select_path);
                b_select.SetResolution(100, 100);
            }
            if (b_press_path != null && b_press_path != "")
            {
                b_press = new Bitmap(b_press_path);
                b_press.SetResolution(100, 100);
            }

        }

        //画出按钮
        public void draw(Graphics g, int x_offset, int y_offset)
        {
            if (status == Status.NORMAL && b_nornal != null)
            {
                g.DrawImage(b_nornal, x_offset + x, y_offset + y);
            }
            if (status == Status.SELECT && b_select != null)
            {
                g.DrawImage(b_select, x_offset + x, y_offset + y);
            }
            if (status == Status.PRESS && b_press != null)
            {
                g.DrawImage(b_press, x_offset + x, y_offset + y);
            }
        }

        public delegate void Click_event();
        public event Click_event click_event;

        public void click()
        {
            if (click_event != null)
            {
                click_event();
            }
        }


    }
}

替换Form_Obj.designer.cs中的代码


namespace vs_Timerbutton
{
    partial class Form_Obj
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            //
            // pictureBox1
            //
            this.pictureBox1.Location = new System.Drawing.Point(-8, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(819, 688);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            //
            // timer1
            //
            this.timer1.Enabled = true;
            this.timer1.Interval = 600;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            //
            // Form_Obj
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(833, 700);
            this.Controls.Add(this.pictureBox1);
            this.KeyPreview = true;
            this.Name = "Form_Obj";
            this.Text = "Form_Obj";
            this.Load += new System.EventHandler(this.Form_Obj_Load);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_Obj_KeyDown);
            this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form_Obj_KeyUp);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Timer timer1;
    }
}

拖拽Timer到对话控制框中,留下一个timer1

 对属性进行设置

对事件进行设置

 

在工具箱中下面随便位置右键点击选项卡

拖拽Windows Media Player到对话控制框中

 拉伸后点击

 将音乐播放器属性中的Visible改成false代表不可见

绑定文件中的歌曲

将以下图片放在指定文件夹中

 

 

 

 

 

 

 

 

 

 

 

 

 另外在百度上下载三个1.mp3 2.mp3 3.mp3的文件放在指定文件夹中以下为图示

回到设计中ctrl+F5运行之后

即可看到npc向下行走,其中npc行动方向时可以在代码中改动的

End.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值