Private myPen as Pen Private formGraphics As System.Drawing.Graphics Private myBrush As System.Drawing.SolidBrush Private p2vbW As Integer Private p2vbH As Integer Private p2vbX1 As Integer Private p2vbY1 As Integer Private p2vbX2 As Integer Private p2vbY2 As Integer ' ' Show a ball bouncing off the sides of the window. ' User always has mouse control over the direction and speed of the ball. ' ' from graphics import * ' import time, random Public Function bounceInBox(shape As Object, dx As Object, dy As Object, xLow As Object, xHigh As Object, yLow As Object, yHigh As Object, win As Object) ' Animate a shape moving in jumps (dx, dy), bouncing when ' its center reaches the low and high x and y coordinates. ' The animation stops when the mouse is clicked, and the ' last mouse click is returned. Dim delay As Integer delay = .001 ' win.clearLastMouse() 'NEW Dim pt As Integer pt = Nothing 'NEW while pt = Nothing: 'NEW shape.X = shape.X + dx shape.Y = shape.Y + dy Dim center As Object center = shape.getCenter() Dim x As Integer x = center.getX() Dim y As Integer y = center.getY() Dim isInside As Integer isInside = True 'NEW if x < xLow or x > xHigh Then dx = -dx isInside = False 'NEW End If if y < yLow or y > yHigh Then dy = -dy isInside = False 'NEW End If Threading.Thread.Sleep(delay * 1000) if isInside Then ' NEW don"t mess with dx, dy when outside pt = win.getLastMouse() 'NEW End If End While Return pt 'NEW End Function Public Sub moveInBox(shape As Object, stopHeight As Object, xLow As Object, xHigh As Object, yLow As Object, yHigh As Object, win As Object)'NEW ' Shape bounces in win so its center stays within the low and high ' x and y coordinates, and changes direction based on mouse clicks,terminating when there is a click above stopHeight. Dim scale As Integer scale = 0.01 Dim pt As Object pt = shape.getCenter() ' starts motionless while pt.getY() < stopHeight Dim Python2VBArray(,) as Object Dim dx As Object Dim dy As Object Python2VBArray(dx, dy) = getShift(shape.getCenter(), pt) pt = bounceInBox(shape, dx*scale, dy*scale,xLow, xHigh, yLow, yHigh, win) End While End Sub Public Function makeDisk(center As Object, radius As Object, win As Object)'NEW ' Return a red disk that is drawn in win with given center and radius. Dim disk As RectangleF formGraphics = Form1.CreateGraphics() myPen = New Pen(System.Drawing.Color.red, 1) disk.X = center.X disk.Y = center.Y disk.Width = radius disk.Height = radius formGraphics.DrawEllipse(myPen, disk) myBrush = New System.Drawing.SolidBrush(System.Drawing.Color.red) formGraphics.FillEllipse(myBrush, disk) Return disk End Function Public Function getShift(point1 As Object, point2 As Object)'NEW ' Returns a tuple (dx, dy) which is the shift from point1 to point2. Dim dx As Object dx = point2.getX() - point1.getX() Dim dy As Object dy = point2.getY() - point1.getY() Return {dx, dy} End Function Public Sub bounceBall() ' Make a ball bounce around the screen, and react to mouse clicks. Dim winWidth As Integer winWidth = 290 Dim winHeight As Integer winHeight = 290 Dim win As Integer Form1.Text = "Ball Bounce 3" Form1.Width = winWidth + 100 p2vbW = winWidth Form1.Height = winHeight + 100 p2vbH = winHeight p2vbX1 = 0 p2vbY1 = 0 p2vbX2 = winWidth p2vbY2 = winHeight 'NEW to mark and label the area where a click stops the program Dim lineHeight As Integer lineHeight = winHeight - 40 Dim textHeight As Integer textHeight = winHeight - 20 myPen = New Pen(Drawing.Color.Black, 1) formGraphics.DrawLine(myPen, 0, lineHeight, winWidth, lineHeight) Dim prompt As String prompt = "Click above the line to stop" + vbNewLine + "or below to move toward the click." Dim Control1 As New Label Control1.Left = p2vbW * ((winWidth/2) / (p2vbX2 - p2vbX1)) Control1.Top = p2vbH - (p2vbH * (( textHeight) / (p2vbY2 - p2vbY1))) Control1.Text = prompt Control1.AutoSize = True Form1.Controls.Add(Control1) Dim radius As Integer radius = 10 Dim xLow As Integer xLow = radius ' center is separated from the wall by the radius at a bounce Dim xHigh As Integer xHigh = winWidth - radius Dim yLow As Integer yLow = radius Dim yHigh As Integer yHigh = lineHeight - radius 'NEW lower top to bouncing limits Dim center As Point center = New Point(winWidth/2, lineHeight/2) Dim ball As Integer ball = makeDisk(center, radius, win) moveInBox(ball, lineHeight, xLow, xHigh, yLow, yHigh, win) 'NEW End Sub Public Sub Routine2 bounceBall() End Sub