Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pin-Pongy
#1
Using code from my Pongy game, I removed the mouse use and added pinball flippers. It still has the evil red ball that destroys your ball and to get points, you make it go into the moving goalie up above, like Pongy. I widened the goalie to make it easier and shrank the red ball in half. I do need some help on getting the flippers to detect the ball every time. They detect it maybe 90% of the time but they also bounce the ball down instead of up about half the time. For now, it's a goofy little game. Use both Ctrl keys to used the flippers. 

Code: (Select All)

'Pin-Pongy - by SierraKen
'November 28, 2024

'This is a little goofy game I made using my Pongy code and adding flippers instead of a mouse ball.
'Sometimes the flippers work, sometimes they don't. That's what makes it's goofy. lol

'Thanks to the QB64 Phoenix Forum for the inspiration and past help.
'Thanks also to Chat GPT for the math code.



Do
    Dim score As Single
    Dim ball As Single
    Dim boxRight As Single
    Dim boxLeft As Single
    Dim BoxTop As Single
    Dim BoxBottom As Single
    Dim a As String
    Dim ag As String
    score = 0
    ball = 15

    ' Set box boundaries
    boxLeft = 200
    boxRight = 600
    BoxTop = 25
    BoxBottom = 575
    seconds = 26
    seconds2 = 35

    Cls
    Screen _NewImage(800, 600, 32)
    ' Ball properties
    Dim As Integer ballX, ballY, ballx2, bally2
    Dim As Single angle, angle2
    Dim As Integer speedX, speedY, speedx2, speedy2
    ballX = (boxRight + boxLeft) / 2 ' Start in the center
    ballY = (BoxTop + BoxBottom) / 2
    angle = 45 ' Starting angle in degrees

    ballx2 = (boxRight + boxLeft) / 2 ' Start in the center
    bally2 = (BoxTop + BoxBottom) / 2 + 100
    angle2 = 45 ' Starting angle in degrees


    ' Convert angle to radians
    Dim As Single radAngle
    radAngle = angle * 3.14159265 / 180

    Dim As Single radAngle2
    radAngle2 = angle2 * 3.14159265 / 180

    Dim goalx As Single
    Dim goaly As Single
    Dim goaldir As Single
    Dim redballout As Single
    Dim starx As Single
    Dim stary As Single


    ' Set speed based on angle
    speedX = Cos(radAngle) * 5
    speedY = Sin(radAngle) * 5

    speedx2 = Cos(radAngle2) * 5
    speedy2 = Sin(radAngle2) * 5

    goalx = 200: goaly = 25
    goaldir = 1
    redballout = 5


    _Title "Pin-Pongy - by SierraKen - Use Both Ctrl Keys"

    Randomize Timer

    Do
        Cls
        a = InKey$
        If a = Chr$(27) Then End
        If _KeyDown(100306) Then
            flip1 = 1: ' left side ctrl
        Else
            flip1 = 0
            flip1stop = 0
        End If
        If _KeyDown(100305) Then
            flip2 = 1: ' right side ctrl
        Else
            flip2 = 0
            flip2stop = 0
        End If
        ' Draw box boundaries
        Line (boxLeft, BoxTop)-(boxRight, BoxBottom), _RGB32(255, 255, 255), B

        ' Draw Flippers
        If flip1 = 0 Or flip1stop = 1 Then
            Line (boxLeft, BoxBottom - 180)-(boxLeft + 180, BoxBottom - 30), _RGB32(253, 253, 253)
            seconds = 26
        End If

        If flip2 = 0 Or flip2stop = 1 Then
            Line (boxRight, BoxBottom - 180)-(boxRight - 180, BoxBottom - 30), _RGB32(253, 253, 253)
            seconds2 = 35
        End If

        'Left Flipper
        If flip1 = 1 And flip1stop = 0 Then
            If seconds > 25 Then flip1up = 1
            If seconds < 5 Then flip1up = 2
            If flip1up = 1 Then seconds = seconds - .75
            If flip1up = 2 And _KeyDown(100306) = 0 Then seconds = seconds + .75
            If flip1up = 2 And seconds > 25 Then
                flip1 = 0: flip1up = 0: flip1stop = 1:
                Line (boxLeft, BoxBottom - 180)-(boxLeft + 180, BoxBottom - 30), _RGB32(253, 253, 253)
                GoTo skip1:
            End If

            s = (60 - seconds) * 6 + 180
            x = Int(Sin(s / 180 * 3.141592) * 200) + boxLeft
            y = Int(Cos(s / 180 * 3.141592) * 200) + BoxBottom - 180
            skip1:
            For f3 = .1 To 5 Step .1
                Line (boxLeft + f3, BoxBottom - 180 + f3)-(x + f3, y + f3), _RGB32(253, 253, 253)
            Next f3
        End If

        'Right Flipper
        If flip2 = 1 And flip2stop = 0 Then
            If seconds2 < 36 Then flip2up = 1
            If seconds2 > 55 Then flip2up = 2
            If flip2up = 1 Then seconds2 = seconds2 + .75
            If flip2up = 2 And _KeyDown(100305) = 0 Then seconds2 = seconds2 - .75
            If flip2up = 2 And seconds2 < 36 Then
                flip2 = 0: flip2up = 0: flip2stop = 1:
                Line (boxRight, BoxBottom - 180)-(boxRight - 180, BoxBottom - 30), _RGB32(253, 253, 253)
                GoTo skip2:
            End If
            s2 = (60 - seconds2) * 6 + 180
            x2 = Int(Sin(s2 / 180 * 3.141592) * 200) + boxRight
            y2 = Int(Cos(s2 / 180 * 3.141592) * 200) + BoxBottom - 180
            skip2:
            For f4 = .1 To 5 Step .1
                Line (boxRight + f4, BoxBottom - 180 + f4)-(x2 + f4, y2 + f4), _RGB32(253, 253, 253)
            Next f4
        End If

        For check = -4 To 4 Step .5
            If Point(Int(ballX + check), Int(ballY + check)) = _RGB32(254, 254, 254) Then GoTo collision:
            If Point(Int(ballX + check), Int(ballY + check)) = _RGB32(253, 253, 253) Then GoTo collision:
            If Point(Int(ballX + check), Int(ballY + check)) = _RGB32(252, 252, 252) Then GoTo collision:
            If Point(Int(ballX + check), Int(ballY + check)) = _RGB32(251, 251, 251) Then GoTo collision:
            If Point(Int(ballX + check), Int(ballY + check)) = _RGB32(250, 250, 250) Then GoTo collision:
            If Point(Int(ballX + check), Int(ballY + check)) = _RGB32(253, 252, 252) Then GoTo collision:
            If Point(Int(ballX + check), Int(ballY + check)) = _RGB32(253, 251, 251) Then GoTo collision:
            If Point(Int(ballX + check), Int(ballY + check)) = _RGB32(252, 251, 251) Then GoTo collision:
            If Point(Int(ballX + check), Int(ballY + check)) = _RGB32(252, 253, 253) Then GoTo collision:
            If Point(Int(ballX + check), Int(ballY + check)) = _RGB32(251, 252, 252) Then GoTo collision:
            If Point(Int(ballX + check), Int(ballY + check)) = _RGB32(251, 251, 252) Then GoTo collision:
            If Point(Int(ballX + check), Int(ballY + check)) = _RGB32(252, 252, 253) Then GoTo collision:
            If Point(Int(ballX + check), Int(ballY + check)) = _RGB32(253, 252, 251) Then GoTo collision:
        Next check
        flipper:

        Line (goalx, goaly)-(goalx + 200, goaly), _RGB32(1, 1, 1)
        goalx = goalx + goaldir
        If goalx = 680 And goaldir = 1 Then goaldir = -1
        If goalx = 20 And goaldir = -1 Then goaldir = 1
        ' Draw the ball
        fillCircle ballX, ballY, 10, _RGB32(255, 255, 255)
        If redballout = 0 Then fillCircle ballx2, bally2, 10, _RGB32(255, 0, 0)

        ' Update ball position
        ballX = ballX + speedX
        ballY = ballY + speedY
        If redballout = 0 Then
            ballx2 = ballx2 + speedx2
            bally2 = bally2 + speedy2
        End If
        If ballX > goalx And ballX < goalx + 200 And ballY < 26 Then
            score = score + 1: ballX = 375: ballY = 275: speedY = -speedY
            For snd = 300 To 900 Step 50
                Sound snd, .5
            Next snd
        End If

        Locate 1, 20: Print "Score: "; score
        Locate 1, 70: Print "Balls: "; ball

        ' Check for collision with box boundaries
        If ballX <= boxLeft Or ballX >= boxRight Then
            speedX = -speedX ' Reflect on the X axis
            If redballout > 0 Then redballout = redballout - 1
            Sound 600, .5
        End If
        If ballY <= BoxTop Or ballY >= BoxBottom Then
            speedY = -speedY ' Reflect on the Y axis
            If redballout > 0 Then redballout = redballout - 1
            Sound 600, .5
        End If
        If ballY > BoxBottom + .4 Then ballY = BoxBottom - 7
        If ballY < BoxTop - .4 Then ballY = BoxTop + 7
        If ballX > boxRight + .4 Then ballX = boxRight - 7
        If ballX < boxLeft - .4 Then ballX = boxLeft + 7
        'If redballout > 0 Then GoTo skip:
        If (ballx2 <= boxLeft Or ballx2 >= boxRight) And redballout = 0 Then
            speedx2 = -speedx2 ' Reflect on the X axis
            Sound 600, .5
        End If
        If (bally2 <= BoxTop Or bally2 >= BoxBottom) And redballout = 0 Then
            speedy2 = -speedy2 ' Reflect on the Y axis
            Sound 600, .5
        End If
        If bally2 > BoxBottom + .4 And redballout = 0 Then bally2 = BoxBottom - 7
        If bally2 < BoxTop - .4 And redballout = 0 Then bally2 = BoxTop + 7
        If ballx2 > boxRight + .4 And redballout = 0 Then ballx2 = boxRight - 7
        If ballx2 < boxLeft - .4 And redballout = 0 Then ballx2 = boxLeft + 7
        'skip:

        ' Check for collision between red ball and white ball.
        'If redballout > 0 Then GoTo skip2:
        If Sqr((ballx2 - ballX) ^ 2 + (bally2 - ballY) ^ 2) < 25 And redballout = 0 Then
            fillCircle ballX, ballY, 10, _RGB32(0, 0, 0)
            snd = 300
            _AutoDisplay
            starx = ballX: stary = ballY
            For t = 1 To 25
                fillCircle starx, stary, t * 5, _RGB32(255, 255, 255)
                Sound snd - t, .5
            Next t
            redballout = 5
            Locate 1, 70: ball = ball - 1: Print "Balls: "; ball
            If ball = 0 Then
                _AutoDisplay
                Locate 20, 50: Print "G A M E  O V E R":
                Locate 25, 50
                Print "Again (Y/N)?"
                Do
                    ag = InKey$
                    If ag = "y" Or ag = "Y" Then Exit Do
                    If ag = "n" Or ag = "N" Then End
                Loop
                Exit Do
            End If
            Sound 600, .5
        End If
        'skip2:

        _Display
        _Limit 60 ' Limit the speed of the loop to 60 FPS
    Loop
Loop

collision:
' Calculate deflection angle
radAngle = _Atan2(ballY, ballX) * 180 / 3.14159265
'radAngle = angle * 3.14159265 / 180
speedX = Cos(radAngle) * 5
speedY = Sin(radAngle) * 5

ballX = ballX + speedX
ballY = ballY + speedY
Sound 100, 1
skip3:
GoTo flipper:

'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

Reply
#2
Yeah it aint easy getting circle intersect line.
b = b + ...
Reply




Users browsing this thread: 3 Guest(s)