Private Sub Form1_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 - Player.Height Then _
Player.Location = New Point(Player.Location.X, e.Y)
End Sub
Dim speed As Single = 10 ' This is 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
Ball.Location = New Point(Ball.Location.X + xVel, Ball.Location.Y
' Colides with top wall.
If Ball.Location.Y < 0 Then
Ball.Location = New Point(Ball.Location.X, 0)
yVel = -yVel
End If
' Colides with down wall.
If Ball.Location.Y > Me.Height - Ball.Size.Height - 45 Then
Ball.Location = New Point(Ball.Location.X, Me.Height - Ball.Size.Height - 45)
yVel = -yVel
End If
' Colides with player panel.
If Ball.Bounds.IntersectsWith(Player.Bounds) Then
Ball.Location = New Point(Player.Location.X - Ball.Size.Width, _
Ball.Location.Y)
xVel = -xVel
End If
' Colides with computer panel.
If Ball.Bounds.IntersectsWith(Computer.Bounds) Then
Ball.Location = New Point(Computer.Location.X + Computer.Size.Width + 1, _
Ball.Location.Y)
xVel = -xVel
End If
'Make the computer moves according to ball position
If Ball.Location.Y > 5 And Ball.Location.Y < Me.Height - 40 _
- Player.Height Then _
Computer.Location = New Point(Computer.Location.X, Ball.Location.Y)
Dim compScore As Integer = 0
Dim plrScore As Integer = 0
'Add 1 score to player if ball reaches the left side of the field
If Ball.Location.X < 0 Then
plrScore += 1
: Ball.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
PlayerScore.Text = Convert.ToString(plrScore)
End If
'Add 1 score to computer if ball reaches the right side of the field
If Ball.Location.X > Me.Width - Ball.Size.Width - Player.Width Then
compScore += 1
Ball.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
: ComputerScore.Text = Convert.ToString(compScore)
End If
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Cursor.Hide()
End Sub
Private Sub From1_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
Public Class Form1
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
Dim compScore As Integer = 0
Dim plrScore As Integer = 0
Private Sub Form1_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 - Player.Height Then _
Player.Location = New Point(Player.Location.X, e.Y)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Ball.Location = New Point(Ball.Location.X + xVel, Ball.Location.Y)
Timer1.Interval = 13
' Colides with top wall.
If Ball.Location.Y < 0 Then
Ball.Location = New Point(Ball.Location.X, 0)
yVel = -yVel
End If
' Colides with down wall.
If Ball.Location.Y > Me.Height - Ball.Size.Height - 45 Then
Ball.Location = New Point(Ball.Location.X, Me.Height - Ball.Size.Height - 45)
yVel = -yVel
End If
' Colides with player panel.
If Ball.Bounds.IntersectsWith(Player.Bounds) Then
Ball.Location = New Point(Player.Location.X - Ball.Size.Width, _
Ball.Location.Y)
xVel = -xVel
End If
' Colides with computer panel.
If Ball.Bounds.IntersectsWith(Computer.Bounds) Then
Ball.Location = New Point(Computer.Location.X + Computer.Size.Width + 1, _
Ball.Location.Y)
xVel = -xVel
End If
'Make the computer moves according to ball position
If Ball.Location.Y > 5 And Ball.Location.Y < Me.Height - 40 _
- Player.Height Then _
Computer.Location = New Point(Computer.Location.X, Ball.Location.Y)
'Add 1 score to player if ball reach the left side of the field
If Ball.Location.X < 0 Then
plrScore += 1
: Ball.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
PlayerScore.Text = Convert.ToString(plrScore)
End If
'Add 1 score to computer if ball reaches the right side of the field
If Ball.Location.X > Me.Width - Ball.Size.Width - Player.Width Then
compScore += 1
Ball.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
: ComputerScore.Text = Convert.ToString(compScore)
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Cursor.Hide()
End Sub
Private Sub From1_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 Class
Copyright © 2024, NextGenUpdate.
All Rights Reserved.