拖拽pictureBox到对话控制框
拉伸外边框
拉伸内框
双击Program.cs
修改这里
修改为
双击Form_Obj中的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_Map
{
public partial class Form_Obj : Form
{
Player[] player = new Player[3];
//地图数组
Map[] map = new Map[2];
//计数器
public int animation_ctrl = 0;
public Form_Obj()
{
InitializeComponent();
}
private void Form_Obj_KeyDown(object sender, KeyEventArgs e)
{
animation_ctrl += 1;
Player.key_ctrl(player,e);
Player.DrawHero(player,pictureBox1.CreateGraphics(),
pictureBox1.DisplayRectangle, animation_ctrl);
Map.drawMap(map, pictureBox1.CreateGraphics());
}
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].bitmap = new Bitmap(@"map1.png");
map[1] = new Map();
map[1].bitmap_path = "map2.png";
map[1].bitmap = new Bitmap(@"map2.png");
}
}
}
将以下代码放在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_Map
{
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 Player()
{
bitmap = new Bitmap(@"r1.png");
bitmap.SetResolution(100, 100);
}
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, KeyEventArgs e)
{
Player p = player[current_player];
if (e.KeyCode == Keys.Tab)
{
key_change_player(player);
}
//通过键位的判断,实现角色的位移
switch (e.KeyCode)
{
case Keys.Left://左
p.x -= 5;
p.face = 2;
break;
case Keys.Up://上
p.y -= 5;
p.face = 4;
break;
case Keys.Right://右
p.x += 5;
p.face = 3;
break;
case Keys.Down://下
p.y += 5;
p.face = 1;
break;
default:
break;
}
}
//绘制角色
//按钮的点击事件触发程序
public static void DrawHero(Player[] player, Graphics g, Rectangle myrec, int animation_ctrl)
{
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 * (animation_ctrl % 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;
g2.DrawImage(bitmapRec, p.x, p.y, 60, 60);
//显示图像并释放资源
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_Map
{
class Map
{
//地图编号
public static int current_map = 0;
//图片路径
public string bitmap_path;
public Bitmap bitmap;
public Map()
{
bitmap_path = "m0.png";
}
public static void drawMap(Map[] map, Graphics g)
{
Map m = map[current_map];
g.DrawImage(m.bitmap, 0, 0);
}
}
}
双击Form_Obj.Designer.cs
将以下代码放在Form_Obj.Designer.cs 中
namespace VS_Map
{
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.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(2, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(1341, 759);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form_Obj
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1343, 756);
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);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
}
}
最后将以下图片放在指定文件包位置中
回到设计.cpp中ctrl+F5运行即可
最后可以在地图中行走并且在运行中按Tab可以切换人物
End.