C#课程设计实验报告(共20页).doc

上传人:飞****2 文档编号:15046608 上传时间:2022-05-10 格式:DOC 页数:20 大小:1.19MB
返回 下载 相关 举报
C#课程设计实验报告(共20页).doc_第1页
第1页 / 共20页
C#课程设计实验报告(共20页).doc_第2页
第2页 / 共20页
点击查看更多>>
资源描述

《C#课程设计实验报告(共20页).doc》由会员分享,可在线阅读,更多相关《C#课程设计实验报告(共20页).doc(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、精选优质文档-倾情为你奉上 苏 州 市 职 业 大 学课程设计说明书 名称 计算机语言C#课程设计 弹珠 游戏 2010年 6月 21日至 2010年 7月 4日共2周院 系计算机工程系 班 级 09软件测试 姓 名 罗苑() 系主任 李 金 祥 教研室主任 叶 良 指导教师 张 苏 王德鹏 摘要:这个小程序是关于弹珠的小游戏,游戏比较简单,以小球的速度来区分游戏的难易。该小程序是用左右键控制游戏的开始和挡板的方向,不让小球落到底端,当然,小球的速度越快,该游戏的难度也就越大。此程序利用C#程序编写,在visual studio 2005的环境下进行调试运行的。弹珠原理:碰撞基本都是参照“反射

2、定理”的,就是出射角=入射角,但是做碰撞时需要角度。碰撞的运动和球的方向有关,球的方向有好几种。有8向的,也有4向的。保证小球横向上在允许范围之内运动,当触发时就反方向运动,和当出了球拍的可击打范围就停止运行了。对于该程序开发,必须达到以下要求:1、熟悉.NET开发环境,能够使用C#语言在.NET平台上独立设计、开发WINDOWS应用程序。2、掌握C#语言的异常处理技术,能够使用.NET各种调试方法调试代码,掌握帮助的使用方法。3、掌握常用控件以及控件的常用属性的使用方法。4、界面要做到简洁易懂,具有人性化等特点。5、程序没有在使用过程中不存在任何的问题。6、可选功能应全面,可以实施速度的选择

3、,游戏的是否继续,还有记录的保存。目 录一、 题目说明当今用简单的代码编写的小游戏越来越受人们的欢迎,所以对于小游戏的开发也成为了各大编程人士的最爱。我选择弹珠游戏这个课题,用代码控制游戏的等级,运用不同的函数来控制球的速度和方向,游戏简单而有趣。二、总体设计2.1系统开发平台系统采用Microsoft Visual Studio 2005三、详细说明 在此弹球游戏中,对于球与挡板的位置,借助于x与y坐标的调整来实现统计,从而确定球落在板上后球的下一次方向。同时借助于Visual Studio中的控件来控制小球的速度。此项游戏的计分运用函数count+,打中一个砖块即可得到一分,看你最多能打多

4、少砖块。.1系统实施 timer1.Interval = dlg.Speed; protected override void Dispose( bool disposing )if( disposing )if (components != null) components.Dispose();base.Dispose( disposing ); private string m_strCurrentSoundFile = BallOut.wav; public void PlayASound() if (m_strCurrentSoundFile.Length 0) m_strCurren

5、tSoundFile = ; oThread.Abort(); public void PlaySoundInThread(string wavefile) m_strCurrentSoundFile = wavefile; oThread = new Thread(new ThreadStart(PlayASound); oThread.Start(); #region Windows Form Designer generated codeprivate void InitializeComponent() ponents = new System.ComponentModel.Conta

6、iner(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1); this.timer1 = new System.Windows.Forms.Timer(ponents); this.SuspendLayout(); this.timer1.Tick += new System.EventHandler(this.timer1_Tick); this.AutoScaleBaseSize = ne

7、w System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(552, 389); this.Icon = (System.Drawing.Icon)(resources.GetObject($this.Icon); this.KeyPreview = true; this.Name = Form1; this.Text = 打砖块; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); this.KeyDown +

8、= new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); #endregionSTAThreadstatic void Main() Application.Run(new Form1(); private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) Graphics

9、g = e.Graphics; g.FillRectangle(Brushes.White, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height); TheScore.Draw(g); ThePaddle.Draw(g); DrawRows(g); TheBall.Draw(g); private void DrawRows(Graphics g) for (int i = 0; i kNumberOfRows; i+) Rowsi.Draw(g); private void CheckForCollision() if

