重庆海天软件学院的WINFORM实训项目疯狂的小鸟.docx
- 文档编号:12747605
- 上传时间:2023-04-21
- 格式:DOCX
- 页数:23
- 大小:281.95KB
重庆海天软件学院的WINFORM实训项目疯狂的小鸟.docx
《重庆海天软件学院的WINFORM实训项目疯狂的小鸟.docx》由会员分享,可在线阅读,更多相关《重庆海天软件学院的WINFORM实训项目疯狂的小鸟.docx(23页珍藏版)》请在冰豆网上搜索。
重庆海天软件学院的WINFORM实训项目疯狂的小鸟
重庆海天软件学院的WINFORM实训项目-疯狂的小鸟
重庆海天软件学院第二学期微软4班WINFORM实训项目截图:
最近学生们学习了2个月的C#和WINFORM,没有学习数据库。
故设计了这样的一个小游戏,基本覆盖了C#语言常用的语法,已经面向对象的基本思想。
本实训课程分为8个阶段来实践,效果非常好。
故录制成视频分为8个阶段,每个阶段15分钟左右。
此游戏开发为在课堂上即兴编写,即兴录制。
没有事先准备,故有些地方不是很完美,请见谅!
不过,大家可以学习丁老师在完成软件开发中的需求的时候,思考的过程和完整的思路,以及遇到错误如果去调试和解决的方法,这个大家会有收获。
下载地址:
主要实现思路为:
用GDI+在窗体上绘制图形,在主窗体加入了一个Time控件来做游戏循环,不断的绘图。
使用的DoubleBuffer技术,屏幕不闪烁。
效果还是可以的,但是在面向对象设计这块设计得有点罗嗦了,没有设计好,需要进一步完善。
代码展示:
[csharp]viewplaincopyprint?
1.using System;
2. using System.Collections.Generic;
3. using System.ComponentModel;
4. using System.Data;
5. using System.Drawing;
6. using System.Linq;
7. using System.Text;
8. using System.Windows.Forms;
9.
10. namespace CrazyBird_GDI
11. {
12. ///
13. /// 重庆海天软件工程学院 微软7班 WINFORM实训项目----疯狂的小鸟
14. /// 微软讲师:
丁浩 Email:
dinghao80@ QQ:
37112555
15. /// 项目需求:
射击游戏
16. /// 实现步骤:
1.采用GDI+技术,双缓冲实训精灵的绘制
17. /// 2.绘制背景
18. /// 3.绘制小鸟
19. /// 4.绘制小猪
20. /// 5.绘制子弹
21. ///
22. public partial class Form1 :
Form
23. {
24. public Form1()
25. {
26. InitializeComponent();
27. }
28. Bitmap bitmap;
29. Pig pig;
30. //第七步4
31. List
32. BirdOP birdop;
33. //第十步6:
当子弹碰到小鸟的开关
34. bool isTouched = false;
35. private void timer1_Tick(object sender, EventArgs e)
36. {
37. //在time控件中循环绘制,约33帧的FPS
38. //第一步:
导入所需资源
39. //第二步:
搭建双缓冲模型,将资源不断的绘制在虚拟画布上面,这个技术可以消除屏幕闪烁
40. //最后将虚拟画布bitmap的内容复制在窗体绘图区
41.
42. using (Graphics dc=Graphics.FromImage(bitmap))
43. {
44. //第三步:
导入背景图片资源
45. BackGround bg = new BackGround(this.Width, this.Height);
46. bg.Draw(dc);
47. //固定窗体不能拖动
48.
49. //**-------//第一阶段完成。
思考:
BackGround设计成了一个类,为什么?
好处在哪儿?
-------------------------------------------------------------------------------------------
50. //第四步:
绘制小鸟 需求一共64只小鸟,成8*8排列 设计小鸟类
51.
52. //第九步6:
绘制小鸟
53. birdop.Draw(dc);//之前要装载小鸟
54.
55.
56.
57. //**------//第二阶段完成。
-----------------------------------------------------------------------------------------------
58. //第五步:
绘制小猪
59.
60. pig.Width = 70;
61. pig.Height = 60;
62.
63. pig.Draw(this,dc);
64.
65. //**------ //第三阶段完成----------------------------------------------------------------------------------------------
66. //第六步:
移动小猪
67. //触发键盘事件:
注意思考,事件源是什么?
----窗体
68.
69. //第七步6:
绘制子弹
70. foreach (Bullet bullet in bullets)
71. {
72. //第八步2:
让speed值增加 ,现在找个地方赋予speed初始值
73. // bullet.Speed+=10; //没有起到作用
74. bullet.X -= bullet.Speed;
75. bullet.Draw(dc);
76. }
77. //***----------第六阶段完成 (让子弹飞)------------------下一阶段:
子弹碰撞小鸟,子弹和小鸟移除集合-------------------------------------------------------------------
78. //思考:
子弹碰撞小鸟,只需要在time控件中从小鸟集合中绘制,子弹从子弹集合里面绘制。
碰撞后,从各种集合移除小鸟或子弹即可。
79.
80. //第十步1:
判断子弹和小鸟相撞,实际上就是2个矩形相交。
追加子弹的方法。
81.
82. //第十步5:
83. for (int i = 0; i < bullets.Count; i++) //遍历子弹,注意这个时候不能用foreach ,为什么?
84. {
85. for (int j = 0; j < BirdOP.birds.Count; j++)//遍历小鸟
86. {
87. if (bullets[i].Intersect(BirdOP.birds[j])) //每一个子弹去判断所有的小鸟碰撞
88. {
89. BirdOP.birds.RemoveAt(j);//移除小鸟 测试成功 但是同时要移除子弹
90. isTouched = true;
91. }
92. }
93. //移除子弹
94. if (isTouched)
95. {
96. bullets.RemoveAt(i);
97. isTouched = false;
98. }
99. //测试成功
100. //**-----------------第八阶段完成 到这儿就结束了 思考加入声音,由于机房没有音响,不好测试。
----------------------------------------------------------------------------------------
101. //我们发现在构造Bird BackGround Pig等到类的时候,发现都有共同的属性,这个时候可以用继承来实现,需要进一步改进,还要有更好的扩展性,大家在学习面向对象的时候
102. //要积极思考面向对象的三大特性,封装 继承 多态 接口 抽象类 结构 枚举等等C#的特性
103. //实训制作人:
丁浩老师 QQ:
37112555 这个视频会放在中下载,欢迎大家学习,谢谢!
重庆海天软件工程学院
104.
105. }
106.
107. }
108. //第二步:
复制绘制在窗体绘图区
109. using (Graphics g=this.CreateGraphics())
110. {
111. g.DrawImage(bitmap, 0, 0);
112. }
113. }
114.
115. private void Form1_Load(object sender, EventArgs e)
116. {
117. //第二步:
创建和窗体一样大小的画布
118. bitmap = new Bitmap(this.Width, this.Height);
119. //初始化小猪
120. pig = new Pig();
121. pig.Step = 10;
122. //第九步5:
初始化小鸟
123. //bird = new Bird();
124. //bird.LoadBird();
125. //做到这个地方,我们发现在bird设计中出现了问题。
需要重新设计,没有思考全面。
126. birdop = new BirdOP();
127. //第九步5:
初始化小鸟
128. birdop.LoadBirds();
129. }
130.
131. //第六步 触发键盘事件移动小猪
132. private void Form1_KeyUp(object sender, KeyEventArgs e)
133. {
134. switch (e.KeyCode)
135. {
136. case Keys.Up:
137. pig.Move(Direct.UP);
138. break;
139. case Keys.Down:
140. pig.Move(Direct.DOWN);
141. break;
142. case Keys.Space:
143. //第七步5:
装载子弹在集合中
144. bullets.Add(pig.FireABullet());
145. break;
146.
147. }
148. }
149. //**-----------第四阶段完成--------------------------------------------
150. }
151. }
背景类:
[csharp]viewplaincopyprint?
1.using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Text;
5. using System.Drawing;
6.
7. namespace CrazyBird_GDI
8. {
9. class BackGround
10. {
11. public BackGround(int width,int height)
12. {
13. Width = width;
14. Height = height;
15. }
16. int width;
17.
18. public int Width
19. {
20. get { return width; }
21. set { width = value; }
22. }
23. int height;
24.
25. public int Height
26. {
27. get { return height; }
28. set { height = value; }
29. }
30.
31. //绘制一个背景图片
32. public void Draw(Graphics g)
33. {
34. Rectangle rect=new Rectangle(0,0,Width,Height);
35. g.DrawImage(MyRes.background, rect);
36. }
37. }
38. }
小鸟类:
[csharp]viewplaincopyprint?
1.using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Text;
5. using System.Drawing;
6.
7. namespace CrazyBird_GDI
8. {
9. public class Bird
10. {
11. int x;//x坐标
12.
13. public int X
14. {
15. get { return x; }
16. set { x = value; }
17. }
18. int y;//y坐标
19.
20. public int Y
21. {
22. get { return y; }
23. set { y = value; }
24. }
25. int width;
26.
27. public int Width
28. {
29. get { return width; }
30. set { width = value; }
31. }
32. int height;
33.
34. public int Height
35. {
36. get { return height; }
37. set { height = value; }
38. }
39.
40. //小鸟的矩形绘图区 第十步 3
41. private Rectangle rectBird;
42.
43. public Rectangle RectBird
44. {
45. get { return rectBird; }
46. set { rectBird = value; }
47. }
48. }
49. }
小鸟操作类:
[csharp]viewplaincopyprint?
1. using System;
2.using System.Collections.Generic;
3.using System.Linq;
4.using System.Text;
5.using System.Drawing;
6.
7.namespace CrazyBird_GDI
8.{
9. ///
10. /// 第九步6:
重新设计小鸟类
11. /// 增加了一个小鸟操作类
12. ///
13. public class BirdOP
14. {
15. //第九步2:
定义一个集合
16. public static List
如
17.
18.果不是静态的,每次new一个小鸟,都会有属于自己的集合,显然不对。
所以用静态的
19.
20.,所有的小鸟对象都对应这一个集合
21.
22. //绘制8*8小鸟阵
23.
24. //第九步3:
装入64只小鸟到集合中
25. public void LoadBirds()
26
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 重庆 软件 学院 WINFORM 项目 疯狂 小鸟
![提示](https://static.bdocx.com/images/bang_tan.gif)