That's it!

Open your VB, insert the following code into
an empty project and press F5! Have fun!

(keys re A+Y for left player and K+M for
right player by default; Escape to exit)

VB Code:
  1. 'Types
  2.     Private Type tKeyTable
  3.         KeyUp As Long
  4.         KeyDown As Long
  5.     End Type
  6.    
  7.     Private Type tCamera
  8.         'Size
  9.         w As Long
  10.         h As Long
  11.        
  12.         'General
  13.         DC As Long
  14.     End Type
  15.    
  16.     Private Type tObject
  17.         'Size
  18.         X As Single
  19.         Y As Single
  20.        
  21.         'Start position
  22.         w As Long
  23.         h As Long
  24.        
  25.         'Movement
  26.         SpeedX As Single
  27.         SpeedY As Single
  28.        
  29.         'General
  30.         IsBall As Boolean
  31.         Color As Long
  32.     End Type
  33.    
  34.     Private Type tPlayer
  35.         'General
  36.         Name As String
  37.         Score As Long
  38.        
  39.         'Internal
  40.         KeyTable As tKeyTable
  41.         Pad As tObject
  42.     End Type
  43.  
  44. 'Declares
  45.     Private Declare Function GetTickCount Lib "kernel32" () As Long
  46.     Private Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  47.  
  48. 'Variables
  49.     'Internal
  50.     Dim Active As Boolean
  51.     Dim MaxFPS As Long
  52.     Dim KD() As Boolean
  53.    
  54.     Dim Camera As tCamera
  55.    
  56.     'Game objects
  57.     Dim PlayerCount As Long
  58.     Dim Player() As tPlayer
  59.    
  60.     Dim BallCount As Long
  61.     Dim Ball() As tObject
  62.    
  63.     Dim PadSpeed As Long
  64.     Dim PadAcceleration As Single
  65.    
  66.     Dim BallSpeed As Long
  67.     Dim BallAcceleration As Single
  68.    
  69.     Dim Friction As Single
  70.    
  71. Private Sub CheckKeys()
  72.     Dim A As Long
  73.    
  74.     If KD(vbKeyEscape) Then: Unload Me
  75.    
  76.     For A = 0 To PlayerCount
  77.         With Player(A)
  78.             If KD(.KeyTable.KeyUp) Then
  79.                 'Key press up
  80.                 .Pad.SpeedY = .Pad.SpeedY - PadAcceleration
  81.                 If .Pad.SpeedY < -PadSpeed Then: .Pad.SpeedY = -PadSpeed
  82.             End If
  83.            
  84.             If KD(.KeyTable.KeyDown) Then
  85.                 'Key press down
  86.                 .Pad.SpeedY = .Pad.SpeedY + PadAcceleration
  87.                 If .Pad.SpeedY > PadSpeed Then: .Pad.SpeedY = PadSpeed
  88.             End If
  89.            
  90.             If Not (KD(.KeyTable.KeyUp) Or KD(.KeyTable.KeyDown)) Then
  91.                 'No keys
  92.                 .Pad.SpeedY = .Pad.SpeedY * Friction
  93.                 If Abs(.Pad.SpeedY) < Friction Then: .Pad.SpeedY = 0
  94.             End If
  95.         End With
  96.     Next
  97. End Sub
  98.  
  99. Private Sub DrawObject(iObject As tObject)
  100.     'Draw object rect
  101.     With iObject
  102.         Me.FillColor = .Color
  103.         Me.ForeColor = .Color
  104.        
  105.         Rectangle Camera.DC, .X, .Y, (.X + .w), (.Y + .h)
  106.     End With
  107. End Sub
  108.  
  109. Private Sub DrawScore()
  110.     Dim A As Long
  111.    
  112.     For A = 0 To PlayerCount
  113.         With Me
  114.             'Text position
  115.             .CurrentX = Player(A).Pad.X
  116.             .CurrentY = 20
  117.            
  118.             'Print text
  119.             .ForeColor = RGB(255, 0, 0)
  120.             Me.Print CStr(Player(A).Score)
  121.         End With
  122.     Next
  123. End Sub
  124.  
  125. Private Sub GameOver(iWinner As Long)
  126.     Dim A As Long
  127.    
  128.     'Beeps
  129.     For A = 0 To 4: Beep: Next
  130.    
  131.     Player(iWinner).Score = Player(iWinner).Score + 1
  132.    
  133.     'Play again?
  134.     If MsgBox(Player(iWinner).Name & " wins!" & vbNewLine & vbNewLine & "Play again?", vbInformation Or vbYesNo, "Game Over") = vbNo Then: Unload Me
  135. End Sub
  136.  
  137. Private Sub InitBall(iObject As tObject)
  138.     With iObject
  139.         'Start position
  140.         .X = (Camera.w - .w) / 2
  141.         .Y = (Camera.h - .h) / 2
  142.        
  143.         'Initial speed
  144.         .SpeedX = IIf(100 * Rnd > 50, BallSpeed, -BallSpeed)
  145.         .SpeedY = ((2 * Rnd) - 1) * BallSpeed
  146.     End With
  147.    
  148.     'Release pressed keys
  149.      ReDim KD(255)
  150. End Sub
  151.  
  152. Sub Main()
  153.     Dim Temp As Long
  154.    
  155.     While Active
  156.         If Temp < GetTickCount Then
  157.             'Timing
  158.             Temp = GetTickCount + (1000 / MaxFPS)
  159.            
  160.             'Interface
  161.             CheckKeys
  162.             MoveObjects
  163.         End If
  164.        
  165.         If Active Then
  166.             'Get context
  167.             Me.Cls
  168.             Camera.DC = Me.hdc
  169.            
  170.             'Update window
  171.             DrawObjects
  172.             DrawScore
  173.            
  174.             Me.Refresh
  175.         End If
  176.        
  177.         DoEvents
  178.     Wend
  179. End Sub
  180.  
  181. Private Sub MoveObject(iObject As tObject)
  182.     Dim A As Long
  183.    
  184.     With iObject
  185.         If .IsBall Then
  186.             For A = 0 To PlayerCount
  187.                 If (.X + .w) > Player(A).Pad.X And (.Y + .h) > Player(A).Pad.Y Then
  188.                     If .X < (Player(A).Pad.X + Player(A).Pad.w) And .Y < (Player(A).Pad.Y + Player(A).Pad.h) Then
  189.                         'Change direction
  190.                         .SpeedX = -(.SpeedX * BallAcceleration)
  191.                         .SpeedY = ((2 * Rnd) - 1) * BallSpeed
  192.                     End If
  193.                 End If
  194.             Next
  195.            
  196.             'Pre-check clipping
  197.             If (.X + .SpeedX) < 0 Then
  198.                 GameOver 1
  199.                 InitBall iObject
  200.             End If
  201.            
  202.             If (.X + .SpeedX + .w) > Camera.w Then
  203.                 GameOver 0
  204.                 InitBall iObject
  205.             End If
  206.            
  207.             If (.Y + .SpeedY) < 0 Then: .SpeedY = -.SpeedY
  208.             If (.Y + .SpeedY + .h) > Camera.h Then: .SpeedY = -.SpeedY
  209.            
  210.         Else
  211.             'Pre-check clipping
  212.             If (.X + .SpeedX) < 0 Then: .SpeedX = -(.SpeedX * Friction)
  213.             If (.Y + .SpeedY) < 0 Then: .SpeedY = -(.SpeedY * Friction)
  214.            
  215.             If (.X + .SpeedX + .w) > Camera.w Then: .SpeedX = -(.SpeedX * Friction)
  216.             If (.Y + .SpeedY + .h) > Camera.h Then: .SpeedY = -(.SpeedY * Friction)
  217.         End If
  218.        
  219.         'Add speed to position
  220.         .X = (.X + .SpeedX)
  221.         .Y = (.Y + .SpeedY)
  222.     End With
  223. End Sub
  224.  
  225. Private Sub MoveObjects()
  226.     Dim A As Long
  227.    
  228.     'Move objects
  229.     For A = 0 To PlayerCount: MoveObject Player(A).Pad: Next
  230.     For A = 0 To BallCount: MoveObject Ball(A): Next
  231. End Sub
  232.  
  233. Private Sub DrawObjects()
  234.     Dim A As Long
  235.    
  236.     'Draw graphics
  237.     For A = 0 To PlayerCount: DrawObject Player(A).Pad: Next
  238.     For A = 0 To BallCount: DrawObject Ball(A): Next
  239. End Sub
  240.  
  241. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  242.     KD(KeyCode) = True
  243. End Sub
  244.  
  245. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  246.     KD(KeyCode) = False
  247. End Sub
  248.  
  249. Private Sub Form_Load()
  250.     Dim A As Long
  251.    
  252.     'General
  253.     Active = True
  254.     MaxFPS = 40
  255.     ReDim KD(255)
  256.    
  257.     PadSpeed = 10
  258.     PadAcceleration = 1
  259.    
  260.     BallSpeed = 5
  261.     BallAcceleration = 1.1
  262.    
  263.     Friction = 0.5
  264.    
  265.     'Setup window
  266.     With Me
  267.         'General
  268.         .Caption = "PCEPR-Game: Ping-Pong"
  269.        
  270.         'Drawing
  271.         .BackColor = 0
  272.         .ScaleMode = vbPixels
  273.         .FillStyle = 0
  274.         .Font = "Fixedsys"
  275.         .FontSize = 9
  276.         .AutoRedraw = True
  277.        
  278.         'Size and position
  279.         .Width = 9000
  280.         .Height = 7000
  281.         .Move (Screen.Width - .Width) / 2, (Screen.Height - .Height) / 2
  282.     End With
  283.    
  284.     'Allocate memory
  285.     PlayerCount = 1
  286.     ReDim Player(1)
  287.    
  288.     BallCount = 0
  289.     ReDim Ball(BallCount)
  290.    
  291.     'Setup objects
  292.     A = 0
  293.     With Player(A)
  294.         .Name = InputBox("Enter name for player " & CStr(A) & ":", "New player")
  295.        
  296.         With .KeyTable
  297.             'Keys
  298.             .KeyUp = vbKeyA
  299.             .KeyDown = vbKeyY
  300.         End With
  301.        
  302.         With .Pad
  303.             'Size
  304.             .w = 10
  305.             .h = 100
  306.            
  307.             'Start position
  308.             .X = 10
  309.             .Y = (Camera.h - .h) / 2
  310.            
  311.             'Movement
  312.             .SpeedX = 0
  313.             .SpeedY = 0
  314.            
  315.             'General
  316.             .IsBall = False
  317.             .Color = RGB(0, 0, 255)
  318.         End With
  319.     End With
  320.    
  321.     A = 1
  322.     With Player(A)
  323.         .Name = InputBox("Enter name for player " & CStr(A) & ":", "New player")
  324.        
  325.         With .KeyTable
  326.             'Keys
  327.             .KeyUp = vbKeyK
  328.             .KeyDown = vbKeyM
  329.         End With
  330.        
  331.         With .Pad
  332.             'Size
  333.             .w = 10
  334.             .h = 100
  335.            
  336.             'Start position
  337.             .X = Camera.w - .w - 10
  338.             .Y = (Camera.h - .h) / 2
  339.            
  340.             'Movement
  341.             .SpeedX = 0
  342.             .SpeedY = 0
  343.            
  344.             'General
  345.             .IsBall = False
  346.             .Color = RGB(0, 255, 0)
  347.         End With
  348.     End With
  349.    
  350.     For A = 0 To BallCount
  351.         With Ball(A)
  352.             'Size
  353.             .w = 20
  354.             .h = 20
  355.            
  356.             InitBall Ball(A)
  357.            
  358.             'General
  359.             .IsBall = True
  360.             .Color = RGB(255, 0, 0)
  361.         End With
  362.     Next
  363.    
  364.     'Run
  365.     Me.Show
  366.     Main
  367. End Sub
  368.  
  369. Private Sub Form_Resize()
  370.     If Active Then
  371.         With Camera
  372.             'Screen size
  373.             .w = Me.ScaleWidth
  374.             .h = Me.ScaleHeight
  375.            
  376.             'General
  377.             .DC = Me.hdc
  378.         End With
  379.     End If
  380. End Sub
  381.  
  382. Private Sub Form_Unload(Cancel As Integer)
  383.     Active = False
  384. End Sub