双击Program.cs
修改这里
修改为
拖拽PictureBox到对话控制框中
拉伸
双击GameManage中的KeyDown右侧空白处
将以下代码放在GameManage.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_Runmap
{
public partial class GameManage : Form
{
Player[] player = new Player[3];
//地图数组
Map[] map = new Map[2];
//计数器
public int animation_ctrl = 0;
public GameManage()
{
InitializeComponent();
}
private void GameManage_KeyDown(object sender, KeyEventArgs e)
{
Player.key_ctrl(player, map, e);
Map.drawMap(map, player, pictureBox1.CreateGraphics(), new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
}
private void GameManage_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].bitmap = new Bitmap(@"map1.png");
map[1] = new Map();
map[1].bitmap_path = "map2.png";
map[1].bitmap = new Bitmap(@"map2.png");*/
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[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.change_map(map, player, 0, 100, 350, 1);//1:所有 2:所有 3:切换地图 4: 5:
}
}
}
将以下代码放在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_Runmap
{
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 anm_frame = 0;
public long last_walk_time = 0;
public long walk_interval = 100;
public int speed = 20;
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, KeyEventArgs e)
{
Player p = player[current_player];
if (e.KeyCode == Keys.Tab)
{
key_change_player(player);
}
//通过键位的判断,实现角色的位移
switch (e.KeyCode)
{
case Keys.Left://左
if (Map.can_through(map, p.x - p.speed, p.y))
{
p.x -= 5;
p.face = 2;
}
break;
case Keys.Up://上
if (Map.can_through(map, p.x, p.y - p.speed))
{
p.y -= 5;
p.face = 4;
}
break;
case Keys.Right://右
if (Map.can_through(map, p.x + p.speed, p.y))
{
p.x += 5;
p.face = 3;
}
break;
case Keys.Down://下
if (Map.can_through(map, p.x, p.y + p.speed))
{
p.y += 5;
p.face = 1;
}
break;
default:
break;
}
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 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);
/*Graphics g1 = g;
//创建缓冲区域
BufferedGraphicsContext bgc = BufferedGraphicsManager.Current;
//缓冲区图形对象
BufferedGraphics myBuffer = bgc.Allocate(g1, myrec);
//缓冲区操作图形
Graphics g2 = myBuffer.Graphics;*/
g.DrawImage(bitmapRec, map_sx + p.x, map_sy + p.y, 100, 100);
//显示图像并释放资源
/*myBuffer.Render();
myBuffer.Dispose();*/
}
}
}
将以下代码放在Map.cs中
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace vs_Runmap
{
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 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, Graphics g, Rectangle stage)
{
Map m = map[current_map];
int map_sx = 0;
int map_sy = 0;
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;
}
//绘制 背景层
if (m.back != null)
{
g.DrawImage(m.back, 0, 0);
}
//画地图
g.DrawImage(m.bitmap, map_sx, map_sy);
//画人物
Player.DrawHero(player, g, map_sx, map_sy);
//绘制覆盖图层
g.DrawImage(m.shade, map_sx, map_sy);
}
public static void change_map(Map[] map, Player[] player, int newindex, int x, int y, int face)
{
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);
}
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_Runmap
{
class Comm
{
public static long Time()
{
DateTime dt1 = new DateTime(1970, 1, 1);
TimeSpan ts = DateTime.Now - dt1;
return (long)ts.TotalMilliseconds;
}
}
}
双击GameManage.Designer.cs重置以下替换代码
namespace vs_Runmap
{
partial class GameManage
{
/// <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.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(4, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(571, 498);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// GameManage
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(577, 503);
this.Controls.Add(this.pictureBox1);
this.KeyPreview = true;
this.Name = "GameManage";
this.Text = "GameManage";
this.Load += new System.EventHandler(this.GameManage_Load);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.GameManage_KeyDown);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
}
}
最后将以下图片放在指定文件夹中
最后回到设计中ctrl+F5运行
运行成功后会发现路径被黑色图片遮挡不能经过
End.