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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

628概念实训吃金豆游戏.docx

1、628概念实训吃金豆游戏概念实训(吃金豆游戏)1代理和事件一、 代理(delegate)1、 代理概念 是一种将方法作为对象封装起来的引用数据类型,一个代理变量可以指向一个方法。(1)定义一个名为OpHandler的代理类型。 delegate double OpHandler(double x,double y);(2)创建一个代理变量h1; class Calc public static double Add(double x,double y)return x+y; public double Sub(double x,double y)return x-y; OpHandler h1

2、 = Calc.Add; /h1指向Add方法(3)通过代理变量h1调用它指向的方法 double result; result = h1(3,4); /调用Add方法 h1 = new Calc().Sub; result = h1(3,4); /调用Sub方法2、 代理作为方法的参数 class MathOp private double left; private double right; public MathOp(double left,double right) this.left = left; this.right = right; public double GetResu

3、lt(OpHandler op) return op(left,right); class App public static void Main() MathOp m1 = new MathOp(3,4); double result = m1.GetResult(Calc.Add); Console.WtriteLine(“result = 0”,result); 3、 代理变量作为类的一个数据成员class MathOp public OpHandler op; /op是代理变量 private double first; private double second; public Ma

4、thOp(double first,double second) this.first = first; this.second = second; op = null; public double Invoke() if(op=null) /如果有方法注册委托变量op throw new Exception(); return op(first,second); /通过委托来调用方法 class App public static void Main() MathOp m1 = new MathOp(3,4); m1.op = Calc.Add; double result = m1.Inv

5、oke(); Console.WtriteLine(“result = 0”,result); 4、 多播代理在 C# 中,代理是“多播”的,这表示它可同时指向一个以上的方法。多播代理将维护一个方法列表。当调用该代理时,将会按FIFO顺序调用列表中的所有方法。public delegate void GreetingDelegate(string name); class GreetingManager public GreetingDelegate say;public void GreetPeople(string name) if(say!=null) say(name);class A

6、pppublic static void EnglishGreeting(string name) Console.WriteLine(Morning, + name); /英文问候public static void ChineseGreeting(string name) Console.WriteLine(早上好, + name); /中文问候 public static void Main() GreetingManager gm = new GreetingManager ();gm.say = EnglishGreeting;gm.say += ChineseGreeting; /

7、用+=合并两个代理gm.GreetPeople(“Marry”); /也可以直接gm.say(“Marry”)调用委托 注:同样用-=可以删除一个代理。二、 事件(event)将代理封装起来就成了事件,事件对外是公开的,而它所对应的代理是私有的。事件可以看作是受限的代理。因此事件也可以绑定(注册)一个方法,但不能通过=,只能通过+=进行。事件最常见的用途是用于窗体编程,当发生像点击按钮、移动鼠标等事件时,对应的方法执行,以响应该事件。例1:将上例的GreetingDelegate代理封装成MakeGreet的事件,代码如下:public delegate void GreetingDelega

8、te(string name);class GreetingManager /* 将代理GreetingDelegate封装成事件MakeGreet*/public event GreetingDelegate MakeGreet; public void GreetPeople(string name) if(MakeGreet!=null) MakeGreet(name); /调用事件class Apppublic static void EnglishGreeting(string name) Console.WriteLine(Morning, + name); /英文问候public

9、 static void ChineseGreeting(string name) Console.WriteLine(早上好, + name); /中文问候 public static void Main() GreetingManager gm = new GreetingManager ();gm.MakeGreet = EnglishGreeting; /编译错误,不允许用=注册方法gm.MakeGreet += ChineseGreeting; gm.GreetPeople(“Marry”); /*不可以直接gm. MakeGreet (“Marry”)调用事件,只能在定义MakeG

10、reet的类中调用事件。 */ 例2: 类Button在GUI中代表按钮,在Button类中定义了一个Click事件,它是对代理EventHandler的封装。(1)EventHandler代理的定义如下:delegate void EventHandler(object sender,EventArgs e); (2)Click事件定义如下: event EventHandler Click; Button btnOK = new Button(); /实例化一个名btnOk的按钮 btnOK.Click += ClickProcess; /为它的Click事件注册ClickProcess方

