[Guide] How to make "Pong" menu

Shout-Out

User Tag List

Results 1 to 5 of 5
  1. #1
    Murdok's Avatar Member
    Reputation
    34
    Join Date
    Oct 2007
    Posts
    69
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Guide] How to make "Pong"

    This guide will show you how to make a simple pong game and I will add some features to make it a bit more advanced at the end.

    This is in Visual Basic 6

    So first we need to create the form so we will need

    Form Size:

    Width = 7545
    Height = 5295

    This is quite important because we need to check if the ball passes a certain point so if you have different Height and Width then you will need to change the code a bit later on

    Actually I will add a form to download with no code just to make it easier

    DOWNLOAD

    MEGAUPLOAD - The leading online storage and file delivery service

    3 shapes:

    2 rectangles called
    Paddle1
    paddle2

    1 circle call this
    Ball

    2 Labels
    1 called LBLp1Score
    1 called LBLp2Score

    the rest of the form can be designed by you mine looks like:




    But for now ignore the bottom but with the level and the time.

    So after we design the form we start coding

    First we will start with the global variables

    Code:
    Option Explicit
    Dim Horizontal As Integer
    Dim Vertical As Integer
    Dim nStep As Long
    These are used to make the ball move we will be using these very soon

    Ok now we need to make a Timer, I just left mine being Called Timer1 but you can call it something more relevant,

    Ok now in the "Form Load" put:


    Code:
       Horizontal = 1
       Vertical = -1
       nStep = 40
    These just add values to the variables,

    Horizontal = 1 'This controls the speed of the ball moving left to right
    Vertical = -1 'This controls the speed of the ball moving up and down
    nStep = 40 'This controls how may spaces it moves each time

    these values can be changed if you want but the way there set now makes it look a lot smoother.

    Ok now back into the Timer1 we need to set:

    interval to 5
    Enabled to true

    and now start to add the code for Timer1

    Code:
    Dim x As Integer
    This is just for a message box later in the code i use X but you can use what ever

    Next we do:

    Code:
       ball.Left = ball.Left + (nStep * Horizontal)
       ball.Top = ball.Top + (nStep * Vertical)
    This makes the ball start moving but it will just go through the wall and carry on going so we also need to add an if statement to make it change direction on collision with the walls

    to do this we add

    Code:
       If ball.Left <= 0 Or ball.Left + ball.Width >= Me.ScaleWidth Then
          Horizontal = Horizontal * -1
       End If
       
       
       If ball.Top <= 0 Or (ball.Top + ball.Height) >= Me.ScaleHeight Then
          Vertical = Vertical * -1
       End If
    this just says if the ball touches the wall then change the direction the ball is going

    There is one other thing we need to bounce the ball off when it comes in contact with it and that is the paddles

    So also in the timer we add:

    Code:
       If ball.Top >= Paddle1.Top And _
          ball.Top <= Paddle1.Top + Paddle1.Height And _
          ball.Left <= Paddle1.Left + Paddle1.Width Then
          Horizontal = Horizontal * -1
       End If
       
       
       If ball.Top >= paddle2.Top And _
          ball.Top <= paddle2.Top + paddle2.Height And _
          ball.Left + ball.Width >= paddle2.Left + paddle2.Width Then
          Horizontal = Horizontal * -1
       End If
    These two If statements are the same just for the two different paddles so lets have a look at the code

    If ball.Top >= Paddle1.Top And _
    ball.Top <= Paddle1.Top + Paddle1.Height And _
    ball.Left <= Paddle1.Left + Paddle1.Width

    This just says if the ball .Top AND .Left is ever the same as the Paddle to execute the if statement

    Horizontal = Horizontal * -1
    End If

    This is the executed bit which we have seen before it just changes the direction of the ball after its hit the paddle

    The next part in the Timer is for score keeping and detecting if the ball ever passes the paddle

    Code:
       If ball.Left < 100 Then
        x = MsgBox("Player 2 Scores!!", vbOKOnly, "Score!")
        Paddle1.Top = 1560
        ball.Left = 3480
        LBLp2score.Caption = LBLp2score.Caption + 1
        ElseIf ball.Left > 7000 Then
        x = MsgBox("Player 1 Scores!!", vbOKOnly, "Score!")
        paddle2.Top = 1800
        ball.Left = 3480
        LBLp1score.Caption = LBLp1score.Caption + 1
       End If
    Ok so lets take a little llook at the code:

    If ball.Left < 100 Then

    Oviously just checks if the Ball.Left is left that 100 and if it is execute the rest of the code

    x = MsgBox("Player 2 Scores!!", vbOKOnly, "Score!")

    Just tells the user that player 2 has scored and also change the X to what you used at the start of the timer if you used a different one

    Paddle1.Top = 1560
    ball.Left = 3480

    This just resets the ball to a place around the middle

    LBLp2score.Caption = LBLp2score.Caption + 1

    This just adds 1 to the player2s score label

    k Now we also need to do it for player one so we can either do it at an other if or just use Elseif

    ElseIf ball.Left > 7000 Then

    Same as the begining of the first if but just checks if its greater than 7000

    The rest is just the same as the first if just for player1 and paddle1

    ok were almost done just 1 last things and you should be able to play a basic pong game

    Click on the form so you get your form load up then where it says "Load we need to change it to Key down

    and in that we need to add

    Code:
       If KeyCode = vbKeyZ Then
          Paddle1.Top = Paddle1.Top + 450
          
       ElseIf KeyCode = vbKeyA Then
          Paddle1.Top = Paddle1.Top - 450
          
       ElseIf KeyCode = vbKeyUp Then
          paddle2.Top = paddle2.Top - 450
          
       ElseIf KeyCode = vbKeyDown Then
          paddle2.Top = paddle2.Top + 450
    If KeyCode = vbKeyZ Then
    Paddle1.Top = Paddle1.Top + 450

    this just says if the key a is press move the paddle 450 spaces up

    and the same but down for z

    so now we can move the paddles player1 uses A and Z and Player2 uses Up and Down

    ok thats all for now i will be adding a few more features like stopping after 5 goals and levels

    any problems ask:

    [Guide] How to make &quot;Pong&quot;
  2. #2
    suicidity's Avatar Contributor
    Reputation
    207
    Join Date
    Oct 2006
    Posts
    1,439
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is actually my source, I made this a long time ago for fun.

    Never really cleaned up the source, but it works just fine.

    Code:
    Public Class main
    
        Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short
    
        Dim plus_x As Double
        Dim plus_y As Double
        Dim direction_x As Integer
    
        Dim rand = New Random()
    
        Dim pong_x As Integer
        Dim pong_y As Integer
        Dim difference As Integer
    
        Dim score_l As Integer
        Dim score_r As Integer
    
        Dim m_BufferBitmap = New Bitmap(Me.Width * 2, Me.Height, Me.CreateGraphics())
        Dim m_BufferGraphics = Graphics.FromImage(m_BufferBitmap)
    
    
        Public Sub updatepong()
            ball.Left += plus_x * direction_x
            ball.Top += plus_y
        End Sub
    
        Public Sub detection()
            ' bounding hit detection
            If ball.Right + Me.Left < Me.Left Then
                score_r += 1
                Sound.PlaySound("fail", Sound.SND_FILENAME, 0)
                reset()
            End If
            If ball.Left + Me.Left > Me.Right Then
                score_l += 1
                Sound.PlaySound("fail", Sound.SND_FILENAME, 0)
                reset()
            End If
            If ball.Top + Me.Top < Me.Top Or ball.Bottom + Me.Top > Me.Bottom Then
                plus_y *= -1
                ball.Top += plus_y
            End If
            ' player one hit detection
            If ball.Right > player_one.Left And ball.Top + 7 > player_one.Top And ball.Bottom - 7 < player_one.Bottom And ball.Left < player_one.Right Then
                direction_x = -1
                plus_x += 0.4
                difference = ball.Top - (player_one.Top + (player_one.Height / 2))
                plus_y = (Math.Atan(difference)) * 1.5
                updatepong()
            End If
            ' player two hit detection
            If ball.Left < player_two.Right And ball.Top + 7 > player_two.Top And ball.Bottom - 7 < player_two.Bottom And ball.Right > player_two.Left Then
                direction_x = 1
                plus_x += 0.4
                difference = ball.Top - (player_two.Top + (player_two.Height / 2))
                plus_y = (Math.Atan(difference)) * 1.5
                updatepong()
            End If
        End Sub
    
        Public Sub control()
            ' controls players
            If GetAsyncKeyState(Keys.L) <> 0 And player_one.Bottom + Me.Top <= Me.Bottom Then
                player_one.Top += 2 + (Math.Abs(plus_y) / 4)
            ElseIf GetAsyncKeyState(Keys.O) <> 0 And player_one.Top + Me.Top >= Me.Top Then
                player_one.Top -= 2 + (Math.Abs(plus_y) / 4)
            End If
    
            If GetAsyncKeyState(Keys.A) <> 0 And player_two.Bottom + Me.Top <= Me.Bottom Then
                player_two.Top += 2 + (Math.Abs(plus_y) / 4)
            ElseIf GetAsyncKeyState(Keys.Q) <> 0 And player_two.Top + Me.Top >= Me.Top Then
                player_two.Top -= 2 + (Math.Abs(plus_y) / 4)
            End If
        End Sub
    
        Public Sub computer()
            ' controls computer
            If GetAsyncKeyState(Keys.A) = 0 And GetAsyncKeyState(Keys.Q) = 0 Then
                If ball.Left < (Me.Width / 2) And direction_x = -1 Then
                    If player_two.Top + Me.Top >= Me.Top Or player_two.Bottom + Me.Top <= Me.Bottom Then
                        If ball.Bottom + 5 > player_two.Bottom Then
                            player_two.Top += 2 + (Math.Abs(plus_y) / 4)
                        ElseIf ball.Top - 5 < player_two.Top Then
                            player_two.Top -= 2 + (Math.Abs(plus_y) / 4)
                        End If
                    Else
                        If ball.Top < player_two.Top Then
                            player_two.Top -= 2 + (Math.Abs(plus_y) / 4)
                        ElseIf ball.Bottom > player_two.Bottom Then
                            player_two.Top += 2 + (Math.Abs(plus_y) / 4)
                        End If
                    End If
                ElseIf direction_x = 1 Then
                    If player_two.Top + Me.Top + 29 < Me.Top + Me.Height / 2 Then
                        player_two.Top += 2 + (Math.Abs(plus_y) / 4)
                    End If
                    If player_two.Top + Me.Top + 29 > Me.Top + Me.Height / 2 Then
                        player_two.Top -= 2 + (Math.Abs(plus_y) / 4)
                    End If
                End If
            End If
        End Sub
    
        Public Sub cheat()
            ' Clear the new bitmap.
            m_BufferGraphics.Clear(Me.BackColor)
    
            Dim ball_point As System.Drawing.Point
            Dim end_point As System.Drawing.Point
    
            Dim slope As Integer
    
            If plus_y = 0 Then
                slope = 0
            Else
                slope = plus_x / plus_y
            End If
    
            ball_point.X = ball.Location.X + Me.Location.X
            ball_point.Y = ball.Location.Y + Me.Location.Y
            If direction_x = 1 Then
                end_point.X = ball_point.X + (Math.Abs(slope) * 58)
                end_point.Y = ball_point.Y + (Math.Abs(slope) * 58)
            Else
                end_point.X = ball_point.X - (plus_x * 58)
                end_point.Y = ball_point.Y + (plus_y * 58)
            End If
    
            m_BufferGraphics.DrawLine(Pens.Blue, _
                Me.PointToClient(ball_point), _
                Me.PointToClient(end_point))
    
            DrawForm(Me.CreateGraphics())
        End Sub
    
        Public Sub reset()
            direction_x *= -1
            ball.Top = Me.Height / 2
            ball.Left = Me.Width / 2
    
            player_one.Top = Me.Height / 2 - 29
            player_two.Top = Me.Height / 2 - 29
    
            plus_x = 2
            plus_y = 0
        End Sub
    
        Private Sub main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim screen_point As Point
            screen_point.X = (My.Computer.Screen.Bounds.Width / 2) - (Me.Width / 2)
            screen_point.Y = (My.Computer.Screen.Bounds.Height / 2) - (Me.Height / 2)
            Me.Location = screen_point
    
            direction_x = -1
            reset()
        End Sub
    
        Private Sub DrawForm(ByVal gr As Graphics)
            If Not (m_BufferBitmap Is Nothing) Then gr.DrawImage(m_BufferBitmap, 0, 0)
        End Sub
    
        Private Sub updateCall_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateCall.Tick
            pong_x = ball.Left + Me.Left
            pong_y = ball.Top + Me.Top
    
            score_left.Text = score_l
            score_right.Text = score_r
    
            control()
            detection()
            updatepong()
        End Sub
    
        Public Class Sound
            Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name _
              As String, ByVal hmod As Integer, ByVal flags As Integer) As Integer
    
            Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name _
              As Byte(), ByVal hmod As Integer, ByVal flags As Integer) As Integer
    
            Public Const SND_FILENAME = &H20000 ' name is file name 
        End Class
    
        Private Sub form_exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles form_exit.Click
            Me.Close()
        End Sub
    End Class
    It features directional hitting, scoring, a computer(forget how hard i made it), a test-scrap of my cheat that tells you where exactly the balls going, and it plays sounds when you fail and miss the ball.
    Last edited by suicidity; 10-27-2008 at 10:24 AM.


  3. #3
    Murdok's Avatar Member
    Reputation
    34
    Join Date
    Oct 2007
    Posts
    69
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice, thought for a minute you said my code was yours

  4. #4
    suicidity's Avatar Contributor
    Reputation
    207
    Join Date
    Oct 2006
    Posts
    1,439
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I guess it would seem that way now reading it , but I figured I'd post to compare.

    I just made mine when I was bored and was doing some tests with math and stuff.


  5. #5
    Murdok's Avatar Member
    Reputation
    34
    Join Date
    Oct 2007
    Posts
    69
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah its amazing at what you can make when your bored :P

Similar Threads

  1. [Guide] How to make any Race be any Class and play it
    By Illidan1 in forum WoW EMU Guides & Tutorials
    Replies: 51
    Last Post: 03-19-2008, 08:32 PM
  2. [Guide] How to make a account creation page! Very easy!! (Hamachi)
    By rfvtgbyhn in forum WoW EMU Guides & Tutorials
    Replies: 1
    Last Post: 11-02-2007, 05:25 PM
  3. [Guide]How to make your Server 2.2.0 !
    By Guysus in forum WoW EMU Guides & Tutorials
    Replies: 17
    Last Post: 10-02-2007, 05:10 PM
  4. Guide how to make TWINK PRIEST
    By Croat in forum World of Warcraft Guides
    Replies: 8
    Last Post: 04-29-2007, 08:18 AM
  5. *Guide* How to make a movie
    By djmazi in forum World of Warcraft Guides
    Replies: 5
    Last Post: 12-22-2006, 09:15 PM
All times are GMT -5. The time now is 05:47 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search