 |
 |
|

09-06-2004, 01:49 PM
|
|
Regular
|
|
Join Date: Sep 2004
Posts: 57
|
|
My First Game (moving objects)
|
I think I am ready for my first game. I am most likely going to make a pong clone. I am pretty sure on how to do everything except the ball. I will most likely use a click event to have the game start however I do not know for sure how to get the ball to react to the paddles and bounch off the top and bottom of the screen
If anyone could post if they know of a tutorial to help or just how they would do it. I would be very thankful.
|
|

09-06-2004, 02:40 PM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
Do you have an idea of how to get the ball to move yet?
If you use Rectangles for the ball and the paddle locations, the collision detection will be made much simpler with the .IntersectsWith() function of the Rectangle.
So, you can do something like:
If Ball.IntersectsWith(Paddle) Then
'change the ball velocity.
End If
I usually have two variables for the X and Y velocity of the ball.
VeX = 5
VeY = -5
And then, change the ball coordinates like this:
BallX += VeX
BallY += VeY.
If you are using a Point, I think you can move it like this:
Ball.Offset(VeX, VeY)  .
|
|

09-06-2004, 04:33 PM
|
 |
Junior Contributor
|
|
Join Date: Aug 2004
Posts: 317
|
|
|
How do you make the ball go in a certain direction or change the direction?
|
|

09-06-2004, 07:39 PM
|
|
Regular
|
|
Join Date: Sep 2004
Posts: 57
|
|
|
Ok Im getting you so far my only question is how to add controls to a rectangle like to make up make the paddle go up and down make the paddle go down
|
|

09-06-2004, 09:43 PM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
So, you mean you want the paddle to go up when you press the up key?
You can use the KeyDown event to check if the Up arrow or Down arrow is pressed.
If e.KeyCode = Keys.Up Then
'Move the paddle up.
ElseIf e.KeyCode = Keys.Down Then
'Move the paddle down.

|
|

09-07-2004, 04:39 PM
|
|
Regular
|
|
Join Date: Sep 2004
Posts: 57
|
|
Ok Im trying to do what you said but it doesnt seem to be working can you take alook and tell me whats wrong
Code:
Imports System.Drawing
Public Class Form1
Inherits System.Windows.Forms.Form
Dim Pos As Point
Dim LocB As String
Dim LocR As String
Dim Dir As String
Dim BPaddle As Rectangle = New Rectangle(10, 300, 25, 350)
Dim RPaddle As Rectangle = New Rectangle(1240, 300, 25, 350)
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.Black
Me.ClientSize = New System.Drawing.Size(1284, 1002)
Me.Name = "Form1"
Me.Text = "Ping-Pong"
Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
End Sub
#End Region
Protected Overrides Sub OnPaint(ByVal e As _
System.Windows.Forms.PaintEventArgs)
Dim Ball As Rectangle = New Rectangle(41, 465, 25, 25)
Dim G As Graphics = e.Graphics
Dim TopPen As Pen = New Pen(Color.Lime, 20)
Dim BottomPen As Pen = New Pen(Color.Lime, 20)
Dim pPen As Pen = New Pen(Color.White, 5)
Dim bPen As Pen = New Pen(Color.Pink, 0)
G.DrawLine(TopPen, 1300, 10, 0, 10)
G.DrawLine(BottomPen, 1300, 958, 0, 958)
G.FillRectangle(New SolidBrush(Color.Blue), BPaddle)
G.DrawRectangle(pPen, BPaddle)
G.FillRectangle(New SolidBrush(Color.Red), RPaddle)
G.DrawRectangle(pPen, RPaddle)
End Sub
Private Sub BPaddle_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Then
'Move the paddle up.
ElseIf e.KeyCode = Keys.Down Then
'Move the paddle down.
End If
End Sub
Private Sub RPaddle_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Then
'Move the paddle up.
ElseIf e.KeyCode = Keys.Down Then
'Move the paddle down.
End If
End Sub
End Class
|
|

09-07-2004, 09:55 PM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
|
What are you trying to do right now? Move the paddle? Because it looks like you just pasted my code into your KeyDown events.
You need to either increment the X and Y properties of the paddles, or use offset as I mentioned.
|
|

