|
-
Aug 22nd, 2012, 10:48 PM
#1
[VB.Net] Pong
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
Edit - Here is a player vs. player version I worked up. It's at 200 lines of code, so it's fairly simple:
Code:
Option Strict On
Option Explicit On
Public Class Form1
#Region "Variables"
'The controls
Private game_timer As New Timer
Private lbl_over As New Label
Private p1_paddle As New Panel
Private p2_paddle As New Panel
Private ball As New Panel
'Other variables
Private playing As Boolean = False
Private game_speed As Integer = 10
Private x_move As Integer = -1
Private y_move As Integer = -1
#End Region
#Region "New/End Game"
Private Sub NewGame()
'Put the paddles in their location
p1_paddle.Location = New Point(15, CInt(Me.Width / 2 - p1_paddle.Width / 2))
p2_paddle.Location = New Point(Me.Width - 30, CInt(Me.Width / 2 - p2_paddle.Width / 2))
'Put the ball in it's location
'It will be Me's <height/width> divided by two minus the ball's <height/width> divided by two for an exact center
ball.Location = New Point(CInt(Me.Width / 2 - ball.Width / 2), CInt(Me.Height / 2 - ball.Height / 2))
'Make the ball visibile again
ball.Visible = True
'Hide the label
lbl_over.Visible = False
End Sub
Private Sub GameOver()
'Stop the timer
playing = False
game_timer.Enabled = playing
'Hide the ball
ball.Visible = False
'Hide the label
lbl_over.Visible = True
End Sub
#End Region
#Region "Movement"
Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
'Move only the y loction value or the top
p1_paddle.Top = CInt(e.Location.Y - p1_paddle.Height / 2)
End Sub
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'Move only the y loction value
If e.KeyCode = Keys.Up Then
p2_paddle.Top -= 5
ElseIf e.KeyCode = Keys.Down Then
p2_paddle.Top += 5
ElseIf e.KeyCode = Keys.Space Then
If Not (playing) Then
playing = True
game_timer.Enabled = playing
Call NewGame()
End If
End If
End Sub
#End Region
#Region "Control Events"
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Set up tick event
AddHandler game_timer.Tick, AddressOf Game_Tick
'Set up paint event for the ball
AddHandler ball.Paint, AddressOf Ball_Paint
'Set up timer
With game_timer
.Enabled = playing
.Interval = game_speed
End With
'Set up the paddles
With p1_paddle
.BackColor = Color.White
.BorderStyle = BorderStyle.FixedSingle
.Size = New Size(10, 100)
End With
With p2_paddle
.BackColor = Color.Honeydew
.BorderStyle = BorderStyle.FixedSingle
.Size = New Size(10, 100)
End With
'Set up the ball
With ball
.BackColor = Color.Ivory
.BorderStyle = BorderStyle.None
.Size = New Size(25, 25)
End With
'Set up the label
With lbl_over
.ForeColor = Color.White
.Left = CInt(Me.Width / 2 - lbl_over.Width / 2)
.Text = "Press the spacebar or left-click for a new game."
.Top = 5
.Visible = True
End With
'Set up me!
With Me
.BackColor = Color.Black
.Controls.AddRange({p1_paddle, p2_paddle, ball, lbl_over})
.Size = New Size(450, 600)
.Text = "Pong - PVP"
.StartPosition = FormStartPosition.CenterScreen
End With
End Sub
Private Sub Form1_Click(sender As Object, e As System.EventArgs) Handles Me.Click
playing = True
game_timer.Enabled = playing
Call NewGame()
End Sub
Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
If Not (playing) Then
'Put the paddles in their location
p1_paddle.Location = New Point(15, CInt(Me.Width / 2 - p1_paddle.Width / 2))
p2_paddle.Location = New Point(Me.Width - 30, CInt(Me.Width / 2 - p2_paddle.Width / 2))
'Put the ball in it's location
'It will be Me's <height/width> divided by two minus the ball's <height/width> divided by two for an exact center
ball.Location = New Point(CInt(Me.Width / 2 - ball.Width / 2), CInt(Me.Height / 2 - ball.Height / 2))
End If
End Sub
Private Sub Ball_Paint(sender As Object, e As PaintEventArgs)
Dim gp As New System.Drawing.Drawing2D.GraphicsPath
' Set a new rectangle to the same size as the button's
' ClientRectangle property.
Dim newRectangle As Rectangle = ball.ClientRectangle
' Decrease the size of the rectangle.
newRectangle.Inflate(-1, -1)
' Draw the button's border.
e.Graphics.DrawEllipse(System.Drawing.Pens.Black, newRectangle)
'Increase the size of the rectangle to include the border.
newRectangle.Inflate(1, 1)
' Create a circle within the new rectangle.
gp.AddEllipse(newRectangle)
e.Graphics.FillPath(New SolidBrush(ball.BackColor), gp)
' Set the button's Region property to the newly created
' circle region.
ball.Region = New System.Drawing.Region(gp)
End Sub
Private Sub Game_Tick(sender As Object, e As EventArgs)
'Move the ball
ball.Left += x_move
ball.Top += y_move
If ball.Bounds.IntersectsWith(p1_paddle.Bounds) OrElse ball.Bounds.IntersectsWith(p2_paddle.Bounds) Then
'the ball hit one of the paddles
x_move = -x_move
ElseIf ball.Top <= 0 OrElse ball.Bottom >= Me.Height - ball.Height Then
'the ball hit the top or bottom
y_move = -y_move
ElseIf ball.Left <= 0 OrElse ball.Right >= Me.Width - ball.Width Then
'game over
Call GameOver()
End If
End Sub
#End Region
End Class
Last edited by dday9; Jun 27th, 2013 at 02:13 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|