11、法。 则当单击btnOK按钮时,ClickProcess方法被调用以响应该单击事件。 2窗体和按钮一、窗体(Form类)1、Form类是所有高级窗口的基类。2、设置Form属性 如: (Text, Location, Size, Name) (FormBorderStyle, BackColor, StartPosition) (MaximunBox, MinimunBox, ControlBox)等。二、按钮控件(Button类)1、属性:(Text, Name, Enabled, )2、方法: Show(),Hide(),Focus(),Invalidate()3、事件: ClickGUI

12、应用程序的例子:1)生成并显示一个标题为Hello的空白窗体:class MyForm:Form public MyForm() this.Text = “Hello”;class WinApp public static void Main() Application.Run(new MyForm();2)在窗体中增加一个按钮,按钮标题为OK,修改MyForm类:class MyForm:Form private Button btnOK; public MyForm() btnOK = new Button(); btnOK.Text = “OK”; this.Text = “Hello”

13、;this.Controls.Add(btnOK);运行程序,单击OK按钮,看看有没有什么发生?3)为OK按钮注册Click事件,当单击OK时,按钮的背景色变红。修改MyForm类:class MyForm:Form private Button btnOK; public MyForm() btnOK = new Button(); btnOK.Text = “OK”; btnOK.Click+= ClickProcess; /注册click事件this.Text = “Hello”;this.Controls.Add(btnOK);public void ClickProcess(obje

14、ct sender,EventArgs e) btnOK.BackColor = Color.Red;运行程序,单击OK按钮,看看发生了什么?3GDI编程GDI(Graphics Device Interface)为开发者提供了一组实现与各种设备进行交互的类库,它在开发人员与设备之间起到了一个重要的中介作用。GDI包括三个部分: 二维矢量图形绘制 图像处理 文字显示一、 Graphics类该类封装了GDI的绘图表面,Windows窗体中所有的绘图操作都必须通过Graphics类进行。1、 创建或获取Graphics对象法一:Graphics g = 控件.CreateGraphics();法二

15、:通过窗体或控件的Paint事件来获取。 以窗体为例:paint事件在窗体的任何部分需要重绘时发生。 引发paint事件的情况有:(1) 窗体首次运行时(2) 覆盖窗体的其他窗体移开时(3) 窗体本身移动,或大小改变时Private void Form1_Paint(object sender,PaintEventArgs e) Graphics g = e.Graphics; g.DrawLine(); 2、 用Graphics类提供的各种方法: 绘图 显示文本 操作和显示图像DrawLine()DrawEllipse()DrawRectangle()DrawImage()DrawStrin

16、g()FillEllipse()二、 创建画笔和画刷1、 Pen类创建指定颜色和线宽画笔对象 Pen p1 = new Pen(Color.Red); /设置颜色 Pen p2 = new Pen(Color.Black,10); /设置颜色和线宽 Pen p3 = Pens.Blue; /Pens类中预定义的画笔2、 画刷Brush抽象类SolidBrush、HatchBrush等是Brush类的子类 Brush b1 = new SolidBrush(Color.Red); Brush b2 = new HachBrush(HatchStyle.Cross,Color.Blue,Color

17、.Yellow); Brush b3 = Brushes.Red; /Brushes类中预定义的画刷三、 x0(0,0)y0widthheight(x,y)坐标系统(1)画椭圆 g.DrawEllipse(Pen pen,int x,int y,int width,int height);(2)画扇形 g.DrawArc(Pen pen,int x,int y,int width,int height,int startAngle,int degree);(3)显示文字g.DrawString(“Hello”,this.Font,Brushes.Red,20,20);g.DrawString(

18、“Hello”,new Font(“宋体”,30,FontStyle.Bold),Brushes.Red);例一:在窗体中绘制如下的图形private void Form1_Paint(object sender, PaintEventArgs e) Graphics g = e.Graphics; float diameter=50; g.DrawLine(Pens.Red, 0, 0, this.ClientSize.Width, this.ClientSize.Height); g.DrawLine(Pens.Blue), 0, this.ClientSize.Height, this.

19、ClientSize.Width, 0); g.DrawEllipse( Pens.Black, (this.ClientSize.Width - diameter)/2, (this.ClientSize.Height - diameter)/2, diameter, diameter ); /窗体大小改变时触发Resize事件 private void Form1_Resize(object sender, EventArgs e) this.Invalidate(); /使窗体或控件无效,并人为引发其Paint事件,进行重绘 this.Update(); 例二:在面板中绘制如下的图形:要