10、(TheBall.Position.X 0) TheBall.XStep *= -1; TheBall.Position.X += TheBall.XStep; PlaySoundInThread(WallHit.wav); if (TheBall.Position.Y this.ClientRectangle.Right - TheBall.Width ) TheBall.XStep *= -1; TheBall.Position.X += TheBall.XStep; PlaySoundInThread(WallHit.wav); if (TheBall.Position.Y this.C

11、lientRectangle.Bottom - TheBall.YStep) IncrementGameBalls(); Reset(); PlaySoundInThread(BallOut.wav); if (RowsCollide(TheBall.Position) TheBall.YStep *= -1; PlaySoundInThread(BrickHit.wav); int hp = HitsPaddle(TheBall.Position); if (hp -1) PlaySoundInThread(PaddleHit.wav); switch (hp) case 1: TheBal

12、l.XStep = -7; TheBall.YStep = -3; break; case 2: TheBall.XStep = -5; TheBall.YStep = -5; break; case 3: TheBall.XStep = 5; TheBall.YStep = -5; break; default: TheBall.XStep = 7; TheBall.YStep = -3; break; private int HitsPaddle(Point p) Rectangle PaddleRect = ThePaddle.GetBounds(); if (p.Y = this.Cl

13、ientRectangle.Bottom - (PaddleRect.Height + TheBall.Height) ) if (p.X PaddleRect.Left) & (p.X PaddleRect.Left) & (p.X PaddleRect.Left + PaddleRect.Width/4) & (p.X PaddleRect.Left + PaddleRect.Width/2) & (p.X = kNumberOfTries) timer1.Stop(); string msg = 游戏结束,您一共打了 + NumTotalBricks; if (NumTotalBrick

14、s = 1) msg += brick.; else msg += bricks. + 继续努力哦!; MessageBox.Show(msg); Application.Exit(); private void Reset() TheBall.XStep = 5; TheBall.YStep = 5; TheBall.Position.Y = this.ClientRectangle.Bottom - 190; TheBall.Position.X = 5; timer1.Stop(); TheBall.UpdateBounds(); Invalidate(TheBall.GetBounds

15、(); private int SumBricks () int sum = 0; for (int i = 0; i kNumberOfRows; i+) sum += Rowsi.BrickOut; return sum; private bool RowsCollide (Point p) for (int i = 0; i kNumberOfRows; i+) if (Rowsi.Collides(TheBall.GetBounds() Rectangle rRow = Rowsi.GetBounds(); Invalidate(rRow); return true; return f

16、alse; private void timer1_Tick(object sender, System.EventArgs e) TheBall.UpdateBounds(); Invalidate(TheBall.GetBounds(); TheBall.Move(); TheBall.UpdateBounds(); Invalidate(TheBall.GetBounds(); CheckForCollision(); NumTotalBricks = SumBricks(); TheScore.Count = NumTotalBricks; Invalidate(TheScore.Ge

17、tFrame(); if (NumTotalBricks = kNumberOfRows*Row.kNumberOfBricks) timer1.Stop(); Form4 Form1 = new Form4(); Form1.ShowDialog(); Application.Exit(); private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) string result = e.KeyData.ToString(); Invalidate(ThePaddle.GetBounds(); s

18、witch (result) case Left: ThePaddle.MoveLeft(); Invalidate(ThePaddle.GetBounds(); if (timer1.Enabled = false) timer1.Start(); break; case Right: ThePaddle.MoveRight(ClientRectangle.Right); Invalidate(ThePaddle.GetBounds(); if (timer1.Enabled = false) timer1.Start(); break; default: break; private vo

19、id Form1_Load(object sender, EventArgs e) 四、遇到的问题和解决方法1.错误:图片的路径错误,在做此程序的时候对游戏中出现的挡板进行选择的时候,选择了在桌面上的图片,后来将此程序拷到自己的电脑上的时候运行代码出现了路径错误。图一:路径错误解决方案:将图片重新拷到游戏文件夹中,并且将路径改到相应的游戏文件夹中,在此拷贝的时候就不会造成图片的损坏和丢失。2.错误:游戏结束时因为缺少结束代码,使得游戏虽然结束,但是窗口无法结束,造成窗口无限堆叠,使程序变成死循环。解决方案:在窗口form1中加入代码timer1.Stop();用来结束前一个窗口。五、课程设计总

20、结经过两周的课程设计,使我对C#编程有了更深一层的了解。在这两周的课程设计中我不断地改错,不断的调试,我的努力终于有了成果。最让我受益匪浅的是整个设计的过程,这个过程也是我不断学习的一个过程,我们在运用旧知识的同时也学到了不少新知识。本次课程设计我发现自己对知识掌握还存在着很多的不足之处,同时我也明白了自己还有哪些地方需要改进,自己要在哪些地方进行自我的提升。尤为重要的是,这次课程设计使我们懂得了理论与实际相结合是很重要的。在这次课程设计中,我虽然遇到一些困难,但我仍能坚持克服。通过这个小小的课题,我也学到了一些课外的东西,多谢学校给了我一次体验的机会,谢谢老师在我困难的时候给我的指导,也谢谢

21、在这段时间给过我们帮助的同学。在此期间我也失落过,也曾一度热情高涨。从开始时满腹盛激情到最后汗水背后的复杂心情,点点滴滴无不令我回味无穷。课程设计是培养学生综合运用所学知识,发现,提出,分析和解决实际问题,锻炼实践能力的重要环节,是对学生实际工作能力的具体训练和考察过程.随着科学技术发展的日新月异,编程已经成为当今计算机应用中空前活跃的领域, 在生活中可以说得是无处不在。在设计过程中,我们遇到许多无法解决的问题,都一一通过网络解决了,同时还问过许多学得比较扎实的同学,是他们让我们有了更多的了解,更好的让我们在这个问题上着手设计. 这次课程设计终于顺利完成了,在设计中遇到了很多编程问题,最后在老

22、师的细心指导下,终于得到解决。同时,在老师的身上我学得到很多实用的知识,在次我表示感谢!对给过我帮助的所有同学和各位指导老师再次表示忠心的感谢!六、参考文献1郑阿奇、梁敬东、钱晓军等,c#实用教程,电子工业出版社,2009年。2钱雪忠、黄学光等,数据库原理及应用,北京邮电大学出版社,2005年。3王进强. Visual Basic.Net基础教程M, 北京:清华大学出版社,2004年。附录(源程序代码)using System;using System.Drawing;using System.Collections;using System.ComponentModel;using Syst

23、em.Windows.Forms;using System.Data;using System.Runtime.InteropServices;using System.Threading;namespace BrickOutpublic class Form1 : System.Windows.Forms.Form private System.ComponentModel.IContainer components; private const int kNumberOfRows = 8; private const int kNumberOfTries = 3; private int

24、NumTotalBricks = 0; private int NumBalls = 0; private Ball TheBall = new Ball(); private Paddle ThePaddle = new Paddle(); private System.Windows.Forms.Timer timer1; private Row Rows = new RowkNumberOfRows; private Score TheScore = null; private Thread oThread = null; DllImport(winmm.dll) public stat

25、ic extern long PlaySound(String lpszName, long hModule, long dwFlags);public Form1()InitializeComponent(); for (int i = 0; i kNumberOfRows; i+) Rowsi = new Row(i); ThePaddle.Position.X = 5; ThePaddle.Position.Y = this.ClientRectangle.Bottom - ThePaddle.Height; TheBall.Position.Y = this.ClientRectang

26、le.Bottom - 200; this.SetBounds(this.Left, this.Top, Rows0.GetBounds().Width, this.Height); TheScore = new Score(ClientRectangle.Right - 50, ClientRectangle.Bottom - 180); / choose Level SpeedDialog dlg = new SpeedDialog(); if (dlg.ShowDialog() = DialogResult.OK) timer1.Interval = dlg.Speed; / TODO:

27、 Add any constructor code after InitializeComponent call/ / Clean up any resources being used./ protected override void Dispose( bool disposing )if( disposing )if (components != null) components.Dispose();base.Dispose( disposing ); private string m_strCurrentSoundFile = BallOut.wav; public void Play

28、ASound() if (m_strCurrentSoundFile.Length 0) PlaySound(Application.StartupPath + + m_strCurrentSoundFile, 0, 0); m_strCurrentSoundFile = ; oThread.Abort(); public void PlaySoundInThread(string wavefile) m_strCurrentSoundFile = wavefile; oThread = new Thread(new ThreadStart(PlayASound); oThread.Start

29、(); #region Windows Form Designer generated code/ / Required method for Designer support - do not modify/ the contents of this method with the code editor./ private void InitializeComponent() ponents = new System.ComponentModel.Container(); this.timer1 = new System.Windows.Forms.Timer(ponents); this

30、.SuspendLayout(); / / timer1 / this.timer1.Tick += new System.EventHandler(this.timer1_Tick); / / Form1 / this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(552, 389); this.KeyPreview = true; this.Name = Form1; this.Text = Brick Out; this.Paint += new

31、System.Windows.Forms.PaintEventHandler(this.Form1_Paint); this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); #endregion/ / The main entry point for the application./ STAThreadstatic void Main() Application.Run(new Form1(); pri

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