|
-
Apr 22nd, 2007, 01:51 PM
#1
Thread Starter
Lively Member
moving the ball in PONG
vb Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick, MyBase.Move
Dim movepix As Integer
movepix = 5
Ball = Me.CreateGraphics
Ball.Clear(Color.Gray)
If y = 230 Then
movepix = (movepix * -1)
End If
If y = 0 Then
movepix = (movepix * -1)
End If
If x = 230 Then
movepix = (movepix * -1)
End If
If x = 0 Then
movepix = (movepix * -1)
Else
x += movepix
y += movepix
End If
Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
End Sub
this is what i came up with for moving the ball in a pong game i am creating.....all the ball does is travel at a diagonal until it gets stuck in the lower-right corner.....i can't get the balll to richochet or move around the dimensions of the window.....also, i cant find a good was of clear the ball graphic without changing the backfround color.
-
Apr 22nd, 2007, 05:14 PM
#2
Lively Member
Re: moving the ball in PONG
The problem that you are having is that you are using the same variable for moving the ball in the x and y directions.
What you should do is have 2 variables for the balls speed, like xSpeed and ySpeed and set them to whatever. Then when the ball's x position is less than 0, make xSpeed = -xSpeed, when the x position+width of ball is greater than the right side of the screen, again make xSpeed = -xSpeed. Similarly for the y bounds and ySpeed. Then in the timer, do the bound checks then at the end do: ball.x += xSpeed and ball.y +=ySpeed.
Just taking your code and changing it:
Code:
Dim xSpeed As Integer 'set the values for these
Dim ySpeed As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick, MyBase.Move
Ball = Me.CreateGraphics
Ball.Clear(Color.Gray)
If (y + Ball.height)>= 230 Then
ySpeed = -ySpeed
End If
If y <= 0 Then
ySpeed = -ySpeed
End If
If (x + Ball.width)>= 230 Then
xSpeed = -xSpeed
End If
If x <= 0 Then
xSpeed = -xSpeed
End If
x += xSpeed
y += ySpeed
Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
End Sub
Those conditions may have to change for the Y, I forget if VB starts counting the Y coordinate from the top or the bottom.
-
Apr 22nd, 2007, 05:44 PM
#3
Thread Starter
Lively Member
Re: moving the ball in PONG
that's kinda of a general problem i have been running in to....... the .height() and .width() are not members of the System.Drawing.Graphics class.....
-
Apr 23rd, 2007, 12:43 PM
#4
Lively Member
Re: moving the ball in PONG
Ok, well if they aren't members of that class, just use constants instead of calling those functions/methods. I see that in the Ball.FillEllipse line, that you are using 20 for the height and width, so just change the conditions to be x + 20 and y + 20.
EDIT
I just looked as MSDN site and that is really odd, and kind of stupid, that the class doesn't have width and height functions OR member variables. lol
Last edited by fartman_900; Apr 23rd, 2007 at 12:47 PM.
-
Apr 24th, 2007, 05:32 AM
#5
Hyperactive Member
Re: moving the ball in PONG
There is nothing strange about the graphics class having to member varibles. This is because the graphics class does not mean a 'graphic' as such but the graphics class is an interface to draw shapes.
-
Apr 24th, 2007, 08:12 AM
#6
Thread Starter
Lively Member
Re: moving the ball in PONG
well, what is the best way to manipulate their location...re-draw them every Timer1_Tick()?
-
Apr 24th, 2007, 08:56 AM
#7
Hyperactive Member
Re: moving the ball in PONG
That or use a managed game loop. It is a lot simpler than it sounds. Just make a while loop and put the DoEvents in it. Usually when you start a loop, windows forgets to breathe until it finishes and your computer crashes. DoEvents stops this from happening and tells windows to breathe.
Note that this code is classic VB code but the principles should remain.
Code:
Dim bLoop as Boolean
bLoop = true
While(bLoop)
'<- Put game code here
System.DoEvents
wend
Avoid using timers as much as you can !
-
Apr 24th, 2007, 09:33 AM
#8
Lively Member
Re: moving the ball in PONG
 Originally Posted by singularis