20、求:在文本框中输入行和列,单击clear按钮,对应位置的小金豆被清除。法一:直接在面板中绘图private void panel1_Paint(object sender, PaintEventArgs e) Graphics g = e.Graphics; int r, c; r = c = 8; /r行,c列 int h, w; /每个方块的高度和宽度 w = panel1.Width / c; h = panel1.Height / r; for (int i = 0; i r; i+) for (int j = 0; j c; j+) g.DrawRectangle(Pens.Blue

21、, j * w, i * h, w, h); g.FillEllipse( Brushes.Yellow, j * w + (w - 20) / 2, i * h + (h - 20) / 2, 20, 20); private void btnClear_Click(object sender, EventArgs e) int r = int.Parse(txtRow.Text); int c = int.Parse(txtCol.Text); int w = panel1.Width / 8; int h = panel1.Height / 8; Graphics g = panel1.

22、CreateGraphics(); Brush b1 = new SolidBrush(panel1.BackColor); g.FillEllipse(b1, c * w + (w - 20) / 2, r *h +(h - 20) / 2, 20, 20); 法二:将一个方块封装成一个Cell对象。Cell-r:int /行-c:int /列-w:int /宽度-h:int /高度+Cell(int r,int c,int w,int h);+Draw(Graphics g):void+Clear(Graphics g,Color foreColor):voidclass Cell pri

23、vate int r,c,w,h; public Cell(int r, int c, int w, int h) this.r = r; this.c = c; this.w = w; this.h = h; public void Draw(Graphics g) int x, y; x = w * c; y = h * r; g.DrawRectangle(Pens.Blue, x, y, w, h); g.FillEllipse(Brushes.Yellow, x + (w - 20) / 2, y + (h - 20) / 2, 20, 20); public void Clear(

24、Graphics g, Color foreColor) int x, y; x = w * c; y = h * r; g.FillEllipse(new SolidBrush(foreColor), x, y, w, h); 在Form1类中如下定义:private Cell, cellArr; public Form1() InitializeComponent(); cellArr = new Cell8, 8; for (int i = 0; i 8; i+) for (int j = 0; j 8; j+) cellArri, j = new Cell(i, j, panel1.W

25、idth / 8, panel1.Height / 8); private void panel1_Paint(object sender, PaintEventArgs e) for (int i = 0; i cellArr.GetLength(0); i+) for (int j = 0; j cellArr.GetLength(1); j+) cellArri, j.Draw(e.Graphics); private void btnClear_Click(object sender, EventArgs e) int r, c; r = int.Parse(txtRow.Text);

26、 c = int.Parse(txtCol.Text); cellArrr, c.Clear(panel1.CreateGraphics(), panel1.BackColor); 例三:设计一个pacman,要求:(1)可以通过按钮控制它的上下左右运动。Cell-r:int /行-c:int /列-w:int /宽度-h:int /高度+Cell(int r,int c,int w,int h);+Draw(Graphics g):void+Clear(Graphics g,Color foreColor):void+abstract DrawImage(Graphics g,int x,i

27、nt y):voidPacman-dir:Directoin /Direction枚举四种方向+Pacman(int r,int c,int w,int h,Direction dir);+Draw(Graphics g):void+Move():void+override DrawImage(Graphics g,int x,int y):void代码参考:enum DirectionUp,Down,Left,Right;class Pacman:Cell protected Direction dir; public Pacman(int r,int c,int w,int h,Direc

28、tion dir):base(r,c,w,h) this.dir = dir; public override void DrawImage(Graphics g, int x, int y) g.FillEllipse(Brushes.Black, x + (w - 20) / 2, y + (h - 20) / 2, 20, 20); public void Move() switch (dir) case Direction.Up: r-; break; case Direction.Down: r+; break; case Direction.Left: c-; break; cas

29、e Direction.Right: c+; break; public Direction Dir get return dir; set dir = value; public partial class Form1 : Form private Pacman myPacman; public Form1() InitializeComponent(); myPacman = new Pacman( 0, 0, panel1.Width / 16, panel1.Height / 8, Direction.Right ); private void panel1_Paint(object sender, PaintEventArgs e) myPacman.Draw(e.Graphics); private void btnUp_Click(object sender, EventArgs e) myPacman.Dir = Direction.Up; myPacman.Move(

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1