Ping Pong Game
Ping Pong Game
Ping Pong Game
Ping Pong Game
Ping Pong Game
Ping Pong Game Ping Pong Game Ping Pong Game Ping Pong Game Ping Pong Game Ping Pong Game Ping Pong Game Ping Pong Game
Ping Pong Game Ping Pong Game
Ping Pong Game
Go Back  Xtreme Visual Basic Talk > > > Ping Pong Game


Reply
 
Thread Tools Display Modes
  #1  
Old 03-15-2012, 11:55 AM
Haxed Haxed is offline
Newcomer
 
Join Date: Mar 2012
Posts: 4
Default Ping Pong Game


Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

#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 - Player.Height Then _
        Player.Location = New Point(Player.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 _
        - Player.Height Then _
        Computer.Location = New Point(Computer.Location.X, gameBall.Location.Y)
        ' Move the game ball.
        gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)

        ' 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 top wall.
        If gameBall.Location.Y < 0 Then
            gameBall.Location = New Point(gameBall.Location.X, 0)
            yVel = -yVel
        End If

        ' Check for player paddle.
        If gameBall.Bounds.IntersectsWith(Player.Bounds) Then
            gameBall.Location = New Point(Player.Location.X - gameBall.Size.Width, _
            gameBall.Location.Y)
            xVel = -xVel
        End If

        ' Check for computer paddle.
        If gameBall.Bounds.IntersectsWith(Computer.Bounds) Then
            gameBall.Location = New Point(Computer.Location.X + Computer.Size.Width + 1, _
            gameBall.Location.Y)
            xVel = -xVel
        End If


        ' The player's scores.
        Dim compScore As Integer = 0
        Dim plrScore As Integer = 0

        ' Check for right wall.
        If gameBall.Location.X > Me.Width - gameBall.Size.Width - Player.Width Then
            compScore += 1
            gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
            PlayerScore.Text = Convert.ToString(compScore)
        End If
    End Sub
#End Region

#Region "Globals"
    Dim speed As Single = 15 ' Ball Speed
    Dim rndInst As New Random() ' Random instance
    Dim xVel As Single = Math.Cos(rndInst.Next(9, 10)) * speed
    Dim yVel As Single = Math.Sin(rndInst.Next(9, 10)) * speed
#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
        Player.Location = New Point(Me.Width - 44, Player.Location.Y)
        ComputerScore.Location = New Point(Me.Width - 54, ComputerScore.Location.Y)
    End Sub
#End Region



End Class

This is the code for my Pong game, Can anybody help me, I have two problems with the code, the first being that when the computer scores, the score only ever goes to 1 and never 1,2,3,...etc.

also the computer seems unbeatable and I cannot score past him

Thanks alot

Last edited by Flyguy; 03-19-2012 at 03:51 AM.
Reply With Quote
  #2  
Old 03-15-2012, 12:26 PM
Qua's Avatar
QuaPing Pong Game Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

You declare your score variables inside the scope of the methods. When you do this the variable will be removed after the method ends. The next time they method is called, the variable will be re-declared and thus have a value of 0 to begin with.

To fix this issue you need to declare your variables in a scope outside the method, such as in the class.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #3  
Old 03-15-2012, 12:48 PM
Haxed Haxed is offline
Newcomer
 
Join Date: Mar 2012
Posts: 4
Default

Quote:
Originally Posted by Qua View Post
You declare your score variables inside the scope of the methods. When you do this the variable will be removed after the method ends. The next time they method is called, the variable will be re-declared and thus have a value of 0 to begin with.

To fix this issue you need to declare your variables in a scope outside the method, such as in the class.
Thanks worked perfectly, anybody any idea's how to make it beatable now, I read something about a number generator making the computers response somewhat random but that is way too complicated for the likes of me
Reply With Quote
  #4  
Old 03-15-2012, 01:44 PM
Qua's Avatar
QuaPing Pong Game Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

Make his paddle move slower, or don't let him react to an incoming ball before it is within a certain range. Possibly a combination of the two.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #5  
Old 03-15-2012, 02:11 PM
Haxed Haxed is offline
Newcomer
 
Join Date: Mar 2012
Posts: 4
Default

Quote:
Originally Posted by Qua View Post
Make his paddle move slower, or don't let him react to an incoming ball before it is within a certain range. Possibly a combination of the two.
how do you go about doing that? :P
Reply With Quote
  #6  
