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. ' Version where all conditions that change the direction of the shape ' are handled inside one animation loop. ' from graphics import * ' import time, random Public Function moveInBox(shape As Object, stopHeight As Object, xLow As Object, xHigh As Object, yLow As Object, yHigh As Object, win As Object) ' Animate a shape moving toward any mouse click below stopHeight and ' bouncing when its center reaches the low or high x or y coordinates. ' The animation stops when the mouse is clicked at stopHeight or above. Dim scale As Integer scale = 0.01 Dim delay As Integer delay = .001 Dim dx As Integer dx = 0 'NEW dx and dy are no longer parameters Dim dy As Integer dy = 0 'NEW ' win.clearLastMouse() while True: 'NEW exit loop at Return statement 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 if x < xLow or x > xHigh Then dx = -dx isInside = False End If if y < yLow or y > yHigh Then dy = -dy isInside = False End If if isInside Then Dim pt As Object pt = win.getLastMouse() if pt <> Nothing Then 'NEW dealing with mouse click now here if pt.getY() < stopHeight Then ' switch direction Dim Python2VBArray(,) as Object Python2VBArray(dx, dy) =getShift(center, pt) Python2VBArray(dx, dy) ={dx*scale, dy*scale} ' win.clearLastMouse() else: 'NEW exit from depths of the loop Exit Function 'NEW End If End If End If shape.X = shape.X + dx shape.Y = shape.Y + dy Threading.Thread.Sleep(delay * 1000) End While End Function Public Function makeDisk(center As Object, radius As Object, win As Object) ' 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) ' 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 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 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 Dim center As Point center = New Point(winWidth/2, lineHeight/2) Dim ball As Integer ball = makeDisk(center, radius, win) 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) moveInBox(ball, lineHeight, xLow, xHigh, yLow, yHigh, win) End Sub Public Sub Routine2 bounceBall() End Sub