09-08-2004, 01:23 PM
|
|
Regular
|
|
Join Date: Sep 2004
Posts: 57
|
|
I am still confused , I fixxed the code to what you said but it still doesnt work. I posted the code again. I would like to thank you for all the help you have given me so. Sorry if I have been a pain, all you help, helps me very much.
Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim Pos As Point
Dim LocB As String
Dim LocR As String
Dim BPaddle As Rectangle = New Rectangle(10, 300, 25, 350)
Dim RPaddle As Rectangle = New Rectangle(1240, 300, 25, 350)
Dim UpX As Integer
Dim UpY As Integer
Dim DownX As Integer
Dim DownY As Integer
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents paddle As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.paddle = New System.Windows.Forms.PictureBox
Me.SuspendLayout()
'
'paddle
'
Me.paddle.BackColor = System.Drawing.Color.Red
Me.paddle.Location = New System.Drawing.Point(224, 328)
Me.paddle.Name = "paddle"
Me.paddle.TabIndex = 0
Me.paddle.TabStop = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.Black
Me.ClientSize = New System.Drawing.Size(1284, 1002)
Me.Controls.Add(Me.paddle)
Me.Name = "Form1"
Me.Text = "Ping-Pong"
Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
Me.ResumeLayout(False)
End Sub
#End Region
Protected Overrides Sub OnPaint(ByVal e As _
System.Windows.Forms.PaintEventArgs)
Dim Ball As Rectangle = New Rectangle(41, 465, 25, 25)
Dim G As Graphics = e.Graphics
Dim TopPen As Pen = New Pen(Color.Lime, 20)
Dim BottomPen As Pen = New Pen(Color.Lime, 20)
Dim pPen As Pen = New Pen(Color.White, 5)
Dim bPen As Pen = New Pen(Color.Pink, 0)
G.DrawLine(TopPen, 1300, 10, 0, 10)
G.DrawLine(BottomPen, 1300, 958, 0, 958)
G.FillRectangle(New SolidBrush(Color.Blue), BPaddle)
G.DrawRectangle(pPen, BPaddle)
G.FillRectangle(New SolidBrush(Color.Red), RPaddle)
G.DrawRectangle(pPen, RPaddle)
End Sub
Private Sub BPaddle_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Then
UpX = 0
UpY = -5
DownX = 0
DownY = 5
BPaddle.Offset(UpX, UpY)
ElseIf e.KeyCode = Keys.Down Then
BPaddle.Offset(DownX, DownY)
End If
End Sub
Private Sub RPaddle_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
UpX = 0
UpY = -5
DownX = 0
DownY = 5
If e.KeyCode = Keys.Up Then
RPaddle.Offset(UpX, UpY)
ElseIf e.KeyCode = Keys.Down Then
RPaddle.Offset(DownX, DownY)
End If
End Sub
End Class
|
|

09-08-2004, 02:26 PM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
Both of your Key events are handling MyBase.KeyDown, despite you naming them as BPaddle_KeyDown and RPaddle_KeyDown, so both of these events will run when the key is pressed.
I don't know what you are trying to do with the controls, now.
And only controls can send key events... it's not possible to click on the actual rectangle that you have identically to the way you can click on a tangible (sort of) object like a button or textbox.
It looks like you want these four lines to be automatically set. You can set them in the Form's Load event procedure.
Code:
UpX = 0
UpY = -5
DownX = 0
DownY = 5
But if you want to handle the keys, you need to use the Form_KeyDown and assign unique keys to handle the separate functionality. It will not be good to have the up key move both paddles upward and gameplay design etiquette says you shouldn't have two different functionalities for the same key within the same screen. 
|
|

09-08-2004, 04:08 PM
|
|
Regular
|
|
Join Date: Sep 2004
Posts: 57
|
|
Ok thanks now I did what you said but it still isnt working. Here is the updated code
Code:
Imports System.Drawing
Public Class Form1
Inherits System.Windows.Forms.Form
Dim Pos As Point
Dim LocB As String
Dim LocR As String
Dim BPaddle As Rectangle = New Rectangle(10, 300, 25, 350)
Dim RPaddle As Rectangle = New Rectangle(1240, 300, 25, 350)
Dim UpX As Integer
Dim UpY As Integer
Dim DownX As Integer
Dim DownY As Integer
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents paddle As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.paddle = New System.Windows.Forms.PictureBox
Me.SuspendLayout()
'
'paddle
'
Me.paddle.BackColor = System.Drawing.Color.Red
Me.paddle.Location = New System.Drawing.Point(224, 328)
Me.paddle.Name = "paddle"
Me.paddle.TabIndex = 0
Me.paddle.TabStop = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.Black
Me.ClientSize = New System.Drawing.Size(1284, 1002)
Me.Controls.Add(Me.paddle)
Me.Name = "Form1"
Me.Text = "Ping-Pong"
Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
Me.ResumeLayout(False)
End Sub
#End Region
Protected Overrides Sub OnPaint(ByVal e As _
System.Windows.Forms.PaintEventArgs)
Dim Ball As Rectangle = New Rectangle(41, 465, 25, 25)
Dim G As Graphics = e.Graphics
Dim TopPen As Pen = New Pen(Color.Lime, 20)
Dim BottomPen As Pen = New Pen(Color.Lime, 20)
Dim pPen As Pen = New Pen(Color.White, 5)
Dim bPen As Pen = New Pen(Color.Pink, 0)
G.DrawLine(TopPen, 1300, 10, 0, 10)
G.DrawLine(BottomPen, 1300, 958, 0, 958)
G.FillRectangle(New SolidBrush(Color.Blue), BPaddle)
G.DrawRectangle(pPen, BPaddle)
G.FillRectangle(New SolidBrush(Color.Red), RPaddle)
G.DrawRectangle(pPen, RPaddle)
End Sub
Private Sub BPaddle_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Form_KeyDown
If e.KeyCode = Keys.Up Then
BPaddle.Offset(UpX, UpY)
ElseIf e.KeyCode = Keys.Down Then
BPaddle.Offset(DownX, DownY)
End If
End Sub
Private Sub RPaddle_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Form_KeyDown
If e.KeyCode = Keys.W Then
RPaddle.Offset(UpX, UpY)
ElseIf e.KeyCode = Keys.S Then
RPaddle.Offset(DownX, DownY)
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
UpX = 0
UpY = -5
DownX = 0
DownY = 5
End Sub
End Class
|
|

