Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Throw or Hit The Ball Example
#1
This can be used in an unlimited amount of games. It shows a ball (or cannonball) randomly being thrown or hit in a 3D perspective toward the Z axis. There is no Z axis in the code though, it uses a parabola equation I found on ChatGPT and after experimentation I came up with this. I also found the SGN command on ChatGPT that helps with choosing either a positive or negative random number. I used to do it a different way in my games, but had forgotten how I did it, so I found this. 

Feel free to use this in any of your games or programs. Would make a cool baseball or golf game I would think. Smile 

Left click the mouse to throw each time.

Code: (Select All)

'Ken's Throw The Ball Demonstration

'I got the parabola equation and SGN command for random numbers from ChatGPT and I made the rest.
'Feel free to use this as you wish.

Screen _NewImage(800, 600, 32)
Cls
_Title "Click The Left Mouse Button To Throw"
' Define the coefficients for the parabola y = ax^2 + bx + c
Dim a As Double, b As Double, c As Double

centerX = 400
centerY = 300
c = 0
s = 100
ball = 8
Do
    Do
        Do While _MouseInput
            oldm = m
            m = _MouseWheel
            If _MouseButton(1) Then
                b = Int(Rnd * _Pi) + 6
                a = Int(Rnd * 7) - 7
                randomx = (Rnd * 10) * Sgn(Rnd - 0.5)
                Cls
            End If
        Loop
    Loop Until _MouseButton(1)
    If m > oldm Then
        a = a + .1
        b = b + .1
        Cls
        oldm = m
    End If
    If m < oldm Then
        a = a - .1
        Cls
        oldm = m
    End If
    ' Set up a loop to draw the parabola
    For x = -20 To 30 + a Step .5
        ' Calculate y using the parabola equation
        y = a * (x / 10) ^ 2 + b * (x / 10) + c

        ' Convert to screen coordinates
        screenX = centerX + x * randomx
        screenY = centerY - y * 10
        If ball > 1.5 Then ball = ball - .2
        ' Plot the point
        Circle (screenX, screenY), ball, _RGB32(255, 255, 255)
        _Delay .05
        s = s + .1
        Sound s, 1
        _Display
        Cls
    Next x
    s = 100
    ball = 8
Loop Until InKey$ = Chr$(27)
End
Reply


Messages In This Thread
Throw or Hit The Ball Example - by SierraKen - 08-23-2024, 10:40 PM
RE: Throw or Hit The Ball Example - by Dav - 08-24-2024, 12:41 PM
RE: Throw or Hit The Ball Example - by bplus - 08-24-2024, 01:44 PM
RE: Throw or Hit The Ball Example - by SierraKen - 08-24-2024, 04:20 PM
RE: Throw or Hit The Ball Example - by SierraKen - 08-24-2024, 05:06 PM
RE: Throw or Hit The Ball Example - by Pete - 08-25-2024, 02:49 PM
RE: Throw or Hit The Ball Example - by a740g - 08-25-2024, 04:19 PM
RE: Throw or Hit The Ball Example - by SierraKen - 08-25-2024, 03:04 PM
RE: Throw or Hit The Ball Example - by SierraKen - 08-26-2024, 03:24 AM
RE: Throw or Hit The Ball Example - by Pete - 08-26-2024, 05:25 AM



Users browsing this thread: 5 Guest(s)