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

上传人:b****3 文档编号:5267179 上传时间:2022-12-14 格式:DOCX 页数:24 大小:104.87KB
下载 相关 举报
628概念实训吃金豆游戏.docx_第1页
第1页 / 共24页
628概念实训吃金豆游戏.docx_第2页
第2页 / 共24页
628概念实训吃金豆游戏.docx_第3页
第3页 / 共24页
628概念实训吃金豆游戏.docx_第4页
第4页 / 共24页
628概念实训吃金豆游戏.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

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

《628概念实训吃金豆游戏.docx》由会员分享,可在线阅读,更多相关《628概念实训吃金豆游戏.docx(24页珍藏版)》请在冰豆网上搜索。

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

628概念实训吃金豆游戏

概念实训(吃金豆游戏)

1.代理和事件

一、代理(delegate)

1、代理概念

是一种将方法作为对象封装起来的引用数据类型,一个代理变量可以指向一个方法。

(1)定义一个名为OpHandler的代理类型。

delegatedoubleOpHandler(doublex,doubley);

(2)创建一个代理变量h1;

classCalc{

publicstaticdoubleAdd(doublex,doubley){returnx+y;}

publicdoubleSub(doublex,doubley){returnx-y;}

}

OpHandlerh1=Calc.Add;//h1指向Add方法

(3)通过代理变量h1调用它指向的方法

doubleresult;

result=h1(3,4);//调用Add方法

h1=newCalc().Sub;

result=h1(3,4);//调用Sub方法

2、代理作为方法的参数

classMathOp{

privatedoubleleft;

privatedoubleright;

publicMathOp(doubleleft,doubleright){

this.left=left;this.right=right;

}

publicdoubleGetResult(OpHandlerop){

returnop(left,right);

}

}

classApp{

publicstaticvoidMain(){

MathOpm1=newMathOp(3,4);

doubleresult=m1.GetResult(Calc.Add);

Console.WtriteLine(“result={0}”,result);

}

}

3、代理变量作为类的一个数据成员

classMathOp{

publicOpHandlerop;//op是代理变量

privatedoublefirst;

privatedoublesecond;

publicMathOp(doublefirst,doublesecond){

this.first=first;this.second=second;op=null;

}

publicdoubleInvoke(){

if(op==null)//如果有方法注册委托变量op

thrownewException();

returnop(first,second);//通过委托来调用方法

}

}

classApp{

publicstaticvoidMain(){

MathOpm1=newMathOp(3,4);

m1.op=Calc.Add;

doubleresult=m1.Invoke();

Console.WtriteLine(“result={0}”,result);

}

}

4、多播代理

在C#中,代理是“多播”的,这表示它可同时指向一个以上的方法。

多播代理将维护一个方法列表。

当调用该代理时,将会按FIFO顺序调用列表中的所有方法。

publicdelegatevoidGreetingDelegate(stringname);

classGreetingManager{

publicGreetingDelegatesay;

publicvoidGreetPeople(stringname){

if(say!

=null)

say(name);

}

}

classApp{

publicstaticvoidEnglishGreeting(stringname){

      Console.WriteLine("Morning,"+name);//英文问候

}

publicstaticvoidChineseGreeting(stringname){

      Console.WriteLine("早上好,"+name);//中文问候

}

publicstaticvoidMain(){

GreetingManagergm=newGreetingManager();

gm.say=EnglishGreeting;

gm.say+=ChineseGreeting;//用+=合并两个代理

gm.GreetPeople(“Marry”);//也可以直接gm.say(“Marry”)调用委托

}

}

注:

同样用-=可以删除一个代理。

二、事件(event)

将代理封装起来就成了事件,事件对外是公开的,而它所对应的代理是私有的。

事件可以看作是受限的代理。

因此事件也可以绑定(注册)一个方法,但不能通过=,只能通过+=进行。

事件最常见的用途是用于窗体编程,当发生像点击按钮、移动鼠标等事件时,对应的方法执行,以响应该事件。

例1:

将上例的GreetingDelegate代理封装成MakeGreet的事件,代码如下:

publicdelegatevoidGreetingDelegate(stringname);

classGreetingManager{

/*将代理GreetingDelegate封装成事件MakeGreet*/

publiceventGreetingDelegateMakeGreet;

publicvoidGreetPeople(stringname){

if(MakeGreet!

=null)

MakeGreet(name);//调用事件

}

}

