Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pinball
#3
@bplus I have improved the flippers a lot! They aren't 100% because they rarely can deflect from the bottom part down, so I might work on that soon. But so far so good! I also reshaped the flippers to a more realistic looking oval shape. Check it out. Smile If I can't fix the flipper deflection anymore than this, I will be satisfied. I used ChatGPT for some help again like you said. Smile I haven't tried PAINT yet on the flippers, but I might look into it. What do you think so far? 
Anyone, please copy any of this code if you wish to use some of it for your own projects, like usual. ChatGPT takes a lot of patience and a lot of testing. lol 

Code: (Select All)

'Pinball by SierraKen
'1-26-25

'Much of this code is from help by B+ with collisions awhile back.
'Thank you B+ and QB64pe Forum!
'Another large chunk of this math is from ChatGPT.

'The paddles only work about 50 percent of the time and I'm not sure how to fix that.
'OItherwise if you keep pressing the paddles they will work.

'The Left Paddle is the Left CTRL key and the Right Paddle is the Right CTRL key.

_Title "Pinball - by SierraKen - Press the Left and Right CTRL keys to move Paddles. Space Bar to reset ball."


Randomize Timer
Screen _NewImage(800, 800, 32)

num = 12

ball = 10


Dim circx(num), circy(num), size(num), red(num), green(num), blue(num)

Dim X As Single, Y As Single
Dim Xc As Single, Yc As Single
Dim a As Single, b As Single
Dim theta As Single, t As Single
Dim thickness As Integer

Dim X2 As Single, Y2 As Single
Dim Xc2 As Single, Yc2 As Single
Dim a2 As Single, b2 As Single
Dim theta2 As Single, t2 As Single
Dim thickness2 As Integer

Xc = 500 ' Center of the screen
Yc = 700
a = 75 ' Semi-major axis
b = 5 ' Semi-minor axis
theta = 45 * _Pi / 180 ' Rotate 45 degrees in radians
thickness = 10 ' Thickness of the ellipse

Xc2 = 300 ' Center of the screen
Yc2 = 700
a2 = 75 ' Semi-major axis
b2 = 10 ' Semi-minor axis
theta2 = 45 * _Pi / -180 ' Rotate 45 degrees in radians
thickness2 = 10 ' Thickness of the ellipse

begin:

slotx = 220
sloty = 690

For cir = 1 To num
    If cir = 1 Then
        circx(cir) = 775
        circy(cir) = 25
        size(cir) = 50
    End If
    If cir = 2 Then
        circx(cir) = 25
        circy(cir) = 25
        size(cir) = 50
    End If
    If cir = 4 Then
        circx(cir) = 635
        circy(cir) = 200
        size(cir) = 40
    End If
    If cir = 5 Then
        circx(cir) = 175
        circy(cir) = 200
        size(cir) = 40
    End If
    If cir = 6 Then
        circx(cir) = 535
        circy(cir) = 350
        size(cir) = 30
    End If
    If cir = 7 Then
        circx(cir) = 275
        circy(cir) = 350
        size(cir) = 30
    End If
    If cir = 8 Then
        circx(cir) = 460
        circy(cir) = 500
        size(cir) = 20
    End If
    If cir = 9 Then
        circx(cir) = 355
        circy(cir) = 500
        size(cir) = 20
    End If
    If cir = 10 Then
        circx(cir) = 400
        circy(cir) = 550
        size(cir) = 15
    End If
    If cir = 11 Then
        circx(cir) = 800
        circy(cir) = 680
        size(cir) = 50
    End If
    If cir = 12 Then
        circx(cir) = 0
        circy(cir) = 680
        size(cir) = 165
    End If
    red(cir) = Rnd * 255
    green(cir) = Rnd * 255
    blue(cir) = Rnd * 255
Next cir


pr = 12 'Radius of ball
pc = _RGB32(0, 255, 0) 'Color of ball

start: '
speedx = 30 '
speedy = 30
gravity = 0.5 ' Gravitational acceleration (adjust this to simulate stronger/weaker gravity)
floorHeight = 680 ' Floor height, where the ball will stop when it hits
px = (Rnd * 550) + 60: py = 60
_Delay 1
Cls

pa = _Pi(1) * Rnd

bounce = 0

Play "MB"

Do

    _Limit 1000

    Line (10, 10)-(790, 10), _RGB32(255, 255, 255)
    Line (790, 10)-(790, 690), _RGB32(255, 255, 255)
    Line (790, 690)-(slotx + 360, 690), _RGB32(255, 255, 255)
    Line (slotx, 690)-(10, 690), _RGB32(255, 255, 255)
    Line (10, 690)-(10, 10), _RGB32(255, 255, 255)

    For cir = 1 To num
        _Limit 1000

        'Bumpers
        fillCircle circx(cir), circy(cir), size(cir), _RGB32(red(cir), green(cir), blue(cir))
        fillCircle circx(cir), circy(cir), size(cir) / 2, _RGB32(255, 255, 127)
        a$ = InKey$
        If a$ = " " Then GoTo start
        If a$ = Chr$(27) Then End

        'Flippers
        If _KeyDown(100305) Then 'Right Side
            If theta > .85 Then GoTo skip1
            theta = theta + .05
        Else
            If theta < -.25 Then GoTo skip1
            theta = theta - .05
        End If
        skip1:
        If _KeyDown(100306) Then 'Left Side
            If theta2 < -.85 Then GoTo skip2
            theta2 = theta2 - .05
        Else
            If theta2 > .25 Then GoTo skip2
            theta2 = theta2 + .05
        End If
        skip2:
        'Right Fklipper
        For offset = 0 To thickness - 1 Step 0.5 ' Add layers for thickness
            For t2 = 0 To 2 * _Pi Step 0.01
                ' Calculate initial x and y for the oval before rotation
                X_initial = (a + offset) * Cos(t2)
                Y_initial = (b + offset) * Sin(t2)

                ' Translate so the end of the oval becomes the origin
                X_translated = X_initial - (a + offset)
                Y_translated = Y_initial

                ' Rotate around the new origin using theta
                X_rotated = X_translated * Cos(theta) - Y_translated * Sin(theta)
                Y_rotated = X_translated * Sin(theta) + Y_translated * Cos(theta)

                ' Translate back to the original position
                X2 = Xc + X_rotated + (a + offset)
                Y2 = Yc + Y_rotated

                ' Draw the point
                PSet (X2, Y2), _RGB32(255, 255, 255)
            Next
        Next

        'Left Flipper
        For offset = 0 To thickness - 1 Step 0.5 ' Add layers for thickness
            For t = 0 To 2 * _Pi Step 0.01
                ' Calculate initial x and y for the second flipper
                X_initial = (a + offset) * Cos(t)
                Y_initial = (b + offset) * Sin(t)

                ' Translate so the opposite end of the oval becomes the origin
                X_translated = X_initial + (a + offset) ' Notice the + instead of -
                Y_translated = Y_initial

                ' Rotate around the new origin using theta for Flipper 2
                X_rotated = X_translated * Cos(theta2) - Y_translated * Sin(theta2)
                Y_rotated = X_translated * Sin(theta2) + Y_translated * Cos(theta2)

                ' Translate back to the original position for Flipper 2
                X = Xc2 + X_rotated - (a + offset) ' Translate back from the opposite end
                Y = Yc2 + Y_rotated

                ' Draw Flipper 2
                PSet (X, Y), _RGB32(255, 255, 255) ' Use a different color for Flipper 2
            Next
        Next

        'Check flippers

        'Left flipper collision
        For chk = -5 To 30 Step .1
            If Sqr(((X - px + chk) / (a + thickness)) ^ 2 + ((Y - py + chk) / (b + thickness)) ^ 2) < 1 And bounce <> -1 Then
                ' Angle of impact considering flipper's rotation
                pa = _Atan2(py - Y, px - X)
                ' Adjust ball speed based on flipper angle
                speedx = speedx + 75 * Cos(theta2)
                speedy = speedy + 150 * Sin(theta2) ' Reverse Y velocity for upward motion
                speedy = -speedy
                px = px + speedx * Cos(pa)
                py = py + speedy * Sin(pa)
            End If
        Next chk
        For chk2 = 0 To -30 Step -.1

            'Right flipper collision
            If Sqr(((X2 - px + chk2) / (a + thickness)) ^ 2 + ((Y2 - py + chk2) / (b + thickness)) ^ 2) < 1 And bounce <> -1 Then
                ' Angle of impact considering flipper's rotation
                pa = _Atan2(py - Y2, px - X2)
                ' Adjust ball speed based on flipper angle
                speedx = speedx + 75 * Cos(theta)
                speedy = speedy + 150 * Sin(theta) ' Reverse Y velocity for upward motion
                speedy = -speedy
                px = px + speedx * Cos(pa)
                py = py + speedy * Sin(pa)
            End If
        Next chk2

        'Check bumpers
        If Sqr((circx(cir) - px) ^ 2 + (circy(cir) - py) ^ 2) < (pr + size(cir)) Then
            pa = _Atan2(py - circy(cir), px - circx(cir))
            pa = pa + Rnd
            px = px + speedx * Cos(pa)
            py = py + speedy * Sin(pa)
            fillCircle circx(cir), circy(cir), size(cir), _RGB32(255, 0, 0)
            fillCircle circx(cir), circy(cir), size(cir) / 1.33, _RGB32(255, 255, 127)
            score = Int(score + 10000 / size(cir))
            Sound 350, .5, , , 2
            Sound 400, .5, , , 3
            Sound 350, .5, , , 4
            Sound 300, .5, , , 5
            _Title "Score: " + Str$(score) + "    Ball: " + Str$(ball) + "    Level: " + Str$(level) + "    Space Bar To Reset Ball. CTRL keys for flippers."
            oldlevel = level
            level = Int(score \ 50000 + 1)
            If oldlevel <> level And level <> 1 Then GoTo begin:
        End If
        If speedx < -60 Then speedx = -60
        If speedy < -150 Then speedy = -150
        If speedx > 30 Then speedx = 30
        If speedy > 150 Then speedy = 150
    Next cir

    'Border reflection.
    If px > 790 - pr Then pa = _Pi - pa * Rnd + 1: px = 790 - pr
    If px < 10 + pr Then pa = _Pi - pa * Rnd + 1: px = 12 + pr
    If py > 790 - pr Then
        If px < slotx Or px > slotx + 360 Then
            pa = -pa * Rnd + 1: py = 790 - pr
        End If
    End If
    If py < 10 + pr Then pa = -pa * Rnd + 1: py = 10 + pr

    If px > slotx + 100 And px < slotx + 300 And py > 670 Then
        floorHeight = floorHeight + 6
        bounce = -1
        '_Title Str$(floorHeight)
        If px > slotx + 150 And px < slotx + 250 And py > 800 Then
            ball = ball - 1
            _Title "Score: " + Str$(score) + "    Ball: " + Str$(ball) + "    Level: " + Str$(level) + "    Space Bar To Reset Ball, CTRL keys for flippers."
            px = (Rnd * 550) + 60: py = 60
            down = 0
            floorHeight = 680
            _Delay 1
            If ball = 0 Then End
        End If
    Else
        bounce = 0
        floorHeight = 680
    End If


    ' Update velocities
    speedy = speedy + gravity ' Apply gravity to vertical speed

    ' Simulate ball bouncing off the floor
    If py + pr >= floorHeight And hit <> 1 Then
        py = floorHeight - pr ' Position ball on the floor
        speedy = -speedy * 0.9 ' Reverse direction and apply damping
    End If

    ' Simulate ball moving with some friction
    If Abs(speedx) > 0.1 Then
        speedx = speedx * 0.99 ' Friction effect on horizontal velocity
    Else
        speedx = 0 ' Stop ball when velocity is too low
    End If
    If speedx < 5 Then speedx = 5
    If speedy < 5 Then speedy = 5
    If speedx > 30 Then speedx = 30
    If speedy > 30 Then speedy = 30

    'Speed
    px = px + speedx * Cos(pa)
    py = py + speedy * Sin(pa)

    fillCircle px, py, pr, pc

    _Display
    Cls
