Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - TerryRitchie

Pages: [1] 2 3 ... 33
1
A while back I mentioned I have a laptop, an HP Compaq nx7400, that will not allow QB64 to display the QB64 editor. I found the issue and am posting this in case others have the same problem.

Laptop Specs:

Intel GMA 950 Graphics
Windows 7 64bit Pro

I normally set Windows 7 to "Windows Classic" view. This appears to be the issue. Two items required for the editor to appear are disabled when setting to classic. Go to Control Panel->System->Advanced system settings->Performance Settings and make sure that "[ ] Use visual styles on windows and buttons" and "[ ] Enable desktop composition" are both selected.

I'm assuming this has something to do with the Intel GMA 950 video chipset. "Enable desktop composition" is not even listed in any of my other Windows 7 machines.

2
Programs / Re: Raycasting
« on: July 13, 2020, 04:14:48 pm »
Yeah, I always viewed ray tracing as coding voodoo, probably because Wolfenstein 3D came out right around the time I was really getting into coding. I figured the math alone behind this was something to tackle. Turns out it's a very simple effect to achieve.

The narrator in the video does a good job of explaining each part of the code. I kept the variable names, functions, and subroutines named the same as well. I had to make a few changes here and there because of the funky way C handles IF statements but for the most part it's a one to one conversion.

3
Programs / Raycasting
« on: July 13, 2020, 01:56:42 pm »
Sorry I've been scarce this past month. The wife was getting upset that the honey do list was not getting done. Had to limit my time on computer.

Any way, I've been trying to learn ray casting for another topic in my tutorial. I found a pretty good video on YouTube explaining the process. The narrator uses OpenGL and C. I converted it to QB64 as I watched the video. If you watch the video and look at the code below you'll be amazed at just how easy it is to do ray casting.

The code is sloppy. I threw it together as the narrator wrote his code. But it works. :-)



