Public Class form1
Dim y As Integer
Dim x As Integer
Dim youhit As Boolean
' Move a paddle by the mouse
' With a bouncing ball
Dim aispeed As Integer
' module variables -- these variables exist across Subs
Dim px, py As Integer
' establish a variable called paper of type Graphics
Dim paper As Graphics
' ball position variables
Dim bx, by, bdx, bdy As Integer
' horizontal mouse position
Dim mousex, oldmx As Integer
' maximum of picture box
Dim xmax, ymax As Integer
Dim px2, py2 As Integer
Const paddleheight As Integer = 10
Const paddlewidth As Integer = 40
Private Sub start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' set paper = to the existing picture box
' (which we named picBox in the form)
paper = PictureBox1.CreateGraphics()
' make the picture box all white
paper.Clear(Color.Blue)
' top left coordinates of paddle
' horiz is center - 20
px = PictureBox1.Width / 2 - 20
px2 = PictureBox1.Width / 2 - 20
' vertical is
py = PictureBox1.Height - 40
py2 = 40
paper.FillRectangle(Brushes.Red, px, py, paddlewidth, paddleheight)
paper.FillRectangle(Brushes.Green, px2, py2, paddlewidth, paddleheight)
' initial values for ball
bx = 35 ' x,y is coordinates of top left corner
by = 3
bdx = 2 ' change in x each tick
bdy = 1 ' change in y each tick
' initial horizontal value for mouse
oldmx = Control.MousePosition.X
' right and bottom sides of picturebox
xmax = PictureBox1.Width
ymax = PictureBox1.Height
aispeed = 2.5
' display the ball
paper.FillEllipse(Brushes.White, bx, by, 20, 20)
' hide the Start button
Button1.Visible = False
' Set timer to tick every 10 milliseconds
Timer1.Interval = 10
Timer2.Interval = 100
' start timer
Timer1.Start()
Timer2.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' update ball
' erase the previous circle
paper.FillEllipse(Brushes.Blue, bx, by, 20, 20)
' change the position
bx = bx + bdx
by = by + bdy
'see if the ball is hitting the paddle
If bx <= px + paddlewidth And bx > px Then
If (by + 20) >= py Then
by = by - 2 * bdy
bdy = -bdy
youhit = True
End If
End If
If youhit = True Then
paper.FillRectangle(Brushes.Blue, px2, py2, paddlewidth, paddleheight)
If bx > px2 Then
px2 = px2 + aispeed
End If
If bx < px2 Then
px2 = px2 - aispeed
End If
paper.FillRectangle(Brushes.Green, px2, py2, paddlewidth, paddleheight)
End If
If youhit = True Then
If by <= 50 Then
If bx >= px2 And bx < px2 + paddlewidth Then
by = by - 2 * bdy
bdy = -bdy
youhit = False
End If
End If
End If
If youhit = False Then
paper.FillRectangle(Brushes.Blue, px2, py2, paddlewidth, paddleheight)
If px2 < (PictureBox1.Width / 2 - 3) Then
px2 = px2 + 3
End If
If px2 > PictureBox1.Width / 2 + 3 Then
px2 = px2 - 3
End If
paper.FillRectangle(Brushes.Green, px2, py2, paddlewidth, paddleheight)
End If
' check for boundaries
If (bx + 20) > xmax Then
bx = bx - 2 * bdx
bdx = -bdx
ElseIf bx < 0 Then
bx = -bx
bdx = -bdx
End If
If (by + 20) > ymax Then
x = x + 1
If x = 1 Then
MsgBox("hai perso ")
Restart("hai vinto ")
Me.Close()
End If
ElseIf by < 0 Then
y = y + 1
If y = 1 Then
MsgBox("you won")
Restart("pong")
Me.Close()
End If
End If
' now draw the circle again
paper.FillEllipse(Brushes.White, bx, by, 20, 20)
' now handle the paddle
' get current horizontal location of mouse pointer
mousex = Control.MousePosition.X
If mousex < oldmx And px > 0 Then
' erase paddle
paper.FillRectangle(Brushes.Blue, px, py, paddlewidth, paddleheight)
' move paddle left
px = px - (oldmx - mousex)
' draw new paddle
paper.FillRectangle(Brushes.Red, px, py, paddlewidth, paddleheight)
ElseIf mousex > oldmx And px < xmax - paddlewidth Then
' erase paddle
paper.FillRectangle(Brushes.Blue, px, py, paddlewidth, paddleheight)
' move paddle right
px = px + (mousex - oldmx)
' draw new paddle
paper.FillRectangle(Brushes.Red, px, py, paddlewidth, paddleheight)
End If
' save location of mouse pointer for comparison
oldmx = mousex
End Sub
Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Sub Restart(ByVal program As String)
For Each proc As Process In Process.GetProcesses
If proc.ProcessName = program Then proc.Kill()
Next
Process.Start(program)
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
End Sub
End Class