Results 1 to 14 of 14

Thread: Resize at runtime?

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Resize at runtime?

    This is a very basic pong game and want to change it slightly. I want to be able to descrease the "paddleComputer" property size if "paddlePlayer" scores (repeats) and decrease the "paddlePlayer" size if the computer scores. (repeats) and also to reduce the size of "gameBall" everytime a score has been triggered for either player or computer.

    I have commented where I would this to go, where it says "--==GOES HERE==--" in the code below. I'm unaware of how to resize properties at runtime, can someone please help me achieve this?

    Main code pong game Code:
    1. Option Explicit On
    2. Public Class pongMain
    3.  
    4. #Region "Globals"
    5.     Dim speed As Single = 10 ' Ball Speed
    6.     Dim rndInst As New Random() ' Random instance
    7.     Dim xVel As Single = Math.Cos(rndInst.Next(5, 10)) * speed
    8.     Dim yVel As Single = Math.Sin(rndInst.Next(5, 10)) * speed
    9. #End Region
    10.  
    11.     ' The player's scores.
    12.     Dim compScore As Integer = 0
    13.     Dim plrScore As Integer = 0
    14.  
    15. #Region "Move the paddle according to the mouse"
    16.     ' Move the paddle according to the mouse position.
    17.     Private Sub pongMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    18.  
    19.         If e.Y > 5 And e.Y < Me.Height - 40 - paddlePlayer.Height Then _
    20.         paddlePlayer.Location = New Point(paddlePlayer.Location.X, e.Y)
    21.  
    22.     End Sub
    23. #End Region
    24.  
    25. #Region "Main Timer"
    26.     Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
    27.         'Set the computer player to move according to the ball's position."
    28.         If gameBall.Location.Y > 5 And gameBall.Location.Y < Me.Height - 40 _
    29.         - paddlePlayer.Height Then _
    30.         paddleComputer.Location = New Point(paddleComputer.Location.X, gameBall.Location.Y)
    31.  
    32.         ' Move the game ball.
    33.         gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)
    34.  
    35.         ' Check for top wall.
    36.         If gameBall.Location.Y < 0 Then
    37.             gameBall.Location = New Point(gameBall.Location.X, 0)
    38.             yVel = -yVel
    39.         End If
    40.  
    41.         ' Check for bottom wall.
    42.         If gameBall.Location.Y > Me.Height - gameBall.Size.Height - 45 Then
    43.             gameBall.Location = New Point(gameBall.Location.X, Me.Height - gameBall.Size.Height - 45)
    44.             yVel = -yVel
    45.         End If
    46.  
    47.         ' Check for player paddle.
    48.         If gameBall.Bounds.IntersectsWith(paddlePlayer.Bounds) Then
    49.             gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, _
    50.             gameBall.Location.Y)
    51.             xVel = -xVel
    52.         End If
    53.  
    54.         ' Check for computer paddle.
    55.         If gameBall.Bounds.IntersectsWith(paddleComputer.Bounds) Then
    56.             gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, _
    57.             gameBall.Location.Y)
    58.             xVel = -xVel
    59.         End If
    60.  
    61.         ' Check for left wall.
    62.         If gameBall.Location.X < 0 Then
    63.             plrScore += 1
    64.             gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
    65.             plrScoreDraw.Text = Convert.ToString(plrScore)
    66.         End If
    67.  
    68.         ' Check for right wall.
    69.         If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
    70.             compScore += 1
    71.             gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
    72.             compScoreDraw.Text = Convert.ToString(compScore)
    73.         End If
    74.  
    75.         ' Decrease computer paddle size if player gets score (repeats)
    76.         If gameBall.Location.X < 0 Then
    77.             --==GOES HERE==--
    78.         End If
    79.  
    80.  
    81.         ' Decrease player paddle size if computer gets score (repeats)
    82.         If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
    83.             --==GOES HERE==--
    84.         End If
    85.  
    86.     End Sub
    87. #End Region
    88.  
    89. #Region "Hide Cursor"
    90.     ' Set up the game.
    91.     Private Sub pongMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    92.         Windows.Forms.Cursor.Hide()
    93.     End Sub
    94. #End Region
    95.  
    96. #Region "End Game on Escape Press"
    97.     ' Escape the game when escape has been pressed.
    98.     Private Sub pongMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    99.         If e.KeyValue = Keys.Escape Then
    100.             Me.Close()
    101.         End If
    102.     End Sub
    103. #End Region
    104.  
    105. #Region "Keep the paddle and score labels in the correct position when the form is resized."
    106.     Private Sub pongMain_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
    107.         paddlePlayer.Location = New Point(Me.Width - 44, paddlePlayer.Location.Y)
    108.         plrScoreDraw.Location = New Point(Me.Width - 54, plrScoreDraw.Location.Y)
    109.     End Sub
    110. #End Region
    111.  
    112. End Class

    Thanks for any help!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Resize at runtime?

    Welcome to VBForums

    Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum

  3. #3
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Resize at runtime?

    Assuming I understand your question correctly, you just change the size property the exact same way you are changing location properties.

    paddlePlayer.Size = New Size(10,10)

    or by a factor

    paddlePlayer.Size = New Size(paddlePlayer.Width * 0.5, paddlePlayer.Height * 0.5 ) 'well something like that

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Re: Resize at runtime?

    This is what I wanted yes but...

    Right now the paddlers are set to 16, 128.
    But I want to use minus a value from 128.

    E.g.

    paddlePlayer.Size = New Size(16, 128-10)

    can I do this?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Resize at runtime?

    Quote Originally Posted by karimali831 View Post
    This is what I wanted yes but...

    Right now the paddlers are set to 16, 128.
    But I want to use minus a value from 128.

    E.g.

    paddlePlayer.Size = New Size(16, 128-10)

    can I do this?
    yes no problem

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Re: Resize at runtime?

    Thanks but look at this:

    plrScore += 1 will plus 1 each time "gameBall.Location.X < 0 Then" statement is true.

    I want the size to be changed also each time this statement is true, how can I do this? It will only change the size once.

    Code Code:
    1. ' Check for left wall.
    2.         If gameBall.Location.X < 0 Then
    3.             paddlePlayer.Size = New Size(paddlePlayer.Width * 1, paddlePlayer.Height - 15)
    4.             plrScore += 1
    5.             gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
    6.             plrScoreDraw.Text = Convert.ToString(plrScore)
    7.         End If
    8.  
    9.         ' Check for right wall.
    10.         If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
    11.             paddleComputer.Size = New Size(paddlePlayer.Width * 1, paddlePlayer.Height - 15)
    12.             compScore += 1
    13.             gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
    14.             compScoreDraw.Text = Convert.ToString(compScore)
    15.         End If

  7. #7
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Resize at runtime?

    You use a flag to tell you.

    Code:
    Dim bolResizePaddle As Boolean = False
    
    If gameBall.Location.X < 0 Then
      bolResizePaddle = True
      ...(other stuff)
    End If
    
    ...
    
    If MyOtherThing = True Then
      bolResizePaddle = True
      ...(other stuff)
    End If
    
    ...
    
    'Later still!
    
    If bolResizePaddle Then
     'resize code only once at the end!
    End If

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Re: Resize at runtime?

    I'm quite confused now, sorry for being newbie
    Can you adapt it into the full code I posted previously? (first post)

    I'd appreciate it alot.. thanks!

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Re: Resize at runtime?

    I did try use your method, but now it resizes until there is nothing left to resize so object is no longer visible. If you copy/paste this entire code and play it in VB, you will notice:

    Bugged Code:
    1. Option Explicit On
    2. Public Class pongMain
    3.  
    4. #Region "Globals"
    5.     Dim speed As Single = 10 ' Ball Speed
    6.     Dim rndInst As New Random() ' Random instance
    7.     Dim xVel As Single = Math.Cos(rndInst.Next(5, 10)) * speed
    8.     Dim yVel As Single = Math.Sin(rndInst.Next(5, 10)) * speed
    9.     Dim bolResizePaddlePlayer As Boolean = False
    10.     Dim bolResizePaddleComputer As Boolean = False
    11. #End Region
    12.  
    13.     ' The player's scores.
    14.     Dim compScore As Integer = 0
    15.     Dim plrScore As Integer = 0
    16.  
    17. #Region "Move the paddle according to the mouse"
    18.     ' Move the paddle according to the mouse position.
    19.     Private Sub pongMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    20.  
    21.         If e.Y > 5 And e.Y < Me.Height - 40 - paddlePlayer.Height Then _
    22.         paddlePlayer.Location = New Point(paddlePlayer.Location.X, e.Y)
    23.  
    24.     End Sub
    25. #End Region
    26.  
    27. #Region "Main Timer"
    28.     Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
    29.         'Set the computer player to move according to the ball's position."
    30.         If gameBall.Location.Y > 5 And gameBall.Location.Y < Me.Height - 40 _
    31.         - paddlePlayer.Height Then _
    32.         paddleComputer.Location = New Point(paddleComputer.Location.X, gameBall.Location.Y)
    33.  
    34.         ' Move the game ball.
    35.         gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)
    36.  
    37.         ' Check for top wall.
    38.         If gameBall.Location.Y < 0 Then
    39.             gameBall.Location = New Point(gameBall.Location.X, 0)
    40.             yVel = -yVel
    41.         End If
    42.  
    43.         ' Check for bottom wall.
    44.         If gameBall.Location.Y > Me.Height - gameBall.Size.Height - 45 Then
    45.             gameBall.Location = New Point(gameBall.Location.X, Me.Height - gameBall.Size.Height - 45)
    46.             yVel = -yVel
    47.         End If
    48.  
    49.         ' Check for player paddle.
    50.         If gameBall.Bounds.IntersectsWith(paddlePlayer.Bounds) Then
    51.             gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, _
    52.             gameBall.Location.Y)
    53.             xVel = -xVel
    54.         End If
    55.  
    56.         ' Check for computer paddle.
    57.         If gameBall.Bounds.IntersectsWith(paddleComputer.Bounds) Then
    58.             gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, _
    59.             gameBall.Location.Y)
    60.             xVel = -xVel
    61.         End If
    62.  
    63.         ' Check for left wall.
    64.         If gameBall.Location.X < 0 Then
    65.             bolResizePaddlePlayer = True
    66.             plrScore += 1
    67.             gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
    68.             plrScoreDraw.Text = Convert.ToString(plrScore)
    69.         End If
    70.  
    71.         ' Check for right wall.
    72.         If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
    73.             bolResizePaddleComputer = True
    74.             compScore += 1
    75.             gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
    76.             compScoreDraw.Text = Convert.ToString(compScore)
    77.         End If
    78.  
    79.         If bolResizePaddlePlayer Then
    80.             paddlePlayer.Size = New Size(paddlePlayer.Width * 1, paddlePlayer.Height - 15)
    81.         End If
    82.  
    83.         If bolResizePaddleComputer Then
    84.             paddleComputer.Size = New Size(paddleComputer.Width * 1, paddleComputer.Height - 15)
    85.         End If
    86.  
    87.     End Sub
    88. #End Region
    89.  
    90. #Region "Hide Cursor"
    91.     ' Set up the game.
    92.     Private Sub pongMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    93.         Windows.Forms.Cursor.Hide()
    94.     End Sub
    95. #End Region
    96.  
    97. #Region "End Game on Escape Press"
    98.     ' Escape the game when escape has been pressed.
    99.     Private Sub pongMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    100.         If e.KeyValue = Keys.Escape Then
    101.             Me.Close()
    102.         End If
    103.     End Sub
    104. #End Region
    105.  
    106. #Region "Keep the paddle and score labels in the correct position when the form is resized."
    107.     Private Sub pongMain_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
    108.         paddlePlayer.Location = New Point(Me.Width - 44, paddlePlayer.Location.Y)
    109.         plrScoreDraw.Location = New Point(Me.Width - 54, plrScoreDraw.Location.Y)
    110.     End Sub
    111. #End Region
    112.  
    113. End Class

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Re: Resize at runtime?

    Ahh had to put it in the statement :P

    Thanks!! Code:
    1. If gameBall.Location.X < 0 Then
    2.             bolResizePaddlePlayer = True
    3.  
    4.             If bolResizePaddlePlayer Then
    5.                 paddleComputer.Size = New Size(paddleComputer.Width * 1, paddleComputer.Height - 15)
    6.             End If

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Re: Resize at runtime?

    How do I say if an object e.g. paddlePlayer has 0 height, do this or do that? I want to learn VBB so sorry I'm asking newbie questions.

  12. #12
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Resize at runtime?

    The interesting thing about your questions are that VB has similar names using the english description you are giving. You said

    e.g paddlePlayer has 0 height, so

    Code:
    If paddlePlayer.Height = 0 Then
      'Do this
    Else
      'Do that
    End If
    To answer your earlier question, about it going soo small you can not see it, try this:

    Code:
    If bolResizePaddlePlayer AndAlso paddlePlayer.Height > 30 Then
                paddlePlayer.Size = New Size(paddlePlayer.Width, paddlePlayer.Height - 15)
    End If
    So, only resize if the boolean = true AND the paddle is longer then 30, meaning it will never be smaller then 15 pixels.

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Re: Resize at runtime?

    Ah very simple, I know alot of PHP and I can see VB just makes things a little confusing for me.

    What is the code for closing the form/window and showing an alert popup or message?

    Thanks.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Resize at runtime?

    to close a form, use:

    vb Code:
    1. Me.Close

    to show a msgbox:

    vb Code:
    1. MessageBox.show("msg")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width