This is the code i have written. I dont think the CBool part is right.. i'm not sure what i would put for that
Private Sub BouncingBallForm_Keydown(ByVal sender As Object, _
ByVal s As System.Windows.Forms.KeyEventArgs) _
Handles BouncingBallForm.Keydown
If Control.ModifierKeys = Keys.S Then
moveBallTimer.Enabled = True And
shrinkSliderTimer.Enabled = True
ElseIf CBool(Keys.Left And CInt(rectangleX > 0)) = True Then
rectangleX -= 10
ElseIf CBool(Keys.Right And CInt(rectangleX < (416 - rectangleWidth))) = True Then
rectangleX += 10
End If
End Sub
This is all of the code
Public Class BouncingBallForm
Private x As Integer ' ball's x-coordinate
Private y As Integer ' ball's y-coordinate
Private rectangleX As Integer ' paddle's x-coordinate
Private rectangleWidth As Integer ' paddle's width
Private deltaX As Integer ' ball's x rate of change
Private deltaY As Integer ' ball's y rate of change
Private xRight As Boolean ' tests if ball can move right
Private yUp As Boolean ' tests if ball can move up
Private Const MAX_X As Integer = 400 ' x boundary
Private Const MAX_Y As Integer = 400 ' y boundary
' object to generate random numbers
Private randomObject As Random = New Random()
Private Sub BouncingBallForm_Keydown(ByVal sender As Object, _
ByVal s As System.Windows.Forms.KeyEventArgs) _
Handles BouncingBallForm.Keydown
If Control.ModifierKeys = Keys.S Then
moveBallTimer.Enabled = True And
shrinkSliderTimer.Enabled = True
ElseIf CBool(Keys.Left And CInt(rectangleX > 0)) = True Then
rectangleX -= 10
ElseIf CBool(Keys.Right And CInt(rectangleX < (416 - rectangleWidth))) = True Then
rectangleX += 10
End If
End Sub
Private Sub BouncingBallForm_Load(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
x = randomObject.Next(100, 301) ' ball's initial x
y = randomObject.Next(100, 301) ' ball's initial y
rectangleX = 175 ' rectangle's intial x position
rectangleWidth = 80 ' rectangle's intial width
xRight = False ' ball can't move left
yUp = False ' ball can't move up
deltaX = 2 ' move ball 2 positions right
deltaY = 2 ' move ball 2 positions down
End Sub ' BouncingBallForm_Load
Protected Overrides Sub OnPaint( _
ByVal e As System.Windows.Forms.PaintEventArgs)
' create graphics object
Dim graphicsObject As Graphics = CreateGraphics()
' create new brush
Dim brush As SolidBrush = New SolidBrush(Color.Blue)
' draw ball
graphicsObject.FillEllipse(brush, x, y, 10, 10)
' set color for, and draw paddle
brush.Color = Color.Brown
graphicsObject.FillRectangle( _
brush, rectangleX, 380, rectangleWidth, 15)
End Sub ' OnPaint
Private Sub moveBallTimer_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles moveBallTimer.Tick
' determine new x position
If xRight = True Then
x += deltaX
Else
x -= deltaX
End If
' determine new y position
If yUp Then
y += deltaY
Else
y -= deltaY
End If
If y <= 0 Then
yUp = True
deltaY = randomObject.Next(2, 6)
ElseIf y >= 370 AndAlso x >= rectangleX _
AndAlso x <= (rectangleX + rectangleWidth) Then
yUp = False
deltaY = randomObject.Next(2, 6)
ElseIf y >= 410 Then ' end game if ball hits floor
moveBallTimer.Enabled = False
shrinkSliderTimer.Enabled = False
MessageBox.Show("Game Over")
End If
If x <= 0 Then
xRight = True
deltaX = randomObject.Next(2, 6)
ElseIf x >= MAX_X - 10 Then
xRight = False
deltaX = randomObject.Next(2, 6)
End If
Invalidate() ' Refresh Form
End Sub ' moveBallTimer_Tick
' shrinks the paddle every 20 seconds
Private Sub shrinkSliderTimer_Tick(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles shrinkSliderTimer.Tick
' shrink paddle if paddle greater than twice ball's width
If rectangleWidth >= 20 Then
rectangleWidth = Convert.ToInt32( _
rectangleWidth / 2)
End If
End Sub ' shrinkpaddleTimer_Tick
Private Function rextangleX() As Object
Throw New NotImplementedException
End Function
End Class ' BouncingBallForm

New Topic/Question
Reply



MultiQuote



|