第十四课 抓球游戏 教案
1.进一步熟悉逻辑运算符“and”的使用方法。
2.进一步熟悉事件驱动程序的编写方法。
抓球游戏的算法比较简单,不需要流程图,学生可以直接编写程序代码。 小球跳动的位置坐标由下面的命令随机产生: x=Math.GetRandomNumber(GraphicsWindow.Width)、 y=Math.GetRandomNumber(GraphicsWindow.Height) 在上面的命令中,随机函数的参数是GraphicsWindow.Width和 GraphicsWindow.Height,这样就保证了不管在程序的运行过程中如何改变图形 窗口的大小,小球的位置坐标始终在图形窗口中。21·cn·jy·com
1、抓球游戏:
ball=shapes.AddEllipse(20,20)
GraphicsWindow.BrushColor="red"
GraphicsWindow.MouseDown = OnMD
While ("true")
x=Math.GetRandomNumber(GraphicsWindow.Width)
y=Math.GetRandomNumber(GraphicsWindow.Height)
Shapes.Move(ball,x ,y)
Program .Delay (1000)
EndWhile
Sub OnMD
xMS = GraphicsWindow.MouseX
yMS = GraphicsWindow.MouseY
If xMS>=x and xMS<=x+20 and yMS>=y and yMS<=y+20 then 21世纪教育网版权所有
GraphicsWindow.FillEllipse(x, y, 20, 20)
GraphicsWindow.ShowMessage ("点中了!","你真棒!")
EndIf
EndSub
2、编写一个拉幕布的程序。点击鼠标把幕布拉上,再点鼠标把幕布拉开。
GraphicsWindow.MouseDown=onMD
i=1
Sub onMD
If i=1 Then
For x=0 To GraphicsWindow.Width
GraphicsWindow.PenColor =GraphicsWindow.GetRandomColor() GraphicsWindow.DrawLine(x,0,x,GraphicsWindow.Height) 21cnjy.com
Program.Delay(10)
EndFor
i=0
Else
GraphicsWindow.PenColor ="white"
For x=0 To GraphicsWindow.Width
GraphicsWindow.DrawLine(x,0,x,GraphicsWindow.Height) 21教育网
Program.Delay(10)
EndFor
i=1
EndIf
EndSub