Pong Game

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
12 posts Page 1 of 2
User avatar
volttide
VIP - Donator
VIP - Donator
Posts: 106
Joined: Mon Jan 17, 2011 8:24 am

Pong Game
volttide
you'll need 3 picture box, and 2 label:

name form1 become pong main

picture box:
- 1 for player name paddlePlayer
- 1 for computer name paddleComputer
- 1 for the ball name gameBall
label:
- for player:
text : 0, name plrScoreDraw
- for computer:
text : 0, name compScoreDraw

the form result:
pong.png
code:
Code: Select all
Public Class pongMain
#Region "Globals"
    Dim speed As Single = 10 ' Ball Speed
    Dim rndInst As New Random() ' Random instance
    Dim xVel As Single = Math.Cos(rndInst.Next(5, 10)) * speed
    Dim yVel As Single = Math.Sin(rndInst.Next(5, 10)) * speed
#End Region
    ' The player's scores.
    Dim compScore As Integer = 0
    Dim plrScore As Integer = 0


#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 - paddlePlayer.Height Then _
        paddlePlayer.Location = New Point(paddlePlayer.Location.X, e.Y)
    End Sub
#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
        paddlePlayer.Location = New Point(Me.Width - 44, paddlePlayer.Location.Y)
        plrScoreDraw.Location = New Point(Me.Width - 54, plrScoreDraw.Location.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 _
        - paddlePlayer.Height Then _
        paddleComputer.Location = New Point(paddleComputer.Location.X, gameBall.Location.Y)
        ' Move the game ball.
        gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)
        ' Check for top wall.
        If gameBall.Location.Y < 0 Then
            gameBall.Location = New Point(gameBall.Location.X, 0)
            yVel = -yVel
        End If

        ' 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 player paddle.
        If gameBall.Bounds.IntersectsWith(paddlePlayer.Bounds) Then
            gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, _gameBall.Location.Y)
            xVel = -xVel
        End If
        ' Check for computer paddle.
        If gameBall.Bounds.IntersectsWith(paddleComputer.Bounds) Then
            gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, _gameBall.Location.Y)
            xVel = -xVel
        End If
        ' Check for left wall.
        If gameBall.Location.X < 0 Then
            plrScore += 1
            gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
            plrScoreDraw.Text = Convert.ToString(plrScore)
        End If
        ' Check for right wall.
        If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
            compScore += 1
            gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
            compScoreDraw.Text = Convert.ToString(compScore)
        End If
    End Sub
#End Region
End Class 
download project with the code:

This file is hosted off-site.

give me 5 stars
You do not have the required permissions to view the files attached to this post.
Last edited by volttide on Fri Apr 01, 2011 5:07 am, edited 2 times in total.
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: Pong Game
GoodGuy17
I have a few tips for you.
1. Use the
Code: Select all
 tags, it is easier on the eyes.
2. Explain your code more.

Other than that, good job cooll;
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: Pong Game
Usman55
I also made one with buttons. And please add the code tags as Reboh said.
Image
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: Pong Game
M1z23R
I already saw this tutorial some where, and i added a new feature to it :)
Both players have 1 bullet, so you can fire it and if you hit him, you also get 1 point :)
I wanted to make it multi player, but i stopped this project cause i didn't have enough time :)
User avatar
RishiRox
New Member
New Member
Posts: 16
Joined: Mon Jan 17, 2011 4:05 pm

Re: Pong Game
RishiRox
As reboh said Please use code box an thanks it is an awesome 1 :D
User avatar
shekoasinger
New Member
New Member
Posts: 23
Joined: Sat Dec 04, 2010 9:40 pm

Re: Pong Game
shekoasinger
Shame on you this isnt even your own code and you are taking credit for it//. Ive seen this on a website too. Shame
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: Pong Game
Usman55
shekoasinger wrote:
Shame on you this isnt even your own code and you are taking credit for it//. Ive seen this on a website too. Shame
Why shame? He never said it was his own, he just made a tutorial on a source code.
Image
User avatar
Zulf
Serious Programmer
Serious Programmer
Posts: 441
Joined: Fri Jun 11, 2010 7:46 am

Re: Pong Game
Zulf
I can soo see macHard coming here, unless he already finished his pong. :P
Image
User avatar
Agust1337
Coding God
Coding God
Posts: 2456
Joined: Fri Feb 19, 2010 8:18 pm

Re: Pong Game
Agust1337
Do not bump old topics as you will earn yourself dereps...
Top-notch casual Dating
User avatar
RunarM
Hardcore Programmer
Hardcore Programmer
Posts: 508
Joined: Wed Nov 18, 2009 11:33 pm

Re: Pong Game
RunarM
Agust1337 wrote:
Do not bump old topics as you will earn yourself dereps...
I'm confused.. Do you want us to derep you?
Just another day in my life.
http://www.codexvideos.com
12 posts Page 1 of 2
Return to “Tutorials”