Talus123 09-06-2004, 01:49 PM 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.
Iceplug 09-06-2004, 02:40 PM 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) :).
blindreaper666 09-06-2004, 04:33 PM How do you make the ball go in a certain direction or change the direction?
Talus123 09-06-2004, 07:39 PM 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
Iceplug 09-06-2004, 09:43 PM 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.
:)
Talus123 09-07-2004, 04:39 PM Ok Im trying to do what you said but it doesnt seem to be working can you take alook and tell me whats wrong
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
Iceplug 09-07-2004, 09:55 PM 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.
Talus123 09-08-2004, 01:23 PM 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.
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
Iceplug 09-08-2004, 02:26 PM 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.
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. :)
Talus123 09-08-2004, 04:08 PM Ok thanks now I did what you said but it still isnt working. Here is the updated 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
Iceplug 09-08-2004, 09:44 PM 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. :)
Talus123 09-09-2004, 02:50 PM 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
Iceplug 09-09-2004, 05:20 PM 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.
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. :)
Talus123 09-09-2004, 08:04 PM 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
Iceplug 09-10-2004, 08:10 AM 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. :)
Talus123 09-11-2004, 09:11 AM How do you suggest to get the game to start on a click event? If so what would i write inside it?
Iceplug 09-11-2004, 01:19 PM 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
:)
Talus123 09-11-2004, 03:03 PM 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?
Iceplug 09-11-2004, 10:03 PM Well, the simple way would be to use a Timer.
Add the declares that I have mentioned above:
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. :)
Talus123 09-12-2004, 09:00 AM 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?
Iceplug 09-12-2004, 09:14 AM That would be in the Timer. .Offset is what moves the ball. You will want to do this periodically. That's why you need to use a Timer.
Me.Invalidate() should come after you have moved the Ball.
Also, it looks like you are only moving the ball when it is hitting the paddle. Surely, you want the ball to be moving always.
So, the Offset shouldn't be in those If statements. Instead, you change the velocity of the ball. You will have to negate the X velocity (VeX) in order to make the ball bounce off of the paddle.
VeX = -VeX
And that works for both paddles, surprisingly, because that just tells the ball to move in the other direction.
Talus123 09-12-2004, 09:26 AM Amsome I think Im almost there! However I know how to use a timer but not exactly how you want me to. Can you post some sample code that would show what you mean
Iceplug 09-12-2004, 09:40 AM Timers are easy. Just set the Enabled property to True, set the Interval to something like 100 (I think 100 is the default), and at design time, you can double-click on the timer you've added to access the Timer's Tick event.
You can also select the timer from the first box in the top of the code window, and the tick event from the other box up there. :)
Talus123 09-12-2004, 09:45 AM AWSOME its working! Now my question is how do I make it so that the ball doesnt fly out of the window and actually bounces off it
Iceplug 09-12-2004, 09:52 AM Interesting with .NET is that the paddle collision detection is easier than the form's collision detection.
But to check if the ball is off the top of the form, check if the Ball's .Top is less than 0.
For the left end, you check if the .Left is less than 0.
For the bottom end, you check if the .Bottom is greater than the clientsize.height of the form.
For the right end, you check if the .Right is greater than the clientsize.width of the form.
:)
Talus123 09-12-2004, 09:59 AM But where do I add these "check .direction" to? an event? whcih one?
Iceplug 09-12-2004, 10:05 AM Well, the ball only moves in the timer, so you will want to check if the ball moves out of bounds while in the timer event because that's where it moves.
Talus123 09-12-2004, 10:07 AM But I mean what would the code look like
Iceplug 09-12-2004, 10:11 AM I shouldn't write the code for you. But, all it is is a simple check with < or >.
If Ball.Bottom < Me.ClientSize.Height Then
Oh, and the Top and Bottom collisions should flip VeY; the other two should negate VeX.
Talus123 09-12-2004, 10:23 AM This is what I but it doesnt work. What goes inside the if statmetns?
[code]
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
Ball.Offset(VeX, VeY)
If Ball.Left < 0 Then
End If
If Ball.Right < Me.ClientSize.Width Then
End If
If Ball.Bottom < 0 Then
End If
If Ball.Top < Me.ClientSize.Height Then
End If
End Sub
[code]
Iceplug 09-12-2004, 07:53 PM In the paddle intersection, you have this:
VeX = -VeX
To make the ball bounce off of the right and left ends of the paddle.
So, you should do the same thing with the left and right ends of the form.
For the top and bottom ends of the form, you negate VeY.
But, before you do that, fix these If statements:
If Ball.Bottom < 0 Then
End If
This says that you want the ball to bounce once it gets totally off of the top of the form. While that might work, it won't look to good to have the ball disappear briefly at the top.
If Ball.Top < Me.ClientSize.Height Then
End If
This does the same except for the bottom of the form.
So, you just need to switch the .Top and the .Bottom that you have here.
If Ball.Bottom < Me.ClientSize.Height Then
End If
If Ball.Top < 0 Then
End If
Talus123 09-12-2004, 08:17 PM Ok everything works but the paddles now this is the code I have
If Ball.IntersectsWith(BPaddle) Then
VeX = -VeX
VeY = -VeY
End If
If Ball.IntersectsWith(RPaddle) Then
VeX = -VeX
VeY = -VeY
End If
Iceplug 09-12-2004, 10:18 PM You don't need to bounce the ball up or down when it hits the paddle, so remove the
VeY = -VeY
line from those If statements. :)
Or is there something else that is wrong with the paddles?
Talus123 09-13-2004, 01:00 PM No the problem is the ball just flys right through the paddles and into the wall
Iceplug 09-13-2004, 07:26 PM Hmm... did you put that collision detection into the Timer?
After all, all of the collision should be in the timer. :)
I didn't see it there in your Tick event that you posted last time.
Talus123 09-13-2004, 07:28 PM WOOT I got it to work now just some weird stuff happening
1. For some reason rather than going strait it just is jumping wile it moves
2. It only goes back and forth how do I make it so it moves according to where you hit it with
This is the code check it out its pretty cool anyway
Imports System.Drawing
Public Class Form1
Inherits System.Windows.Forms.Form
Dim Pos As Point
Dim Loc 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
Dim Ball As Rectangle = New Rectangle(620, 450, 25, 25)
Dim VeX As Integer = 10
Dim VeY As Integer = -10
Dim GameReady As Boolean
Dim t As New System.Timers.Timer(1000)
#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
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.paddle = New System.Windows.Forms.PictureBox
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.SuspendLayout()
'
'paddle
'
Me.paddle.Location = New System.Drawing.Point(0, 0)
Me.paddle.Name = "paddle"
Me.paddle.TabIndex = 0
Me.paddle.TabStop = False
'
'Timer1
'
Me.Timer1.Enabled = True
'
'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 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)
Dim Open As Rectangle = New Rectangle(390, 300, 500, 350)
G.FillRectangle(New SolidBrush(Color.Blue), BPaddle)
G.DrawRectangle(pPen, BPaddle)
G.FillRectangle(New SolidBrush(Color.Red), RPaddle)
G.DrawRectangle(pPen, RPaddle)
G.FillRectangle(New SolidBrush(Color.Plum), Ball)
End Sub
Private Sub BPaddle_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Me.Invalidate()
If GameReady = True Then
If e.KeyCode = Keys.Up Then
BPaddle.Offset(UpX, UpY)
ElseIf e.KeyCode = Keys.Down Then
BPaddle.Offset(DownX, DownY)
End If
End If
End Sub
Private Sub RPaddle_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Me.Validate()
If GameReady = True Then
If e.KeyCode = Keys.W Then
RPaddle.Offset(UpX, UpY)
ElseIf e.KeyCode = Keys.S Then
RPaddle.Offset(DownX, DownY)
End If
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If GameReady = True Then
Loc = "Start.bmp"
End If
UpX = 0
UpY = -5
DownX = 0
DownY = 5
Me.Invalidate()
End Sub
Private Sub Form1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Click
t.Enabled = True
GameReady = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
Ball.Offset(VeX, VeY)
If Ball.IntersectsWith(BPaddle) Then
VeX = -VeX
VeY = -VeY
End If
If Ball.IntersectsWith(RPaddle) Then
VeX = -VeX
VeY = -VeY
End If
'SO BALL DOESNT LEAVE FORM
If Ball.Left < 0 Then
VeX = -VeX
End If
If Ball.Right > Me.ClientSize.Width Then
VeX = -VeX
End If
If Ball.Bottom > Me.ClientSize.Height Then
VeY = -VeY
End If
If Ball.Top > 0 Then
VeY = -VeY
End If
Me.Invalidate()
End Sub
End Class
Talus123 09-13-2004, 07:47 PM Everything is actually working believe it or not it is almost done now all I need is a scoring system. What is the easiest way to do this? To have textboxs or to have a status bar?
Iceplug 09-13-2004, 07:50 PM If Ball.Top > 0 Then
You need to flip around the inequality that you are using here, because the ball top should be less than zero for the majority of the game. :)
Talus123 09-14-2004, 05:19 PM How do you suggest I make the scoring system?
Iceplug 09-14-2004, 11:11 PM That's up to you. I cannot make all of the decisions in order for you to create unique games yourself. :)
But, I think the typical pong game has two scores: one for the left paddle and one for the right.
So, you can use two integers to hold the scores for both paddles.
When the ball intersects with the red paddle, increment the red score.
When the ball intersects with the blue paddle, increment the blue score.
You can use the .DrawString method of the Graphics object to draw the scores. Using the form font will keep you from having to declare a new font. :)
Talus123 09-15-2004, 01:52 PM Ok right now Im trying to make it so the paddles cannot leave the screen it works for one paddle but no the second here is my code
Private Sub RPaddle_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Me.Validate()
If GameReady = True Then
If RPaddle.Bottom > Me.ClientSize.Height Then
If e.KeyCode = Keys.W Then
RPaddle.Offset(UpX, UpY)
End If
ElseIf RPaddle.Top < 0 Then
If e.KeyCode = Keys.S Then
RPaddle.Offset(DownX, DownY)
End If
End If
If RPaddle.Top > 0 Then
If e.KeyCode = Keys.W Then
RPaddle.Offset(UpX, UpY)
End If
End If
If RPaddle.Bottom < Me.ClientSize.Height Then
If e.KeyCode = Keys.S Then
RPaddle.Offset(DownX, DownY)
End If
End If
End If
If GameReady = True Then
If BPaddle.Bottom > Me.ClientSize.Height Then
If e.KeyCode = Keys.Up Then
BPaddle.Offset(UpX, UpY)
End If
ElseIf BPaddle.Top < 0 Then
If e.KeyCode = Keys.Down Then
BPaddle.Offset(DownX, DownY)
End If
End If
If BPaddle.Top > 0 Then
If e.KeyCode = Keys.Up Then
BPaddle.Offset(UpX, UpY)
End If
End If
If BPaddle.Bottom < Me.ClientSize.Height Then
If e.KeyCode = Keys.Down Then
BPaddle.Offset(DownX, DownY)
End If
End If
End If
End Sub
Why for Rpaddle it works but for Bpaddle its doesnt
Iceplug 09-15-2004, 05:03 PM Hmmm... they look identical. I don't see why one would work and the other doesn't.
Maybe, you should be calling Invalidate() instead of Validate() at the top?
Also, the first two blocks under each If GameReady = True block can be removed, because the following two blocks check the same range of values and more.
And, the two If GameReady = True Then blocks can be lumped into one.
(by removing the End If and the If GameReady = True lines in the middle).
:)
Talus123 09-15-2004, 07:01 PM How do you suggest I fix this Ive tryed everything you said and also how would I increment the score I tried and that didnt work
Iceplug 09-16-2004, 07:55 AM Did you make the changes that I suggested to make in the previous post?
What does your code look like now?
Talus123 09-16-2004, 08:19 AM ICEPLUG IT WORKS. The problem had nothing to do with that at all it was somewhere else in the code. Im so happy it works now I would like to put it on my website how do you suggest I do this. And once again thanks so much Iceplug you have taught me so much
Talus123 09-16-2004, 08:23 AM Besides the question I just posted I only have two more. 1. for some reason since this game is two player if playerone is moving his paddle up and playertwo moves his paddle playerone stops moving.
Iceplug 09-16-2004, 08:24 AM You mean so that people can download your game?
Edit:
In order to have more than two people, you'd have to declare four more boolean variables (one to represent if W is pressed, if S is pressed, same for Up and Down).
In the KeyDown, you set the appropriate boolean to True.
In the KeyUp event, you set the appropriate boolean to False.
Now, you'll do the movement in the Timer, and instead of checking the KeyCode, you check the Boolean. :)
Let me know if you get stuck.
Talus123 09-16-2004, 08:33 AM Ya I want to post it on my website people can download and play it
Iceplug 09-16-2004, 02:03 PM Well, just zip up your project, put it on your webspace, and add a hyperlink to it. :)
How're the key booleans coming?
Talus123 09-16-2004, 09:01 PM awsome
|