Code: QB64: [Select]
  1. '  [youtube]https://www.youtube.com/watch?v=gYRrGTC7GtA[/youtube]
  2.  
  3.  
  4.  
  5. CONST DARKGRAY = _RGB32(76, 76, 76)
  6. CONST WHITE = _RGB32(255, 255, 255)
  7. CONST BLACK = _RGB32(0, 0, 0)
  8. CONST YELLOW = _RGB32(255, 255, 0)
  9. CONST GREEN = _RGB32(0, 255, 0)
  10. CONST RED = _RGB32(255, 0, 0)
  11. CONST DARKRED = _RGB32(128, 0, 0)
  12. CONST PI = 3.1415926
  13. CONST P2 = PI / 2
  14. CONST P3 = 3 * PI / 2
  15. CONST DR = .0174533
  16.  
  17. DIM SHARED px AS SINGLE '  player position
  18. DIM SHARED pdx AS SINGLE ' delta X of player
  19. DIM SHARED pdy AS SINGLE ' delta Y of player
  20. DIM SHARED pa AS SINGLE '  angle of player
  21.  
  22. mapX = 8
  23. mapY = 8
  24. mapS = 64
  25. FOR i = 0 TO 63: READ map(i): NEXT i
  26.  
  27.  
  28. Init
  29.     _LIMIT 60
  30.     CLS , DARKGRAY
  31.     drawMap2D
  32.     drawRays2D
  33.     DrawPlayer
  34.     Buttons
  35.  
  36.  
  37.     _DISPLAY
  38.  
  39. DATA 1,1,1,1,1,1,1,1
  40. DATA 1,0,1,0,0,0,0,1
  41. DATA 1,0,1,0,0,0,0,1
  42. DATA 1,0,1,0,0,0,0,1
  43. DATA 1,0,0,0,0,0,0,1
  44. DATA 1,0,0,0,0,1,0,1
  45. DATA 1,0,0,0,0,0,0,1
  46. DATA 1,1,1,1,1,1,1,1
  47.  
  48.  
  49. FUNCTION dist (ax AS SINGLE, ay AS SINGLE, bx AS SINGLE, by AS SINGLE, ang AS SINGLE)
  50.  
  51.     dist = SQR((bx - ax) * (bx - ax) + (by - ay) * (by - ay))
  52.  
  53.  
  54.  
  55. SUB drawRays2D ()
  56.  
  57.     DIM r AS INTEGER
  58.     DIM mx AS INTEGER
  59.     DIM my AS INTEGER
  60.     DIM mp AS INTEGER
  61.     DIM dof AS INTEGER
  62.     DIM rx AS SINGLE
  63.     DIM ry AS SINGLE
  64.     DIM ra AS SINGLE
  65.     DIM xo AS SINGLE
  66.     DIM yo AS SINGLE
  67.     DIM aTan AS SINGLE
  68.     DIM nTan AS SINGLE
  69.     DIM disH AS SINGLE
  70.     DIM hx AS SINGLE
  71.     DIM hy AS SINGLE
  72.     DIM disV AS SINGLE
  73.     DIM vx AS SINGLE
  74.     DIM vy AS SINGLE
  75.     DIM disTT AS SINGLE
  76.     DIM lineH AS SINGLE
  77.     DIM lineO AS SINGLE
  78.     DIM ca AS SINGLE
  79.     DIM clr AS _UNSIGNED LONG
  80.  
  81.     ra = pa - DR * 30
  82.     IF ra < 0 THEN ra = ra + 2 * PI
  83.     IF ra > 2 * PI THEN ra = ra - 2 * PI
  84.  
  85.     FOR r = 0 TO 59
  86.         '--- Check Horizontal Lines ---
  87.         dof = 0
  88.         disH = 1000000
  89.         hx = px
  90.         hy = py
  91.         aTan = -1 / TAN(ra)
  92.         IF ra > PI THEN
  93.             'ry = ((INT(py) / 64) * 64) - .0001
  94.  
  95.             ry = INT(py / 64) * 64 - .0001
  96.  
  97.  
  98.             rx = (py - ry) * aTan + px
  99.             yo = -64
  100.             xo = -yo * aTan
  101.         END IF
  102.         IF ra < PI THEN
  103.             'ry = ((INT(py) / 64) * 64) + 64
  104.  
  105.             ry = INT(py / 64) * 64 + 64
  106.  
  107.  
  108.             rx = (py - ry) * aTan + px
  109.             yo = 64
  110.             xo = -yo * aTan
  111.         END IF
  112.         IF (ra = 0) OR (ra = PI) THEN
  113.             rx = px
  114.             ry = py
  115.             dof = 8
  116.         END IF
  117.         WHILE dof < 8
  118.             'mx = INT(rx) / 64
  119.             'my = INT(ry) / 64
  120.  
  121.             mx = INT(rx / 64)
  122.             my = INT(ry / 64)
  123.  
  124.             mp = my * mapX + mx
  125.  
  126.             'LOCATE 1, 1: PRINT mp; pa;
  127.             '_DISPLAY
  128.  
  129.             IF mp < 0 THEN mp = 0
  130.             IF mp > 63 THEN mp = 63
  131.  
  132.             'IF (mp < mapX * mapY) AND map(mp) = 1 THEN ' -- hit wall
  133.             IF map(mp) = 1 THEN
  134.                 hx = rx
  135.                 hy = ry
  136.                 disH = dist(px, py, hx, hy, ra)
  137.                 dof = 8
  138.             ELSE '                                       -- next line
  139.                 rx = rx + xo
  140.                 ry = ry + yo
  141.                 dof = dof + 1
  142.             END IF
  143.         WEND
  144.  
  145.  
  146.         '--- Check Vertical Lines ---
  147.         dof = 0
  148.         disV = 1000000
  149.         vx = px
  150.         vy = py
  151.  
  152.         nTan = -TAN(ra)
  153.         IF ra > P2 AND ra < P3 THEN
  154.             'ry = ((INT(py) / 64) * 64) - .0001
  155.  
  156.             rx = INT(px / 64) * 64 - .0001
  157.  
  158.  
  159.             ry = (px - rx) * nTan + py
  160.             xo = -64
  161.             yo = -xo * nTan
  162.         END IF
  163.         IF ra < P2 OR ra > P3 THEN
  164.             'ry = ((INT(py) / 64) * 64) + 64
  165.  
  166.             rx = INT(px / 64) * 64 + 64
  167.  
  168.  
  169.             ry = (px - rx) * nTan + py
  170.             xo = 64
  171.             yo = -xo * nTan
  172.         END IF
  173.         IF (ra = 0) OR (ra = PI) THEN
  174.             rx = px
  175.             ry = py
  176.             dof = 8
  177.         END IF
  178.         WHILE dof < 8
  179.             'mx = INT(rx) / 64
  180.             'my = INT(ry) / 64
  181.  
  182.             mx = INT(rx / 64)
  183.             my = INT(ry / 64)
  184.  
  185.             mp = my * mapX + mx
  186.  
  187.             'LOCATE 1, 1: PRINT mp; pa;
  188.             '_DISPLAY
  189.  
  190.             IF mp < 0 THEN mp = 0
  191.             IF mp > 63 THEN mp = 63
  192.  
  193.             'IF (mp < mapX * mapY) AND map(mp) = 1 THEN ' -- hit wall
  194.             IF map(mp) = 1 THEN
  195.                 vx = rx
  196.                 vy = ry
  197.                 disV = dist(px, py, vx, vy, ra)
  198.                 dof = 8
  199.             ELSE '                                       -- next line
  200.                 rx = rx + xo
  201.                 ry = ry + yo
  202.                 dof = dof + 1
  203.             END IF
  204.         WEND
  205.  
  206.         IF disV < disH THEN ' -- Vertical wall hit
  207.             rx = vx
  208.             ry = vy
  209.             disTT = disV
  210.             clr = RED
  211.         END IF
  212.         IF disH < disV THEN ' -- Horizontal wall hit
  213.             rx = hx
  214.             ry = hy
  215.             disTT = disH
  216.             clr = DARKRED
  217.         END IF
  218.  
  219.         LINE (px, py)-(rx, ry), RED
  220.  
  221.         '--- Draw 3D Walls ---
  222.  
  223.         ca = pa - ra
  224.         IF ca < 0 THEN ca = ca + 2 * PI
  225.         IF ca > 2 * PI THEN ca = ca - 2 * PI
  226.         disTT = disTT * COS(ca) '          -- fix fish eye
  227.  
  228.         lineH = mapS * 320 / disTT
  229.         IF lineH > 320 THEN lineH = 320 ' -- Line height
  230.         lineO = 160 - lineH / 2 '         -- Line offset
  231.         LINE (r * 8 + 530 - 4, lineO)-(r * 8 + 530 + 4, lineH + lineO), clr, BF
  232.  
  233.  
  234.  
  235.         ra = ra + DR
  236.         IF ra < 0 THEN ra = ra + 2 * PI
  237.         IF ra > 2 * PI THEN ra = ra - 2 * PI
  238.  
  239.  
  240.  
  241.     NEXT r
  242.  
  243.  
  244.  
  245.  
  246.  
  247. SUB drawMap2D ()
  248.  
  249.     DIM x AS INTEGER
  250.     DIM y AS INTEGER
  251.     DIM xo AS INTEGER
  252.     DIM yo AS INTEGER
  253.     DIM clr AS _UNSIGNED LONG
  254.  
  255.     FOR y = 0 TO mapY - 1
  256.         FOR x = 0 TO mapX - 1
  257.             IF map(y * mapX + x) = 1 THEN clr = WHITE ELSE clr = BLACK
  258.             xo = x * mapS
  259.             yo = y * mapS
  260.             LINE (xo + 1, yo + 1)-(xo + mapS - 1, yo + mapS - 1), clr, BF
  261.         NEXT x
  262.     NEXT y
  263.  
  264.  
  265.  
  266. SUB Buttons ()
  267.  
  268.     IF _KEYDOWN(97) THEN
  269.         pa = pa - .1
  270.         IF pa < 0 THEN pa = pa + 2 * PI
  271.     END IF
  272.     IF _KEYDOWN(100) THEN
  273.         pa = pa + .1
  274.         IF pa > 2 * PI THEN pa = pa - 2 * PI
  275.     END IF
  276.     pdx = COS(pa) * 5
  277.     pdy = SIN(pa) * 5
  278.     IF _KEYDOWN(119) THEN px = px + pdx: py = py + pdy
  279.     IF _KEYDOWN(115) THEN px = px - pdx: py = py - pdy
  280.  
  281.  
  282.  
  283. SUB DrawPlayer ()
  284.  
  285.     LINE (px - 4, py - 4)-(px + 4, py + 4), YELLOW, BF
  286.  
  287.     LINE (px, py)-(px + pdx * 5, py + pdy * 5), YELLOW
  288.  
  289.  
  290.  
  291.  
  292. SUB Init ()
  293.  
  294.  
  295.     SCREEN _NEWIMAGE(1024, 512, 32)
  296.     CLS , DARKGRAY
  297.     px = 300
  298.     py = 300
  299.     pdx = COS(pa) * 5
  300.     pdy = SIN(pa) * 5
  301.  
  302.  
  303.  

