Results 1 to 2 of 2

Thread: Creating 2 player pong, not working!

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    11

    Creating 2 player pong, not working!

    So I'm trying to create a 2 player pong game, however I have 2 problems.
    1. After adding a moving ball, the player1's paddle will now not move. The arrow keys will not make it move, however it was all working before adding the ball
    2. I haven't done this in this version yet, but in another version I added 2 players, and only one player could move at a time. As soon as I used the arrow keys (player 1) player 2 could not move using WASD.

    Thanks for any help! I'm using VB 2008 Express, Code below:
    Code:
    Public Class Pong
    
        
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            Select Case e.KeyCode
                Case Keys.Left
                    Me.Player1.Left -= 1
                Case Keys.Up
                    Me.Player1.Top -= 1
                Case Keys.Right
                    Me.Player1.Left += 1
                Case Keys.Down
                    Me.Player1.Top += 1
            End Select
        End Sub
        Private Sub Ball_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Ball.PreviewKeyDown
            Select Case e.KeyCode
                Case Keys.Left, Keys.Up, Keys.Right, Keys.Down
                    e.IsInputKey = True
            End Select
        End Sub
    
        Private Sub Pong_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        'Set up ball speeed and movement
        Dim speed As Single = 10
        'Create a random instance to keep the game exciting
        Dim rndInst As New Random()
        'Set the minimum and maximum angle of randomisation
        Dim xVel As Single = Math.Cos(rndInst.Next(8, 12)) * speed
        Dim yVel As Single = Math.Sin(rndInst.Next(4, 14)) * speed
        Private Sub Gametimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Gametimer.Tick
            'Move the ball
            Ball.Location = New Point(Ball.Location.X + xVel, Ball.Location.Y + yVel)
    
            'Now we must make sure the ball never leaves the game area
            If Ball.Location.Y < 0 Then
                Ball.Location = New Point(Ball.Location.X, 0)
                'Set the velocity to the opposite of the velocity when hitting the ball
                yVel = -yVel
            End If
    
            'Check if the ball hits the bottom of the playing feild
            If Ball.Location.Y > Me.Height - Ball.Size.Height - 45 Then
                Ball.Location = New Point(Ball.Location.X, Me.Height - Ball.Size.Height - 45)
                yVel = -yVel
            End If
    
            'Check if the ball hits a players paddle
            If Ball.Bounds.IntersectsWith(Player1.Bounds) Then
                Ball.Location = New Point(Player1.Location.X - Ball.Size.Width, Ball.Location.Y)
                xVel = -xVel
            End If
    
            'Check if the ball hits a players paddle
            If Ball.Bounds.IntersectsWith(player2.Bounds) Then
                Ball.Location = New Point(player2.Location.X + Ball.Size.Width, Ball.Location.Y)
                xVel = -xVel
            End If
    
    
    
        End Sub
    
        Private Sub Ball_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ball.Click
    
        End Sub
    
        Private Sub play_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles play.Click
            Gametimer.Enabled = True
        End Sub
    End Class

  2. #2
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Creating 2 player pong, not working!

    I don't see exactly whats stopping player 1 from moving but you need better controls. So I'm gonna help you on that. In this simple project, just add a picturebox and a timer. This is just for player 1 though. For player 2, just add more constants but keep going on the binary ones from player one. It is currently at 1 2 4 8, so for player 2 itll be at 16 32 64 128:

    vb.net Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Public Class Form1
    5.  
    6.     Private Const P1_UP As Keys = Keys.Up
    7.     Private Const P1_DOWN As Keys = Keys.Down
    8.     Private Const P1_LEFT As Keys = Keys.Left
    9.     Private Const P1_RIGHT As Keys = Keys.Right
    10.  
    11.     'Key Flags (MUST BE IN BINARY FORMAT THE MORE KEYS YOU USE: ex. 1, 2, 4, 8, 16, 32, 64, 128, etc.
    12.     Private Const P1_UP_FLAG As Integer = 1
    13.     Private Const P1_DOWN_FLAG As Integer = 2
    14.     Private Const P1_LEFT_FLAG As Integer = 4
    15.     Private Const P1_RIGHT_FLAG As Integer = 8
    16.  
    17.     Private Keystate As Integer
    18.  
    19.     Private Function Check_Key(ByVal Keyflag As Integer) As Integer
    20.  
    21.         Check_Key = Keystate And Keyflag
    22.  
    23.     End Function
    24.  
    25.     Private Sub Keyboard_Control()
    26.  
    27.         If Check_Key(P1_LEFT_FLAG) > 0 Then PictureBox1.Left -= 5
    28.         If Check_Key(P1_RIGHT_FLAG) > 0 Then PictureBox1.Left += 5
    29.         If Check_Key(P1_UP_FLAG) > 0 Then PictureBox1.Top -= 5
    30.         If Check_Key(P1_DOWN_FLAG) > 0 Then PictureBox1.Top += 5
    31.  
    32.     End Sub
    33.  
    34.     Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    35.  
    36.         Select Case e.KeyCode
    37.             Case P1_UP
    38.                 Keystate = Keystate Or P1_UP_FLAG
    39.             Case P1_DOWN
    40.                 Keystate = Keystate Or P1_DOWN_FLAG
    41.             Case P1_LEFT
    42.                 Keystate = Keystate Or P1_LEFT_FLAG
    43.             Case P1_RIGHT
    44.                 Keystate = Keystate Or P1_RIGHT_FLAG
    45.         End Select
    46.  
    47.     End Sub
    48.  
    49.     Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    50.  
    51.         Select Case e.KeyCode
    52.             Case P1_UP
    53.                 Keystate = Keystate And (Not P1_UP_FLAG)
    54.             Case P1_DOWN
    55.                 Keystate = Keystate And (Not P1_DOWN_FLAG)
    56.             Case P1_LEFT
    57.                 Keystate = Keystate And (Not P1_LEFT_FLAG)
    58.             Case P1_RIGHT
    59.                 Keystate = Keystate And (Not P1_RIGHT_FLAG)
    60.         End Select
    61.  
    62.     End Sub
    63.  
    64.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Gametimer.Tick[B][/B]
    65.           Keyboard_Control
    66.     End Sub
    67. End Class

    You'll notice the controls are vastly improved, and you can press more than one key at the same time, which should help when player 2 comes along. Another thing is that it is never wise to use a Timer at all for game programming. Its better to use a realtime Game Loop locked at 60 frames per second, like this example spliced with the above example, this time without a timer:

    vb.net Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Public Class Form1
    5.  
    6.     Private Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef X As Long) As Integer
    7.     Private Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef X As Long) As Integer
    8.  
    9.     Private Structure Sprite_Type
    10.         Public X As Integer
    11.         Public Y As Integer
    12.         Public Width As Integer
    13.         Public Height As Integer
    14.         Public Texture As Image
    15.         Public Texture_Graphics As Graphics
    16.     End Structure
    17.  
    18.     'Key Functions
    19.     Private Const P1_UP As Keys = Keys.Up
    20.     Private Const P1_DOWN As Keys = Keys.Down
    21.     Private Const P1_LEFT As Keys = Keys.Left
    22.     Private Const P1_RIGHT As Keys = Keys.Right
    23.  
    24.     'Key Flags (MUST BE IN BINARY FORMAT THE MORE KEYS YOU USE: ex. 1, 2, 4, 8, 16, 32, 64, 128, etc.
    25.     Private Const P1_UP_FLAG As Integer = 1
    26.     Private Const P1_DOWN_FLAG As Integer = 2
    27.     Private Const P1_LEFT_FLAG As Integer = 4
    28.     Private Const P1_RIGHT_FLAG As Integer = 8
    29.  
    30.     Private Ticks_Per_Second As Long
    31.     Private Start_Time As Long
    32.  
    33.     Private Milliseconds As Integer
    34.     Private Get_Frames_Per_Second As Integer
    35.     Private Frame_Count As Integer
    36.  
    37.     Private Keystate As Integer
    38.     Private Running As Boolean
    39.  
    40.     Dim Backbuffer_Graphics As Graphics
    41.  
    42.     Private Sprite As Sprite_Type
    43.  
    44.     Private Function Hi_Res_Timer_Initialize() As Boolean
    45.         If QueryPerformanceFrequency(Ticks_Per_Second) = 0 Then
    46.             Hi_Res_Timer_Initialize = False
    47.         Else
    48.             QueryPerformanceCounter(Start_Time)
    49.             Hi_Res_Timer_Initialize = True
    50.         End If
    51.     End Function
    52.  
    53.     Private Function Get_Elapsed_Time() As Single
    54.         Dim Last_Time As Long
    55.         Dim Current_Time As Long
    56.  
    57.         QueryPerformanceCounter(Current_Time)
    58.         Get_Elapsed_Time = Convert.ToSingle((Current_Time - Last_Time) / Ticks_Per_Second)
    59.         QueryPerformanceCounter(Last_Time)
    60.     End Function
    61.  
    62.     Private Function Get_Elapsed_Time_Per_Frame() As Single
    63.         Static Last_Time As Long
    64.         Static Current_Time As Long
    65.  
    66.         QueryPerformanceCounter(Current_Time)
    67.         Get_Elapsed_Time_Per_Frame = CSng((Current_Time - Last_Time) / Ticks_Per_Second)
    68.         QueryPerformanceCounter(Last_Time)
    69.     End Function
    70.  
    71.     Private Sub Lock_Framerate(ByVal Target_FPS As Long)
    72.         Static Last_Time As Long
    73.         Dim Current_Time As Long
    74.         Dim FPS As Single
    75.  
    76.         Do
    77.             QueryPerformanceCounter(Current_Time)
    78.             FPS = Convert.ToSingle(Ticks_Per_Second / (Current_Time - Last_Time))
    79.         Loop While (FPS > Target_FPS)
    80.  
    81.         QueryPerformanceCounter(Last_Time)
    82.     End Sub
    83.  
    84.     Private Function Get_FPS() As String
    85.         Frame_Count = Frame_Count + 1
    86.  
    87.         If Get_Elapsed_Time() - Milliseconds >= 1 Then
    88.             Get_Frames_Per_Second = Frame_Count
    89.             Frame_Count = 0
    90.             Milliseconds = Convert.ToInt32(Get_Elapsed_Time)
    91.         End If
    92.  
    93.         Get_FPS = "Frames Per Second: " & Get_Frames_Per_Second
    94.     End Function
    95.  
    96.     Private Function Check_Key(ByVal Keyflag As Integer) As Integer
    97.         Check_Key = Keystate And Keyflag
    98.     End Function
    99.  
    100.     Private Sub Keyboard_Control()
    101.         If Check_Key(P1_LEFT_FLAG) > 0 Then Sprite.X -= 5
    102.         If Check_Key(P1_RIGHT_FLAG) > 0 Then Sprite.X += 5
    103.     End Sub
    104.  
    105.     Private Sub Load_Textures()
    106.         Sprite.Texture_Graphics = Me.CreateGraphics()
    107.         Sprite.Texture = Image.FromFile(Application.StartupPath() & "\Link.png")
    108.     End Sub
    109.  
    110.     Private Sub Unload_Textures()
    111.         Sprite.Texture_Graphics.Dispose()
    112.     End Sub
    113.  
    114.     Private Sub Setup_Sprites()
    115.         With Sprite
    116.             .Width = 54
    117.             .Height = 69
    118.             .X = CInt(Me.Width / 2) - CInt(Sprite.Width / 2)
    119.             .Y = 490
    120.         End With
    121.     End Sub
    122.  
    123.     Private Sub Clear_Screen()
    124.         Backbuffer_Graphics.Clear(Color.FromArgb(255, 0, 0, 0))
    125.     End Sub
    126.  
    127.     Private Sub Render()
    128.         Clear_Screen()
    129.         Sprite.Texture_Graphics.DrawImage(Sprite.Texture, New RectangleF(Sprite.X, Sprite.Y, Sprite.Width, Sprite.Height))
    130.     End Sub
    131.  
    132.     Private Sub Game_Loop()
    133.         Do While Running = True
    134.             Keyboard_Control()
    135.             Render()
    136.             Lock_Framerate(60)
    137.             Me.Text = Get_FPS()
    138.             Application.DoEvents()
    139.         Loop
    140.     End Sub
    141.  
    142.     Private Sub Main()
    143.         With Me
    144.             .Show()
    145.             .Focus()
    146.             .DoubleBuffered = True
    147.             .KeyPreview = True
    148.         End With
    149.  
    150.         Backbuffer_Graphics = Me.CreateGraphics()
    151.         Load_Textures()
    152.         Setup_Sprites()
    153.         Hi_Res_Timer_Initialize()
    154.         Running = True
    155.         Game_Loop()
    156.     End Sub
    157.  
    158.     Private Sub Shutdown()
    159.         Unload_Textures()
    160.         Running = False
    161.         Application.Exit()
    162.     End Sub
    163.  
    164.     Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    165.         Select Case e.KeyCode
    166.             Case P1_UP
    167.                 Keystate = Keystate Or P1_UP_FLAG
    168.             Case P1_DOWN
    169.                 Keystate = Keystate Or P1_DOWN_FLAG
    170.             Case P1_LEFT
    171.                 Keystate = Keystate Or P1_LEFT_FLAG
    172.             Case P1_RIGHT
    173.                 Keystate = Keystate Or P1_RIGHT_FLAG
    174.         End Select
    175.     End Sub
    176.  
    177.     Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    178.         Select Case e.KeyCode
    179.             Case P1_UP
    180.                 Keystate = Keystate And (Not P1_UP_FLAG)
    181.             Case P1_DOWN
    182.                 Keystate = Keystate And (Not P1_DOWN_FLAG)
    183.             Case P1_LEFT
    184.                 Keystate = Keystate And (Not P1_LEFT_FLAG)
    185.             Case P1_RIGHT
    186.                 Keystate = Keystate And (Not P1_RIGHT_FLAG)
    187.         End Select
    188.     End Sub
    189.  
    190.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    191.         Main()
    192.     End Sub
    193.  
    194.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.FormClosing
    195.         Shutdown()
    196.     End Sub
    197.  
    198. End Class

    And of course theres collision. You dont need all them if statements for collision. And there are many ways to check for it. This is a function that will check for collision of 2 rectangles:

    vb.net Code:
    1. Private Structure Box
    2.     X As Single
    3.     Y As Single
    4.     W As Single
    5.     H As Single
    6.     VX As Single
    7.     VY As Single
    8. End Structure
    9.  
    10. Private Function AABBCheck(B1 As Box, B2 As Box) As Boolean
    11.     'returns true if the boxes are colliding (velocities are not used)
    12.     Return Not (((B1.X + B1.W < B2.X) Or (B1.X > B2.X + B2.W) Or (B1.Y + B1.H < B2.Y) Or (B1.Y > B2.Y + B2.H)))
    13. End Function

    Theres a much better way out there called AABB Sweep collision detection, which is designed for fast moving objects, but I'm still figuring that out. I'm also creating Pong, only in 3D using VB.Net 2010, and DirectX9.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width