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.
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: code:


This file is hosted off-site.
give me 5 stars
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: code:
Code: Select all
download project with the code: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


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.
I have a few tips for you.
1. Use the
1. Use the
Code: Select all
tags, it is easier on the eyes.
2. Explain your code more.
Other than that, good job cooll;
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

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

Shame on you this isnt even your own code and you are taking credit for it//. Ive seen this on a website too. Shame
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. ShameWhy shame? He never said it was his own, he just made a tutorial on a source code.
Do not bump old topics as you will earn yourself dereps...
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
http://www.codexvideos.com
Copyright Information
Copyright © Codenstuff.com 2020 - 2023