Basketballspiel

  • VB6

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von Neptun.

    Basketballspiel

    Hey Leute,

    wir machen gerade in der Schule visual basic und ich wollte fragen, wie man eine kugel in einen basket ball korb (als image im hintergrund) reinwerfen kann, und wie man der Kugel je nach Mausbewegung eine andere Flugbahn geben kann. Man soll mit der Maus den Ball ziehen und wieder loslassen so dass er in die gewählte Richtung fliegt.

    Vielen Dank schon im vorraus,

    xDerPaddix :) :thumbup:
    Ihr lernt in der Schule ernsthaft VB 6?! *faceplam*
    Wenn das dein ernst ist, würde ich anfangen, keine Pictureboxen zu nehmen sondern, sofern in VB 6 vorhanden, irgend eine Grafikengine (GDI(+) ?) zu nutzten.
    Mfg
    Vincent

    wie wäre es dann es zu lernen ?
    dafür bist du ja in der Schule.

    MFG BlackNetworkBit
    MFG 0x426c61636b4e6574776f726b426974
    InOffical VB-Paradise IRC-Server
    webchat.freenode.net/
    Channel : ##vbparadise
    Hallo xDerPaddix,
    für einen Einsteiger ist das ein bisschen schwierig.
    Hier mal ein Ansatz:

    Visual Basic-Quellcode

    1. ' Controls: 1 * Timer, 1 * Picturebox
    2. Option Explicit
    3. Private XBall&, YBall&, Rad&, Speed&, XV&, YV&
    4. Private Sub Form_Load()
    5. Me.ScaleMode = vbPixels
    6. Me.BackColor = RGB(128, 128, 128)
    7. Me.WindowState = vbMaximized
    8. End Sub
    9. Private Sub Form_Activate()
    10. DoEvents
    11. Rad = 40 ' Ballgrösse
    12. With Picture1
    13. .BorderStyle = vbBSNone
    14. .ScaleMode = vbPixels
    15. .Move 100, 100, Me.ScaleWidth - 200, Me.ScaleHeight - 100
    16. .BackColor = RGB(221, 155, 44)
    17. .AutoRedraw = True
    18. Picture1.Line (0, .ScaleHeight / 2)-(.ScaleWidth, 0), RGB(222, 222, 222), BF
    19. Set .Picture = .Image
    20. XBall = .ScaleWidth / 2
    21. YBall = 0
    22. End With
    23. Speed = 1
    24. Timer1.Interval = 12
    25. Timer1.Enabled = True
    26. End Sub
    27. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    28. Dim dx&, dy&
    29. dx = XBall - X
    30. dy = YBall - Y
    31. If dx * dx + dy * dy <= Rad * Rad Then
    32. Timer1.Enabled = False
    33. XV = XBall - X
    34. YV = YBall - Y
    35. End If
    36. End Sub
    37. Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    38. If Button = vbLeftButton Then
    39. XBall = X + XV
    40. YBall = Y + YV
    41. Call DrawBall
    42. Picture1.Refresh
    43. End If
    44. End Sub
    45. Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    46. Speed = 1
    47. Timer1.Enabled = True
    48. End Sub
    49. Private Sub Timer1_Timer()
    50. If YBall + Rad + Speed < Picture1.ScaleHeight - 1 Then
    51. YBall = YBall + Speed
    52. Speed = Speed + 1
    53. Else
    54. YBall = Picture1.ScaleHeight - 1 - Rad
    55. Timer1.Enabled = False
    56. Beep
    57. End If
    58. Call DrawBall
    59. End Sub
    60. Private Sub DrawBall()
    61. With Picture1
    62. .Cls
    63. .FillStyle = vbFSSolid
    64. .FillColor = RGB(255, 128, 0) ' Ballfarbe
    65. Picture1.Circle (XBall, YBall), Rad
    66. .FillStyle = vbDiagonalCross
    67. .FillColor = vbBlack
    68. Picture1.Circle (XBall, YBall), Rad, vbBlack
    69. .FillStyle = vbTransparent
    70. End With
    71. End Sub
    Gruss,

    Neptun
    Hallo xDerPaddix,
    war noch ein Bug drin. Hab's noch mal überarbeitet:

    Visual Basic-Quellcode

    1. ' Controls: 1 * Timer, 3 * Picturebox
    2. Option Explicit
    3. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    4. Private Declare Function TransparentBlt& Lib "msimg32.dll" (ByVal hdcDest&, _
    5. ByVal nXOriginDest&, ByVal nYOriginDest&, ByVal nWidthDest&, ByVal nHeightDest&, _
    6. ByVal hdcSrc&, ByVal nXOriginSrc&, ByVal nYOriginSrc&, ByVal nWidthSrc&, _
    7. ByVal nHeightSrc&, ByVal crTransparent&)
    8. Private XBall&, YBall&, Rad&, Speed&, XV&, YV&
    9. Private SW&, SH&, flgMove As Boolean
    10. Private Sub Form_Load()
    11. With Me
    12. .ScaleMode = vbPixels
    13. .BackColor = RGB(128, 128, 128)
    14. .WindowState = vbMaximized
    15. End With
    16. End Sub
    17. Private Sub Form_Activate()
    18. DoEvents
    19. Rad = 40 ' Ballgrösse
    20. SW = Me.ScaleWidth - 200
    21. SH = Me.ScaleHeight - 100
    22. With Picture1 ' Sichtbares Bild
    23. .BorderStyle = vbBSNone
    24. .ScaleMode = vbPixels
    25. .Move 100, 100, SW, SH
    26. .AutoRedraw = True
    27. End With
    28. With Picture3 ' Hintergrundbild
    29. .Visible = False
    30. .BorderStyle = vbBSNone
    31. .ScaleMode = vbPixels
    32. .BackColor = RGB(221, 155, 44)
    33. .Move 0, 0, SW, SH
    34. .AutoRedraw = True
    35. Picture3.Line (0, SH / 2)-(SW, 0), RGB(222, 222, 222), BF
    36. End With
    37. With Picture2 ' Ball
    38. .Visible = False
    39. .BorderStyle = vbBSNone
    40. .ScaleMode = vbPixels
    41. .BackColor = vbCyan
    42. .Move 0, 0, 2 * Rad, 2 * Rad
    43. .AutoRedraw = True
    44. .FillStyle = vbFSSolid
    45. .FillColor = RGB(255, 128, 0)
    46. Picture2.Circle (Rad, Rad), Rad - 1, vbBlack
    47. .FillStyle = vbFSTransparent
    48. End With
    49. Picture2.Circle (Rad, Rad), Rad - 1, vbBlack, , , 10
    50. Picture2.Circle (Rad, Rad), Rad - 1, vbBlack, , , 1.7
    51. XBall = SW \ 2
    52. YBall = 0
    53. Speed = 1
    54. flgMove = False
    55. Timer1.Interval = 12
    56. Timer1.Enabled = True
    57. End Sub
    58. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    59. Dim DX&, DY&
    60. DX = XBall - x
    61. DY = YBall - y
    62. If DX * DX + DY * DY <= Rad * Rad Then
    63. Timer1.Enabled = False
    64. XV = XBall - x
    65. YV = YBall - y
    66. flgMove = True
    67. End If
    68. End Sub
    69. Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    70. If Button = vbLeftButton And flgMove = True Then
    71. XBall = x + XV
    72. YBall = y + YV
    73. Call DrawBall
    74. End If
    75. End Sub
    76. Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    77. Speed = 2
    78. flgMove = False
    79. Timer1.Enabled = True
    80. End Sub
    81. Private Sub Timer1_Timer()
    82. If YBall + Rad + Speed < SH - 1 Then
    83. YBall = YBall + Speed
    84. Speed = Speed + 1
    85. Else
    86. YBall = SH - 1 - Rad
    87. Timer1.Enabled = False
    88. Beep
    89. End If
    90. Call DrawBall
    91. End Sub
    92. Private Sub DrawBall()
    93. With Picture1
    94. Call BitBlt(.hdc, 0, 0, SW, SH, Picture3.hdc, 0, 0, vbSrcCopy)
    95. Call TransparentBlt(.hdc, XBall - Rad, YBall - Rad, 2 * Rad, 2 * Rad, _
    96. Picture2.hdc, 0, 0, 2 * Rad, 2 * Rad, vbCyan)
    97. .Refresh
    98. End With
    99. End Sub
    Gruss,

    Neptun