4
QB64 Discussion / Re: Math help with puck/paddle bounce
« on: June 04, 2020, 10:15:54 pm »
A more subtle arc like from an ellipse?

I have a Breakout Game that uses a "loaf-of-bread" paddle, LOL. I have to check the math I used for that.

Yes, in my drawing I envisioned the ellipse to go from upper left to lower left passing through the center of the paddle. Not sure if that is accurate but what I envisioned.

5
QB64 Discussion / Re: Math help with puck/paddle bounce
« on: June 04, 2020, 10:14:09 pm »
So to be clear - the paddle needs to look like a paddle and move like a paddle, but reflect the ball as if curved. (This is a one-liner solution with vectors but it seems like bplus has something close to cooked already.)

Yes, exactly. Sorry for the delay, wife came home and had to leave the computer for a while.

6
QB64 Discussion / Re: Math help with puck/paddle bounce
« on: June 04, 2020, 08:38:26 pm »
OH! I totally get what you want.

This reminds me of a fresnel lens, where its a huge lens for light, but perfectly flat. The surface is graded as if the whole thing was curved. IIIIII know what you want.

bplus want this or should I?

Yeah :-)  Yes, right now the only time the puck gets its Y vector component changed is when it hits a side line. Being able to change the Y vector with the paddle will add variableness (is that word?) to the trajectory of the puck. Just like air hockey, but a more subtle arc.

7
QB64 Discussion / Re: Math help with puck/paddle bounce
« on: June 04, 2020, 08:31:29 pm »
I might be getting a little lost in the question...

So it looks like the game posted above works fine - the ball just reverses its x-velocity when it hits the paddle, or so it seems. Is this the desired behavior or no? Going from the figure at the top of the page, I see some things that ordinarily wouldn't be there in this problem - like why is the paddle drawn with some kind of generating circle? Is there something about the pong mechanics that's not so obvious?

I would like the front of the paddle to be seen as an arc rather than a flat surface. The puck would then have its Y vector component modified based on bouncing off that arc instead of flat surface.

8
QB64 Discussion / Re: Math help with puck/paddle bounce
« on: June 04, 2020, 08:04:30 pm »
You mean this? That I solved. https://www.qb64.org/forum/index.php?topic=2309.msg115507#msg115507

Terry just wants a vertical paddle like used in pong I think. That is much simpler collision because you can treat the paddle like a moving wall (vertical or horizontal).

Terry do you have a pong game? I can probably dig one up.

If the ball is moving dx and dy then just reverse the dx, eg dx = -dx also you can put the ball x, y tangent to the paddle (touching the paddle) so ball looks as if it hit paddle but does not sink through it, nor starts it's reverse course too early (before touching the paddle).

That's what I created for Task 22, A pong game that will be built step by step to show simple game design. I have it complete except for the Yvector() function returning the correct figure. Right now the puck just bounces back and forth. (see below) This will get called in the MovePuck() subroutine.