classApp{

publicstaticvoidEnglishGreeting(stringname){

      Console.WriteLine("Morning,"+name);//英文问候

}

publicstaticvoidChineseGreeting(stringname){

      Console.WriteLine("早上好,"+name);//中文问候

}

publicstaticvoidMain(){

GreetingManagergm=newGreetingManager();

gm.MakeGreet=EnglishGreeting;//编译错误,不允许用=注册方法

gm.MakeGreet+=ChineseGreeting;

gm.GreetPeople(“Marry”);

/*

不可以直接gm.MakeGreet(“Marry”)调用事件,只能在定义

MakeGreet的类中调用事件。

*/

}

}

例2:

类Button在GUI中代表按钮,在Button类中定义了一个Click事件,它是对代理EventHandler的封装。

(1)EventHandler代理的定义如下:

delegatevoidEventHandler(objectsender,EventArgse);

(2)Click事件定义如下:

eventEventHandlerClick;

ButtonbtnOK=newButton();//实例化一个名btnOk的按钮

btnOK.Click+=ClickProcess;//为它的Click事件注册ClickProcess方法。

则当单击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、事件:

Click

GUI应用程序的例子:

1)生成并显示一个标题为Hello的空白窗体:

classMyForm:

Form{

publicMyForm(){

this.Text=“Hello”;

}

}

classWinApp{

publicstaticvoidMain(){

Application.Run(newMyForm());

}

}

2)在窗体中增加一个按钮,按钮标题为OK,修改MyForm类:

classMyForm:

Form{

privateButtonbtnOK;

publicMyForm(){

btnOK=newButton();

btnOK.Text=“OK”;

this.Text=“Hello”;

this.Controls.Add(btnOK);

}

}

运行程序,单击OK按钮,看看有没有什么发生?

3)为OK按钮注册Click事件,当单击OK时,按钮的背景色变红。

修改MyForm类:

classMyForm:

Form{

privateButtonbtnOK;

publicMyForm(){

btnOK=newButton();

btnOK.Text=“OK”;

btnOK.Click+=ClickProcess;//注册click事件

this.Text=“Hello”;

this.Controls.Add(btnOK);

}

publicvoidClickProcess(objectsender,EventArgse){

btnOK.BackColor=Color.Red;

}

}

运行程序,单击OK按钮,看看发生了什么?

3.GDI编程

GDI(GraphicsDeviceInterface)为开发者提供了一组实现与各种设备进行交互的类库,它在开发人员与设备之间起到了一个重要的中介作用。

GDI包括三个部分:

二维矢量图形绘制

图像处理

文字显示

一、Graphics类

该类封装了GDI的绘图表面,Windows窗体中所有的绘图操作都必须通过Graphics类进行。

1、创建或获取Graphics对象

法一:

Graphicsg=控件.CreateGraphics();

法二:

通过窗体或控件的Paint事件来获取。

以窗体为例:

paint事件在窗体的任何部分需要重绘时发生。

引发paint事件的情况有:

(1)窗体首次运行时

(2)覆盖窗体的其他窗体移开时

(3)窗体本身移动,或大小改变时

PrivatevoidForm1_Paint(objectsender,PaintEventArgse){

Graphicsg=e.Graphics;

g.DrawLine(…);

}

 

2、用Graphics类提供的各种方法:

绘图

显示文本

操作和显示图像

DrawLine(…)

DrawEllipse(…)

DrawRectangle(…)

DrawImage(…)

DrawString(…)

FillEllipse(…)

二、创建画笔和画刷

1、Pen类创建指定颜色和线宽画笔对象

Penp1=newPen(Color.Red);//设置颜色

Penp2=newPen(Color.Black,10);//设置颜色和线宽

Penp3=Pens.Blue;//Pens类中预定义的画笔

2、画刷Brush抽象类

SolidBrush、HatchBrush等是Brush类的子类

Brushb1=newSolidBrush(Color.Red);

Brushb2=newHachBrush(HatchStyle.Cross,Color.Blue,Color.Yellow);

Brushb3=Brushes.Red;////Brushes类中预定义的画刷

三、

x>0

(0,0)

y>0

width

height

(x,y)

坐标系统

 

(1)画椭圆

g.DrawEllipse(Penpen,intx,inty,intwidth,intheight);

(2)画扇形

g.DrawArc(Penpen,intx,inty,intwidth,intheight,intstartAngle,intdegree);

(3)显示文字

g.DrawString(“Hello”,this.Font,Brushes.Red,20,20);

g.DrawString(“Hello”,newFont(“宋体”,30,FontStyle.Bold),Brushes.Red…);

 

例一:

在窗体中绘制如下的图形

privatevoidForm1_Paint(objectsender,PaintEventArgse){

Graphicsg=e.Graphics;

floatdiameter=50;

g.DrawLine(Pens.Red,0,0,this.ClientSize.Width,this.ClientSize.Height);

g.DrawLine(Pens.Blue),0,this.ClientSize.Height,this.ClientSize.Width,0);

