Advertisement
Guest User

Pong Code

a guest
Jun 23rd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.38 KB | None | 0 0
  1. Public Class Form1
  2. #Region "Move the paddle according to the mouse"
  3.     ' Move the paddle according to the mouse position.
  4.     Private Sub pongMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
  5.  
  6.         If e.Y > 5 And e.Y < Me.Height - 40 - paddlePlayer.Height Then _
  7.         paddlePlayer.Location = New Point(paddlePlayer.Location.X, e.Y)
  8.  
  9.     End Sub
  10. #End Region
  11. #Region "Main Timer"
  12.     Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
  13.         'Set the computer player to move according to the ball's position."
  14.         If gameBall.Location.Y > 5 And gameBall.Location.Y < Me.Height - 40 - paddlePlayer.Height Then
  15.             paddleComputer.Location = New Point(paddleComputer.Location.X, gameBall.Location.Y)
  16.         End If
  17.         ' Move the game ball.
  18.         gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)
  19.         ' Check for top wall.
  20.         If gameBall.Location.Y < 0 Then
  21.             gameBall.Location = New Point(gameBall.Location.X, 0)
  22.             yVel = -yVel
  23.         End If
  24.         ' Check for bottom wall.
  25.         If gameBall.Location.Y > Me.Height - gameBall.Size.Height - 45 Then
  26.             gameBall.Location = New Point(gameBall.Location.X, Me.Height - gameBall.Size.Height - 45)
  27.             yVel = -yVel
  28.         End If
  29.         ' Check for player paddle.
  30.         If gameBall.Bounds.IntersectsWith(paddlePlayer.Bounds) Then
  31.             gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, gameBall.Location.Y)
  32.             xVel = -xVel
  33.         End If
  34.         ' Check for computer paddle.
  35.         If gameBall.Bounds.IntersectsWith(paddleComputer.Bounds) Then
  36.             gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, gameBall.Location.Y)
  37.             xVel = -xVel
  38.         End If
  39.         ' Check for left wall.
  40.         If gameBall.Location.X < 0 Then
  41.             compScore += 1
  42.             gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
  43.             compScoreDraw.Text = Convert.ToString(compScore)
  44.         End If
  45.         ' Check for right wall.
  46.         If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
  47.             plrScore += 1
  48.             gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
  49.             plrScoreDraw.Text = Convert.ToString(plrScore)
  50.         End If
  51.  
  52.     End Sub
  53. #End Region
  54. #Region "Globals"
  55.     Dim speed As Single = 10 ' Ball Speed
  56.     Dim rndInst As New Random() ' Random instance
  57.     Dim xVel As Single = Math.Cos(rndInst.Next(5, 10)) * speed
  58.     Dim yVel As Single = Math.Sin(rndInst.Next(5, 10)) * speed
  59.     ' The player's scores.
  60.     Dim compScore As Integer = 0
  61.     Dim plrScore As Integer = 0
  62.  
  63. #End Region
  64. #Region "Hide Cursor"
  65.     ' Set up the game.
  66.     Private Sub pongMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  67.         Windows.Forms.Cursor.Hide()
  68.     End Sub
  69. #End Region
  70. #Region "End Game on Escape Press"
  71.     ' Escape the game when escape has been pressed.
  72.     Private Sub pongMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
  73.         If e.KeyValue = Keys.Escape Then
  74.             Me.Close()
  75.         End If
  76.     End Sub
  77. #End Region
  78. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement