Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pinball
#1
I worked over 8 hours on this game so far, but I can't figure out how to make the oval flippers collide with the ball efficiently. So far, they work around 50% of the time. Much of this code is from when B+ helped me with hocky puck colliders awhile back and another big part of this code is math from ChatGPT, such as making the ovals turn on the axis at the end of the oval, for the flippers. 

You can still play this game if you keep pressing the 2 CTRL keys to move the flippers many times to hit the ball. But it would be nice if I could fix this, thanks. You can probably tell a lot of it has been made experimenting with numbers, etc. Smile I'm just zomped out. lol 

Code: (Select All)

'Pinball by SierraKen
'1-25-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."


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 = 25 ' 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 = 25 ' Semi-minor axis
theta2 = 45 * _Pi / -180 ' Rotate 45 degrees in radians
thickness2 = 10 ' Thickness of the ellipse

begin:

slotx = 280
sloty = 690
dir = 1

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 = 7 'Radius of ball
pc = _RGB32(0, 255, 0) 'Color of ball
'
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


start:
px = (Rnd * 550) + 60: py = 60
_Delay 1
Cls

pa = _Pi(1) * Rnd


Play "MB"

Do

    _Limit 800
    Line (10, 10)-(790, 690), _RGB32(255, 255, 255), B

    For cir = 1 To num
        _Limit 800

        '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:
        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

        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
        For chk = -10 To 10 Step 1
            If Sqr((X + chk - px) ^ 2 + (Y + chk - py) ^ 2) < (pr + 35) Then
                pa = _Atan2(py - Y, px - X)
                collision px, py, pa, speedx, speedy
                px = px + speedx * Cos(pa)
                py = py + speedy * Sin(pa)
            End If
            If Sqr((X2 + chk - px) ^ 2 + (Y2 + chk - py) ^ 2) < (pr + 35) Then
                pa = _Atan2(py - Y2, px - Y2)
                collision px, py, pa, speedx, speedy
                px = px + speedx * Cos(pa)
                py = py + speedy * Sin(pa)
            End If
        Next chk

        '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)
            oldlevel = level
            level = Int(score \ 50000 + 1)
            If oldlevel <> level And level <> 1 Then GoTo begin:
        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
    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 = 10 + pr
    If py > 790 - pr And floorHeight = 680 Then pa = -pa * Rnd + 1: py = 790 - pr
    If py < 10 + pr Then pa = -pa * Rnd + 1: py = 10 + pr

    If px > slotx + 30 And px < slotx + 170 And py > sloty - 70 Then
        floorHeight = floorHeight + 8
        If px > slotx + 60 And px < slotx + 200 And floorHeight > 760 Then
            ball = ball - 1
            _Title "Score: " + Str$(score) + "    Ball: " + Str$(ball) + "    Level: " + Str$(level)
            px = (Rnd * 550) + 60: py = 60
            down = 0
            floorHeight = 680
            _Delay 1
            If ball = 0 Then End
        End If
    Else
        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

    Line (slotx, sloty)-(slotx + 300, sloty), _RGB32(0, 0, 0)
    _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)