g.DrawEllipse(Pens.Black,

(this.ClientSize.Width-diameter)/2,

(this.ClientSize.Height-diameter)/2,

diameter,

diameter

);

}

//窗体大小改变时触发Resize事件

privatevoidForm1_Resize(objectsender,EventArgse){

this.Invalidate();//使窗体或控件无效,并人为引发其Paint事件,进行重绘

this.Update();

}

 

例二:

在面板中绘制如下的图形:

要求:

在文本框中输入行和列,单击clear按钮,对应位置的小金豆被清除。

法一:

直接在面板中绘图

privatevoidpanel1_Paint(objectsender,PaintEventArgse){

Graphicsg=e.Graphics;

intr,c;

r=c=8;//r行,c列

inth,w;//每个方块的高度和宽度

w=panel1.Width/c;

h=panel1.Height/r;

for(inti=0;i

for(intj=0;j

g.DrawRectangle(Pens.Blue,j*w,i*h,w,h);

g.FillEllipse(Brushes.Yellow,

j*w+(w-20)/2,

i*h+(h-20)/2,

20,20);

}

}

}

privatevoidbtnClear_Click(objectsender,EventArgse){

intr=int.Parse(txtRow.Text);

intc=int.Parse(txtCol.Text);

intw=panel1.Width/8;

inth=panel1.Height/8;

Graphicsg=panel1.CreateGraphics();

Brushb1=newSolidBrush(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(intr,intc,intw,inth);

+Draw(Graphicsg):

void

+Clear(Graphicsg,ColorforeColor):

void

classCell{

privateintr,c,w,h;

publicCell(intr,intc,intw,inth){

this.r=r;

this.c=c;

this.w=w;

this.h=h;

}

publicvoidDraw(Graphicsg){

intx,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);

}

publicvoidClear(Graphicsg,ColorforeColor){

intx,y;

x=w*c;

y=h*r;

g.FillEllipse(newSolidBrush(foreColor),x,y,w,h);

}

}

 

在Form1类中如下定义:

privateCell[,]cellArr;

publicForm1(){

InitializeComponent();

cellArr=newCell[8,8];

for(inti=0;i<8;i++){

for(intj=0;j<8;j++)

cellArr[i,j]=newCell(i,j,panel1.Width/8,panel1.Height/8);

}

}

privatevoidpanel1_Paint(objectsender,PaintEventArgse){

for(inti=0;i

for(intj=0;j

(1);j++)

cellArr[i,j].Draw(e.Graphics);

}

}

privatevoidbtnClear_Click(objectsender,EventArgse){

intr,c;

r=int.Parse(txtRow.Text);

c=int.Parse(txtCol.Text);

cellArr[r,c].Clear(panel1.CreateGraphics(),panel1.BackColor);

}

 

例三:

设计一个pacman,要求:

(1)可以通过按钮控制它的上下左右运动。

Cell

-r:

int//行

-c:

int//列

-w:

int//宽度

-h:

int//高度

+Cell(intr,intc,intw,inth);

+Draw(Graphicsg):

void

+Clear(Graphicsg,ColorforeColor):

void

+abstractDrawImage(Graphicsg,intx,inty):

void

 

Pacman

-dir:

Directoin//Direction枚举四种方向

+Pacman(intr,intc,intw,inth,Directiondir);

+Draw(Graphicsg):

void

+Move():

void

+overrideDrawImage(Graphicsg,intx,inty):

void

代码参考:

enumDirection{Up,Down,Left,Right};

classPacman:

Cell{

protectedDirectiondir;

publicPacman(intr,intc,intw,inth,Directiondir):

base(r,c,w,h){

this.dir=dir;

}

publicoverridevoidDrawImage(Graphicsg,intx,inty){

g.FillEllipse(Brushes.Black,x+(w-20)/2,y+(h-20)/2,20,20);

}

publicvoidMove(){

switch(dir){

caseDirection.Up:

r--;break;

caseDirection.Down:

r++;break;

caseDirection.Left:

c--;break;

caseDirection.Right:

c++;break;

}

}

publicDirectionDir{

get{

returndir;

}

set{

dir=value;

}

}

}

publicpartialclassForm1:

Form{

privatePacmanmyPacman;

publicForm1(){

InitializeComponent();

myPacman=newPacman(0,

0,

panel1.Width/16,

panel1.Height/8,

Direction.Right

);

}

privatevoidpanel1_Paint(objectsender,PaintEventArgse){

myPacman.Draw(e.Graphics);

}

privatevoidbtnUp_Click(objectsender,EventArgse){

myPacman.Dir=Direction.Up;

myPacman.Move(

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

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

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