Code: QB64: [Select]
  1.  
  2. '-------------------------------------------------------------------------------------------------------------
  3. '** Declare constants
  4. '-------------------------------------------------------------------------------------------------------------
  5.  
  6. CONST FALSE = 0, TRUE = NOT FALSE '               truth detectors
  7. CONST BRIGHTWHITE = _RGB32(255, 255, 255) '       define colors
  8. CONST WHITE = _RGB32(192, 192, 192)
  9. CONST GRAY = _RGB32(128, 128, 128) '
  10. CONST SCREENWIDTH = 768 '                         screen width
  11. CONST SCREENHEIGHT = 576 '                        screen height
  12. CONST PUCKSIZE = 10 '                             puck square size
  13. CONST PADDLEWIDTH = 10 '                          paddle width
  14. CONST PADDLEHEIGHT = 60 '                         paddle height
  15. CONST PADDLESPEED = 2 '                           paddle Y speed
  16. CONST PADDLEOFFSET = 20 '                         paddle offset from side of screen
  17. CONST P1MISSED = PUCKSIZE \ 2 '                   puck on left side edge of screen
  18. CONST P2MISSED = SCREENWIDTH - 1 - PUCKSIZE \ 2 ' puck on right side edge of screen
  19. CONST PADDLEYMIN = PADDLEHEIGHT \ 2 + 30 '                                     min Y coordinate for paddles
  20. CONST PADDLEYMAX = SCREENHEIGHT - 30 - PADDLEHEIGHT \ 2 '                      max Y coordinate for paddles
  21. CONST PUCKXMIN = PADDLEOFFSET + PADDLEWIDTH + PUCKSIZE \ 2 '                   min X coordinate for puck
  22. CONST PUCKXMAX = SCREENWIDTH - 1 - PADDLEOFFSET - PADDLEWIDTH - PUCKSIZE \ 2 ' max X coordinate for puck
  23. CONST PUCKYMIN = 30 + PUCKSIZE \ 2 '                                           min Y coordinate for puck
  24. CONST PUCKYMAX = SCREENHEIGHT - 30 - PUCKSIZE \ 2 '                            max Y coordinate for puck
  25. CONST FPS = 300 '                                                              game frames per second
  26.  
  27. '-------------------------------------------------------------------------------------------------------------
  28. '** Declare TYPE definitions
  29. '-------------------------------------------------------------------------------------------------------------
  30.  
  31. TYPE RECTTYPE '        rectangle definition for collision detection
  32.     x1 AS INTEGER '    upper left X coordinate
  33.     y1 AS INTEGER '    upper left Y coordinate
  34.     x2 AS INTEGER '    lower right X coordinate
  35.     y2 AS INTEGER '    lower right Y coordinate
  36.  
  37. TYPE MOVINGOBJECT '    moving object definition
  38.     x AS SINGLE '      center X coordinate
  39.     y AS SINGLE '      center Y coordinate
  40.     xv AS SINGLE '     X vector component
  41.     yv AS SINGLE '     Y vector component
  42.     rect AS RECTTYPE ' rectangle coordinates for collision detection
  43.  
  44. '-------------------------------------------------------------------------------------------------------------
  45. '** Declare global variables
  46. '-------------------------------------------------------------------------------------------------------------
  47.  
  48. DIM PlayField AS LONG '        playing field image
  49. DIM PlayFieldCopy AS LONG '    playing field image copy (without scores)
  50. DIM P1Paddle AS MOVINGOBJECT ' player 1 paddle
  51. DIM P2Paddle AS MOVINGOBJECT ' player 2 paddle
  52. DIM Puck AS MOVINGOBJECT '     puck properties
  53. DIM Computer AS INTEGER '      TRUE if computer playing
  54. DIM Score(9) AS STRING * 15 '  encoded font data
  55. DIM Pscore(1) AS INTEGER '     player scores (0=player1, 1=player2
  56. DIM Missed AS INTEGER '        TRUE when a player misses the puck
  57. DIM GameOver AS INTEGER '      TRUE when a player reaches a score of 9
  58.  
  59. '-------------------------------------------------------------------------------------------------------------
  60. '** Begin main program
  61. '-------------------------------------------------------------------------------------------------------------
  62.  
  63. SCREEN _NEWIMAGE(SCREENWIDTH, SCREENHEIGHT, 32) ' enter graphics screen
  64. _TITLE "QB64 PONG!" '                             give window a title
  65. _DELAY .5 '                                       slight delay
  66. _SCREENMOVE _MIDDLE '                             move window to center of desktop
  67. Initialize '                                      initialize game settings
  68. DO '                                              begin main program loop
  69.     NewGame '                                     start a new game
  70.     DO '                                          begin game loop
  71.         PlayGame '                                play the game
  72.     LOOP UNTIL GameOver OR _KEYDOWN(27) '         leave when game over or player exits
  73. LOOP UNTIL _KEYDOWN(27) '                         leave when player exits
  74. Cleanup '                                         clean player's computer memory and exit to operating system
  75.  
  76. '-------------------------------------------------------------------------------------------------------------
  77. '** end main program
  78. '-------------------------------------------------------------------------------------------------------------
  79.  
  80. '-------------------------------------------------------------------------------------------------------------
  81. '** Declare subroutines and functions
  82. '-------------------------------------------------------------------------------------------------------------
  83.  
  84. '-------------------------------------------------------------------------------------------------------------
  85. SUB ResetRound ()
  86.     '---------------------------------------------------------------------------------------------------------
  87.  
  88.     SHARED Missed AS INTEGER '    need access to puck miss indicator
  89.     SHARED Puck AS MOVINGOBJECT ' need access to puck
  90.  
  91.     IF SGN(Puck.xv) = 1 THEN '                          puck was heading right?
  92.         Puck.x = SCREENWIDTH * .25 '                    yes, serve from player 1 side
  93.         Puck.xv = .5 '                                  puck will travel right
  94.     ELSE '                                              no, puck was travleing left
  95.         Puck.x = SCREENWIDTH * .75 '                    serve from player 2 side
  96.         Puck.xv = -.5 '                                 puck will travel left
  97.     END IF
  98.     Puck.y = INT(RND(1) * (SCREENHEIGHT - 100)) + 50 '  random puck Y coordinate
  99.     Puck.yv = (RND(1) - RND(1)) / 2 '                   random puck Y vector component
  100.     Missed = FALSE '                                    reset puck missed indicator
  101.  
  102.  
  103. '-------------------------------------------------------------------------------------------------------------
  104. SUB PlayGame ()
  105.     '---------------------------------------------------------------------------------------------------------
  106.  
  107.     SHARED PlayField AS LONG '    need access to playing field image
  108.     SHARED Missed AS INTEGER '    need access to puck miss indicator
  109.     SHARED Puck AS MOVINGOBJECT ' need access to puck
  110.     SHARED Pscore() AS INTEGER '  need access to player scores
  111.     SHARED GameOver AS INTEGER '  need access to game over indicator
  112.  
  113.     ResetRound '                                             set up next game round
  114.     DO '                                                     begin round loop
  115.         _LIMIT FPS '                                         limit game speed
  116.         _PUTIMAGE , PlayField '                              reset playing field
  117.         MovePaddles '                                        update player paddles
  118.         MovePuck '                                           update puck
  119.         _DISPLAY '                                           update screen with changes
  120.     LOOP UNTIL Missed OR _KEYDOWN(27) '                      leave when puck missed or a player presses ESC
  121.     IF SGN(Puck.xv) = -1 THEN '                              was puck heading left?
  122.         Pscore(1) = Pscore(1) + 1 '                          yes, increase player 2 score
  123.     ELSE '                                                   no, puck was heading right
  124.         Pscore(0) = Pscore(0) + 1 '                          increase player 1 score
  125.     END IF
  126.     IF Pscore(0) = 9 OR Pscore(1) = 9 THEN GameOver = TRUE ' set game over indicator when player reaches 9
  127.     UpdateScores '                                           update player scores
  128.  
  129.  
  130. '-------------------------------------------------------------------------------------------------------------
  131. SUB UpdateScores ()
  132.     '---------------------------------------------------------------------------------------------------------
  133.  
  134.     SHARED PlayField AS LONG '     need access to playing field image
  135.     SHARED PlayFieldCopy AS LONG ' need access to playing field image copy
  136.     SHARED Pscore() AS INTEGER '   need access to player scores
  137.     DIM Clr AS _UNSIGNED LONG '    font color
  138.  
  139.     _PUTIMAGE , PlayFieldCopy, PlayField '                      restore playing field
  140.     _DEST PlayField '                                           make playing field image the destination
  141.     IF Pscore(0) = 9 THEN Clr = BRIGHTWHITE ELSE Clr = WHITE '  set color of font
  142.     DrawScore SCREENWIDTH \ 2 - 100, 50, Pscore(0), Clr '       draw player 1's score
  143.     IF Pscore(1) = 9 THEN Clr = BRIGHTWHITE ELSE Clr = WHITE '  set color of font
  144.     DrawScore SCREENWIDTH \ 2 + 55, 50, Pscore(1), Clr '        draw player 2's score
  145.     _DEST 0 '                                                   set destination back to screen
  146.  
  147.  
  148. '-------------------------------------------------------------------------------------------------------------
  149. SUB DrawScore (dx AS INTEGER, dy AS INTEGER, n AS INTEGER, clr AS _UNSIGNED LONG)
  150.     '---------------------------------------------------------------------------------------------------------
  151.  
  152.     SHARED Score() AS STRING * 15 ' need access to encoded font data
  153.     DIM x AS INTEGER '              font column counter
  154.     DIM y AS INTEGER '              font row counter
  155.     DIM p AS INTEGER '              string position counter
  156.  
  157.     p = 0 '                                       reset string position counter
  158.     FOR y = 0 TO 4 '                              cycle through 5 rows
  159.         FOR x = 0 TO 2 '                          cycle through 3 columns
  160.             p = p + 1 '                           increment string position counter
  161.             IF MID$(Score(n), p, 1) <> " " THEN ' character in this position?
  162.                 LINE (dx + x * 15, dy + y * 15)-(dx + x * 15 + 15, dy + y * 15 + 15), clr, BF ' yes, draw cube
  163.             END IF
  164.     NEXT x, y '                                   leave when all rows and columns processed
  165.  
  166.  
  167. '-------------------------------------------------------------------------------------------------------------
  168. SUB MovePuck ()
  169.     '---------------------------------------------------------------------------------------------------------
  170.  
  171.     SHARED P1Paddle AS MOVINGOBJECT ' need access to player 1 paddle
  172.     SHARED P2Paddle AS MOVINGOBJECT ' need access to player 2 paddle
  173.     SHARED Puck AS MOVINGOBJECT '     need access to puck
  174.     SHARED Missed AS INTEGER '        need access to puck miss indicator
  175.     SHARED Pscore() AS INTEGER '      need access to player scores
  176.  
  177.     Puck.x = Puck.x + Puck.xv '            add X vector component to X coordinate
  178.     Puck.y = Puck.y + Puck.yv '            add Y vector component to Y coordinate
  179.     Puck.rect.x1 = Puck.x - PUCKSIZE \ 2 ' calculate puck rectangle coordinates
  180.     Puck.rect.y1 = Puck.y - PUCKSIZE \ 2
  181.     Puck.rect.x2 = Puck.x + PUCKSIZE \ 2
  182.     Puck.rect.y2 = Puck.y + PUCKSIZE \ 2
  183.     IF Puck.x < PUCKXMIN OR Puck.x > PUCKXMAX THEN '                             is puck within a paddle area?
  184.         IF SGN(Puck.xv) = -1 AND RectCollide(Puck.rect, P1Paddle.rect) THEN '    going left and hit paddle?
  185.             Puck.xv = -Puck.xv + .025 '                                          yes, reverse X vector
  186.             Puck.x = PUCKXMIN '                                                  place puck to right of paddle
  187.  
  188.             '** change in Y vector component here
  189.  
  190.             SOUND 440, 1 '                                                       audio report
  191.         ELSEIF SGN(Puck.xv) = 1 AND RectCollide(Puck.rect, P2Paddle.rect) THEN ' going right and hit paddle?
  192.             Puck.xv = -Puck.xv - .025 '                                          yes, reverse X vector
  193.             Puck.x = PUCKXMAX '                                                  place puck to left of paddle
  194.  
  195.             '** change in Y vector component here
  196.  
  197.             SOUND 440, 1 '                                                       audio report
  198.         END IF
  199.     END IF
  200.     IF Puck.y < PUCKYMIN OR Puck.y > PUCKYMAX THEN '                             is puck hitting a side line?
  201.         IF SGN(Puck.yv) = -1 THEN Puck.y = PUCKYMIN ELSE Puck.y = PUCKYMAX '     yes, place puck above/below
  202.         Puck.yv = -Puck.yv '                                                     reverse Y vector
  203.         SOUND 880, 1 '                                                           audio report
  204.     END IF
  205.     IF Puck.x < P1MISSED OR Puck.x > P2MISSED THEN '                             did puck hit edge of screen?
  206.         Missed = TRUE '                                                          yes, set missed indicator
  207.         SOUND 220, 1 '                                                           audio report
  208.     END IF
  209.     LINE (Puck.rect.x1, Puck.rect.y1)-(Puck.rect.x2, Puck.rect.y2), BRIGHTWHITE, BF ' draw puck
  210.  
  211.  
  212. '-------------------------------------------------------------------------------------------------------------
  213. SUB MovePaddles ()
  214.     '---------------------------------------------------------------------------------------------------------
  215.  
  216.     SHARED P1Paddle AS MOVINGOBJECT ' need access to player 1 paddle
  217.     SHARED P2Paddle AS MOVINGOBJECT ' need access to player 2 paddle
  218.     SHARED Puck AS MOVINGOBJECT '     need access to puck
  219.     SHARED Computer AS INTEGER '      need access to computer opponent
  220.  
  221.     IF _KEYDOWN(87) OR _KEYDOWN(119) THEN '                       player 1 pressing W or w keys?
  222.         P1Paddle.y = P1Paddle.y - PADDLESPEED '                   yes, move paddle up
  223.         IF P1Paddle.y < PADDLEYMIN THEN P1Paddle.y = PADDLEYMIN ' keep paddle in playing field
  224.     ELSEIF _KEYDOWN(83) OR _KEYDOWN(115) THEN '                   player 1 pressing S or s keys?
  225.         P1Paddle.y = P1Paddle.y + PADDLESPEED '                   yes, move paddle down
  226.         IF P1Paddle.y > PADDLEYMAX THEN P1Paddle.y = PADDLEYMAX ' keep paddle in playing field
  227.     END IF
  228.     IF Computer THEN '                                            is player 1 playing computer?
  229.         IF SGN(Puck.xv) = 1 THEN '                                yes, is puck heading for computer?
  230.             IF Puck.y - P2Paddle.y < 0 THEN '                     yes, is puck higher than center of paddle?
  231.                 P2Paddle.y = P2Paddle.y - .5 - RND(1) / 3 '       yes, move computer paddle up
  232.                 IF P2Paddle.y < PADDLEYMIN THEN P2Paddle.y = PADDLEYMIN ' keep paddle in playing field
  233.             ELSE '                                                no, puck is below center of paddle
  234.                 P2Paddle.y = P2Paddle.y + .5 + RND(1) / 3 '       move computer paddle down
  235.                 IF P2Paddle.y > PADDLEYMAX THEN P2Paddle.y = PADDLEYMAX ' keep paddle in playing field
  236.             END IF
  237.         END IF
  238.     ELSEIF _KEYDOWN(18432) THEN '                                 player 2 pressing up arrow key?
  239.         P2Paddle.y = P2Paddle.y - PADDLESPEED '                   yes, move paddle up
  240.         IF P2Paddle.y < PADDLEYMIN THEN P2Paddle.y = PADDLEYMIN ' keep paddle in playing field
  241.     ELSEIF _KEYDOWN(20480) THEN '                                 player 2 pressing down arrow key?
  242.         P2Paddle.y = P2Paddle.y + PADDLESPEED '                   yes, move paddle down
  243.         IF P2Paddle.y > PADDLEYMAX THEN P2Paddle.y = PADDLEYMAX ' keep paddle in playing field
  244.     END IF
  245.     P1Paddle.rect.y1 = P1Paddle.y - PADDLEHEIGHT \ 2 '            calc player 1 paddle rectangle
  246.     P1Paddle.rect.y2 = P1Paddle.y + PADDLEHEIGHT \ 2
  247.     P2Paddle.rect.y1 = P2Paddle.y - PADDLEHEIGHT \ 2 '            calc player 2 paddle rectangle
  248.     P2Paddle.rect.y2 = P2Paddle.y + PADDLEHEIGHT \ 2
  249.     LINE (P1Paddle.rect.x1, P1Paddle.rect.y1)-(P1Paddle.rect.x2, P1Paddle.rect.y2), BRIGHTWHITE, BF ' draw
  250.     LINE (P2Paddle.rect.x1, P2Paddle.rect.y1)-(P2Paddle.rect.x2, P2Paddle.rect.y2), BRIGHTWHITE, BF ' paddles
  251.  
  252.  
  253. '-------------------------------------------------------------------------------------------------------------
  254. SUB NewGame ()
  255.     '---------------------------------------------------------------------------------------------------------
  256.  
  257.     SHARED P1Paddle AS MOVINGOBJECT ' need access toplayer 1 paddle
  258.     SHARED P2Paddle AS MOVINGOBJECT ' need access to player 2 paddle
  259.     SHARED Puck AS MOVINGOBJECT '     need access to puck
  260.     SHARED PlayField AS LONG '        need access to playing field image
  261.     SHARED Pscore() AS INTEGER '      need access to player scores
  262.     SHARED Missed AS INTEGER '        need access to puck miss indicator
  263.     SHARED GameOver AS INTEGER '      need access to game over indicator
  264.     SHARED Computer AS INTEGER '      need access to computer opponent
  265.     DIM Blink AS INTEGER '            blink bit
  266.     DIM NumPlayers AS INTEGER '       number of players selected
  267.     DIM Frames AS INTEGER '           frame counter
  268.  
  269.     P1Paddle.y = SCREENHEIGHT \ 2 '                                                reset paddle locations
  270.     P2Paddle.y = P1Paddle.y
  271.     Puck.xv = .5 '                                                                 reset puck X speed
  272.     Puck.y = SCREENHEIGHT \ 2
  273.     Missed = FALSE '                                                               reset missed indicator
  274.     GameOver = FALSE '                                                             reset game over indicator
  275.     Computer = FALSE '                                                             player 2 is human
  276.     SOUND 440, 1 '                                                                 new game audio
  277.     SOUND 880, 1
  278.     DO '                                                                           begin setup loop
  279.         _LIMIT FPS '                                                               limit frame speed
  280.         _PUTIMAGE , PlayField '                                                    restore playing field
  281.         MovePaddles '                                                              allow paddle movement
  282.         Frames = Frames + 1 '                                                      increment frame counter
  283.         IF Frames = FPS THEN '                                                     max frames reached?
  284.             Frames = 0 '                                                           yes, reset frame counter
  285.             Blink = 1 - Blink '                                                    toggle blink bit
  286.         END IF
  287.         LOCATE (SCREENHEIGHT \ 16), ((SCREENWIDTH \ 8) - 51) \ 2 + 1 '             position cursor on screen
  288.         SELECT CASE Blink '                                                        which blinker bit?
  289.             CASE 0 '                                                               blinker bit 0
  290.                 IF NumPlayers = 0 THEN '                                           number of players chosen?
  291.                     PRINT "    SELECT (1) OR (2) PLAYERS NOW : ESC TO EXIT    "; ' no, display player select
  292.                 ELSE '                                                             yes
  293.                     PRINT "(1) OR (2) PLAYERS : SPACEBAR TO PLAY : ESC TO EXIT"; ' display all instructions
  294.                 END IF
  295.             CASE 1 '                                                               blinker bit 1
  296.                 PRINT SPACE$(51); '                                                remove instructions
  297.         END SELECT
  298.         LOCATE (SCREENHEIGHT \ 16) - 2, ((SCREENWIDTH \ 16) - 27) \ 2 + 1 '        position cursor on screen
  299.         PRINT "PLAYER 1: W = UP / S = DOWN" '                                      display keys to player 1
  300.         LOCATE (SCREENHEIGHT \ 16) - 2, ((SCREENWIDTH \ 8) - 31) \ 2 + (SCREENWIDTH \ 32) + 1 'position cursor
  301.         SELECT CASE NumPlayers '                                                   how many players selected?
  302.             CASE 0: PRINT " SELECT (1) OR (2) PLAYERS NOW " '                      ask number of players
  303.             CASE 1: PRINT " PLAYER 2: THE CPU HAS CONTROL " '                      inform computer playing
  304.             CASE 2: PRINT "PLAYER 2: UP ARROW / DOWN ARROW" '                      display keys player 2
  305.         END SELECT
  306.         IF NumPlayers <> 1 THEN '                                                  has one player been chosen?
  307.             IF _KEYDOWN(49) THEN '                                                 no, "1" key been pressed?
  308.                 NumPlayers = 1 '                                                   yes, set players to 1
  309.                 Computer = TRUE '                                                  remember AI now has control
  310.                 SOUND 440, 1 '                                                     play affirmation beep
  311.                 SOUND 880, 1 '                                                     play affirmation beep
  312.             END IF
  313.         END IF
  314.         IF NumPlayers <> 2 THEN '                                                  has two player been chosen?
  315.             IF _KEYDOWN(50) THEN '                                                 no, "2" key been pressed?
  316.                 NumPlayers = 2 '                                                   yes, set players to 2
  317.                 Computer = FALSE '                                                 human is playing human
  318.                 SOUND 440, 1 '                                                     play affirmation beep
  319.                 SOUND 880, 1 '                                                     play affirmation beep
  320.             END IF
  321.         END IF
  322.         _DISPLAY '                                                                 update screen with changes
  323.     LOOP UNTIL (_KEYDOWN(32) AND NumPlayers) OR _KEYDOWN(27) '                     leave when players select
  324.     Pscore(0) = 0 '                                                                reset scores
  325.     Pscore(1) = 0
  326.     UpdateScores '                                                                 update on screen scores
  327.  
  328.  
  329. '-------------------------------------------------------------------------------------------------------------
  330. SUB Initialize ()
  331.     '---------------------------------------------------------------------------------------------------------
  332.  
  333.     SHARED PlayField AS LONG '        need access to playing field image
  334.     SHARED PlayFieldCopy AS LONG '    need access to playing field image copy
  335.     SHARED P1Paddle AS MOVINGOBJECT ' need access to player 1 paddle
  336.     SHARED P2Paddle AS MOVINGOBJECT ' need access to player 2 paddle
  337.     SHARED Puck AS MOVINGOBJECT '     need access to puck
  338.     SHARED Score() AS STRING * 15 '   need access to encoded font data
  339.     DIM y AS INTEGER '                center line counter
  340.  
  341.     RANDOMIZE TIMER '                                               seed random number generator
  342.  
  343.     '** do as many precalculations as possible for values that will not change
  344.  
  345.     P1Paddle.x = PADDLEOFFSET + PADDLEWIDTH \ 2 '                   calculate player 1 paddle X coordinate
  346.     P2Paddle.x = SCREENWIDTH - 1 - PADDLEOFFSET - PADDLEWIDTH \ 2 ' calculate player 2 paddle X coordinate
  347.     P1Paddle.rect.x1 = P1Paddle.x - PADDLEWIDTH \ 2 '               calculate player 1 paddle rectangle
  348.     P1Paddle.rect.x2 = P1Paddle.x + PADDLEWIDTH \ 2
  349.     P2Paddle.rect.x1 = P2Paddle.x - PADDLEWIDTH \ 2 '               calculate player 2 paddle rectangle
  350.     P2Paddle.rect.x2 = P2Paddle.x + PADDLEWIDTH \ 2
  351.  
  352.     '** draw default playing field
  353.  
  354.     PlayField = _NEWIMAGE(SCREENWIDTH, SCREENHEIGHT, 32) '          create image
  355.     _DEST PlayField '                                               make it destination
  356.     CLS '                                                           clear image
  357.     LINE (0, 19)-(SCREENWIDTH - 1, 29), BRIGHTWHITE, BF '           draw top side line
  358.     LINE (0, SCREENHEIGHT - 30)-(SCREENWIDTH - 1, SCREENHEIGHT - 20), BRIGHTWHITE, BF ' draw bottom side line
  359.     FOR y = 40 TO SCREENHEIGHT - 52 STEP 30 '                       cycle top to bottom
  360.         LINE ((SCREENWIDTH \ 2) - 5, y)-((SCREENWIDTH \ 2) + 5, y + 15), GRAY, BF '     draw center lines
  361.     NEXT y
  362.     _DEST 0 '                                                       destination back to screen
  363.     PlayFieldCopy = _COPYIMAGE(PlayField) '                         save a copy of the playing field image
  364.     Score(0) = "0000 00 00 0000" '                                  create numeric font data
  365.     Score(1) = "  1  1  1  1  1"
  366.     Score(2) = "222  22222  222"
  367.     Score(3) = "333  3333  3333"
  368.     Score(4) = "4 44 4444  4  4"
  369.     Score(5) = "5555  555  5555"
  370.     Score(6) = "6666  6666 6666"
  371.     Score(7) = "777  7  7  7  7"
  372.     Score(8) = "8888 88888 8888"
  373.     Score(9) = "9999 9999  9999"
  374.  
  375.  
  376. '-------------------------------------------------------------------------------------------------------------
  377. FUNCTION RectCollide (Rect1 AS RECTTYPE, Rect2 AS RECTTYPE)
  378.     '---------------------------------------------------------------------------------------------------------
  379.  
  380.     RectCollide = FALSE '                       assume no collision
  381.     IF Rect1.x2 >= Rect2.x1 THEN '              rect 1 lower right X >= rect 2 upper left  X ?
  382.         IF Rect1.x1 <= Rect2.x2 THEN '          rect 1 upper left  X <= rect 2 lower right x ?
  383.             IF Rect1.y2 >= Rect2.y1 THEN '      rect 1 lower right Y >= rect 2 upper left  Y ?
  384.                 IF Rect1.y1 <= Rect2.y2 THEN '  rect 1 upper left  Y <= rect 2 lower right Y ?
  385.                     RectCollide = TRUE '        if all 4 IFs TRUE then a collision is occurring
  386.                 END IF
  387.             END IF
  388.         END IF
  389.     END IF
  390.  
  391.  
  392. '-------------------------------------------------------------------------------------------------------------
  393. SUB Cleanup ()
  394.     '---------------------------------------------------------------------------------------------------------
  395.  
  396.     SHARED PlayField AS LONG '     need access to playing field image
  397.     SHARED PlayFieldCopy AS LONG ' need access to playing field image copy
  398.  
  399.     _FREEIMAGE PlayField '         free images from memory
  400.     _FREEIMAGE PlayFieldCopy
  401.     SYSTEM '                       return to operating system
  402.  
  403.  
  404. '-------------------------------------------------------------------------------------------------------------
  405. FUNCTION Yvector (PaddleY AS INTEGER, PuckY AS INTEGER, PuckYV)
  406.     '---------------------------------------------------------------------------------------------------------
  407.  
  408.  
  409.     '** math here (ugh)
  410.  
  411.  
  412.  