Old 03-15-2012, 03:27 PM
passel's Avatar
passelPing Pong Game passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Either don't move the paddle as often (won't work in your case), or move it less distance.
Right now you place the paddle instantly at the same level as the ball, so it is tracking one to one.
If on the other hand, you determined that the ball is above or below center of the paddle, and you move the paddle a set amount in that direction, not instantly jump to that position, then the paddle can fall behind or not get to the ball in time.
You will probably have to play with the distance moved per frame to get what you want.
You could also change the distance per frame depending on how far the computer falls behind, so that if it starts loosing badly it can improve to try to catch up.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #7  
Old 03-16-2012, 04:00 PM
Haxed Haxed is offline
Newcomer
 
Join Date: Mar 2012
Posts: 4
Default

that sounds pretty complicated, wouldn't even know how to go about coding that :P
Reply With Quote
  #8  
Old 03-17-2012, 06:33 PM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default Programming ping pong in vb.net shouldn't be hard at all..

Quote:
Originally Posted by Haxed
that sounds pretty complicated, wouldn't even know how to go about coding that :P
Coding a pong game really shouldn't have to be that complicated.
I did a search of the .net part of the forum for "pong" and found 38 threads, but only one example attachment on this thread.

The attachment had some paddle collision issues that weren't corrected in the attachment, so I guess I'll post a simple example to get you started.

It's only a single paddle version so you have to do a little work to convert it to two paddles but hopefully you'll be able to do that once you have a working sample and maybe a tutorial:
Creating a fully-functional pong game: A step-by-step guide to creating a pong game in VB.NET

Note: you didn't post (in your original code snippet, post #1) what keyboard keys you were using to move the paddles (just esc key to close), so I took the liberty of providing two options in the demo.
Attached Files
File Type: zip PingPong.zip (11.3 KB, 21 views)

Last edited by surfR2911; 03-17-2012 at 06:42 PM.
Reply With Quote
  #9  
Old 03-17-2012, 07:08 PM
passel's Avatar
passelPing Pong Game passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Quote:
Originally Posted by surfR2911 View Post
...
Note: you didn't post (in your original code snippet, post #1) what keyboard keys you were using to move the paddles (just esc key to close), so I took the liberty of providing two options in the demo.
He's not using keys.
The player moves his paddle with the mouse and the computer moves its paddle by putting the paddle at the same Y level as the ball.
It is near the top of the posted code, look for
' Move the paddle according to the mouse position.
and
'Set the computer player to move according to the ball's position."

It's not two players, it's one player against the computer.
I'm not sure at this point that he wrote the code, so is lost about how to modify it.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #10  
Old 03-18-2012, 04:07 PM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default pong redux

I did see the mouse code, but it just seemed kind of awkward..I've always thought of pong as an arcade game (it is listed as such on the pong wiki page), and for me arcade games are always thumbmasher/buttonmasher type games - i.e. keyboard driven.

My first experience with Pong was the Atari 2600 version, but I've read that it was actually "inspired by" (but probably not based on) a game called "Tennis for Two" run off a vacuum tube analog computer (a Donner Model 30)..at least that's one of the listings on the Pong Story page.

I also read that a group of a group of scientists and engineers at Brookhaven
Labs decided to recreate the Tennis for Two game:
Resurrecting One of the World's 1st Video Games
Not quite as involved as re-creating the original Babbage Difference Engine, but still a nice techno-history project.

Quote:
I'm not sure at this point that he wrote the code, so is lost about how to modify it.
I'm not sure either, but I hope he is using the development of the game as a learning experience, both in ui design and general .net programming.

Plus it would be a nice to have somewhere on the forum:
1.) A VB.Net pong example that uses both mouse and keyboard
2.) VB.Net versions demo-ing a single paddle and/or set of double paddle

Last edited by surfR2911; 03-18-2012 at 04:14 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Ping Pong Game
Ping Pong Game
Ping Pong Game Ping Pong Game
Ping Pong Game
Ping Pong Game
Ping Pong Game Ping Pong Game Ping Pong Game Ping Pong Game Ping Pong Game Ping Pong Game Ping Pong Game
Ping Pong Game
Ping Pong Game
 
Ping Pong Game
Ping Pong Game
 
-->