坦克大战游戏步骤 思路 与代码(一)

坦克大战步骤

1.首先创建一个父类,设置好高度、宽度、左、中间的属性 创建一个构造方法、将这些参数传递,将这些值赋予到父类中
并且在父类中构建一个重画的虚拟类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace WindowsFormsApplication1
{
public abstract class GameObject //在父类中定义抽象类,在父类中规定,在子类中实现
{

public int Width { get; set; }
public int Heigth { get; set; }
public int Left { get; set; }
public int Top { get; set; }
public GameObject(int width,int height,int left,int top)

{

this.Width = width;
this.Heigth = height;
this.Left = left;
this.Top = top;

}

public abstract void Draw(Graphics g);//从窗体中传一个对象才能开始画

}
}


2.然后再建立一个会动的类 并且创建速度和方向 从父类继承高宽等属性,并且定义自己的属性。再定义一个方向的方法
public abstract class ActivityGameObject:GameObject //活动类也是抽象的,并不知道如何绘图
{

public int Spend { get; set; }//速度
public Dirction Dir { get; set; }//方向
public ActivityGameObject (int width,int height,int left,int top,int spend,Dirction Dir):base(width,height,left,top)
{

this.Spend = spend; //通过父类的引用将这些数据传给子类,子类再定义自己的属性
this.Dir = Dir;

}
public virtual void Move()
{

switch (this.Dir)
{


case Dirction.Up: this.Top -= Spend; break; //判断方向
case Dirction.Down: this.Top += Spend; break;
case Dirction.Left: this.Left -= Spend; break;
case Dirction.Rigth: this.Left += Spend; break;


}


}
}

public enum Dirction
{

Up,
Left,
Down,
Rigth


}
}

3.再创建一个坦克类 继承活动类 定义生命 和四个方向的图片 将父类继承的东西全部继承 在坦克类重写方向
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace WindowsFormsApplication1
{
public class Tank:ActivityGameObject //坦克类继承活动的类
{

public int Life { get; set; } //生命
private Image[] images; //坦克的四个方向的图片
public Tank(Image[] images, int left, int top, int spend, int life, Dirction dir)
: base(images[0].Width, images[0].Height, left, top, spend, dir)
{
//构造坦克的图片数据,并在父类中继承

this.Left = left;
this.images = images;



}
public override void Draw(Graphics g)
{


switch (this.Dir)
{


case Dirction.Up: g.DrawImage(images[0], this.Left, this.Top); break; //位置是第一0个,则是当前的位置
case Dirction.Down: g.DrawImage(images[1], this.Left, this.Top); break;
case Dirction.Left: g.DrawImage(images[2], this.Left, this.Top); break;
case Dirction.Rigth: g.DrawImage(images[3], this.Left, this.Top); break;

}

}

}
}
4.创建一个玩家坦克继承坦克类
创建对象之前静态初始化坦克的图片,并且将值传给坦克类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using WindowsFormsApplication1.Properties;

namespace WindowsFormsApplication1
{

public class playerTank : Tank //玩家坦克
{

private static Image[] images = new Image[] //创建对象之前的静态初始化

{

Resources.p1tankU,
Resources.p1tankD,
Resources.p1tankL, //玩家坦克向父类传递一组图片
Resources.p1tankR






};
public playerTank(int left, int top, int spend, int life, Dirction dir)
: base(images, left, top, spend, life, dir) //传递
{

 

}
}
}

5.创建一个所有对象管理员的类
首先创建一个单例让管理员成为唯一,创建一个玩家属性,添加游戏对象并且验证将它转成玩家,方向的也传递的同事,并且调用玩家坦克的方向方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace WindowsFormsApplication1
{
public class GameObjectManager
{
private GameObjectManager() { }
private static GameObjectManager gom=null;
public static GameObjectManager GetInstance()
{

if (gom == null)
{

gom = new GameObjectManager();
}
return gom;

}
private playerTank pltank;

public playerTank Pltank
{
get { return pltank; }
set { pltank = value; }
}
//添加游戏对象
public void Add(GameObject go) //如果扔给一个玩家坦克,转成玩家坦克,并且赋予管理者
{

if (go is playerTank)
{
Pltank= go as playerTank;

}

 

}
public void Draw(Graphics g)

{

pltank.Draw(g);

}

5.窗体在构建的时候将坦克的图片构造方法传递

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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.InitGame();
}

public void InitGame()
{

playerTank pltank = new playerTank(200,200,10,10,Dirction.Up); //将参数传递
GameObjectManager.GetInstance().Add(pltank);


}

private void Form1_Paint(object sender, PaintEventArgs e)
{

GameObjectManager.GetInstance().Draw(e.Graphics);
}

private void timer1_Tick(object sender, EventArgs e)
{

this.Invalidate();
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
playerTank tank= GameObjectManager.GetInstance().Pltank;
switch (e.KeyCode)
{

case Keys.W: tank.Dir = Dirction.Up; tank.Move(); break;
case Keys.S: tank.Dir = Dirction.Down; tank.Move(); break;
case Keys.A: tank.Dir = Dirction.Left; tank.Move(); break;
case Keys.D: tank.Dir = Dirction.Rigth; tank.Move(); break;

}
}

}
}

posted @ 2016-01-12 17:06  西瓜冰镇老板  阅读(1519)  评论(0编辑  收藏  举报