This is the source code and the .exe
Features:
- Plays Pong
Drawbacks:
- The ai is very hard. You can make it a little easier, but I like it like it is.
- It is a very basic program. No graphics, or special effects. Just a very simple game
Notes:
- It's just a basic example of one of my favorite atari games
Full Project:
pong.zip
Source Code:
Code:
Option Strict On
Option Explicit On
Public Class Form1
'Variabl
Private speed As Integer = 35
Private playing As Boolean = False
Private wins As Integer = 0
Private x As Integer = -10
Private y As Integer = -10
'Controls
Private userPaddle As New Panel
Private cpuPaddle As New Panel
Private ball As New Panel
Private tmr As New System.Windows.Forms.Timer
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With Me
.Text = "Pong"
.WindowState = FormWindowState.Maximized
.KeyPreview = True
End With
With userPaddle
userPaddle.BackColor = Color.Black
userPaddle.BorderStyle = BorderStyle.FixedSingle
userPaddle.Size = New Size(10, 150)
userPaddle.Location = New Point(15, CInt(Me.Height / 2 - userPaddle.Height / 2))
Me.Controls.Add(userPaddle)
End With
With cpuPaddle
cpuPaddle.BackColor = Color.Black
cpuPaddle.BorderStyle = BorderStyle.FixedSingle
cpuPaddle.Size = New Size(10, 150)
cpuPaddle.Location = New Point(Me.Width - cpuPaddle.Width - 30, CInt(Me.Height / 2 - cpuPaddle.Height / 2))
Me.Controls.Add(cpuPaddle)
End With
With ball
ball.BackColor = Color.Black
ball.BorderStyle = BorderStyle.FixedSingle
ball.Size = New Size(10, 10)
ball.Location = New Point(CInt(Me.Width / 2 - ball.Width / 2), CInt(Me.Height / 2 - ball.Height / 2))
Me.Controls.Add(ball)
End With
With tmr
.Interval = speed
.Enabled = False
End With
AddHandler tmr.Tick, AddressOf Timer_Tick
End Sub
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Space Then
If playing = False Then
playing = True
tmr.Enabled = True
End If
End If
If e.KeyCode = Keys.Up Then
If playing Then
userPaddle.Top -= 20
End If
End If
If e.KeyCode = Keys.Down Then
If playing Then
userPaddle.Top += 20
End If
End If
End Sub
Private Sub Timer_Tick(sender As Object, e As EventArgs)
If playing Then
collision()
End If
End Sub
Private Sub collision()
ai()
ball.Location = New Point(ball.Location.X + x, ball.Location.Y + y)
'If it hits the top of the form
If ball.Top <= Me.Top Then
y = -y
End If
'If it hits the bottom of the form
If ball.Bottom >= Me.Bottom Then
y = -y
End If
'If it hits the userPaddle
If ball.Bounds.IntersectsWith(userPaddle.Bounds) Then
x = -x
speed -= 1
End If
'If it hits the cpuPaddle
If ball.Bounds.IntersectsWith(cpuPaddle.Bounds) Then
x = -x
End If
'If it hits the cpu's wall
If ball.Right >= Me.Right Then
wins += 1
tmr.Enabled = False
reset()
End If
'If it hits the player's wall
If ball.Left <= Me.Left Then
tmr.Enabled = False
reset()
End If
End Sub
Private Sub reset()
playing = False
x = -10
y = -10
With Me
.Text = "Pong | Score: " & wins
.WindowState = FormWindowState.Maximized
.KeyPreview = True
End With
With userPaddle
userPaddle.BackColor = Color.Black
userPaddle.BorderStyle = BorderStyle.FixedSingle
userPaddle.Size = New Size(10, 150)
userPaddle.Location = New Point(15, CInt(Me.Height / 2 - userPaddle.Height / 2))
Me.Controls.Add(userPaddle)
End With
With cpuPaddle
cpuPaddle.BackColor = Color.Black
cpuPaddle.BorderStyle = BorderStyle.FixedSingle
cpuPaddle.Size = New Size(10, 150)
cpuPaddle.Location = New Point(Me.Width - cpuPaddle.Width - 30, CInt(Me.Height / 2 - cpuPaddle.Height / 2))
Me.Controls.Add(cpuPaddle)
End With
With ball
ball.BackColor = Color.Black
ball.BorderStyle = BorderStyle.FixedSingle
ball.Size = New Size(10, 10)
ball.Location = New Point(CInt(Me.Width / 2 - ball.Width / 2), CInt(Me.Height / 2 - ball.Height / 2))
Me.Controls.Add(ball)
End With
With tmr
.Interval = speed
.Enabled = False
End With
End Sub
Private Sub ai()
If ball.Location.Y < cpuPaddle.Bottom - 10 Then
cpuPaddle.Location = New Point(cpuPaddle.Location.X, cpuPaddle.Location.Y - 20)
End If
If ball.Location.Y > cpuPaddle.Top + 10 Then
cpuPaddle.Location = New Point(cpuPaddle.Location.X, cpuPaddle.Location.Y + 20)
End If
End Sub
End Class