ImageVerifierCode 换一换
格式:DOCX , 页数:24 ,大小:47.32KB ,
资源ID:9890478      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9890478.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C#坦克游戏详细讲解.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C#坦克游戏详细讲解.docx

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