9
QB64 Discussion / Re: Math help with puck/paddle bounce
« on: June 04, 2020, 03:02:17 pm »
Yes, I do, but I don't plan on adding any spin or "english" to the ball using the Y velocity of the paddle.

10
QB64 Discussion / Math help with puck/paddle bounce
« on: June 04, 2020, 02:12:54 pm »
Calling all math wizards. I have no idea why this problem is eluding me but I just cant seem to create the math needed for this problem I have.

A rectangular paddle of known size (rw x rh) moves only in a vertical direction.

When a ball hits the surface it needs to deflect at the tangent point of an arc instead of a flat surface.

I drew the diagram below when I set out to create the math to help as a visual aid.

Would anyone care to help with the math needed to accomplish this? I would very much appreciate it.

11
QB64 Discussion / Re: QB64SOURCECODE.COM
« on: June 04, 2020, 03:45:22 am »
@TerryRitchie Finally! After near a month of learning SQL and PHP just for making that page i have finished yesterday
But to give it to you I need a few favors, none of which allow me access to anything which you dont want me to access. As u told me in a mail

So by this ill have access to a database which is totally safe :) and access to uploads folder and that's it
no harm done

Awesome :-)  Give me a few days (or a week) to get this set up though. I'm in the process of doing a home improvement project and have to have it completed by Tuesday when they haul the dumpster away.

12
QB64 Discussion / Re: Help with my program
« on: June 02, 2020, 09:45:51 pm »
Looks like _vince took care of you. :-)

13
QB64 Discussion / Re: Help with my program
« on: June 02, 2020, 08:28:08 pm »
https://imgur.com/a/peSVZeE sorry, keep forgetting

No problem. I'll take a look at it in a bit. Working on another program right now.

Oh, by the way, welcome to the forum! :-)

14
QB64 Discussion / Re: Help with my program
« on: June 02, 2020, 03:35:44 pm »
need fire.png

15
QB64 Discussion / Re: QB64SOURCECODE.COM
« on: June 02, 2020, 12:27:44 pm »
Sup Terry,
:)
I'm nearly finished making a webpage that I'll send you on mail on which users can upload projects they've made using your tutorials and all their projects (or at least their G-Drive links as I'm not so good) will be shown
:)

Looking forward to see what you have. Thank you :-)

Pages: [1] 2 3 ... 33