Author Topic: QB64 Pong  (Read 4826 times)

0 Members and 1 Guest are viewing this topic.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
QB64 Pong
« on: July 31, 2021, 05:23:26 am »
I know... This game has been "done to death"... Now it's my turn... lol

BPlus,

This was originally coded with Naalaa then SDLBasic the RCBasic and now QB64.
This is a direct conversion and may not be optimized... No. Definitely NOT optimized... for QB64.
It's playable but could do with a "once over" to bring it up to scratch.

Notes:

Computer can be easily beaten... not sure how to make it harder.
Each time the ball is hit, it will gradually, increase speed. Could be faster...
(computer controlled player is a bit too slow...)
First to 9 points.
Player is mouse controlled.
"Borrowed" your AABB from your shooter...

J

 
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QB64 Pong
« Reply #1 on: July 31, 2021, 10:11:56 am »
Quote
"Borrowed" your AABB from your shooter...

?

Look forward to checking it out.  :)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QB64 Pong
« Reply #2 on: July 31, 2021, 11:39:11 am »
Looks and sounds fabulous!

Nicely commented code, this makes ball return a little unpredictable which is good thing for pong:
Code: QB64: [Select]
  1.             '   The ball hits the player's paddle
  2.             '
  3.             If RectCollide(plyX, plyY - 48, 20, 96, Int(ballX), Int(ballY), 16, 16) = 1 Then
  4.                 human = plyHit
  5.                 playerHit = 1
  6.                 _SndPlay (Hit)
  7.                 dy = (ballY - plyY) / 64
  8.                 dx = 0.5
  9.                 k = 1 / Sqr(dx * dx + dy * dy)
  10.                 ballDX = k * dx
  11.                 ballDY = k * dy
  12.                 ballSpeed = ballSpeed * 1.1
  13.                 If ballSpeed > maxSpeed Then ballSpeed = maxSpeed
  14.             End If
  15.  
  16.  

You are right it could be faster.
« Last Edit: July 31, 2021, 11:40:43 am by bplus »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: QB64 Pong
« Reply #3 on: July 31, 2021, 06:02:48 pm »
The object of the original Naalaa version was to start slowly and slightly speed up with each ball strike until it reaches a preset maximum speed. Setting the initial and maxSpeed or changing the ballSpeed multiplier from 1.1 to something higher is not a problem. It's the speed of the computer's paddle. It is designed so as to "lag" to give the player a chance to win. It doesn't take long before the player can "out wit" the computer. That kind of puts a "wet blanket" on the game's "flow". I will continue to experiment and if I find the "lag factor", and can successfully change it, I will let you know... In the meantime.... It's always a mean time for coffee...
Logic is the beginning of wisdom.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: QB64 Pong
« Reply #4 on: August 01, 2021, 06:01:08 am »
"Shooter?"

Torpedo Shooter (sdlbasic 2016-08-23) - Almost 5 years ago! lol
Logic is the beginning of wisdom.

Offline DANILIN

  • Forum Regular
  • Posts: 128
    • View Profile
    • Danilin youtube
Re: QB64 Pong
« Reply #5 on: August 01, 2021, 07:55:58 am »
matrix pingpong 15 years later HD

Russia looks world from future. big data is peace data.
https://youtube.com/playlist?list=PLBBTP9oVY7IagpH0g9FNUQ8JqmHwxDDDB
i never recommend anything to anyone and always write only about myself

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: QB64 Pong
« Reply #6 on: August 01, 2021, 08:02:22 am »
Very cool... But I would find it a bit difficult to code a game like that one... lol
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QB64 Pong
« Reply #7 on: August 01, 2021, 11:26:17 am »
Hey @johnno56

I dusted off my pong game, looking for assets ;-))

