首页 > 编程语言 >C#WinForm开发坦克大战

C#WinForm开发坦克大战

时间:2023-04-29 14:55:06浏览次数:39  
标签:Direction C# void 大战 int static public Resources WinForm

 1 using System.Drawing;
 2 using System.Threading;
 3 using System.Windows.Forms;
 4 
 5 /// <summary>
 6 /// 窗体
 7 /// </summary>
 8 namespace _03_WinForm
 9 {
10     public partial class Form1 : Form
11     {
12         //子线程
13         private Thread t;
14         //窗体画布
15         private static Graphics windowG;
16         //场景图片(临时画布)
17         private static Bitmap tempBmp;
18 
19         public Form1()
20         {
21             InitializeComponent();
22             this.StartPosition = FormStartPosition.CenterScreen;
23         }
24 
25         private void Form1_Paint(object sender, PaintEventArgs e)
26         {
27             windowG = CreateGraphics();
28             //将场景绘制到临时画布上,再将临时画布绘制到窗体画布上,解决画面闪烁
29             tempBmp = new Bitmap(450, 450);
30             Graphics bmpG = Graphics.FromImage(tempBmp);
31             GameFramework.graphics = bmpG;
32             //创建子线程
33             t = new Thread(delegate () { GameMainThread(); }, 1073741824);//防止递归调用的方法会内存溢出
34 t.Start(); 35 } 36 37 //子线程 38 public static void GameMainThread() 39 { 40 //GameFramework 41 GameFramework.Start(); 42 //更新时间 43 int sleepTime = 1000 / 60; 44 //每秒调用60次 45 while (true) 46 { 47 //清空画布 48 GameFramework.graphics.Clear(Color.Black); 49 //重新刷新 50 GameFramework.Update(); 51 //重新绘制 52 windowG.DrawImage(tempBmp, 0, 0); 53 //间隔 54 Thread.Sleep(sleepTime); 55 } 56 } 57 58 //窗口关闭后终止线程 59 private void Form1_FormClosed(object sender, FormClosedEventArgs e) 60 { 61 t.Abort(); 62 } 63 64 //点击按键 65 private void Form1_KeyDown(object sender, KeyEventArgs e) 66 { 67 GameObjectManager.KeyDown(e); 68 } 69 70 //抬起按键 71 private void Form1_KeyUp(object sender, KeyEventArgs e) 72 { 73 GameObjectManager.KeyUp(e); 74 } 75 } 76 }
 1 using _03_WinForm.Properties;
 2 using System.Drawing;
 3 
 4 /// <summary>
 5 /// 游戏框架
 6 /// </summary>
 7 namespace _03_WinForm
 8 {
 9     enum GameState
10     { 
11         Running,
12         GameOver
13     }
14 
15     class GameFramework
16     {
17         private static GameState gameState = GameState.Running;
18         public static Graphics graphics;
19         public static void Start()
20         {
21             //声音初始化
22             SoundManager.SoundInit();
23             //播放背景音乐
24             SoundManager.PlayStart();
25             //初始化敌人位置
26             GameObjectManager.Start();
27             //初始化创建地图
28             GameObjectManager.CreateMap();
29             //创建玩家坦克
30             GameObjectManager.CreateMyTank();
31         }
32 
33         public static void Update()
34         {
35             //实时刷新场景元素
36             if (gameState == GameState.Running)
37             {
38                 GameObjectManager.Update();
39             }
40             else
41             {//游戏结束界面
42                 graphics.DrawImage(Resources.GameOver, new Point(450 / 2 - Resources.GameOver.Width / 2, 450 / 2 - Resources.GameOver.Height / 2));
43             }
44         }
45 
46         public static void GameOver()
47         {
48             gameState = GameState.GameOver;
49         }
50     }
51 }
 1 using _03_WinForm.Properties;
 2 using System.Media;
 3 
 4 /// <summary>
 5 /// 声音管理
 6 /// </summary>
 7 namespace _03_WinForm
 8 {
 9     class SoundManager
10     {
11         private static SoundPlayer start = new SoundPlayer();
12         private static SoundPlayer add = new SoundPlayer();
13         private static SoundPlayer hit = new SoundPlayer();
14         private static SoundPlayer blast = new SoundPlayer();
15         private static SoundPlayer fire = new SoundPlayer();
16 
17         public static void SoundInit()
18         {
19             start.Stream = Resources.start;
20             add.Stream = Resources.add;
21             hit.Stream = Resources.hit;
22             blast.Stream = Resources.blast;
23             fire.Stream = Resources.fire;
24         }
25 
26         //播放背景音乐
27         public static void PlayStart()
28         {
29             start.PlayLooping();
30         }
31 
32         //添加玩家和敌人
33         public static void Add()
34         {
35             add.Play();
36         }
37 
38         //玩家受伤
39         public static void Hit()
40         {
41             hit.Play();
42         }
43 
44         //爆炸
45         public static void Blast()
46         {
47             blast.Play();
48         }
49 
50         //射击
51         public static void Fire()
52         {
53             fire.Play();
54         }
55     }
56 }
  1 using _03_WinForm.Properties;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Drawing;
  5 using System.Windows.Forms;
  6 
  7 /// <summary>
  8 /// 场景管理
  9 /// </summary>
 10 namespace _03_WinForm
 11 {
 12     class GameObjectManager
 13     {
 14         //场景元素集合
 15         private static List<GameObject> objList = new List<GameObject>();
 16         //墙集合
 17         private static List<NotMovething> wallList = new List<NotMovething>();
 18         //钢铁墙集合
 19         private static List<NotMovething> steelList = new List<NotMovething>();
 20         //boss
 21         private static NotMovething boss;
 22         //玩家
 23         private static MyTank myTank;
 24         //敌人生成速度
 25         private static int EnemyBornSpeed = 60;
 26         //计数器
 27         private static int EnemyBornCount = 60;
 28         //敌人位置数组
 29         private static Point[] points = new Point[3];
 30         //敌人集合
 31         private static List<EnemyTank> enemyList = new List<EnemyTank>();
 32         //子弹集合
 33         private static List<Bullet> bulletList = new List<Bullet>();
 34         //特效集合
 35         private static List<Explosion> explosionList = new List<Explosion>();
 36 
 37         public static void Start()
 38         {
 39             points[0].X = 0;
 40             points[0].Y = 0;
 41             points[1].X = 7 * 30;
 42             points[1].Y = 0;
 43             points[2].X = 14 * 30;
 44             points[2].Y = 0;
 45         }
 46 
 47         public static void Update()
 48         {
 49             foreach (GameObject item in objList)
 50             {
 51                 item.Update();
 52             }
 53             foreach (GameObject item in bulletList)
 54             {
 55                 item.Update();
 56             }
 57             foreach (GameObject item in explosionList)
 58             {
 59                 item.Update();
 60             }
 61             EnemyBorn();
 62             CheckDestroyBullet();
 63             CheckDestroyExp();
 64         }
 65 
 66         //检测是否销毁子弹
 67         private static void CheckDestroyBullet()
 68         {
 69             List<Bullet> bullets = new List<Bullet>(); 
 70             foreach (Bullet item in bulletList)
 71             {
 72                 if (item.bDestroy)
 73                     bullets.Add(item);
 74             }
 75             foreach (Bullet item in bullets)
 76             {
 77                 bulletList.Remove(item);
 78             }
 79         }
 80 
 81         //检测是否销毁特效
 82         public static void CheckDestroyExp()
 83         {
 84             List<Explosion> explosions = new List<Explosion>();
 85             foreach (Explosion item in explosionList)
 86             {
 87                 if (item.bDestroy)
 88                     explosions.Add(item);
 89             }
 90             foreach (Explosion item in explosions)
 91             {
 92                 explosionList.Remove(item);
 93             }
 94         }
 95 
 96         //销毁墙
 97         public static void DestroyWall(NotMovething wall)
 98         {
 99             objList.Remove(wall);
100             wallList.Remove(wall);
101         }
102 
103         //销毁敌人
104         public static void DestroyEnemy(EnemyTank enemy)
105         {
106             objList.Remove(enemy);
107             enemyList.Remove(enemy);
108         }
109 
110         //销毁玩家
111         public static void DestroyPlayer()
112         {
113             objList.Remove(myTank);
114         }
115 
116         //销毁boss
117         public static void DestroyBoss()
118         {
119             objList.Remove(boss);
120         }
121 
122         //创建地图
123         public static void CreateMap()
124         {
125             CreateWall(1, 1, 4, Resources.wall);
126             CreateWall(3, 1, 4, Resources.wall);
127             CreateWall(5, 1, 4, Resources.wall);
128             CreateWall(7, 1, 4, Resources.wall);
129             CreateSteel(7, 7, 1, Resources.steel);
130             CreateWall(9, 1, 4, Resources.wall);
131             CreateWall(11, 1, 4, Resources.wall);
132             CreateWall(13, 1, 4, Resources.wall);
133 
134             CreateSteel(2, 7, 1, Resources.steel);
135             CreateWall(3, 7, 1, Resources.wall);
136             CreateWall(6, 7, 1, Resources.wall);
137             CreateWall(8, 7, 1, Resources.wall);
138             CreateWall(11, 7, 1, Resources.wall);
139             CreateSteel(12, 7, 1, Resources.steel);
140 
141             CreateWall(1, 10, 4, Resources.wall);
142             CreateWall(3, 10, 4, Resources.wall);
143             CreateWall(5, 10, 2, Resources.wall);
144             CreateWall(6, 10, 1, Resources.wall);
145             CreateWall(7, 10, 1, Resources.wall);
146             CreateWall(8, 10, 1, Resources.wall);
147             CreateWall(9, 10, 2, Resources.wall);
148             CreateWall(11, 10, 4, Resources.wall);
149             CreateWall(13, 10, 4, Resources.wall);
150 
151             CreateWall(6, 13, 2, Resources.wall);
152             CreateWall(7, 13, 1, Resources.wall);
153             CreateBoss(7, 14, Resources.Boss);
154             CreateWall(8, 13, 2, Resources.wall);
155         }
156 
157         //创建墙
158         public static void CreateWall(int x, int y, int count, Image img)
159         {
160             int xPosition = x * 30;
161             int yPosition = y * 30;
162             for (int i = yPosition; i < yPosition + count * 30; i += 15)
163             {
164                 NotMovething item1 = new NotMovething(xPosition, i, img);
165                 NotMovething item2 = new NotMovething(xPosition + 15, i, img);
166                 objList.Add(item1);
167                 objList.Add(item2);
168                 wallList.Add(item1);
169                 wallList.Add(item2);
170             }
171         }
172 
173         //创建钢铁墙
174         public static void CreateSteel(int x, int y, int count, Image img)
175         {
176             int xPosition = x * 30;
177             int yPosition = y * 30;
178             for (int i = yPosition; i < yPosition + count * 30; i += 15)
179             {
180                 NotMovething item1 = new NotMovething(xPosition, i, img);
181                 NotMovething item2 = new NotMovething(xPosition + 15, i, img);
182                 objList.Add(item1);
183                 objList.Add(item2);
184                 steelList.Add(item1);
185                 steelList.Add(item2);
186             }
187         }
188 
189         //创建Boss
190         public static void CreateBoss(int x, int y, Image img)
191         {
192             int xPosition = x * 30;
193             int yPosition = y * 30;
194             boss = new NotMovething(xPosition, yPosition, img);
195             objList.Add(boss);
196         }
197 
198 
199         //创建玩家坦克
200         public static void CreateMyTank()
201         {
202             int xPosition = 5 * 30;
203             int yPosition = 14 * 30;
204             myTank = new MyTank(xPosition, yPosition, 5);
205             objList.Add(myTank);
206         }
207 
208         //创建子弹
209         public static void CreateBullet(int x, int y, Direction dir, Tag tag)
210         {
211             SoundManager.Fire();
212             Bullet bullet = new Bullet(x, y, 10, dir, tag);
213             bulletList.Add(bullet);
214         }
215 
216         //创建爆炸特效
217         public static void CreateExplosion(int x, int y)
218         {
219             Explosion explosion = new Explosion(x, y);
220             explosionList.Add(explosion);
221         }
222 
223         #region 创建敌人
224         private static void EnemyBorn()
225         {
226             EnemyBornCount++;
227             if (EnemyBornCount < EnemyBornSpeed) return;
228             Random rd = new Random();
229             int index = rd.Next(0, 3);
230             Point point = points[index];
231             Random rd1 = new Random();
232             int enemyType = rd1.Next(1, 5);
233             switch (enemyType)
234             {
235                 case 1:
236                     CreateEnemyTank1(point.X,point.Y);
237                     break;
238                 case 2:
239                     CreateEnemyTank2(point.X, point.Y);
240                     break;
241                 case 3:
242                     CreateEnemyTank3(point.X, point.Y);
243                     break;
244                 case 4:
245                     CreateEnemyTank4(point.X, point.Y);
246                     break;
247             }
248             EnemyBornCount = 0;
249         }
250 
251         private static void CreateEnemyTank1(int x, int y)
252         {
253             EnemyTank enemy = new EnemyTank(x,y,5,Resources.YellowUp,Resources.YellowDown, Resources.YellowLeft, Resources.YellowRight);
254             objList.Add(enemy);
255             enemyList.Add(enemy);
256         }
257 
258         private static void CreateEnemyTank2(int x, int y)
259         {
260             EnemyTank enemy = new EnemyTank(x, y, 4, Resources.GrayUp, Resources.GrayDown, Resources.GrayLeft, Resources.GrayRight);
261             objList.Add(enemy);
262             enemyList.Add(enemy);
263         }
264 
265         private static void CreateEnemyTank3(int x, int y)
266         {
267             EnemyTank enemy = new EnemyTank(x, y, 3, Resources.GreenUp, Resources.GreenDown, Resources.GreenLeft, Resources.GreenRight);
268             objList.Add(enemy);
269             enemyList.Add(enemy);
270         }
271 
272         private static void CreateEnemyTank4(int x, int y)
273         {
274             EnemyTank enemy = new EnemyTank(x, y, 2, Resources.QuickUp, Resources.QuickDown, Resources.QuickLeft, Resources.QuickRight);
275             objList.Add(enemy);
276             enemyList.Add(enemy);
277         }
278         #endregion
279 
280         #region 碰撞检测
281         //是否跟红墙发生碰撞
282         public static NotMovething IsCollidedWall(Rectangle rt)
283         {
284             foreach (NotMovething wall in wallList)
285             {
286                 if (wall.GetRectangle().IntersectsWith(rt))
287                 {
288                     return wall;
289                 }
290             }
291             return null;
292         }
293 
294         //是否跟钢铁墙发生碰撞
295         public static NotMovething IsCollidedSteel(Rectangle rt)
296         {
297             foreach (NotMovething steel in steelList)
298             {
299                 if (steel.GetRectangle().IntersectsWith(rt))
300                 {
301                     return steel;
302                 }
303             }
304             return null;
305         }
306 
307         //是否跟玩家发生碰撞
308         public static Movething IsCollidedPlayer(Rectangle rt)
309         {
310             if (myTank.GetRectangle().IntersectsWith(rt))
311             {
312                 return myTank;
313             }
314             return null;
315         }
316 
317         //是否跟敌人发生碰撞
318         public static Movething IsCollidedEnemy(Rectangle rt)
319         {
320             foreach (Movething enemy in enemyList)
321             {
322                 if (enemy.GetRectangle().IntersectsWith(rt))
323                 {
324                     return enemy;
325                 }
326             }
327             return null;
328         }
329 
330         //是否跟子弹发生碰撞
331         public static Bullet IsCollidedBullet(Rectangle rt)
332         {
333             foreach (Bullet bullet in bulletList)
334             {
335                 if (bullet.GetRectangle().IntersectsWith(rt))
336                 {
337                     return bullet;
338                 }
339             }
340             return null;
341         }
342 
343         //是否跟Boss发生碰撞
344         public static NotMovething IsCollidedBoss(Rectangle rt)
345         {
346             if (boss.GetRectangle().IntersectsWith(rt))
347             {
348                 return boss;
349             }
350             return null;
351         }
352         #endregion
353 
354         //点击触发
355         public static void KeyDown(KeyEventArgs e)
356         {
357             myTank.KeyDown(e);
358         }
359 
360         //抬起触发
361         public static void KeyUp(KeyEventArgs e)
362         {
363             myTank.KeyUp(e);
364         }
365     }
366 }
 1 using System.Drawing;
 2 
 3 /// <summary>
 4 /// 场景物体基类
 5 /// </summary>
 6 namespace _03_WinForm
 7 {
 8     abstract class GameObject
 9     {
10         public int X { get; set; }
11         public int Y { get; set; }
12 
13         public int width { get; set; }
14         public int height { get; set; }
15 
16         protected abstract Image GetImage();
17 
18         public virtual void DrawSelf()
19         {
20             Graphics graphics = GameFramework.graphics;
21             graphics.DrawImage(GetImage(), X, Y);
22         }
23 
24         public virtual void Update()
25         {
26             DrawSelf();
27         }
28 
29         public Rectangle GetRectangle()
30         {
31             Rectangle rectangle = new Rectangle(X, Y, width, height);
32             return rectangle;
33         }
34     }
35 }
 1 using System.Drawing;
 2 
 3 /// <summary>
 4 /// 可移动物体
 5 /// </summary>
 6 namespace _03_WinForm
 7 {
 8     enum Direction
 9     {
10         Up = 0,
11         Down = 1,
12         Left = 2,
13         Right = 3
14     }
15 
16     class Movething : GameObject
17     {
18         private object _lock = new object();
19         public Bitmap BitmapUp { get; set; }
20         public Bitmap BitmapDown { get; set; }
21         public Bitmap BitmapLeft { get; set; }
22         public Bitmap BitmapRight { get; set; }
23 
24         public int Speed { get; set; }
25         private Direction dir { get; set; }
26         public Direction Dir { 
27             get 
28             {
29                 return dir; 
30             }
31             set
32             { 
33                 dir = value;
34                 Bitmap bmp = null;
35                 switch (dir)
36                 {
37                     case Direction.Up:
38                         bmp = BitmapUp;
39                         break;
40                     case Direction.Down:
41                         bmp = BitmapDown;
42                         break;
43                     case Direction.Left:
44                         bmp = BitmapLeft;
45                         break;
46                     case Direction.Right:
47                         bmp = BitmapRight;
48                         break;
49                 }
50                 lock (_lock)
51                 {//加锁解决资源冲突
52                     width = bmp.Width;
53                     height = bmp.Height;
54                 }
55             }
56         }
57 
58         public override void DrawSelf()
59         {
60             lock (_lock)
61             {//加锁解决资源冲突
62                 base.DrawSelf();
63             }
64         }
65 
66         protected override Image GetImage()
67         {
68             Bitmap bitmap = null;
69             switch (Dir)
70             {
71                 case Direction.Up:
72                     bitmap = BitmapUp;
73                     break;
74                 case Direction.Down:
75                     bitmap = BitmapDown;
76                     break;
77                 case Direction.Left:
78                     bitmap = BitmapLeft;
79                     break;
80                 case Direction.Right:
81                     bitmap = BitmapRight;
82                     break;
83                 default:
84                     bitmap = BitmapUp;
85                     break;
86             }
87             bitmap.MakeTransparent(Color.Black);
88             return bitmap;
89         }
90     }
91 }
 1 using System.Drawing;
 2 
 3 /// <summary>
 4 /// 不可移动物体
 5 /// </summary>
 6 namespace _03_WinForm
 7 {
 8     class NotMovething : GameObject
 9     {
10         private Image img { get; set; }
11         public Image Img
12         {
13             get 
14             { 
15                 return img; 
16             }
17             set
18             {
19                 img = value; 
20                 width = img.Width;
21                 height = img.Height;
22             }
23         }
24 
25         protected override Image GetImage()
26         {
27             return Img;
28         }
29 
30         public NotMovething(int x, int y, Image img)
31         {
32             this.X = x;
33             this.Y = y;
34             this.Img = img;
35         }
36     }
37 }
  1 using _03_WinForm.Properties;
  2 using System.Drawing;
  3 using System.Windows.Forms;
  4 
  5 /// <summary>
  6 /// 玩家类
  7 /// </summary>
  8 namespace _03_WinForm
  9 {
 10     class MyTank:Movething
 11     {
 12         private bool bMove { get; set; }
 13         public int Hp { get; set; }
 14         public MyTank(int x, int y, int speed)
 15         {
 16             this.bMove = false;
 17             this.Hp = 1;
 18             this.X = x;
 19             this.Y = y;
 20             this.Speed = speed;
 21             BitmapUp = Resources.MyTankUp;
 22             BitmapDown = Resources.MyTankDown;
 23             BitmapLeft = Resources.MyTankLeft;
 24             BitmapRight = Resources.MyTankRight;
 25             this.Dir = Direction.Up;
 26         }
 27 
 28         public override void Update()
 29         {
 30             base.Update();
 31             Check();
 32             Move();
 33         }
 34 
 35         //检测是否可移动
 36         private void Check()
 37         {
 38             #region 碰撞检测
 39             Rectangle rect = GetRectangle();
 40             switch (Dir)
 41             {
 42                 case Direction.Up:
 43                     rect.Y -= Speed;
 44                     break;
 45                 case Direction.Down:
 46                     rect.Y += Speed;
 47                     break;
 48                 case Direction.Left:
 49                     rect.X -= Speed;
 50                     break;
 51                 case Direction.Right:
 52                     rect.X += Speed;
 53                     break;
 54             }
 55             if (GameObjectManager.IsCollidedWall(rect) != null)
 56             {
 57                 this.bMove = false; return;
 58             }
 59             if (GameObjectManager.IsCollidedSteel(rect) != null)
 60             {
 61                 this.bMove = false; return;
 62             }
 63             if (GameObjectManager.IsCollidedBoss(rect) != null)
 64             {
 65                 this.bMove = false; return;
 66             }
 67             if (GameObjectManager.IsCollidedEnemy(rect) != null)
 68             {
 69                 this.bMove = false; return;
 70             }
 71             #endregion
 72 
 73             #region 检测是否超出窗体边界
 74             if (this.Dir == Direction.Left)
 75             {
 76                 if (this.X - Speed < 0)
 77                     this.bMove = false; return;
 78             }
 79             else if (this.Dir == Direction.Right)
 80             {
 81                 if (this.X + Speed + width > 450)
 82                     this.bMove = false; return;
 83             }
 84             else if (this.Dir == Direction.Up)
 85             {
 86                 if (this.Y - Speed < 0)
 87                     this.bMove = false; return;
 88             }
 89             else if (this.Dir == Direction.Down)
 90             {
 91                 if (this.Y + Speed + height > 450)
 92                     this.bMove = false; return;
 93             }
 94             #endregion
 95         }
 96 
 97         //移动
 98         private void Move()
 99         {
100             if (bMove)
101             {
102                 if (this.Dir == Direction.Left)
103                 {
104                     this.X -= Speed;
105                 }
106                 if (this.Dir == Direction.Right)
107                 {
108                     this.X += Speed;
109                 }
110                 if (this.Dir == Direction.Up)
111                 {
112                     this.Y -= Speed;
113                 }
114                 if (this.Dir == Direction.Down)
115                 {
116                     this.Y += Speed;
117                 }
118             }
119         }
120 
121         //点击触发
122         public void KeyDown(KeyEventArgs e)
123         {
124             if (e.KeyCode == Keys.A)
125             {
126                 bMove = true;
127                 this.Dir = Direction.Left;
128             }
129             if (e.KeyCode == Keys.D)
130             {
131                 bMove = true;
132                 this.Dir = Direction.Right;
133             }
134             if (e.KeyCode == Keys.W)
135             {
136                 bMove = true;
137                 this.Dir = Direction.Up;
138             }
139             if (e.KeyCode == Keys.S)
140             {
141                 bMove = true;
142                 this.Dir = Direction.Down;
143             }
144             if (e.KeyCode == Keys.Space)
145             {//发射子弹
146                 int x = this.X;
147                 int y = this.Y;
148                 switch (this.Dir)
149                 {
150                     case Direction.Up:
151                         x += width / 2;
152                         break;
153                     case Direction.Down:
154                         x += width / 2;
155                         y += height;
156                         break;
157                     case Direction.Left:
158                         y += height / 2;
159                         break;
160                     case Direction.Right:
161                         x += width;
162                         y += height / 2;
163                         break;
164                     default:
165                         break;
166                 }
167                 GameObjectManager.CreateBullet(x, y, this.Dir, Tag.MyTank);
168             }
169         }
170 
171         //抬起触发
172         public void KeyUp(KeyEventArgs e)
173         {
174             bMove = false;
175         }
176     }
177 }
  1 using System;
  2 using System.Drawing;
  3 
  4 /// <summary>
  5 /// 敌人类
  6 /// </summary>
  7 namespace _03_WinForm
  8 {
  9     class EnemyTank:Movething
 10     {
 11         private Random rd = new Random();
 12 
 13         //攻击间隔
 14         public int attackSpeed { get; set; }
 15         private int attackCount = 0;
 16 
 17         //随机转向间隔
 18         public int turnSpeed { get; set; }
 19         private int turnCount = 0;
 20 
 21         public EnemyTank(int x, int y, int speed, Bitmap bitmapUp, Bitmap bitmapDown, Bitmap bitmapLeft, Bitmap bitmapRight)
 22         {
 23             this.X = x;
 24             this.Y = y;
 25             this.Speed = speed;
 26             BitmapUp = bitmapUp;
 27             BitmapDown = bitmapDown;
 28             BitmapLeft = bitmapLeft;
 29             BitmapRight = bitmapRight;
 30             this.Dir = Direction.Down;
 31             attackSpeed = 60;
 32             turnSpeed = 60;
 33             SoundManager.Add();
 34         }
 35 
 36         public override void Update()
 37         {
 38             base.Update();
 39             Check();
 40             Move();
 41             AttackCheck();
 42             TurnCheck();
 43         }
 44 
 45         //检测是否攻击
 46         private void AttackCheck()
 47         {
 48             attackCount++;
 49             if (attackCount >= attackSpeed)
 50             {
 51                 Attack();
 52                 attackCount = 0;
 53             }
 54         }
 55 
 56         //检测是否随机转向
 57         private void TurnCheck()
 58         {
 59             turnCount++;
 60             if (turnCount >= turnSpeed)
 61             {
 62                 ChangeDirection();
 63                 turnCount = 0;
 64             }
 65         }
 66 
 67         //检测是否可移动
 68         private void Check()
 69         {
 70             #region 碰撞检测
 71             Rectangle rect = GetRectangle();
 72             switch (Dir)
 73             {
 74                 case Direction.Up:
 75                     rect.Y -= Speed;
 76                     break;
 77                 case Direction.Down:
 78                     rect.Y += Speed;
 79                     break;
 80                 case Direction.Left:
 81                     rect.X -= Speed;
 82                     break;
 83                 case Direction.Right:
 84                     rect.X += Speed;
 85                     break;
 86             }
 87             if (GameObjectManager.IsCollidedWall(rect) != null)
 88             {
 89                 ChangeDirection(); return;
 90             }
 91             if (GameObjectManager.IsCollidedSteel(rect) != null)
 92             {
 93                 ChangeDirection(); return;
 94             }
 95             if (GameObjectManager.IsCollidedBoss(rect) != null)
 96             {
 97                 ChangeDirection(); return;
 98             }
 99             if (GameObjectManager.IsCollidedPlayer(rect) != null)
100             {
101                 ChangeDirection(); return;
102             }
103             #endregion
104 
105             #region 检测是否超出窗体边界
106             if (this.Dir == Direction.Left)
107             {
108                 if (this.X - Speed < 0)
109                 {
110                     ChangeDirection();return;
111                 }
112             }
113             else if (this.Dir == Direction.Right)
114             {
115                 if (this.X + Speed + width > 450)
116                 {
117                     ChangeDirection(); return;
118                 }
119             }
120             else if (this.Dir == Direction.Up)
121             {
122                 if (this.Y - Speed < 0)
123                 {
124                     ChangeDirection(); return;
125                 }
126             }
127             else if (this.Dir == Direction.Down)
128             {
129                 if (this.Y + Speed + height > 450)
130                 {
131                     ChangeDirection(); return;
132                 }
133             }
134             #endregion
135         }
136 
137         //改变朝向
138         private void ChangeDirection()
139         {
140             while (true)
141             {
142                 Direction dir = (Direction)rd.Next(0, 4);
143                 if (dir == Dir)
144                 {
145                     continue;
146                 }
147                 else
148                 {
149                     Dir = dir;
150                     break;
151                 }
152             }
153             Check();
154         }
155 
156         //移动
157         private void Move()
158         {
159             if (this.Dir == Direction.Left)
160             {
161                 this.X -= Speed;
162             }
163             if (this.Dir == Direction.Right)
164             {
165                 this.X += Speed;
166             }
167             if (this.Dir == Direction.Up)
168             {
169                 this.Y -= Speed;
170             }
171             if (this.Dir == Direction.Down)
172             {
173                 this.Y += Speed;
174             }
175         }
176 
177         //攻击
178         private void Attack()
179         {
180             int x = this.X;
181             int y = this.Y;
182             switch (this.Dir)
183             {
184                 case Direction.Up:
185                     x += width / 2;
186                     break;
187                 case Direction.Down:
188                     x += width / 2;
189                     y += height;
190                     break;
191                 case Direction.Left:
192                     y += height / 2;
193                     break;
194                 case Direction.Right:
195                     x += width;
196                     y += height / 2;
197                     break;
198                 default:
199                     break;
200             }
201             GameObjectManager.CreateBullet(x, y, this.Dir, Tag.EnemyTank);
202         }
203     }
204 }
  1 using _03_WinForm.Properties;
  2 using System.Drawing;
  3 
  4 /// <summary>
  5 /// 子弹类
  6 /// </summary>
  7 namespace _03_WinForm
  8 {
  9     enum Tag
 10     {
 11         MyTank,
 12         EnemyTank
 13     }
 14 
 15     class Bullet : Movething
 16     {
 17         public Tag tag { get; set; }
 18         public bool bDestroy { get; set; }
 19 
 20         public Bullet(int x, int y, int speed, Direction dir,Tag tag)
 21         {
 22             this.X = x;
 23             this.Y = y;
 24             this.Speed = speed;
 25             BitmapUp = Resources.BulletUp;
 26             BitmapDown = Resources.BulletDown;
 27             BitmapLeft = Resources.BulletLeft;
 28             BitmapRight = Resources.BulletRight;
 29             this.Dir = dir;
 30             this.tag = tag;
 31             //位置修正,减去图片大小一半
 32             this.X -= width / 2;
 33             this.Y -= height / 2;
 34             bDestroy = false;
 35         }
 36 
 37         public override void Update()
 38         {
 39             base.Update();
 40             Check();
 41             Move();
 42         }
 43 
 44         //检测
 45         private void Check()
 46         {
 47             #region 碰撞检测
 48             Rectangle rect = GetRectangle();
 49             rect.X += width / 2 - 3;
 50             rect.Y += height / 2 - 3;
 51             rect.Width = 3;
 52             rect.Height = 3;
 53             switch (Dir)
 54             {
 55                 case Direction.Up:
 56                     rect.Y -= Speed;
 57                     break;
 58                 case Direction.Down:
 59                     rect.Y += Speed;
 60                     break;
 61                 case Direction.Left:
 62                     rect.X -= Speed;
 63                     break;
 64                 case Direction.Right:
 65                     rect.X += Speed;
 66                     break;
 67             }
 68             NotMovething wall = null;
 69             Movething enemy = null;
 70             Bullet bullet = null;
 71             Movething myTank = null;
 72             int _expX = this.X + width / 2;
 73             int _expY = this.Y + height / 2;
 74             if ((wall = GameObjectManager.IsCollidedWall(rect)) != null)
 75             {
 76                 //音效
 77                 SoundManager.Blast();
 78                 //爆炸特效
 79                 GameObjectManager.CreateExplosion(_expX, _expY);
 80                 //移除红墙
 81                 GameObjectManager.DestroyWall(wall);
 82                 bDestroy = true; return;
 83             }
 84             if (GameObjectManager.IsCollidedSteel(rect) != null)
 85             {
 86                 //音效
 87                 SoundManager.Blast();
 88                 //爆炸特效
 89                 GameObjectManager.CreateExplosion(_expX, _expY);
 90                 bDestroy = true; return;
 91             }
 92             if (GameObjectManager.IsCollidedBoss(rect) != null)
 93             {
 94                 //音效
 95                 SoundManager.Blast();
 96                 //爆炸特效
 97                 GameObjectManager.CreateExplosion(_expX, _expY);
 98                 //移除boss
 99                 GameObjectManager.DestroyBoss();
100                 //游戏结束
101                 GameFramework.GameOver();
102                 bDestroy = true; return;
103             }
104             if ((enemy = GameObjectManager.IsCollidedEnemy(rect)) != null)
105             {
106                 //音效
107                 SoundManager.Blast();
108                 //移除敌人
109                 GameObjectManager.CreateExplosion(_expX, _expY);
110                 //爆炸特效
111                 GameObjectManager.DestroyEnemy((EnemyTank)enemy);
112                 bDestroy = true; return;
113             }
114             if ((myTank = GameObjectManager.IsCollidedPlayer(rect)) != null)
115             {
116                 if (this.tag == Tag.EnemyTank)
117                 {
118                     //爆炸特效
119                     GameObjectManager.CreateExplosion(_expX, _expY);
120                     //音效
121                     SoundManager.Hit();
122                     //扣血
123                     MyTank my = (MyTank)myTank;
124                     my.Hp--;
125                     if (my.Hp <= 0)
126                     {
127                         //移除玩家
128                         GameObjectManager.DestroyPlayer();
129                         //重生
130                         GameObjectManager.CreateMyTank();
131                     }
132                     bDestroy = true; return;
133                 }
134             }
135             if ((bullet = GameObjectManager.IsCollidedBullet(rect)) != null && GameObjectManager.IsCollidedBullet(rect) != this)
136             {
137                 //音效
138                 SoundManager.Blast();
139                 //爆炸特效
140                 GameObjectManager.CreateExplosion(_expX, _expY);
141                 //子弹相互抵消
142                 bDestroy = true; bullet.bDestroy = true; return;
143             }
144             #endregion
145 
146             #region 检测是否超出窗体边界
147             if (this.Dir == Direction.Left)
148             {
149                 if (this.X + width / 2 - 3 < 0)
150                     bDestroy = true; return;
151             }
152             else if (this.Dir == Direction.Right)
153             {
154                 if (this.X + width / 2 + 3 < 0)
155                     bDestroy = true; return;
156             }
157             else if (this.Dir == Direction.Up)
158             {
159                 if (this.Y + height / 2 + 3 < 0)
160                     bDestroy = true; return;
161             }
162             else if (this.Dir == Direction.Down)
163             {
164                 if (this.Y + height / 2 - 3 < 0)
165                     bDestroy = true; return;
166             }
167             #endregion
168         }
169 
170         //移动
171         private void Move()
172         {
173             if (this.Dir == Direction.Left)
174             {
175                 this.X -= Speed;
176             }
177             if (this.Dir == Direction.Right)
178             {
179                 this.X += Speed;
180             }
181             if (this.Dir == Direction.Up)
182             {
183                 this.Y -= Speed;
184             }
185             if (this.Dir == Direction.Down)
186             {
187                 this.Y += Speed;
188             }
189         }
190     }
191 }
 1 using _03_WinForm.Properties;
 2 using System.Drawing;
 3 
 4 /// <summary>
 5 /// 爆炸特效
 6 /// </summary>
 7 namespace _03_WinForm
 8 {
 9     class Explosion : GameObject
10     {
11         public bool bDestroy { get; set; }
12         private int playSpeed = 1;
13         private int playCount = 0;
14         private int index = 0;
15 
16         public Bitmap[] bmpArray = new Bitmap[]
17             {
18                 Resources.EXP1,
19                 Resources.EXP2,
20                 Resources.EXP3,
21                 Resources.EXP4,
22                 Resources.EXP5,
23             };
24         public Explosion(int x,int y)
25         {
26             foreach (Bitmap item in bmpArray)
27             {
28                 item.MakeTransparent(Color.Black);
29             }
30             this.X = x - bmpArray[0].Width / 2;
31             this.Y = y - bmpArray[0].Height / 2;
32             bDestroy = false;
33         }
34 
35         protected override Image GetImage()
36         {
37             return bmpArray[index];
38         }
39 
40         public override void Update()
41         {
42             playCount++;
43             if (playCount > bmpArray.Length)
44             {
45                 bDestroy = true; return;
46             }
47             index = (playCount - 1) / playSpeed;
48             base.Update();
49         }
50     }
51 }
 

标签:Direction,C#,void,大战,int,static,public,Resources,WinForm
From: https://www.cnblogs.com/zerobeyond/p/17363481.html

相关文章

  • 我刚才用了dynamic_cast 你给我普及一下C++ 中这几种类型转换吧
    我刚才用了dynamic_cast你给我普及一下C++中这几种类型转换吧在C++中,有几种类型转换的方式,包括:隐式转换在一些情况下,编译器会自动进行类型转换。比如将整型变量赋值给浮点型变量,编译器就会自动将整型变量转换为浮点型变量。但是在大多数情况下,使用隐式转换可能会引起一些问......
  • Tool-CMake-list
    Tool-CMake-listhttps://www.visgraf.impa.br/seminar/slides/rodlima_cmake_presentation.pdfUsefultomanagelonglistofelementsElementscanbemanipulateddependingonrunningplatformUsefulforsourcefilelistsExample:set(sourcesviewer.cpp......
  • 《asyncio 系列》2. 详解 asyncio 的协程、任务、future,以及事件循环
    楔子上一篇文章我们深入讨论了并发性,探讨了如何同时使用进程和线程实现并发,还探索了如何利用非阻塞IO和事件循环来实现只使用一个线程的并发性。本篇文章将介绍在asyncio中使用单线程并发模型编写程序的基础知识,使用本文中的技术,你将能执行长时间运行的操作,如Web请求、数据......
  • CF1656F Parametric MST 题解
    为了便于解题,先对\(a\)数组从小到大进行排序。首先,根据定义可以得出总价值的表达式:\[\begin{aligned}W&=\sum\limits_{(u,v)\inE}[a_ua_v+t(a_u+a_v)]\\&=\sum\limits_{(u,v)\inE}a_ua_v+t\sum\limits_{(u,v)\inE}(a_u+a_v)\end{aligned}\]接着,我们需要发现一个比较......
  • Tool-CMake-Own Finder(-I -L -l)-compiling
    Tool-CMake-OwnFinder(-I-L-l)-compilingWhatisafinderWhencompilingapieceofsoftwarewhichlinkstothird­partylibraries,weneedtoknow:Wheretofindthe.hfiles(­Iingcc)Wheretofindthelibraries(.so/.dll/.lib/.dylib/...)(­Lingcc......
  • scipy.signal.butter实现带通滤波器
    fromscipy.signalimportbutter,lfilter#带通滤波器defbutter_bandpass_filter(data,lowcut,highcut,fs,order):low=lowcut*2/fshigh=highcut*2/fsb,a=butter(order,[low,high],btype='bandpass')y=lfilter(b,a,......
  • To build this project, the following workloads must be installed: macos问题的处
    如遇跨平台的NETSDK1147错误的处理方法:【报错提示】NETSDK1147Tobuildthisproject,thefollowingworkloadsmustbeinstalled:macosToinstalltheseworkloads,runthefollowingcommand:dotnetworkloadrestore   KristofferStrube.Blazor.SVGEditor   You......
  • 免费无需魔法会语音聊天的ChatGPT
    今天发现了一个很好的ChatGPT,可以语音聊天,而且免费无需魔法角色目前包括夏洛克、雷电影等等,对话的声调完全模拟了原角色!目前只有英文和日语两种对话,我们可以文字输入或者语音输入,中文即可,系统会语音回答,自带翻译。联系口语交流还是很不错的,目前响应速度很快,无广告。具体的机器人......
  • apollo启动报错java.lang.NoClassDefFoundError PemObjectGenerator
    启动apollo时,报错。java.lang.NoClassDefFoundError:org/bouncycastle/util/io/pem/PemObjectGenerator很明显类没找到,根据经验判断,很可能是maven依赖问题。解决方案apollo版本1.1.4。目前相关的是:org.bouncycastle:bcpkix-jdk15on:jar:1.55org.bouncycastle:bcprov-jdk15on:jar......
  • 毕业设计 医学图像阅读器 DICOM CT MRI 阅读器 三维重建 可视化编程技术及应用
    一、概述     此系统实现了常见VTK四视图,实现了很好的DICOM图像显示,可用于DICOM超声X线CTMR三维重建拾取像素值窗宽窗位像素,距离测量,角度测量,提供源码;并且通过三维重建实现可视化。使用了第三方库VTK,ITK实现分割和生不重建。窗口分为(横断面)、冠状面、矢状......