`
namespace game
{
public partial class Form1 : Form
{
private Thread t;
private Graphics g;
public Form1()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
g = this.CreateGraphics();
GameFramework.g = g;
t = new Thread(new ThreadStart(GameMainThread));
t.Start();
}
private static void GameMainThread()
{
GameFramework.Start();
while (true)
{
GameFramework.g.Clear(Color.Black);//黑色背景
GameFramework.Update();
Thread.Sleep(1000 / 60);//帧率为60
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e){t.Abort();}
`
窗体代码(一般不用)
`
namespace game
{
public partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1337, 747);
this.Name = "Form1";
this.Text = "怪物大战";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
this.ResumeLayout(false);
}
#endregion
}
游戏框架!
`
namespace game
{
class GameFramework
{
public static Graphics g;
public static void Start()
{
GameObjectManager.CreateMine();
}
public static void Update()
{
GameObjectManager.DisplayMine();
}
}
}
游戏物体管理
`
namespace game
{
class GameObjectManager
{
private static Player p;
/*public static void CreateWall()
{
}
public static void CreateMap()
{
//Wall w = new Wall();
//w.Drawself();
}*/
public static void CreateMine()
{
Random r = new Random();
int x = r.Next(50,1000);
int y = r.Next(30,600);
p = new Player(x,y);
}
public static void DisplayMine()
{
p.Display();
}
}
}
玩家类
namespace game
{
class Player:Spirit
{
private int speed;
private string direction;
public Player(int x,int y)
{
this.x = x;
this.y = y;
this.speed = 1;
this.direction = "w";
this.img = Resources.player;
}
}
}
精灵类
abstract class Spirit
{
public int x { get; set; }
public int y { get; set; }
public Image img { get; set; }
public void Display()
{
Graphics g = GameFramework.g;
g.DrawImage(img,x,y);
}
}
`