Code: QB64: [Select]
  1. _Title "Reflective Pong" 'try a modern structured approach 2019-06-20 by B+
  2. ' 2020-08-28 fix how ball starts
  3. ' 2021-07-31 fix AI play when ball is behind paddle, just give up the point too ugly otherwise
  4.  
  5. Const xmax = 800, ymax = 600
  6. Screen _NewImage(xmax, ymax, 32)
  7. _ScreenMove 300, 40
  8.  
  9. Type Rectangle
  10.     x As Single
  11.     y As Single
  12.     w As Single
  13.     h As Single
  14.     clr As _Unsigned Long
  15.     x As Single
  16.     y As Single
  17.     s As Single
  18.     a As Single
  19.     r As Integer
  20.     clr As _Unsigned Long
  21. Type Player
  22.     id As String
  23.     pdl As Rectangle
  24.     pts As Integer
  25. Dim Shared human As Player, computer As Player, table As Rectangle, ball As Circle, cx, cy, midC
  26. initGame
  27. playGame
  28.  
  29. Sub initGame 'most of theses remain constant                     start with table
  30.     Dim mult As Integer, l As String
  31.     l = Chr$(10)
  32.     table.x = 50: table.y = 150: table.w = 700: table.h = 400: table.clr = &HFF005046
  33.     computer.id = "Computer": computer.pts = 0 '            computer on the left
  34.     computer.pdl.w = 15: computer.pdl.clr = &HFF0000FF
  35.     computer.pdl.x = table.x + 3 * computer.pdl.w
  36.     human.id = "Human": human.pts = 0 '
  37.     tryAgain: '                                             human player on the right
  38.     Cls
  39.     Print "  Levels, Enter:" + l + "  1 for Super Pro" + l + "  2 for Pro" + l + "  3 Human" + l + "  4 Handicapped" + l + "  5 Bigger Handicap" + l + "  6 Help Me!" + l
  40.     Input ""; mult
  41.     If mult < 1 Or mult > 6 Then Beep: GoTo tryAgain
  42.     Select Case mult
  43.         Case 1: human.id = "Super Pro"
  44.         Case 2: human.id = "Pro"
  45.         Case 3: human.id = "Human"
  46.         Case 4: human.id = "Handicapped"
  47.         Case 5: human.id = "Bigger Handicap"
  48.         Case 6: human.id = "Help Me!": mult = 12
  49.     End Select
  50.     human.pdl.w = mult * 5: human.pdl.h = 40: human.pdl.clr = &HFFFF0000
  51.     ball.s = 5: ball.r = 5: ball.clr = &HFFFFFFFF '                     ball
  52.     midC = table.x + .25 * table.w
  53.     Print: Print: Print "    Move your mouse to position the red circle (paddle)..."
  54.     Sleep 2
  55.  
  56. Sub playGame
  57.     Dim mx, my, inbounds
  58.     Do
  59.         getBallRolling
  60.         inbounds = 1
  61.         While inbounds
  62.             While _MouseInput: Wend
  63.             my = _MouseY: mx = _MouseX
  64.             If my > table.y And my <= table.y + table.h Then '    update human paddle
  65.                 If mx > table.x + .5 * table.w And mx < table.x + table.w Then
  66.                     human.pdl.x = mx: human.pdl.y = my
  67.                 End If
  68.             End If
  69.  
  70.             cx = cx + _Pi(1 / 80): cy = cy + _Pi(1 / 40)
  71.             If ball.x > computer.pdl.x Then
  72.                 computer.pdl.x = midC + (.5 * midC - 10) * Sin(cx)
  73.                 computer.pdl.y = ball.y + 20 * Sin(cy)
  74.             End If
  75.  
  76.             If computer.pdl.y - computer.pdl.h < table.y Then computer.pdl.y = table.y + computer.pdl.h
  77.             If computer.pdl.y + computer.pdl.h > table.y + table.h Then computer.pdl.y = table.y + table.h - computer.pdl.h
  78.  
  79.             ball.x = ball.x + ball.s * Cos(ball.a): ball.y = ball.y + ball.s * Sin(ball.a) '  update ball
  80.             'past the table edge? or paddle edge
  81.             If dist(human.pdl.x, human.pdl.y, ball.x, ball.y) < ball.r + human.pdl.w Then 'ball finds human paddle
  82.                 ball.a = _Atan2(ball.y - human.pdl.y, ball.x - human.pdl.x)
  83.                 ball.x = ball.x + 2 * ball.s * Cos(ball.a)
  84.                 ball.y = ball.y + 2 * ball.s * Sin(ball.a)
  85.  
  86.             ElseIf dist(computer.pdl.x, computer.pdl.y, ball.x, ball.y) < ball.r + computer.pdl.w Then 'ball finds AI paddle
  87.                 ball.a = _Atan2(ball.y - computer.pdl.y, ball.x - computer.pdl.x)
  88.                 ball.x = ball.x + 2 * ball.s * Cos(ball.a)
  89.                 ball.y = ball.y + 2 * ball.s * Sin(ball.a)
  90.  
  91.             ElseIf ball.x <= computer.pdl.x - 2 * computer.pdl.w Then '            ball paste left edge
  92.                 inbounds = 0: human.pts = human.pts + 1
  93.  
  94.             ElseIf ball.x + ball.r >= table.x + table.w Then '                ball paste right edge
  95.                 inbounds = 0: computer.pts = computer.pts + 1
  96.  
  97.             ElseIf ball.y - ball.r <= table.y Then '                                          top edge
  98.                 ball.a = -ball.a
  99.                 ball.y = table.y + ball.r
  100.  
  101.             ElseIf ball.y + ball.r >= table.y + table.h Then '                            bottom edge
  102.                 ball.a = -ball.a
  103.                 ball.y = table.y + table.h - ball.r
  104.             End If
  105.             drawGame
  106.             _Display
  107.             _Limit 90
  108.         Wend
  109.         _Delay .5
  110.     Loop Until human.pts = 21 Or computer.pts = 21
  111.  
  112. Sub getBallRolling
  113.     Dim d, ra, s$, tstart, my, mx
  114.  
  115.     computer.pdl.y = table.y + (table.h - computer.pdl.h) / 2 '       center paddles y
  116.     If Rnd < .5 Then d = _Pi / 2 Else d = -_Pi / 2 '                 ball movement absolutely  more dx than dy
  117.     ra = Rnd * _Pi(.15)
  118.     If Rnd < .5 Then ball.a = d - ra Else ball.a = d + ra
  119.     ball.x = table.x + .5 * table.w
  120.     If ball.a > _Pi(.5) And ball.a < _Pi(1.5) Then
  121.         computer.pdl.x = table.x + .1 * table.w
  122.     Else
  123.         computer.pdl.x = table.x + computer.pdl.w
  124.     End If
  125.     ball.y = table.y + table.h / 2
  126.     tstart = Timer(.001)
  127.     While Timer(.001) - tstart < 2 ' <<<<<<<<<<<<<<<< This allows player to get es paddle alligned with mouse
  128.         While _MouseInput: Wend
  129.         mx = _MouseX: my = _MouseY
  130.         If my > table.y And my <= table.y + table.h Then '    update human paddle
  131.             If mx > table.x + .5 * table.w And mx < table.x + table.w Then
  132.                 human.pdl.x = mx: human.pdl.y = my - human.pdl.h / 2
  133.             End If
  134.         End If
  135.         drawGame
  136.         s$ = "Get ready for random serve..."
  137.         Color , table.clr
  138.         _PrintString (table.x + (table.w - 8 * Len(s$)) / 2, table.y + table.h / 2 - 8), s$
  139.         Color , &HFF000000
  140.         _Display
  141.         _Limit 60
  142.     Wend
  143.  
  144. Sub drawGame
  145.     Dim s$
  146.     Cls
  147.     _PrintString (table.x + 120, table.y / 2 - 4), computer.id + ":" + Str$(computer.pts)
  148.     s$ = human.id + ":" + Str$(human.pts)
  149.     _PrintString (table.x + table.w - 8 * Len(s$) - 120, table.y / 2 - 4), s$
  150.     Line (table.x, table.y)-Step(table.w, table.h), table.clr, BF 'table
  151.     Line (table.x, table.y)-Step(table.w, table.h), &HFFFFFFFF, B 'table
  152.     Line (table.x + table.w / 2, table.y)-Step(0, table.h), &HFFFFFFFF
  153.     fcirc computer.pdl.x, computer.pdl.y, computer.pdl.w, computer.pdl.clr
  154.     fcirc human.pdl.x, human.pdl.y, human.pdl.w, human.pdl.clr
  155.     fcirc ball.x, ball.y, ball.r, ball.clr
  156.  
  157. Function dist (x1, y1, x2, y2)
  158.     dist = ((x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ .5
  159.  
  160. Sub fcirc (CX As Integer, CY As Integer, R As Integer, C As _Unsigned Long)
  161.     Dim Radius As Integer, RadiusError As Integer
  162.     Dim X As Integer, Y As Integer
  163.     Radius = Abs(R): RadiusError = -Radius: X = Radius: Y = 0
  164.     If Radius = 0 Then PSet (CX, CY), C: Exit Sub
  165.     Line (CX - X, CY)-(CX + X, CY), C, BF
  166.     While X > Y
  167.         RadiusError = RadiusError + Y * 2 + 1
  168.         If RadiusError >= 0 Then
  169.             If X <> Y + 1 Then
  170.                 Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  171.                 Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  172.             End If
  173.             X = X - 1
  174.             RadiusError = RadiusError - X * 2
  175.         End If
  176.         Y = Y + 1
  177.         Line (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  178.         Line (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  179.     Wend
  180.  
  181.  
« Last Edit: August 01, 2021, 11:29:51 am by bplus »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: QB64 Pong
« Reply #8 on: August 01, 2021, 05:04:34 pm »
I dunno... If you swap the lawn or clay surface to something much colder and smoother.... This game reminds me of another... Can't quite put my finger on it... lol

The fill circle function is quite fast. Two paddles and a fast moving ball... Have you tested the number of circles before lagging occurs? Homework...

I like the "Help Me" option... Unlike most 'tennis' games, I could actually hit the ball more than once... nice feature.

In regards to graphics, would I be correct in assuming that, curved paddles would be preferred so as to maintain an air of unpredictability?
Originally, you had the player setup as a rectangle... But, calculating the distance between two circles, would be quicker than including collision detection... Cool...
If ever you decided to use collision detection, you could use AABB, because the ball is quite small and can be treated as a square... On the subject of the ball... The colour could be white, yellow or even lime green. Steer clear or black. Black would make it look like a puck. Puck? Interesting... Just a thought...

Speaking of lawn and clay... perhaps either allowing the player to choose or randomly select a surface could effect the player's "friction". eg: if the surface is clay, then the surface friction is less - similar to ice but not so much - ice? Interesting.... and the lawn would have greater friction so as to make player movement more responsive... Just another though...

Here's another weird thought... I know... three in one day... Who knew?  lol  If the player is using the 'easier' paddles then perhaps the player's paddle can reduce in size with each ball strike... Striker? Interesting... Just yet another thought...  I'm going to stop there. Fear of wearing out the old grey matter is reason enough... But, the best reason is, coffee...

Have a great day!

J
Logic is the beginning of wisdom.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: QB64 Pong
« Reply #9 on: August 02, 2021, 04:53:24 pm »
Bplus.

In regards to graphics... I'm toying with the idea of a horror/comedy look... Just a concept... Player is a zombie the computer is an eyeball and the ball is a skull... With the right backing track, sound effects, images and fonts (all free of course) it could make for an amusing project... Not 100% certain of success but might be good for a giggle... Also testing a simple menu operated by arrows and space keys... another crazy thought... Have critters walk across the players side of the court. If the player can hit them with the "ball" then a game point is removed from the computer...  anyway... best get the game working before adding more stuff... This could be fun...
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QB64 Pong
« Reply #10 on: August 02, 2021, 05:31:01 pm »
Oh yeah, a creepy crawler Break-out game!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: QB64 Pong
« Reply #11 on: August 02, 2021, 06:19:16 pm »
Creepy break-out? Hmm... Interesting. I like the sound of that... Moo Ha Ha....
Logic is the beginning of wisdom.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: QB64 Pong
« Reply #12 on: August 02, 2021, 06:34:33 pm »
Quote
matrix pingpong 15 years later HD
LOL

@jonnho56
Cool
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: QB64 Pong
« Reply #13 on: August 02, 2021, 07:03:54 pm »
I forgot the screenshot
 
QB64PongGame.JPG
Programming isn't difficult, only it's  consuming time and coffee

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: QB64 Pong
« Reply #14 on: August 02, 2021, 07:50:33 pm »
I can take no credit. It was merely a modification of the original Naalaa listing by Marcus Johannsen.
Logic is the beginning of wisdom.