1 Replies - 6957 Views - Last Post: 30 April 2012 - 04:16 PM Rate Topic: -----

#1 tehlatter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-April 12

bouncing ball game application

Posted 29 April 2012 - 06:29 PM

I have to make a bouncing ball game application. all the code is given to me except for code to move the paddle left and right, and code to start the game. i have to write an if...then statement in a keydown event handler that tests whether the user presses the s key. i then have to add an elseif statement that tests whether the user pressed the left-arrow key and whether the paddle's horizontal position (rectangleX) is greater than zero. if the paddle's horizontal position equals zero, the left edge of the paddle is touching the left wall and the paddle should not be allowed to move farther to the left( i am not sure how to do this part). if the conditions are true in the if then statement, the paddle's x-position should decrease by 10. then i need an elseif statement that tests whether the right arrow key is pressed and whether the paddles x-coord is less than the width of the form minus the width of the paddle. if it equals out the paddle should not be allowed to move farther right. if both conditions are true the paddles x-coordinate is increased by 10

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


Is This A Good Question/Topic? 0
  • +

Replies To: bouncing ball game application

#2 fabrivera99  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 27-January 12

Re: bouncing ball game application

Posted 30 April 2012 - 04:16 PM

http://www.dreaminco...onal-pong-game/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1