双击Program.cs
修改这里
修改为
拖拽PictureBox到对话控制框里
拉伸
双击Form_Obj.cs的小闪电里的KeyDown右侧空白处
将以下代码放在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_Message
{
partial class Form_Obj : Form
{
public static Player[] player = new Player[3];
//地图数组
public static Map[] map = new Map[2];
//多媒体播放器
public static WMPLib.WindowsMediaPlayer music_player = new WMPLib.WindowsMediaPlayer();
public static 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);
if (Panel.panel != null)
{
Panel.key_ctrl(e);
}
Map.drawMap(map, player, npc, pictureBox1.CreateGraphics(),
new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
if (Panel.panel != null)
{
Panel.draw(pictureBox1.CreateGraphics());
}
}
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)
{
/* Title.init();
Title.show();*/
RPGMessage.init();
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_Message
{
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_Message.Layer_Sort_comparer;
namespace vs_Message
{
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_Message
{
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_Message
{
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_Message
{
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_Message
{
class Task
{
public static void story(int i)
{
DialogResult r1;
if (i == 0)
{
/* r1 = MessageBox.Show("我是NPC1");*/
RPGMessage.show("李逍遥", "小李子,志向高", "face1_1.png", RPGMessage.Face.LEFT);
MessageBox.Show("msg show");
}
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_Message
{
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_normal;
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_normal = new Bitmap(b_normal_path);
b_normal.SetResolution(100, 100);
if (w <= 0)
{
w = b_normal.Width;
}
if (h <= 0)
{
h = b_normal.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_normal != null)
{
g.DrawImage(b_normal, 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();
}
}
}
}
将以下代码放在Panel.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_Message
{
//面板
class Panel
{
public static Panel panel = null;
public int x;
public int y;
public string bitmap_path;
public Bitmap bitmap;
public Button[] button;
public int default_button = 0;
public int cancel_button = -1;
public int current_button = 0;
public void set(int x0, int y0, string path, int default_button0, int cancel_button0)
{
x = x0;
y = y0;
bitmap_path = path;
default_button = default_button0;
cancel_button = cancel_button0;
}
//初始化面板
public void init()
{
//背景图
if (bitmap_path != null && bitmap_path != "")
{
bitmap = new Bitmap(bitmap_path);
bitmap.SetResolution(100, 100);
}
if (button != null)
{
for (int i = 0; i < button.Length; i++)
{
if (button[i] == null)
{
continue;
}
button[i].load();
}
}
}
//面板的显示
public void show()
{
panel = this;
current_button = default_button;
set_button_status(Button.Status.SELECT);
}
//面板的隐藏
public void hide()
{
panel = null;
}
public delegate void Draw_event(Graphics g, int x_offset, int y_offset);
public event Draw_event draw_event;
public delegate void Drawbg_event(Graphics g, int x_offset, int y_offset);
public event Drawbg_event drawbg_event;
public void draw_me(Graphics g)
{
//背景
if (drawbg_event != null)
{
drawbg_event(g, this.x, this.y);
}
//背景图
if (bitmap != null)
{
g.DrawImage(bitmap, x, y);
}
//外部调用
if (draw_event != null)
{
draw_event(g, this.x, this.y);
}
//面板上的按钮
if (button != null)
{
for (int i = 0; i < button.Length; i++)
{
if (button[i] == null)
{
continue;
}
button[i].draw(g, x, y);
}
}
}
public void set_button_status(Button.Status status)
{
if (button != null)
{
for (int i = 0; i < button.Length; i++)
{
if (button[i] == null)
{
continue;
}
button[i].status = Button.Status.NORMAL;
}
if (button[current_button] != null)
{
button[current_button].status = status;
}
}
}
public static void draw(Graphics g)
{
if (panel != null)
{
panel.draw_me(g);
}
}
public static void key_ctrl(KeyEventArgs e)
{
if (panel != null)
{
panel.key_ctrl_me(e);
}
}
public void key_ctrl_me(KeyEventArgs e)
{
if (button == null)
{
return;
}
Button btn = button[current_button];
if (btn == null)
{
return;
}
//根据不同键位设置不同的方向
int newindex = -1;
if (e.KeyCode == Keys.Up)
{
newindex = btn.key_ctrl.up;
}
else if (e.KeyCode == Keys.Down)
{
newindex = btn.key_ctrl.down;
}
else if (e.KeyCode == Keys.Left)
{
newindex = btn.key_ctrl.left;
}
else if (e.KeyCode == Keys.Right)
{
newindex = btn.key_ctrl.right;
}
if (newindex >= 0 && newindex < button.Length && button[newindex] != null)
{
current_button = newindex;
set_button_status(Button.Status.SELECT);
}
//确定
if (e.KeyCode == Keys.Space && e.KeyCode == Keys.Enter)
{
set_button_status(Button.Status.PRESS);
btn.click();
}
//取消
else if (e.KeyCode == Keys.Escape)
{
if (cancel_button >= 0 && cancel_button < button.Length)
{
button[cancel_button].click();
}
}
}
}
}
将以下代码放在Title.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_Message
{
static class Title
{
public static Panel title = new Panel();
//确认框
public static Panel confirm = new Panel();
public static string title_music = "2.mp3";
public static Bitmap bg_1 = new Bitmap("T_bg1.png");
public static Bitmap bg_2 = new Bitmap("T_bg2.png");
public static Bitmap bg_3 = new Bitmap("T_bg3.png");
public static Bitmap bg_font = new Bitmap("T_logo.png");
public static long last_change_bg_time = 0;
public static int bg_now = 2;
//页面的初始化
public static void init()
{
//开始游戏按钮
Button btn_new = new Button();
btn_new.set(325, 350, 0, 0, "T_start_1.png", "T_start_2.png", "T_start_2.png", 2, 1, -1, -1);
btn_new.click_event += new Button.Click_event(newgame);
//载入游戏
Button btn_load = new Button();
btn_load.set(325, 400, 0, 0, "T_load_1.png", "T_load_2.png", "T_load_2.png", 0, 2, -1, -1);
btn_load.click_event += new Button.Click_event(loadgame);
//退出游戏
Button btn_exit = new Button();
btn_exit.set(325, 450, 0, 0, "T_exit_1.png", "T_exit_2.png", "T_exit_2.png", 1, 0, -1, -1);
btn_exit.click_event += new Button.Click_event(exitgame);
Button btn_yes = new Button();
btn_yes.set(42, 60, 0, 0,
"confirm_yes_1.png", "confirm_yes_2.png", "confirm_yes_2.png",
-1, 1, -1, -1);
btn_yes.click_event += new Button.Click_event(comfirm_yes);
Button btn_no = new Button();
btn_no.set(42, 100, 0, 0,
"confirm_no_1.png", "confirm_no_2.png", "confirm_no_2.png",
0, -1, -1, -1);
btn_no.click_event += new Button.Click_event(comfirm_no);
bg_1.SetResolution(100, 100);
bg_2.SetResolution(100, 100);
bg_3.SetResolution(100, 100);
bg_font.SetResolution(100, 100);
title.draw_event += new Panel.Draw_event(drawtitle);
//按钮添加到面板
title.button = new Button[3];
title.button[0] = btn_new;
title.button[1] = btn_load;
title.button[2] = btn_exit;
title.set(0, 0, "T_bg1.png", 0, -1);
title.init();
//确认和取消按钮放到确认面板
confirm.button = new Button[2];
confirm.button[0] = btn_yes;
confirm.button[1] = btn_no;
//确认面板的位置
confirm.set(283, 250, "confirm_bg.png", 0, 1);
confirm.init();
}
//确认
public static void comfirm_yes()
{
//退出应用
Application.Exit();
}
//取消
public static void comfirm_no()
{
title.show();
}
public static void drawconfirm(Graphics g, int x_offset, int y_offset)
{
title.draw_me(g);
}
//新游戏
public static void newgame()
{
Map.change_map(Form_Obj.map, Form_Obj.player, Form_Obj.npc, 0, 800, 400, 1, Form_Obj.music_player);
title.hide();
}
//加载游戏
public static void loadgame()
{
MessageBox.Show("加载游戏");
}
//退出游戏
public static void exitgame()
{
confirm.show();
confirm.drawbg_event += new Panel.Drawbg_event(drawconfirm);
}
//展示
public static void show()
{
Form_Obj.music_player.URL = title_music;
title.show();
}
public static void drawtitle(Graphics g, int x_offset, int y_offset)
{
if (bg_now == 0)
{
g.DrawImage(bg_1, 0, 0);
}
else if (bg_now == 1)
{
g.DrawImage(bg_2, 0, 0);
}
else if (bg_now == 2)
{
g.DrawImage(bg_3, 0, 0);
}
//绘制logo
g.DrawImage(bg_font, 260, 80);
//背景自动换
if (Comm.Time() - last_change_bg_time > 500)
{
bg_now = bg_now + 1;
if (bg_now > 2)
{
bg_now = 0;
}
last_change_bg_time = Comm.Time();
}
}
}
}
将以下代码放在RPGMessage.cs中
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace vs_Message
{
class RPGMessage
{
public static Panel message = new Panel();
public static Bitmap face;
public enum Face
{
LEFT = 1,
RIGHT = 2,
}
public static Face face_pos = Face.LEFT;
public static string name = "";
public static string content = "";
public static void init()
{
Button btn_ok = new Button();
btn_ok.set(-1000, -1000, 2000, 2000, "", "", "", -1, -1, -1, -1);
btn_ok.click_event += new Button.Click_event(btn_ok_event);
message.button = new Button[1];
message.button[0] = btn_ok;
message.set(0, 380, "msg.png", 0, -1);
message.draw_event += new Panel.Draw_event(msgdraw);
message.init();
}
//按钮事件
public static void btn_ok_event()
{
message.hide();
}
//展示面板
public static void show(string name0, string content0,
string face_path, Face face_pos0)
{
name = name0;
content = content0;
if (face_path != null && face_path != "")
{
face = new Bitmap(face_path);
face.SetResolution(96, 96);
}
else
{
face = null;
}
face_pos = face_pos0;
message.show();
}
//自动换行
public static string linefeed(string str, int num)
{
if (str == null)
{
return null;
}
string ret = "";
int start_pos = 0;
while (start_pos < str.Length)
{
if (start_pos + num > str.Length)
{
num = str.Length - start_pos;
}
ret = ret + str.Substring(start_pos, num) + "\n";
start_pos = start_pos + num;
}
return ret;
}
public static void msgdraw(Graphics g, int x_offset, int y_offset)
{
if (face != null)
{
if (face_pos == Face.LEFT)
{
g.DrawImage(face, 0, 245);
}
else if (face_pos == Face.RIGHT)
{
g.DrawImage(face, 486, 245);
}
}
//名字
Font name_font = new Font("黑体", 14);
Brush name_brush = Brushes.Peru;
StringFormat name_sf = new StringFormat();
if (face_pos == Face.LEFT)
{
g.DrawString(name, name_font, name_brush,
x_offset + 240, y_offset + 25, name_sf);
}
else
{
g.DrawString(name, name_font, name_brush,
x_offset + 150, y_offset + 25, name_sf);
}
//内容
Font content_font = new Font("黑体", 12);
Brush content_brush = Brushes.WhiteSmoke;
StringFormat content_sf = new StringFormat();
string show_content = linefeed(content, 25);
if (face_pos == Face.LEFT)
{
g.DrawString(show_content, content_font, content_brush,
x_offset + 260, y_offset + 55, content_sf);
}
else
{
g.DrawString(show_content, content_font, content_brush,
x_offset + 170, y_offset + 55, content_sf);
}
}
}
}
最后替换Form_Obj.Designer.cs中的代码
namespace vs_Message
{
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(1180, 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(1208, 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
如果有两个时间delete删除掉time2
对属性进行设置
对事件进行设置
搜索Player然后
在工具箱中下面随便位置右键点击选择项
拖拽Windows Media Player到对话控制框中
拉伸
将音乐播放器属性中的Visible改成false代表不可见
绑定文件中的歌曲
将以下图片放在指定文件夹中
百度下载音乐背景设置为1.mp3 2.mp3 3.mp3 或者(英文+数字).mp3
最后将双音乐关闭
回到设计中ctrl+F5运行后
按空格对话,回车结束对话
End.