|
-
Feb 4th, 2011, 10:59 AM
#1
Thread Starter
Member
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:
Option Explicit On
Public Class pongMain
#Region "Globals"
Dim speed As Single = 10 ' Ball Speed
Dim rndInst As New Random() ' Random instance
Dim xVel As Single = Math.Cos(rndInst.Next(5, 10)) * speed
Dim yVel As Single = Math.Sin(rndInst.Next(5, 10)) * speed
#End Region
' The player's scores.
Dim compScore As Integer = 0
Dim plrScore As Integer = 0
#Region "Move the paddle according to the mouse"
' Move the paddle according to the mouse position.
Private Sub pongMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Y > 5 And e.Y < Me.Height - 40 - paddlePlayer.Height Then _
paddlePlayer.Location = New Point(paddlePlayer.Location.X, e.Y)
End Sub
#End Region
#Region "Main Timer"
Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
'Set the computer player to move according to the ball's position."
If gameBall.Location.Y > 5 And gameBall.Location.Y < Me.Height - 40 _
- paddlePlayer.Height Then _
paddleComputer.Location = New Point(paddleComputer.Location.X, gameBall.Location.Y)
' Move the game ball.
gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)
' Check for top wall.
If gameBall.Location.Y < 0 Then
gameBall.Location = New Point(gameBall.Location.X, 0)
yVel = -yVel
End If
' Check for bottom wall.
If gameBall.Location.Y > Me.Height - gameBall.Size.Height - 45 Then
gameBall.Location = New Point(gameBall.Location.X, Me.Height - gameBall.Size.Height - 45)
yVel = -yVel
End If
' Check for player paddle.
If gameBall.Bounds.IntersectsWith(paddlePlayer.Bounds) Then
gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, _
gameBall.Location.Y)
xVel = -xVel
End If
' Check for computer paddle.
If gameBall.Bounds.IntersectsWith(paddleComputer.Bounds) Then
gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, _
gameBall.Location.Y)
xVel = -xVel
End If
' Check for left wall.
If gameBall.Location.X < 0 Then
plrScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
plrScoreDraw.Text = Convert.ToString(plrScore)
End If
' Check for right wall.
If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
compScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
compScoreDraw.Text = Convert.ToString(compScore)
End If
' Decrease computer paddle size if player gets score (repeats)
If gameBall.Location.X < 0 Then
--==GOES HERE==--
End If
' Decrease player paddle size if computer gets score (repeats)
If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
--==GOES HERE==--
End If
End Sub
#End Region
#Region "Hide Cursor"
' Set up the game.
Private Sub pongMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Windows.Forms.Cursor.Hide()
End Sub
#End Region
#Region "End Game on Escape Press"
' Escape the game when escape has been pressed.
Private Sub pongMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyValue = Keys.Escape Then
Me.Close()
End If
End Sub
#End Region
#Region "Keep the paddle and score labels in the correct position when the form is resized."
Private Sub pongMain_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
paddlePlayer.Location = New Point(Me.Width - 44, paddlePlayer.Location.Y)
plrScoreDraw.Location = New Point(Me.Width - 54, plrScoreDraw.Location.Y)
End Sub
#End Region
End Class
Thanks for any help!
-
Feb 4th, 2011, 11:05 AM
#2
Re: Resize at runtime?
Welcome to VBForums 
Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum
-
Feb 4th, 2011, 11:35 AM
#3
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
-
Feb 4th, 2011, 12:06 PM
#4
Thread Starter
Member
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?
-
Feb 4th, 2011, 12:15 PM
#5
Re: Resize at runtime?
 Originally Posted by karimali831
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 4th, 2011, 01:14 PM
#6
Thread Starter
Member
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:
' Check for left wall.
If gameBall.Location.X < 0 Then
paddlePlayer.Size = New Size(paddlePlayer.Width * 1, paddlePlayer.Height - 15)
plrScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
plrScoreDraw.Text = Convert.ToString(plrScore)
End If
' Check for right wall.
If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
paddleComputer.Size = New Size(paddlePlayer.Width * 1, paddlePlayer.Height - 15)
compScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
compScoreDraw.Text = Convert.ToString(compScore)
End If
-
Feb 4th, 2011, 01:35 PM
#7
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
-
Feb 4th, 2011, 01:58 PM
#8
Thread Starter
Member
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!
-
Feb 4th, 2011, 02:03 PM
#9
Thread Starter
Member
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:
Option Explicit On
Public Class pongMain
#Region "Globals"
Dim speed As Single = 10 ' Ball Speed
Dim rndInst As New Random() ' Random instance
Dim xVel As Single = Math.Cos(rndInst.Next(5, 10)) * speed
Dim yVel As Single = Math.Sin(rndInst.Next(5, 10)) * speed
Dim bolResizePaddlePlayer As Boolean = False
Dim bolResizePaddleComputer As Boolean = False
#End Region
' The player's scores.
Dim compScore As Integer = 0
Dim plrScore As Integer = 0
#Region "Move the paddle according to the mouse"
' Move the paddle according to the mouse position.
Private Sub pongMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Y > 5 And e.Y < Me.Height - 40 - paddlePlayer.Height Then _
paddlePlayer.Location = New Point(paddlePlayer.Location.X, e.Y)
End Sub
#End Region
#Region "Main Timer"
Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
'Set the computer player to move according to the ball's position."
If gameBall.Location.Y > 5 And gameBall.Location.Y < Me.Height - 40 _
- paddlePlayer.Height Then _
paddleComputer.Location = New Point(paddleComputer.Location.X, gameBall.Location.Y)
' Move the game ball.
gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)
' Check for top wall.
If gameBall.Location.Y < 0 Then
gameBall.Location = New Point(gameBall.Location.X, 0)
yVel = -yVel
End If
' Check for bottom wall.
If gameBall.Location.Y > Me.Height - gameBall.Size.Height - 45 Then
gameBall.Location = New Point(gameBall.Location.X, Me.Height - gameBall.Size.Height - 45)
yVel = -yVel
End If
' Check for player paddle.
If gameBall.Bounds.IntersectsWith(paddlePlayer.Bounds) Then
gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, _
gameBall.Location.Y)
xVel = -xVel
End If
' Check for computer paddle.
If gameBall.Bounds.IntersectsWith(paddleComputer.Bounds) Then
gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, _
gameBall.Location.Y)
xVel = -xVel
End If
' Check for left wall.
If gameBall.Location.X < 0 Then
bolResizePaddlePlayer = True
plrScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
plrScoreDraw.Text = Convert.ToString(plrScore)
End If
' Check for right wall.
If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
bolResizePaddleComputer = True
compScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
compScoreDraw.Text = Convert.ToString(compScore)
End If
If bolResizePaddlePlayer Then
paddlePlayer.Size = New Size(paddlePlayer.Width * 1, paddlePlayer.Height - 15)
End If
If bolResizePaddleComputer Then
paddleComputer.Size = New Size(paddleComputer.Width * 1, paddleComputer.Height - 15)
End If
End Sub
#End Region
#Region "Hide Cursor"
' Set up the game.
Private Sub pongMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Windows.Forms.Cursor.Hide()
End Sub
#End Region
#Region "End Game on Escape Press"
' Escape the game when escape has been pressed.
Private Sub pongMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyValue = Keys.Escape Then
Me.Close()
End If
End Sub
#End Region
#Region "Keep the paddle and score labels in the correct position when the form is resized."
Private Sub pongMain_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
paddlePlayer.Location = New Point(Me.Width - 44, paddlePlayer.Location.Y)
plrScoreDraw.Location = New Point(Me.Width - 54, plrScoreDraw.Location.Y)
End Sub
#End Region
End Class
-
Feb 4th, 2011, 02:10 PM
#10
Thread Starter
Member
Re: Resize at runtime?
Ahh had to put it in the statement :P
Thanks!! Code:
If gameBall.Location.X < 0 Then
bolResizePaddlePlayer = True
If bolResizePaddlePlayer Then
paddleComputer.Size = New Size(paddleComputer.Width * 1, paddleComputer.Height - 15)
End If
-
Feb 4th, 2011, 02:18 PM
#11
Thread Starter
Member
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.
-
Feb 4th, 2011, 03:20 PM
#12
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.
-
Feb 4th, 2011, 03:50 PM
#13
Thread Starter
Member
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.
-
Feb 4th, 2011, 03:53 PM
#14
Re: Resize at runtime?
to close a form, use:
to show a msgbox:
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|