There is nothing strange about the graphics class having to member varibles. This is because the graphics class does not mean a 'graphic' as such but the graphics class is an interface to draw shapes. 
Oh, ok. I guess I didn't read the document like I should have lol, thanks.
-
Apr 24th, 2007, 07:03 PM
#9
Frenzied Member
Re: moving the ball in PONG
You might also want to look into 2D vectors if you plan on doing more 2D programming(or 3D for that matter) - they help a lot when calculating and understanding objects in a coordinate system.
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Apr 24th, 2007, 07:46 PM
#10
Thread Starter
Lively Member
Re: moving the ball in PONG
vb Code:
Dim movepix As Integer
Dim negmovepix As Integer
Dim go As Boolean
go = False
movepix = 5
negmovepix = -5
Ball = Me.CreateGraphics
Ball.Clear(Me.BackColor)
While go
x += 5
y += 5
Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
Application.DoEvents()
End While
I put that in, but when i run it, the program just flashed a ball and bar and then disappears, how do i get the program to move my ball around and my bar to NOT disappear. also i want to be able to run my keypress...
vb Code:
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles Me.KeyPress
Bar = Me.CreateGraphics
Select Case e.KeyChar
Case ","c
Ball.Clear(Me.BackColor)
Bar.FillRectangle(Brushes.Black, (x1 - 10), 295, 80, 15)
x1 = x1 - 10
Case "<"c
Ball.Clear(Me.BackColor)
Bar.FillRectangle(Brushes.Black, (x1 - 10), 295, 80, 15)
x1 = x1 - 10
Case "."c
Ball.Clear(Me.BackColor)
Bar.FillRectangle(Brushes.Black, (x1 - 10), 295, 80, 15)
x1 = x1 + 10
Case ">"c
Ball.Clear(Me.BackColor)
Bar.FillRectangle(Brushes.Black, (x1 - 10), 295, 80, 15)
x1 = x1 + 10
Case "s"c
Timer1.Start()
Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
Bar.FillRectangle(Brushes.Black, x1, 295, 80, 15)
End Select
End Sub
-
Apr 24th, 2007, 09:19 PM
#11
Lively Member
Re: moving the ball in PONG
The first thing that is wrong is that you set go to false, when it should be true. So the loop is executing once then ending. Also, inside the while loop you need to put the collision statements (or a method call to check for collisions). As for the key press, I'm not entirely sure (I don't code in VB at all) but I thought that the KeyPress method gets called automatically when a key is pressed so it might be fine. Just another thing about the case statements, all of them move the bar down, just change two of them to + 10 instead of - 10.
-
Apr 25th, 2007, 02:51 AM
#12
Hyperactive Member
Re: moving the ball in PONG
@kylekocak: Read Fartman's post. If you want nice smooth keyboard input then I suggest trying a little trick:
You create a varible remembering the velocity of the paddle
e.g. Dim Vel as Long
Then in keypress event set the velocity to 1 if they press right and -1 if they press left.
Then in the code in the loop you tell the ball to move depending on its velocity
e.g. x1 = x1 + Vel
That way the paddle will move smoothly
-
Apr 25th, 2007, 08:06 PM
#13
Thread Starter
Lively Member
Re: moving the ball in PONG
bb Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick, MyBase.Move
Ball = Me.CreateGraphics
Ball.Clear(Me.BackColor)
If y >= 230 Then
Do
y -= 5
Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
Application.DoEvents()
Loop Until y <= 0
Else
x += 5
y += 5
Ball.FillEllipse(Brushes.Yellow, x + 5, y + 5, 20, 20)
End If
If x >= 230 Then
Do
x -= 5
Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
Application.DoEvents()
Loop Until x <= 0
Else
x += 5
y += 5
Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
End If
End Sub[HIGHLIGHT=vb]
[/HIGHLIGHT]
This code gets the ball to move around the screen, problem is once it hits the 230 for the x and y coordinates, it "transports" to the x or y =0 respectively.....and yes, it is because it is in a Do loop, no need to point that out....i thought the application.DoEvents would have taken care of that.....how do i get the ball to SLOWLY move?
-
Apr 26th, 2007, 02:40 AM
#14
Hyperactive Member
Re: moving the ball in PONG
Lower the value from 5 to 0.1 !
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
|