Loop Until InKey$ = Chr$(27)

End

'from Steve Gold standard
Sub fillCircle (CX As Integer, CY As Integer, R As Integer, C As _Unsigned Long)
    Dim Radius As Integer, RadiusError As Integer
    Dim X As Integer, Y As Integer
    Radius = Abs(R): RadiusError = -Radius: X = Radius: Y = 0
    If Radius = 0 Then PSet (CX, CY), C: Exit Sub
    Line (CX - X, CY)-(CX + X, CY), C, BF
    While X > Y
        RadiusError = RadiusError + Y * 2 + 1
        If RadiusError >= 0 Then
            If X <> Y + 1 Then
                Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
                Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
            End If
            X = X - 1
            RadiusError = RadiusError - X * 2
        End If
        Y = Y + 1
        Line (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
        Line (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
    Wend
End Sub

Sub collision (px, py, pa, speedx, speedy)
    speedx = speedx + 5
    speedy = -speedy
    px = px + speedx * Cos(pa)
    py = py + speedy * Sin(pa)
End Sub
Reply


Messages In This Thread
Pinball - by SierraKen - 01-26-2025, 12:21 AM
RE: Pinball - by bplus - 01-26-2025, 07:34 PM
RE: Pinball - by SierraKen - 01-27-2025, 06:28 AM
RE: Pinball - by bplus - 01-27-2025, 08:33 AM
RE: Pinball - by SierraKen - 01-27-2025, 07:46 PM
RE: Pinball - by bplus - 01-27-2025, 11:06 PM
RE: Pinball - by Pete - 01-27-2025, 11:12 PM



Users browsing this thread: 1 Guest(s)