CS 100 Homework Animations – Solutions

CS 100 Homework Animations – Solutions
Problem 1. Shoot the Arrow
Code
'
' Shoot the arrow. This program moves a label containing an image of arrow across
' the screen frm left to right when the "Shoot Arrow" button is clicked. A timer is
' used to move the arrow.
'
Public Class frmArrow
Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
End
End Sub
Private Sub tmrMoveArrow_Tick(sender As Object, e As EventArgs) Handles
tmrMoveArrow.Tick
lblArrow.Left = lblArrow.Left + 10
If lblArrow.Right >= lblTarget.Left Then
tmrMoveArrow.Stop()
End If
End Sub
Private Sub btnShoot_Click(sender As Object, e As EventArgs) Handles btnShoot.Click
tmrMoveArrow.Start()
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
lblArrow.Left = 0
End Sub
End Class
Problem 2. Bounce the Ball
Code
'
'
'
'
'
'
'
'
'
This program animates a label containing the image of a
red ball as it moves from the top
of the yellow panel to the bottom and then back to the
starting position at the top.
Two timers are used. One for downward motion and one for
upward motion
Public Class frmBounce
Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
End
End Sub
Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
tmrDown.Start()
End Sub
Private Sub tmrDown_Tick(sender As Object, e As EventArgs) Handles tmrDown.Tick
lblBall.Top = lblBall.Top + 10
If lblBall.Bottom >= pnlOne.Height Then
tmrDown.Stop()
tmrUp.Start()
End If
End Sub
Private Sub tmrUp_Tick(sender As Object, e As EventArgs) Handles tmrUp.Tick
lblBall.Top = lblBall.Top - 10
If lblBall.Top <= 0 Then
tmrUp.Stop()
End If
End Sub
End Class
Problem 3. Off the Table
Code
'
' This program animates a label containing an image of a ball. The ball moves across the
table
' from left to right until it reaches the edge of teh table. At tht point it falls off
' the table taking a diagonal path to the floor. When the ball hits the floor all motion
stops.
'
Public Class frmDrop
Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
End
End Sub
Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
tmrRight.Start()
End Sub
Private Sub tmrRight_Tick(sender As Object, e As EventArgs) Handles tmrRight.Tick
lblBall.Left = lblBall.Left + 10
If lblBall.Left >= btnTableTop.Right Then
tmrDown.Start()
End If
End Sub
Private Sub tmrDown_Tick(sender As Object, e As EventArgs) Handles tmrDown.Tick
lblBall.Top = lblBall.Top + 10
If lblBall.Bottom >= pnlOne.Height Then
tmrDown.Stop()
tmrRight.Stop()
End If
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
lblBall.Left = 0
lblBall.Top = 160
End Sub
End Class
Problem 4 Trick Shot
'
' This program animates the collision of three balls. Ball 1 moves horizontally from left
to right
' colliding with both ball 2 and ball 3 at the same time. Ball 1 then stops while ball 2
moves
' up and to the right until it disappears off the green panel.
' Ball 3 moves down and to the right until it moves of the green panel. The animation
begins
' when the Go button is pressed.
'
Public Class frmPool
Private Sub tmrBall1Right_Tick(sender As Object, e As EventArgs) Handles
tmrBall1Right.Tick
lblBall1.Left = lblBall1.Left + 10
If lblBall1.Right >= lblBall2.Left Then
tmrBall1Right.Stop()
tmrBall2Right.Start()
tmrBall2Up.Start()
tmrBall3Down.Start()
tmrBall3Right.Start()
End If
End Sub
Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
End
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
lblBall1.Top = 130
lblBall1.Left = 30
lblBall2.Top = 110
lblBall2.Left = 360
lblBall3.Top = 170
lblBall3.Left = 360
tmrBall1Right.Stop()
tmrBall2Right.Stop()
tmrBall2Up.Stop()
tmrBall3Down.Stop()
tmrBall3Right.Stop()
End Sub
Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
tmrBall1Right.Start()
End Sub
Private Sub tmrBall2Up_Tick(sender As Object, e As EventArgs) Handles tmrBall2Up.Tick
lblBall2.Top = lblBall2.Top - 10
End Sub
Private Sub tmrBall2Right_Tick(sender As Object, e As EventArgs) Handles
tmrBall2Right.Tick
lblBall2.Left = lblBall2.Left + 10
End Sub
Private Sub tmrBall3Right_Tick(sender As Object, e As EventArgs) Handles
tmrBall3Right.Tick
lblBall3.Left = lblBall3.Left + 10
End Sub
Private Sub tmrBall3Down_Tick(sender As Object, e As EventArgs) Handles
tmrBall3Down.Tick
lblBall3.Top = lblBall3.Top + 10
End Sub
End Class
Problem 5 Zig-Zag
Code
'
'
'
'
'
'
'
This program animates a ball where the ball begins at the top of a panel
pnlOne. When the Go button is pressed the ball begins to fall in a zig zag
fashion bouncing off the left and right sides of the panel until it
reaches the bottom of the panel. When the ball reaches the bottom of the panel
all animation stops.
' The reset button resets the ball at the top in its original position with
' all animation stopped.
'
' Three timers control the left, right and down motion of the ball.
'
Public Class frmBounce
Private Sub btnquit_Click(sender As Object, e As EventArgs) Handles btnquit.Click
End
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
lblDot.Top = 0
lblDot.Left = pnlOne.Width / 2 + lblDot.Width / 2
tmrDown.Stop()
tmrRight.Stop()
tmrLeft.Stop()
End Sub
Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
tmrDown.Start()
tmrRight.Start()
tmrLeft.Stop()
End Sub
Private Sub tmrRight_Tick(sender As Object, e As EventArgs) Handles tmrRight.Tick
lblDot.Left = lblDot.Left + 10
If lblDot.Right > pnlOne.Width Then
tmrRight.Stop()
tmrLeft.Start()
End If
End Sub
Private Sub tmrLeft_Tick(sender As Object, e As EventArgs) Handles tmrLeft.Tick
lblDot.Left = lblDot.Left - 10
If lblDot.Left < 0 Then
tmrRight.Start()
tmrLeft.Stop()
End If
End Sub
Private Sub tmrDown_Tick(sender As Object, e As EventArgs) Handles tmrDown.Tick
lblDot.Top = lblDot.Top + 10
If lblDot.Bottom > pnlOne.Height Then
tmrDown.Stop()
tmrRight.Stop()
tmrLeft.Stop()
End If
End Sub
End Class