private void timer1_Tick(object sender, EventArgs e) { //对窗体进行更新 this.Invalidate(); //调用碰撞检测方法 SingleObject.GetObject().Check(); }
private void TankMenu_Click(object sender, EventArgs e) { if (SingleObject.GetObject().T == null) { Type = OperType.DrawTank; } }
private void StarMenu_Click(object sender, EventArgs e) { if (SingleObject.GetObject().S == null) { Type = OperType.DrawStar; } }
/// <summary> /// 【新游戏】 /// </summary> public void NewGame() { #region 清理所有游戏对象 SingleObject.listBrick.Clear(); SingleObject.listRiver.Clear(); SingleObject.listBullet.Clear(); SingleObject.listBoom.Clear(); SingleObject.listStell.Clear(); #endregion #region 初始化变量 for (int i = 0; i < GridNum; i++) { for (int j = 0; j < GridNum; j++) { GameObject.InitMap[i, j] = '#'; GameObject.IsVisit[i, j] = true; GameObject.InitIsVisit[i, j] = true; } } GameObject.StartPoint.X = -1; GameObject.EndPoint.X = -1; GameObject.IsDrawOk = true; GameObject.Cost = 0; AutoModelOpen = false; ManualModelOpen = false; Type = OperType.None; SingleObject.GetObject().T = null; SingleObject.GetObject().S = null; label2.Text = "当前坦克坐标 : "; label3.Text = "当前鼠标坐标: "; #endregion }
public static SingleObject GetObject() { if (_singleObject == null) { _singleObject = new SingleObject(); } return(_singleObject); }
/// <summary> /// 初始化游戏 /// </summary> public void InitGame() { #region 清理所有游戏对象 SingleObject.listBrick.Clear(); SingleObject.listRiver.Clear(); SingleObject.listBullet.Clear(); SingleObject.listBoom.Clear(); SingleObject.listStell.Clear(); #endregion #region 重新添加游戏对象 int TmpWidth, TmpHeight; for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { TmpWidth = i * GridWidth; TmpHeight = j * GridWidth + Tank.LeftUpY; switch (GameObject.InitMap[i, j]) { case 'T': Tank TmpTank = new Tank(TmpWidth, TmpHeight, Direction.Up); SingleObject.GetObject().T = TmpTank; break; case 'B': Brick TmpBrick = new Brick(TmpWidth, TmpHeight); SingleObject.listBrick.Add(TmpBrick); break; case 'S': Stell TmpStell = new Stell(TmpWidth, TmpHeight); SingleObject.listStell.Add(TmpStell); break; case 'R': River TmpRiver = new River(TmpWidth, TmpHeight); SingleObject.listRiver.Add(TmpRiver); break; case 'X': Star TmpStar = new Star(TmpWidth, TmpHeight); SingleObject.GetObject().S = TmpStar; break; } } } #endregion #region 变量初始化 for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { GameObject.IsVisit[i, j] = GameObject.InitIsVisit[i, j]; } } GameObject.Cost = 0; #endregion }
/// <summary> /// 重写绘制函数 /// </summary> /// <param name="g"></param> public override void Draw(Graphics g) { for (int i = 0; i < imgs.Length; i++) { g.DrawImage(imgs[i], this.X, this.Y); } //爆炸图片播放完成 就销毁自己 SingleObject.GetObject().RemoveGameObject(this); }
private void MainForm_Paint(object sender, PaintEventArgs e) { //更新当前坦克能量消耗值 EnergyCost.Text = GameObject.Cost.ToString(); //绘制当前界面 SingleObject.GetObject().Draw(e.Graphics); //更新当前坦克坐标 if (SingleObject.GetObject().T != null) { label2.Text = "当前坦克坐标 : " + (SingleObject.GetObject().T.X / GridWidth).ToString() + "," + ((SingleObject.GetObject().T.Y - Tank.LeftUpY) / GridWidth).ToString(); } }
/// <summary> /// 碰撞检测方法 /// </summary> public void Check() { #region 子弹打到砖块 for (int i = 0; i < listBullet.Count; i++) { for (int j = 0; j < listBrick.Count; j++) { if (listBullet[i].Getangle().IntersectsWith(listBrick[j].Getangle())) { listBullet.Remove(listBullet[i]); //播放爆炸声音 SoundPlayer sp = new SoundPlayer(Resources.blast); sp.Play(); //出现爆炸的图片 int tempX = listBrick[j].X / MainForm.GridWidth; int tempY = (listBrick[j].Y - Tank.LeftUpY) / MainForm.GridWidth; GameObject.IsVisit[tempX, tempY] = true; SingleObject.GetObject().AddGameObject(new Boom(listBrick[j].X, listBrick[j].Y)); listBrick.Remove(listBrick[j]); break; } } } #endregion #region 子弹打到铁墙 for (int i = 0; i < listBullet.Count; i++) { for (int j = 0; j < listStell.Count; j++) { if (listBullet[i].Getangle().IntersectsWith(listStell[j].Getangle())) { listStell[j].Life -= listBullet[i].Power; listBullet.Remove(listBullet[i]); if (listStell[j].Life == 0) { int tempX = listStell[j].X / MainForm.GridWidth; int tempY = (listStell[j].Y - Tank.LeftUpY) / MainForm.GridWidth; GameObject.IsVisit[tempX, tempY] = true; //播放爆炸声音 SoundPlayer sp = new SoundPlayer(Resources.blast); sp.Play(); //出现爆炸的图片 SingleObject.GetObject().AddGameObject(new Boom(listStell[j].X, listStell[j].Y)); listStell.Remove(listStell[j]); } break; } } } #endregion }
private void MainForm_KeyDown(object sender, KeyEventArgs e) { if (AutoModelOpen == true) { //如果自动模式开启,则不响应鼠标事件 return; } if (GameObject.IsDrawOk == true) { MessageBox.Show("你还未选择游戏模式,不能开始游戏"); return; } SingleObject.GetObject().T.KeyDown(e); }
public void Fire() { SingleObject.GetObject().AddGameObject(new Bullet(this)); GameObject.Cost++; }
public static void AutoRun() { for (int i = 0; i < TotalStep; i++) { switch (GameObject.AcationPath[i]) { case TankAcationType.ChangeTheDirToUp: MessageBox.Show("向上"); GameObject.Cost++; SingleObject.GetObject().T.Dir = Direction.Up; MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.ChangeTheDirToDown: MessageBox.Show("向下"); GameObject.Cost++; SingleObject.GetObject().T.Dir = Direction.Down; MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.ChangeTheDirToLeft: MessageBox.Show("向左"); GameObject.Cost++; SingleObject.GetObject().T.Dir = Direction.Left; MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.ChangeTheDirToRight: MessageBox.Show("向右"); GameObject.Cost++; SingleObject.GetObject().T.Dir = Direction.Right; MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.FireOneWall: MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.FireTwoWall: MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.MoveStep: MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.ChangeToDownFireOne: MessageBox.Show("向下"); GameObject.Cost++; SingleObject.GetObject().T.Dir = Direction.Down; MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.ChangeToDownFireTwo: MessageBox.Show("向下"); GameObject.Cost++; SingleObject.GetObject().T.Dir = Direction.Down; MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.ChangeToLeftFireOne: MessageBox.Show("向左"); GameObject.Cost++; SingleObject.GetObject().T.Dir = Direction.Left; MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.ChangeToLeftFireTwo: MessageBox.Show("向左"); GameObject.Cost++; SingleObject.GetObject().T.Dir = Direction.Left; MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.ChangeToRightFireOne: MessageBox.Show("向右"); GameObject.Cost++; SingleObject.GetObject().T.Dir = Direction.Right; MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.ChangeToRightFireTwo: MessageBox.Show("向右"); GameObject.Cost++; SingleObject.GetObject().T.Dir = Direction.Right; MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.ChangeToUpFireOne: MessageBox.Show("向上"); GameObject.Cost++; SingleObject.GetObject().T.Dir = Direction.Up; MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; case TankAcationType.ChangeToUpFireTwo: MessageBox.Show("向上"); GameObject.Cost++; SingleObject.GetObject().T.Dir = Direction.Up; MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("开火"); SingleObject.GetObject().T.Fire(); MessageBox.Show("向前"); SingleObject.GetObject().T.Move(); break; } } }
/// <summary> /// 响应鼠标事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MainForm_MouseDown(object sender, MouseEventArgs e) { //判断现在当前是否允许绘制 if (GameObject.IsDrawOk == false) { return; } if (Type == OperType.None) { MessageBox.Show("请选择你要绘制的类型!"); } //获取格子坐标 int GridX = e.X / GridWidth; int GridY = (e.Y - Tank.LeftUpY) / GridWidth; //如果该点出现在坦克或是星星上,则return if (GridX == GameObject.StartPoint.X && GridY == GameObject.StartPoint.Y || GridX == GameObject.EndPoint.X && GridY == GameObject.EndPoint.Y) { return; } //判断该点是否还能放置 if (GameObject.IsVisit[GridX, GridY] == false) { return; } //获取该点坐标 int MouseX = GridX * GridWidth; int MouseY = GridY * GridWidth + Tank.LeftUpY; switch (Type) { case OperType.DrawTank: GameObject.StartPoint.X = GridX; GameObject.StartPoint.Y = GridY; GameObject.InitMap[GridX, GridY] = 'T'; SingleObject.GetObject().AddGameObject(new Tank(MouseX, MouseY, Direction.Up)); Type = OperType.None; break; case OperType.DrawBrick: GameObject.InitMap[GridX, GridY] = 'B'; GameObject.IsVisit[GridX, GridY] = false; SingleObject.GetObject().AddGameObject(new Brick(MouseX, MouseY)); break; case OperType.DrawSteel: GameObject.InitMap[GridX, GridY] = 'S'; GameObject.IsVisit[GridX, GridY] = false; SingleObject.GetObject().AddGameObject(new Stell(MouseX, MouseY)); break; case OperType.DrawRiver: GameObject.InitMap[GridX, GridY] = 'R'; GameObject.IsVisit[GridX, GridY] = false; SingleObject.GetObject().AddGameObject(new River(MouseX, MouseY)); break; case OperType.DrawStar: GameObject.InitMap[GridX, GridY] = 'X'; GameObject.EndPoint.X = GridX; GameObject.EndPoint.Y = GridY; SingleObject.GetObject().AddGameObject(new Star(MouseX, MouseY)); Type = OperType.None; break; } //保存IsVisit for (int i = 0; i < GridNum; i++) { for (int j = 0; j < GridNum; j++) { GameObject.InitIsVisit[i, j] = GameObject.IsVisit[i, j]; } } }