Results 1 to 2 of 2

Thread: VB6 - CPP: Pong [source]

  1. #1

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088

    VB6 - CPP: Pong [source]

    Erm... well *g* Open a new project, add the following code and run.
    Remember to delete the default "Form_Load" sub before copying.
    There's 2 postings because it was too long for one.

    VB Code:
    1. 'Copy-Pong by [email][email protected][/email]
    2. 'Written on 2003-04-21
    3. 'Visit [url]http://vbfx.yhoko.com/[/url]
    4.  
    5. 'Types
    6.     Private Type tKeyTable
    7.         KeyUp As Long
    8.         KeyDown As Long
    9.     End Type
    10.    
    11.     Private Type tCamera
    12.         'Size
    13.         w As Long
    14.         h As Long
    15.        
    16.         'General
    17.         DC As Long
    18.     End Type
    19.    
    20.     Private Type tObject
    21.         'Size
    22.         x As Single
    23.         y As Single
    24.        
    25.         'Start position
    26.         w As Long
    27.         h As Long
    28.        
    29.         'Movement
    30.         SpeedX As Single
    31.         SpeedY As Single
    32.        
    33.         'General
    34.         IsBall As Boolean
    35.         Color As Long
    36.     End Type
    37.    
    38.     Private Type tPlayer
    39.         'General
    40.         Name As String
    41.         Score As Long
    42.        
    43.         'Internal
    44.         KeyTable As tKeyTable
    45.         Pad As tObject
    46.     End Type
    47.  
    48. 'Declares
    49.     Private Declare Function GetTickCount Lib "kernel32" () As Long
    50.     Private Declare Function Rectangle Lib "gdi32" (ByVal iDC As Long, _
    51.         ByVal x1 As Long, ByVal y1 As Long, ByVal x2 As Long, ByVal y2 As Long) As Long
    52.  
    53. 'Variables
    54.     'Internal
    55.     Dim Active As Boolean
    56.     Dim MaxFPS As Long
    57.     Dim KD() As Boolean
    58.    
    59.     Dim Camera As tCamera
    60.    
    61.     'Game objects
    62.     Dim PlayerCount As Long
    63.     Dim Player() As tPlayer
    64.    
    65.     Dim BallCount As Long
    66.     Dim Ball() As tObject
    67.    
    68.     Dim PadSpeed As Long
    69.     Dim PadAcceleration As Single
    70.    
    71.     Dim BallSpeed As Long
    72.     Dim BallAcceleration As Single
    73.    
    74.     Dim Friction As Single
    75.    
    76. Private Sub CheckKeys()
    77.     Dim A As Long
    78.    
    79.     If KD(vbKeyEscape) Then: Unload Me
    80.    
    81.     For A = 0 To PlayerCount
    82.         With Player(A)
    83.             If KD(.KeyTable.KeyUp) Then
    84.                 'Key press up
    85.                 .Pad.SpeedY = .Pad.SpeedY - PadAcceleration
    86.                 If .Pad.SpeedY < -PadSpeed Then: .Pad.SpeedY = -PadSpeed
    87.             End If
    88.            
    89.             If KD(.KeyTable.KeyDown) Then
    90.                 'Key press down
    91.                 .Pad.SpeedY = .Pad.SpeedY + PadAcceleration
    92.                 If .Pad.SpeedY > PadSpeed Then: .Pad.SpeedY = PadSpeed
    93.             End If
    94.            
    95.             If Not (KD(.KeyTable.KeyUp) Or KD(.KeyTable.KeyDown)) Then
    96.                 'No keys
    97.                 .Pad.SpeedY = .Pad.SpeedY * Friction
    98.                 If Abs(.Pad.SpeedY) < Friction Then: .Pad.SpeedY = 0
    99.             End If
    100.         End With
    101.     Next
    102. End Sub
    103.  
    104. Private Sub DrawObject(iObject As tObject)
    105.     Dim Temp As tKeyTable
    106.    
    107.     'Draw object rect
    108.     With iObject
    109.         Me.FillColor = .Color
    110.         Me.ForeColor = .Color
    111.        
    112.         If .IsBall Then
    113.             Me.Circle (CLng(.x), CLng(.y)), .w
    114.         Else
    115.             Rectangle Camera.DC, CLng(.x), CLng(.y), CLng(.x + .w), CLng(.y + .h)
    116.         End If
    117.     End With
    118. End Sub
    119.  
    120. Private Sub DrawScore()
    121.     Dim A As Long
    122.    
    123.     For A = 0 To PlayerCount
    124.         With Me
    125.             'Text position
    126.             .CurrentX = Player(A).Pad.x
    127.             .CurrentY = 20
    128.            
    129.             'Print text
    130.             .ForeColor = RGB(255, 0, 0)
    131.             Me.Print CStr(Player(A).Score)
    132.         End With
    133.     Next
    134. End Sub
    135.  
    136. Private Sub GameOver(iWinner As Long)
    137.     Dim A As Long
    138.    
    139.     'Beeps
    140.     For A = 0 To 4: Beep: Next
    141.    
    142.     Player(iWinner).Score = Player(iWinner).Score + 1
    143.    
    144.     'Play again?
    145.     If MsgBox(Player(iWinner).Name & " wins!" & vbNewLine & vbNewLine & _
    146.         "Play again?", vbInformation Or vbYesNo, "Game Over") = vbNo Then: Unload Me
    147. End Sub
    148.  
    149. Private Sub InitBall(iObject As tObject)
    150.     With iObject
    151.         'Start position
    152.         .x = (Camera.w - .w) / 2
    153.         .y = (Camera.h - .h) / 2
    154.        
    155.         'Initial speed
    156.         .SpeedX = IIf(100 * Rnd > 50, BallSpeed, -BallSpeed)
    157.         .SpeedY = ((2 * Rnd) - 1) * BallSpeed
    158.     End With
    159.    
    160.     'Release pressed keys
    161.      ReDim KD(255)
    162. End Sub
    163.  
    164. Sub Main()
    165.     Dim Temp As Long
    166.    
    167.     While Active
    168.         If Temp < GetTickCount Then
    169.             'Timing
    170.             Temp = GetTickCount + (1000 / MaxFPS)
    171.            
    172.             'Interface
    173.             CheckKeys
    174.             MoveObjects
    175.            
    176.             If Active Then
    177.                 'Get context
    178.                 Me.Cls
    179.                 Camera.DC = Me.hdc
    180.                
    181.                 'Update window
    182.                 DrawObjects
    183.                 DrawScore
    184.                
    185.                 Me.Refresh
    186.             End If
    187.         End If
    188.        
    189.         DoEvents
    190.     Wend
    191. End Sub
    Last edited by Fox; Sep 12th, 2008 at 11:00 PM. Reason: Links updated

  2. #2

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    VB Code:
    1. Private Sub MoveObject(iObject As tObject)
    2.     Dim A As Long
    3.    
    4.     With iObject
    5.         If .IsBall Then
    6.             For A = 0 To PlayerCount
    7.                 If (.x + .w) > Player(A).Pad.x And (.y + .h) > Player(A).Pad.y Then
    8.                     If .x < (Player(A).Pad.x + Player(A).Pad.w) Then
    9.                         If .y < (Player(A).Pad.y + Player(A).Pad.h) Then
    10.                             'Change direction
    11.                             .SpeedX = -(.SpeedX * BallAcceleration)
    12.                             .SpeedY = ((2 * Rnd) - 1) * BallSpeed
    13.                         End If
    14.                     End If
    15.                 End If
    16.             Next
    17.            
    18.             'Pre-check clipping
    19.             If (.x + .SpeedX) < 0 Then
    20.                 GameOver 1
    21.                 InitBall iObject
    22.             End If
    23.            
    24.             If (.x + .SpeedX + .w) > Camera.w Then
    25.                 GameOver 0
    26.                 InitBall iObject
    27.             End If
    28.            
    29.             If (.y + .SpeedY) < 0 Then: .SpeedY = -.SpeedY
    30.             If (.y + .SpeedY + .h) > Camera.h Then: .SpeedY = -.SpeedY
    31.            
    32.         Else
    33.             'Pre-check clipping
    34.             If (.x + .SpeedX) < 0 Then: .SpeedX = -(.SpeedX * Friction)
    35.             If (.y + .SpeedY) < 0 Then: .SpeedY = -(.SpeedY * Friction)
    36.            
    37.             If (.x + .SpeedX + .w) > Camera.w Then: .SpeedX = -(.SpeedX * Friction)
    38.             If (.y + .SpeedY + .h) > Camera.h Then: .SpeedY = -(.SpeedY * Friction)
    39.         End If
    40.        
    41.         'Add speed to position
    42.         .x = (.x + .SpeedX)
    43.         .y = (.y + .SpeedY)
    44.     End With
    45. End Sub
    46.  
    47. Private Sub MoveObjects()
    48.     Dim A As Long
    49.    
    50.     'Move objects
    51.     For A = 0 To PlayerCount: MoveObject Player(A).Pad: Next
    52.     For A = 0 To BallCount: MoveObject Ball(A): Next
    53. End Sub
    54.  
    55. Private Sub DrawObjects()
    56.     Dim A As Long
    57.    
    58.     'Draw graphics
    59.     For A = 0 To PlayerCount: DrawObject Player(A).Pad: Next
    60.     For A = 0 To BallCount: DrawObject Ball(A): Next
    61. End Sub
    62.  
    63. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    64.     KD(KeyCode) = True
    65. End Sub
    66.  
    67. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    68.     KD(KeyCode) = False
    69. End Sub
    70.  
    71. Private Sub Form_Load()
    72.     Dim A As Long
    73.     Dim Temp As String
    74.    
    75.     'General
    76.     Active = True
    77.     MaxFPS = 40
    78.     ReDim KD(255)
    79.    
    80.     PadSpeed = 10
    81.     PadAcceleration = 1
    82.    
    83.     BallSpeed = 5
    84.     BallAcceleration = 1.1
    85.    
    86.     Friction = 0.5
    87.    
    88.     'Setup window
    89.     With Me
    90.         'General
    91.         .Caption = "CPP (Copy-Paste-Project): Pong"
    92.        
    93.         'Drawing
    94.         .BackColor = 0
    95.         .ScaleMode = vbPixels
    96.         .FillStyle = 0
    97.         .Font = "Fixedsys"
    98.         .FontSize = 9
    99.         .AutoRedraw = True
    100.        
    101.         'Size and position
    102.         .Width = 9000
    103.         .Height = 7000
    104.         .Move (Screen.Width - .Width) / 2, (Screen.Height - .Height) / 2
    105.     End With
    106.    
    107.     'Allocate memory
    108.     PlayerCount = 1
    109.     ReDim Player(1)
    110.    
    111.     BallCount = 0
    112.     ReDim Ball(BallCount)
    113.    
    114.     'User setup
    115.     A = 0
    116.     With Player(A)
    117.         .Name = InputBox("Enter name for player " & CStr(A + 1) & ":", "New player")
    118.        
    119.         With .KeyTable
    120.             'Keys
    121.             .KeyUp = vbKeyA
    122.             .KeyDown = vbKeyY 'vbKeyZ
    123.         End With
    124.        
    125.         'Individual setup
    126.         With .Pad
    127.             'Size
    128.             .w = 10
    129.             .h = 100
    130.            
    131.             'Start position
    132.             .x = 10
    133.             .y = (Camera.h - .h) / 2
    134.            
    135.             'Movement
    136.             .SpeedX = 0
    137.             .SpeedY = 0
    138.            
    139.             'General
    140.             .IsBall = False
    141.             .Color = RGB(0, 0, 255)
    142.         End With
    143.        
    144.         'Key-help
    145.         Temp = Temp & "Player " & CStr(A + 1) & ": " & Chr(Player(A).KeyTable.KeyUp) & _
    146.             " and " & Chr(Player(A).KeyTable.KeyDown) & vbNewLine
    147.     End With
    148.    
    149.     A = 1
    150.     With Player(A)
    151.         .Name = InputBox("Enter name for player " & CStr(A + 1) & ":", "New player")
    152.        
    153.         With .KeyTable
    154.             'Keys
    155.             .KeyUp = vbKeyK
    156.             .KeyDown = vbKeyM
    157.         End With
    158.        
    159.         'Individual setup
    160.         With .Pad
    161.             'Size
    162.             .w = 10
    163.             .h = 100
    164.            
    165.             'Start position
    166.             .x = Camera.w - .w - 10
    167.             .y = (Camera.h - .h) / 2
    168.            
    169.             'Movement
    170.             .SpeedX = 0
    171.             .SpeedY = 0
    172.            
    173.             'General
    174.             .IsBall = False
    175.             .Color = RGB(0, 255, 0)
    176.         End With
    177.        
    178.         'Key-help
    179.         Temp = Temp & "Player " & CStr(A + 1) & ": " & Chr(Player(A).KeyTable.KeyUp) & _
    180.             " and " & Chr(Player(A).KeyTable.KeyDown) & vbNewLine
    181.     End With
    182.    
    183.     MsgBox "Key configuration:" & vbNewLine & vbNewLine & Temp, vbInformation
    184.    
    185.     For A = 0 To BallCount
    186.         With Ball(A)
    187.             'Size
    188.             .w = 20
    189.             .h = 20
    190.            
    191.             InitBall Ball(A)
    192.            
    193.             'General
    194.             .IsBall = True
    195.             .Color = RGB(255, 0, 0)
    196.         End With
    197.     Next
    198.    
    199.     'Run
    200.     Me.Show
    201.     Main
    202. End Sub
    203.  
    204. Private Sub Form_Resize()
    205.     If Active Then
    206.         With Camera
    207.             'Screen size
    208.             .w = Me.ScaleWidth
    209.             .h = Me.ScaleHeight
    210.            
    211.             'General
    212.             .DC = Me.hdc
    213.         End With
    214.     End If
    215. End Sub
    216.  
    217. Private Sub Form_Unload(Cancel As Integer)
    218.     Active = False
    219. End Sub
    Last edited by Fox; Nov 29th, 2004 at 12:06 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
  •  



Click Here to Expand Forum to Full Width