09-08-2004, 09:44 PM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
What do you mean by 'it's not working'? Do you get any errors?
You are probably getting an error here:
Handles Form_KeyDown
Because the event you should be handling is MyBase.KeyDown
Form_KeyDown would be the name of the event procedure if you selected it from the event boxes at the top of the code window, not the actual event. 
|
|

09-09-2004, 02:50 PM
|
|
Regular
|
|
Join Date: Sep 2004
Posts: 57
|
|
|
Ok now Im really confused please try to help me out. Basicly I want to make it so when you press up and down the blue paddle goes up and down and i want so that wen u press s and w the red paddle goes up and down. this will soon develop into two player pong
|
|

09-09-2004, 05:20 PM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
So it didn't work by changing
Handles Form_KeyDown
to
MyBase.KeyDown
Ah... you need to call Me.Invalidate() so that your form redraws at the end of your KeyDown event subroutine...s.
I would just put the KeyDown in one block. You might be confusing yourself with two.
Code:
Private Sub Form1_KeyDown(the usual goes here) Handles MyBase.KeyDown
End Sub
And then, you just check the keycode for Keys.Up and see if you can move the blue paddle up. 
|
|

09-09-2004, 08:04 PM
|
|
Regular
|
|
Join Date: Sep 2004
Posts: 57
|
|
|
Also I need to know how to start the game I think a click event that will have the ball intersect with the paddle to get the game started but I dont know how
|
Last edited by Talus123; 09-09-2004 at 08:09 PM.
|

09-10-2004, 08:10 AM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
So, you got the paddles working?
You will first need to declare some variables for the ball's position and velocity. You can use a rectangle for the ball and two integers for the velocity. 
|
|

09-11-2004, 09:11 AM
|
|
Regular
|
|
Join Date: Sep 2004
Posts: 57
|
|
|
How do you suggest to get the game to start on a click event? If so what would i write inside it?
|
|

09-11-2004, 01:19 PM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
A click event?
Well, I don't know how you are getting the ball to move, but I would suggest having a boolean variable that indicates if you have started the game.
Set this variable to true in the click event.
GameReady = True
Then, in the KeyDown event...s, you just check if GameReady is true and if it is true, you process the key events.
If GameReady Then
'keydown stuff here
End If

|
|

09-11-2004, 03:03 PM
|
|
Regular
|
|
Join Date: Sep 2004
Posts: 57
|
|
|
Ok so far I have everything set up except the balls movment and change direction basicly the ball and all its code. Im not really sure how to do it i mean ive tryed but it hasnt worked can u tell me step by step how to do this?
|
|

09-11-2004, 10:03 PM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
Well, the simple way would be to use a Timer.
Add the declares that I have mentioned above:
Quote:
|
You can use a rectangle for the ball and two integers for the velocity.
|
If you set up some initial values for the ball velocity and rectangle, you can move the ball like this.
BallRect.Offset(BallVx, BallVy)
If you give the rectangle a nonzero width and height, you should be able to draw the ball. Refreshing the display in the timer tick should let you make the ball move. 
|
|

09-12-2004, 09:00 AM
|
|
Regular
|
|
Join Date: Sep 2004
Posts: 57
|
|
|
So should this code alow the ball to move?
[code]
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.Invalidate()
If Ball.IntersectsWith(BPaddle) Then
Ball.Offset(VeX, VeY)
End If
If Ball.IntersectsWith(RPaddle) Then
Ball.Offset(VeX, VeY)
End If
[code]
Because its not working. Is it because before this I need to ball to intersect with a paddle? If so how do I code that?
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|