1、C#坦克游戏详细讲解本游戏主要分4个内容讲解:1、设计坦克类2、设计子弹类3、设计音乐类4、设计窗体类通俗易懂,希望对广大的学生们都有帮助欢迎下载技术qq 1278263100 邮箱*游戏所需图片游戏运行界面如下图:1、设计坦克类:新建一个类文件tank.cs(代码加注释如下)using System;using System.Collections.Generic;using System.Text;using System.Drawing; /addusing System.Collections;/addnamespace 坦克 class Tank private int width;
2、 /坦克的宽度 private int height; /坦克的高度 private int top; /坦克位置的纵坐标 private int left; /坦克位置的横坐标 private int type; /坦克的类型(2-5敌方,6己方) private int direct; /0-上,1-下,2-左,3-右 public ArrayList bList=new ArrayList();/子弹序列 public Tank(int tank_type)/构造函数 Random r = new Random(); this.direct = r.Next(0, 4);/产生03的数
3、this.width = 32; this.height = 32; this.left = r.Next(0, 10);/产生09的数 this.top = r.Next(0, 10);/产生09的数 this.type = tank_type; public int Top/Top属性 get return top; set if (top = 0 & top = 1 & top = 0 & left =0; i-)/遍历子弹序列 /for (int i = 0; i bList.Count;i+ ) bullet t = (bullet)bListi); /移动以前 if (t.Left
4、 9 | t.Top 9) /超出边界 bList.RemoveAt(i); continue;/删除此颗子弹 if (Mapt.Left, t.Top != 0 & Mapt.Left, t.Top!= this.type ) /已遇到坦克和墙等障碍物 bList.RemoveAt(i);/删除此颗子弹 if (t.hitE(Mapt.Left, t.Top)/击中对方坦克 Mapt.Left, t.Top = -1;/此处坦克被打中 continue; t.move();/移动以后 if (t.Left 9 | t.Top 9) /超出边界 bList.RemoveAt(i);contin
5、ue;/删除此颗子弹 if (Mapt.Left, t.Top != 0)/已遇到物体 bList.RemoveAt(i);/删除此颗子弹 if (t.hitE(Mapt.Left, t.Top)/击中对方坦克 Mapt.Left, t.Top = -1;/此处坦克被打中 continue; public void DrawBullet(Graphics g,int ,Map)/画子弹 MoveBullet(ref Map); foreach (bullet t in bList)/遍历子弹序列 t.Draw(g); /public void NewPosition()/产生新坐标 / / R
6、andom r = new Random(); / this.left = r.Next(0, 10);/产生09的数 / this.top = r.Next(0, 10);/产生09的数 / /public bool ComparePosition(Tank s)/比较坦克位置 / / if (this.left = s.left & this.top = s.top) / return true; / else / return false; / 2、设计子弹类新建一个类文件:bullet.csusing System;using System.Collections.Generic;us
7、ing System.Text;using System.Drawing; /addusing System.Collections;/addnamespace 坦克 class bullet private int top; /子弹坐标(Top,Left) private int left; private int direct;/子弹行进方向 private int width=32; private int height = 32; private bool type; /己方子弹true,敌方子弹false public bullet(int type)/ 子弹类构造函数 if (ty
8、pe = 6)/己方 this.type = true; else this.type = false ; public int Top/Top属性 get return top; set top = value; public int Left/Left属性 get return left; set left = value; public int Direct/Direct属性(子弹行进方向) get return direct; set direct = value; public void move() switch (Direct) case 0: Top-; break; case
9、 1: Top+; break; case 2: Left-; break; case 3: Left+; break; public void Draw(Graphics g) Image bulletImage; if(type=true)/己方 bulletImage = Image.FromFile(BMP/missile1.bmp); else bulletImage = Image.FromFile(BMP/missile2.bmp); /得到绘制这个子弹图形的在游戏面板中的矩形区域 Rectangle destRect = new Rectangle(this.left * wi
10、dth, this.top * height, width, height); Rectangle srcRect = new Rectangle(0, 0, width, height); g.DrawImage(bulletImage, destRect, srcRect, GraphicsUnit.Pixel); public bool hitE(int tanktype)/是否击中对方坦克 if (type = false)/敌方子弹 if (tanktype = 2 & tanktype = 5) /坦克的类型(2-5敌方,6己方) return false; else return
11、 true; if (type = true)/己方子弹 if (tanktype = 6)/坦克的类型(2-5敌方,6己方) return false; else return true; return false; 3、设计音乐播放类新建类文件 PlaySound.csusing System;using System.Runtime.InteropServices;namespace 坦1克? / / PlaySound 的?摘a要a说明。 / public class PlaySound private const int SND_SYNC = 0x0; private const i
12、nt SND_ASYNC = 0x1; private const int SND_NODEFAULT = 0x2; private const int SND_LOOP = 0x8; private const int SND_NOSTOP = 0x10; public static void Play(string file) int flags = SND_ASYNC | SND_NODEFAULT; sndPlaySound(file,flags); DllImport(winmm.dll) private extern static int sndPlaySound(string f
13、ile,int uFlags); 4、设计游戏窗体类using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Collections;/addnamespace 坦克 public partial class Form1 : Form public Form1() InitializeComponent(); /p
14、rivate Tank eTanks=new Tank11; private int eCount=0; /敌方坦克数量 private int eMaxCount = 10;/eMaxCount敌方坦克最大量 private string path; /应用程序路径 private Tank eTank; private ArrayList eTanks = new ArrayList(); private int, Map = new int10, 10;/砖块地图 public int, TMap = new int10, 10;/含坦克,砖的地图 private int width=3
15、2; private Tank MyTank=new Tank (6); private int Score = 0; /计分 private void Form1_Load(object sender, System.EventArgs e) pictureBox1.Width = 10 * width; pictureBox1.Height = 10 * width; path = Application.StartupPath; Random r = new Random(); for(int x=0;x10;x+=2) for (int y = 0; y 10; y+=2) /产生0,
16、1数其中代表空地,1代表墙砖 Mapx, y = r.Next(0, 2); Map4, 9 = 0; MyTank.Top = 9; MyTank.Left = 4 ; MyTank.Direct = 0; lblX.Text = X坐标: + MyTank.Left+ Y坐标: + MyTank.Top; private void DragWall(Graphics g)/画游戏地图 Image WallImage = Image.FromFile(BMP/TQ.BMP); for(int x=0;x10;x+) for (int y = 0; y 10; y+) if (Mapx, y
17、= 1) /得到绘制这个墙砖块的在游戏面板中的矩形区域 Rectangle Rect = new Rectangle(x * width, y * width, width, width); g.DrawImage(WallImage, Rect); private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) switch (e.KeyCode) case Keys.Up: /上 if (MyTank.Top = 0 | MapMyTank.Left, MyTank.Top - 1 = 1 | M
18、eet_Tank(MyTank.Left, MyTank.Top - 1)/遇到墙砖或坦克 ;/不动 else if(MyTank.Direct =0)MyTank.Top-; MyTank.Direct = 0; break; case Keys.Down: /下 if (MyTank.Top = 9 | MapMyTank.Left, MyTank.Top + 1 = 1 | Meet_Tank(MyTank.Left, MyTank.Top + 1)/遇到墙砖或坦克 ; /不动 else if (MyTank.Direct = 1) MyTank.Top+; MyTank.Direct
19、= 1; break; case Keys.Left: /左 if (MyTank.Left = 0 | MapMyTank.Left - 1, MyTank.Top = 1 | Meet_Tank(MyTank.Left - 1, MyTank.Top)/遇到墙砖或坦克 ;/不动 else if (MyTank.Direct = 2) MyTank.Left-; MyTank.Direct = 2; break; case Keys.Right: /右 if (MyTank.Left = 9 | MapMyTank.Left + 1, MyTank.Top = 1 | Meet_Tank(M
20、yTank.Left + 1, MyTank.Top)/遇到墙砖或坦克 ;/不动 else if (MyTank.Direct = 3) MyTank.Left+; MyTank.Direct = 3; break; case Keys.Space :/空格发射子弹 MyTank.fire(); break; pictureBox1.Invalidate();/重画游戏面板区域 lblX.Text = X坐标: + MyTank.Left+ Y坐标: + MyTank.Top; private void timer1_Tick(object sender, EventArgs e) forea
21、ch (Tank t in eTanks) switch (t.Direct)/0-上,1-下,2-左,3-右 case 0: /向上 if (t.Top = 0 | Mapt.Left, t.Top - 1 = 1 |Meet_Tank(t.Left, t.Top - 1)/遇到墙砖或坦克 t.newDirect();/坦克转向 else t.Top-; break; case 1:/向下 if (t.Top=9 |Mapt.Left, t.Top + 1 = 1 | Meet_Tank(t.Left, t.Top + 1)/遇到墙砖或坦克 t.newDirect();/坦克转向 else t.Top+; break; case 2:/向左 if (t.Left = 0 | Mapt.Left-1, t.Top = 1 | Meet_Tank(t.Left-1,
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1