Posts: 424
Threads: 76
Joined: Apr 2022
Reputation:
23
08-23-2024, 10:40 PM
(This post was last modified: 08-23-2024, 10:41 PM by SierraKen.)
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.
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
Posts: 730
Threads: 120
Joined: Apr 2022
Reputation:
106
Nice small example. Would be useful for a throw a ball at the milk bottles game, or a dart game. I may use it for something like that.
I sometimes ask DeepAI for algorithm examples when I’m totally stuck (I’m not very math smart). It’s only 2021 trained, and it’s QB64 code is usually broken, but not bad for a free AI that needs no registering.
- Dav
Posts: 4,001
Threads: 180
Joined: Apr 2022
Reputation:
222
08-24-2024, 01:44 PM
(This post was last modified: 08-24-2024, 01:50 PM by bplus.)
What is supposed to be happening with _MouseWheel, you have m, oldm and they are affecting a, b of parabolic equation but I can't see any difference when I scroll mouse?
See any difference?
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
b = b + ...
Posts: 424
Threads: 76
Joined: Apr 2022
Reputation:
23
LOL Woops! I forgot to remove the mouse wheel code from when I was trying to learn the parabola variables. I had it where the mouse wheel was making the parabola wider or smaller. lol Thanks for pointing that out B+. Here is the code without it.
Dav, I'm glad you like this. But please get this code instead, thanks.
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
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)
' 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
Posts: 424
Threads: 76
Joined: Apr 2022
Reputation:
23
08-24-2024, 05:06 PM
(This post was last modified: 08-24-2024, 05:08 PM by SierraKen.)
Here's an even better one. The ball somewhat goes toward the direction of where the mouse pointer is now. It's still very random, but I think it would be a fun game.
Feel free to adjust the code of course. This was all done with experimentation.
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
Randomize Timer
Do
Do
Do While _MouseInput
If _MouseButton(1) Then
b = Int(Rnd * _Pi) + 6
a = Int(Rnd * 7) - 7
'randomx = (Rnd * 10) * Sgn(Rnd - 0.5)
If _MouseX < 400 Then randomx = _MouseX / 30 * -1
If _MouseX > 399 Then randomx = _MouseX / 50
randomy = _MouseY / 30
Cls
End If
Loop
Loop Until _MouseButton(1)
' 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
screeny = centerY - y * randomy
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
Posts: 2,197
Threads: 224
Joined: Apr 2022
Reputation:
104
Makes me wish once again the SOUND statement was improved to allow for volume control. Fade the volume as the ball progresses. I really never figured out why the QuickBASIC developers never structured it that way.
+1 for pseudo 3-D effect.
Pete
Shoot first and shoot people who ask questions, later.
Posts: 424
Threads: 76
Joined: Apr 2022
Reputation:
23
Pete, probably because QuickBasic used the internal speaker. I don't remember anything ever adjusting the volume of that, probably has a set volume.
Posts: 372
Threads: 23
Joined: May 2022
Reputation:
56
(08-25-2024, 02:49 PM)Pete Wrote: Makes me wish once again the SOUND statement was improved to allow for volume control. Fade the volume as the ball progresses. I really never figured out why the QuickBASIC developers never structured it that way.
+1 for pseudo 3-D effect.
Pete
Actually, in QB64-PE SOUND does support volume, panning and more.
Code: (Select All) SOUND frequency#, duration#[, volume#][, panning#][, waveform&]
Posts: 424
Threads: 76
Joined: Apr 2022
Reputation:
23
Wow thanks A740g! I just improved the ball throwing example to make the ball sound volume go less and less the farther away it is.
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
volume = 1
Randomize Timer
Do
Do
Do While _MouseInput
If _MouseButton(1) Then
b = Int(Rnd * _Pi) + 6
a = Int(Rnd * 7) - 7
'randomx = (Rnd * 10) * Sgn(Rnd - 0.5)
If _MouseX < 400 Then randomx = _MouseX / 30 * -1
If _MouseX > 399 Then randomx = _MouseX / 50
randomy = _MouseY / 30
Cls
End If
Loop
Loop Until _MouseButton(1)
' 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
screeny = centerY - y * randomy
If ball > 1.5 Then ball = ball - .2
' Plot the point
Circle (screenX, screeny), ball, _RGB32(255, 255, 255)
_Delay .05
s = s + .1
If volume > .05 Then volume = volume - .02
Sound s, 1, volume
_Display
Cls
Next x
s = 100
ball = 8
volume = 1
Loop Until InKey$ = Chr$(27)
End
Posts: 2,197
Threads: 224
Joined: Apr 2022
Reputation:
104
(08-25-2024, 04:19 PM)a740g Wrote: (08-25-2024, 02:49 PM)Pete Wrote: Makes me wish once again the SOUND statement was improved to allow for volume control. Fade the volume as the ball progresses. I really never figured out why the QuickBASIC developers never structured it that way.
+1 for pseudo 3-D effect.
Pete
Actually, in QB64-PE SOUND does support volume, panning and more.
Code: (Select All) SOUND frequency#, duration#[, volume#][, panning#][, waveform&]
Nice! I see Ken already took advantage of it. Thanks, Sam, for adding this to the project. +2.
Pete
Shoot first and shoot people who ask questions, later.
|