Author Topic: Level 1 repeats before moving to the next and I don't know why! Please help  (Read 3269 times)

0 Members and 1 Guest are viewing this topic.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
I'm writing a clone to the old old old Atari game call BreakThru. You move a paddle back and forth to reflect the ball and make it break through the bricks at the top. I plan on writing this with graphics after I finish doing it with strings. The game is mostly finished. I'm having trouble with the first level being loaded again after it is beaten once. After it's beat the second time, the second level is loaded. I can't figure out why the first level gets loaded twice. Please help me spot the bug.
Code: QB64: [Select]
  1. WIDTH 80, 50
  2.  
  3. TYPE coordinate
  4.     x AS INTEGER
  5.     y AS INTEGER
  6.  
  7.  
  8. GOTO beginning
  9. crap:
  10. PRINT "Error, error line"
  11.  
  12. beginning:
  13. CONST TRUE = 1
  14. CONST FALSE = 0
  15. CONST leftDirection = 19200 '   to make the code more readable with the left and right arrowkeys
  16. CONST rightDirection = 19712 '
  17. CONST upAndLeft = 1 'the ball always moves in one diagonal direction
  18. CONST upAndRight = 2
  19. CONST downAndLeft = 3
  20. CONST downAndRight = 4
  21. CONST theBackground = 0 '           to determine what the ball hit
  22. CONST theBrick = 1
  23. CONST theWall = 2
  24. CONST thePaddle = 3
  25. CONST hitWallSound$ = "O2L30G"
  26. CONST hitPaddleSound$ = "O3L30C"
  27. CONST hitBrickSound$ = "O3L30G"
  28. CONST deathSound$ = "O4L15CO3CO2CO1L10C"
  29. CONST successBeep = "L9O2DL15DL9DL4F"
  30.  
  31. DIM SHARED creationBoard(2 TO 79, 2 TO 46) AS INTEGER
  32. FOR initializeArrayX = 2 TO 79
  33.     FOR initializeArrayY = 2 TO 46
  34.         creationBoard(initializeArrayX, initializeArrayY) = 1
  35. DIM SHARED brick AS coordinate: brick.x = 2: brick.y = 2
  36. DIM SHARED ball AS coordinate
  37. DIM SHARED ballDirection AS INTEGER
  38. DIM SHARED paddleLeft AS SINGLE
  39. DIM SHARED board(1 TO 80, 1 TO 48, 1 TO 2) AS INTEGER
  40. DIM SHARED numberOfBricks AS INTEGER
  41. DIM SHARED currentLevel AS INTEGER: currentLevel = 1
  42. DIM SHARED numberOfGuys AS INTEGER: numberOfGuys = 5
  43. DIM SHARED score AS INTEGER: scorte = 0
  44. DIM SHARED manDied AS INTEGER: manDied = FALSE
  45. DIM SHARED spaceToStart: spaceToStart = FALSE
  46. DIM SHARED debugMode AS INTEGER: debugMode = FALSE
  47. DIM SHARED penUp, currentColor, eraserOn AS INTEGER: penUp = TRUE: currentColor = 0: eraserOn = FALSE
  48. DIM SHARED mute AS INTEGER: mute = -1 ' -1 mute is off (sound on)
  49. DIM SHARED lastLevelCreated AS INTEGER: lastLevelCreated = 0
  50. DIM levelChecking AS INTEGER: levelChecking = 0
  51.     levelChecking = levelChecking + 1
  52. LOOP UNTIL _FILEEXISTS("Level " + LTRIM$(STR$(levelChecking))) = 0
  53. lastLevelCreated = levelChecking - 1
  54.  
  55. WIDTH 80, 50
  56.  
  57. CALL Menu
  58.  
  59. SUB Menu
  60.     printTheMenu = TRUE
  61.     DO
  62.         IF printTheMenu = TRUE THEN
  63.             COLOR 14, 1
  64.             CLS
  65.             LOCATE 20, 1
  66.             PRINT CenterText$("Menu")
  67.             PRINT CenterText$("------")
  68.             PRINT
  69.             PRINT CenterText$("1.) Instructions")
  70.             PRINT
  71.             PRINT CenterText$("2.) Create A Level")
  72.             PRINT
  73.             PRINT CenterText$("3.) Play The Game")
  74.             PRINT
  75.             PRINT CenterText$("4.) Exit")
  76.             printTheMenu = FALSE
  77.         END IF
  78.         menuCommand$ = INKEY$
  79.         SELECT CASE menuCommand$
  80.             CASE "1"
  81.                 CALL Instructions
  82.                 printTheMenu = TRUE
  83.             CASE "2"
  84.                 CALL CreationMain
  85.                 printTheMenu = TRUE
  86.             CASE "3"
  87.                 CALL Main
  88.                 printTheMenu = TRUE
  89.         END SELECT
  90.     LOOP UNTIL menuCommand$ = "4"
  91.     END
  92.  
  93. SUB Instructions
  94.     COLOR 10, 1
  95.     CLS
  96.     LOCATE 2
  97.     PRINT CenterText$("Level Creator")
  98.     PRINT CenterText$("---------------")
  99.     PRINT
  100.     PRINT CenterText$("The available commands are listed at the bottom of the screen.")
  101.     PRINT CenterText$("When the pen is up, the cursor moves without adding to the level")
  102.     PRINT CenterText$("being created. When the pen is down the current brick is added")
  103.     PRINT CenterText$("to the level being created. The other commands are self explanatory.")
  104.     COLOR 12, 1
  105.     PRINT
  106.     PRINT CenterText$("When the created level is saved, it is saved under " + CHR$(34) + "Level #" + CHR$(34) + ".")
  107.     PRINT CenterText$("If no levels have been created, the default level is used.")
  108.     PRINT CenterText$("If you progress beyond all levels created, the last level created repeats.")
  109.     PRINT
  110.     PRINT
  111.     PRINT
  112.     COLOR 14, 1
  113.     PRINT CenterText$("Game")
  114.     PRINT CenterText$("------")
  115.     PRINT
  116.     PRINT CenterText$("The object of the game is to progress to the next level by")
  117.     PRINT CenterText$("destroying all of the bricks. To do this you must position the")
  118.     PRINT
  119.     COLOR 15, 1
  120.     PRINT CenterText$(CHR$(219) + CHR$(219) + CHR$(219) + CHR$(219) + CHR$(219) + CHR$(219) + CHR$(219) + CHR$(219))
  121.     PRINT
  122.     COLOR 14, 1
  123.     PRINT CenterText$("under the square so that it is reflected toward the bricks using.")
  124.     PRINT CenterText$("the left and right arrow keys.")
  125.     LOCATE 29, 21
  126.     PRINT "Push ";: COLOR 13, 1: PRINT "[SPACEBAR]";: COLOR 14, 1: PRINT " to set the square in motion."
  127.     PRINT: PRINT
  128.     COLOR 13, 1: PRINT CenterText$("Available Commands:"): COLOR 14, 1: PRINT
  129.     LOCATE , 10: PRINT "+ to increase the game speed": PRINT
  130.     LOCATE , 10: PRINT "- to decrease the game speed": PRINT
  131.     LOCATE , 10: PRINT "P to pause and wait for any key to be pressed": PRINT
  132.     LOCATE , 10: PRINT "M to toggle mute on and off": PRINT
  133.     LOCATE , 10: PRINT "[ESC] to return to the main menu"
  134.     PRINT
  135.     PRINT CenterText$("Push C to turn on debug mode and O to turn it off.")
  136.     LOCATE 48
  137.     COLOR 15, 1
  138.     PRINT CenterText$("Push any key to continue.")
  139.  
  140.  
  141.     CALL P
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148. FUNCTION CenterText$ (thisString$)
  149.  
  150.     returnString$ = SPACE$(INT((80 - LEN(thisString$)) / 2)) + thisString$
  151.     CenterText = returnString$
  152.  
  153. SUB PrintBrick (x, y, onOrOff)
  154.     IF onOrOff = TRUE THEN
  155.         useColor = board(x, y, 2) 'print the brick instead of erasing it, pick up the color there
  156.     ELSEIF onOrOff = FALSE THEN
  157.         useColor = 1
  158.     END IF
  159.     FOR across = 0 TO 5
  160.         LOCATE y, x + across
  161.         COLOR useColor
  162.         PRINT CHR$(219)
  163.         board(x + across, y, 1) = 219
  164.         board(x + across, y, 2) = useColor
  165.     NEXT across
  166.     IF onOrOff = FALSE THEN
  167.         numberOfBricks = numberOfBricks - 1
  168.         score = score + 5
  169.     END IF
  170.  
  171. SUB PrintLevel
  172.     FOR x = 1 TO 80: FOR y = 1 TO 48 'make the board blue
  173.             board(x, y, 1) = 219 '    ASCII code of board space
  174.             board(x, y, 2) = 1 '      color of board space
  175.     NEXT y: NEXT x
  176.     COLOR 1, 1: CLS
  177.     'print left, right and top borders
  178.     FOR y = 1 TO 48
  179.         LOCATE y, 1: COLOR 12, 1: PRINT CHR$(219) 'print the left border to the screen
  180.         LOCATE 7, 80: COLOR 12, 1: PRINT CHR$(219) 'print the right border to the screen
  181.         CALL PlaceChar(1, y, 219, 12) ' put the position, ASCII code and color on the board to make the left border
  182.         CALL PlaceChar(80, y, 219, 12) ' same to make the left border on the board
  183.     NEXT y
  184.     FOR x = 1 TO 80
  185.         LOCATE 1, x: COLOR 12, 1: PRINT CHR$(219) 'print the top border to the screen
  186.         CALL PlaceChar(x, 1, 219, 12) ' put the top border on the board
  187.     NEXT x
  188.     paddleLeft = 36
  189.     CALL PrintPaddle(TRUE) ' put the paddle on the board with the left at x coordinate 36
  190.     ball.x = 40: ball.y = 47 ' position the ball/moving square
  191.     CALL PrintBall(TRUE) ' put the ball on the board
  192.     ballDirection = INT(RND * 2) + 1 'randomize the starting direction of the ball as up and left or up and right
  193.  
  194.     IF lastLevelCreated <> 0 THEN
  195.         filename$ = "Level " + S$(currentLevel)
  196.         IF _FILEEXISTS(filename$) = 0 THEN filename$ = "Level " + S$(lastLevelCreated)
  197.         OPEN filename$ FOR INPUT AS #1 'open the file with the level information
  198.         FOR xCounting = 2 TO 79 ' x coordinate of the board
  199.             FOR yCounting = 2 TO 46 ' y coordinate of the board
  200.                 INPUT #1, boardColor$ ' get the color of the board at xCounting, yCounting
  201.                 board(xCounting, yCounting, 2) = VAL(boardColor$) 'color the board at xCounting, yCounting
  202.         NEXT: NEXT
  203.         '        LOCATE 20, 20: COLOR 15, 0: PRINT "Filename: " + filename$: CALL P
  204.         CLOSE #1
  205.     ELSE ' if no levels were created
  206.         FOR x = 2 TO 79
  207.             CALL PlaceChar(x, 2, 219, 14) 'put a yellow (14) block (219) at x, y on the board
  208.             CALL PlaceChar(x, 4, 219, 14)
  209.             CALL PlaceChar(x, 6, 219, 14)
  210.         NEXT x
  211.     END IF
  212.     numberOfBricks = 0 ' after a level has been put on the board, count the number of bricks on it
  213.     FOR brickCountingY = 2 TO 47
  214.         FOR brickCountingX = 2 TO 79 STEP 6 'each brick is 6 blocks long
  215.             IF board(brickCountingX, brickCountingY, 2) <> 1 THEN numberOfBricks = numberOfBricks + 1
  216.     NEXT: NEXT
  217.     CALL PrintBoard 'print the board to the screen
  218.     spaceToStart = FALSE 'spacebar has yet to be puched to start the current level
  219.  
  220. SUB Main
  221.  
  222.     CALL PrintLevel 'put the current level on the board and print it out
  223.     printOutBoard = FALSE ' to avoid flicker of the board being constantly printed, the need to print it is false
  224.     delaySpeed = 0.029 'delay to use inside the main DO-LOOP
  225.     DO
  226.         debugging$ = INKEY$ 'using strings instead of _KEYDOWN(code) is easier. i initially added INKEY$ for debugging
  227.         IF spaceToStart = TRUE THEN ' the spacebar has been hit to start the game
  228.             CALL PrintBall(FALSE) 'remove the ball from the board
  229.             CALL MoveBall 'move the ball in the current direction
  230.             CALL PrintBall(TRUE) 'put the ball on the board at the new position
  231.             IF _KEYDOWN(leftDirection) = -1 THEN 'the left arrow key had been pushed
  232.                 CALL PrintPaddle(FALSE) ' remove the paddle from the board
  233.                 paddleLeft = paddleLeft - 1.5 ' move the paddle leftward
  234.                 CALL PrintPaddle(TRUE) ' put the paddle on the board at the new position
  235.                 printOutBoard = TRUE 'it is now needed to print the board to the screen
  236.             ELSEIF _KEYDOWN(rightDirection) = -1 THEN
  237.                 CALL PrintPaddle(FALSE)
  238.                 paddleLeft = paddleLeft + 1.5
  239.                 CALL PrintPaddle(TRUE)
  240.                 printOutBoard = TRUE
  241.             END IF
  242.             IF printOutBoard = TRUE THEN 'print the board to the screen
  243.                 CALL PrintBoard ' print the board to the screen
  244.                 printOutBoard = FALSE 'stop the board from being printed now
  245.             END IF
  246.             IF numberOfBricks = 0 THEN 'there are no bricks left on the board
  247.                 currentLevel = currentLevel + 1 'increment the current level
  248.                 CALL PrintLevel 'put the newest level on the board
  249.                 spaceToStart = FALSE 'the spacebar has not been pressed since the new level has been output
  250.                 score = score + 200 '200 points for finishing a level
  251.                 IF mute = -1 THEN PLAY successBeep '1 is mute, -1 is note muted
  252.             END IF
  253.         END IF
  254.         IF debugging$ = "C" THEN debugMode = TRUE
  255.         IF debugging$ = "O" THEN debugMode = FALSE
  256.         IF _KEYDOWN(32) = -1 THEN ' the spacebar has been pushed
  257.             spaceToStart = TRUE ' the spacebar has been pushed
  258.             printOutBoard = TRUE ' it is needed to print the board to the screen
  259.         END IF
  260.         _DELAY (delaySpeed) ' insert a delay to make the movement playable and visible
  261.         IF numberOfGuys = 0 THEN CALL EndOfGame
  262.         IF score MOD 2000 = 0 AND score <> 0 THEN numberOfGuys = numberOfGuys + 1 ' new guy every 2,000 points
  263.         IF debugging$ = "+" THEN 'increase the game speed
  264.             delaySpeed = delaySpeed - 0.01
  265.             IF delaySpeed < 0 THEN delaySpeed = 0 'don't allow a negative delay
  266.         END IF
  267.         IF debugging$ = "-" THEN delaySpeed = delaySpeed + 0.005 'decrease the game speed
  268.         IF debugging$ = "m" OR debugging$ = "M" THEN mute = mute * -1 'turn mute on or off
  269.         IF debugging$ = "p" OR debugging$ = "P" THEN pause$ = INPUT$(1) 'pause the game
  270.         IF debugging$ = CHR$(27) THEN EXIT DO 'end the game and return to the main menu
  271.     LOOP
  272.  
  273. SUB EndOfGame
  274.  
  275. SUB MoveBall
  276.     SELECT CASE ballDirection
  277.         CASE upAndRight
  278.             CALL MoveUpAndRight
  279.         CASE upAndLeft
  280.             CALL MoveUpAndLeft
  281.         CASE downAndRight
  282.             CALL MoveDownAndRight
  283.         CASE downAndLeft
  284.             CALL MoveDownAndLeft
  285.     END SELECT
  286.  
  287. FUNCTION Report (x, y)
  288.     'if 12 then border, if 1 then background, if 15 then paddle, else brick
  289.     rtn = -1
  290.     IF board(x, y, 2) = 12 THEN
  291.         rtn = theWall
  292.     ELSEIF board(x, y, 2) = 1 THEN
  293.         rtn = theBackground
  294.     ELSEIF board(x, y, 2) = 15 THEN
  295.         rtn = thePaddle
  296.     ELSE
  297.         rtn = theBrick
  298.     END IF
  299.     Report = rtn
  300.  
  301. SUB MoveUpAndRight
  302.     SELECT CASE Report(ball.x + 1, ball.y - 1)
  303.         CASE theBackground
  304.             ball.x = ball.x + 1
  305.             ball.y = ball.y - 1
  306.         CASE theWall
  307.             IF mute = -1 THEN PLAY hitWallSound
  308.             IF ball.x + 1 = 80 THEN
  309.                 'hit the right wall
  310.                 ballDirection = upAndLeft
  311.             ELSEIF ball.y - 1 = 1 THEN
  312.                 'hit top wall
  313.                 ballDirection = downAndRight
  314.             END IF
  315.         CASE theBrick
  316.             IF mute = -1 THEN PLAY hitBrickSound
  317.             leftSideOfBrick = findBrickLeft(ball.x + 1)
  318.             vertOfBrick = findBrickVertical(ball.y - 1)
  319.             CALL PrintBrick(leftSideOfBrick, vertOfBrick, FALSE)
  320.             ballDirection = downAndRight
  321.     END SELECT
  322.  
  323. SUB MoveUpAndLeft
  324.     SELECT CASE Report(ball.x - 1, ball.y - 1)
  325.         CASE theBackground
  326.             ball.x = ball.x - 1
  327.             ball.y = ball.y - 1
  328.         CASE theWall
  329.             IF mute = -1 THEN PLAY hitWallSound
  330.             IF ball.x - 1 = 1 THEN
  331.                 ballDirection = upAndRight
  332.             ELSEIF ball.y - 1 = 1 THEN 'hit the top
  333.                 ballDirection = downAndLeft
  334.             END IF
  335.         CASE theBrick
  336.             IF mute = -1 THEN PLAY hitBrickSound
  337.             leftSideOfBrick = findBrickLeft(ball.x - 1)
  338.             vertOfBrick = findBrickVertical(ball.y - 1)
  339.             CALL PrintBrick(leftSideOfBrick, vertOfBrick, FALSE)
  340.             ballDirection = downAndLeft
  341.     END SELECT
  342.  
  343. SUB MoveDownAndRight
  344.     SELECT CASE Report(ball.x + 1, ball.y + 1)
  345.         CASE theBackground
  346.             IF ball.y + 1 = 48 THEN
  347.                 IF debugMode = FALSE THEN
  348.                     manDied = TRUE
  349.                     score = score - 50
  350.                     spaceToStart = FALSE
  351.                     CALL PrintPaddle(FALSE)
  352.                     CALL PrintBall(FALSE)
  353.                     ball.x = 40: ball.y = 47
  354.                     CALL PrintBall(TRUE)
  355.                     paddleLeft = 36
  356.                     CALL PrintPaddle(TRUE)
  357.                     ballDirection = INT(RND * 2) + 1
  358.                     IF mute = -1 THEN PLAY deathSound
  359.                     numberOfGuys = numberOfGuys - 1
  360.                     CALL PrintBoard
  361.                 ELSE
  362.                     IF mute = -1 THEN PLAY hitWallSound
  363.                     ballDirection = upAndRight
  364.                     ball.x = ball.x + 1
  365.                     ball.y = ball.y - 1
  366.                 END IF
  367.             ELSE
  368.                 ball.x = ball.x + 1
  369.                 ball.y = ball.y + 1
  370.             END IF
  371.         CASE theWall
  372.             IF mute = -1 THEN PLAY hitWallSound
  373.             IF ball.x + 1 = 80 THEN ballDirection = downAndLeft
  374.         CASE theBrick
  375.             IF mute = -1 THEN PLAY hitBrickSound
  376.             leftSideOfBrick = findBrickLeft(ball.x + 1)
  377.             vertOfBrick = findBrickVertical(ball.y + 1)
  378.             CALL PrintBrick(leftSideOfBrick, vertOfBrick, FALSE)
  379.             ballDirection = upAndRight
  380.         CASE thePaddle
  381.             IF mute = -1 THEN PLAY hitPaddleSound
  382.             ballDirection = upAndRight
  383.     END SELECT
  384.  
  385. SUB MoveDownAndLeft
  386.     SELECT CASE Report(ball.x - 1, ball.y + 1)
  387.         CASE theBackground
  388.             IF ball.y + 1 = 48 THEN
  389.                 IF debugMode = FALSE THEN
  390.                     manDied = TRUE
  391.                     score = score - 50
  392.                     spaceToStart = FALSE
  393.                     CALL PrintPaddle(FALSE)
  394.                     CALL PrintBall(FALSE)
  395.                     ball.x = 40: ball.y = 47
  396.                     CALL PrintBall(TRUE)
  397.                     paddleLeft = 36
  398.                     CALL PrintPaddle(TRUE)
  399.                     ballDirection = INT(RND * 2) + 1
  400.                     IF mute = -1 THEN PLAY deathSound
  401.                     numberOfGuys = numberOfGuys - 1
  402.                     CALL PrintBoard
  403.                 ELSE
  404.                     IF mute = -1 THEN PLAY hitWallSound
  405.                     ballDirection = upAndLeft
  406.                     ball.x = ball.x - 1
  407.                     ball.y = ball.y - 1
  408.                 END IF
  409.             ELSE
  410.                 ball.x = ball.x - 1
  411.                 ball.y = ball.y + 1
  412.             END IF
  413.         CASE theWall
  414.             IF mute = -1 THEN PLAY hitWallSound
  415.             IF ball.x - 1 = 1 THEN ballDirection = downAndRight
  416.         CASE theBrick
  417.             IF mute = -1 THEN PLAY hitBrickSound
  418.             leftSideOfBrick = findBrickLeft(ball.x - 1)
  419.             vertOfBrick = findBrickVertical(ball.y + 1)
  420.             CALL PrintBrick(leftSideOfBrick, vertOfBrick, FALSE)
  421.             ballDirection = upAndLeft
  422.         CASE thePaddle
  423.             IF mute = -1 THEN PLAY hitPaddleSound
  424.             ballDirection = upAndLeft
  425.     END SELECT
  426.  
  427. FUNCTION findBrickLeft (x)
  428.     rtn = 0
  429.     FOR cnt = 2 TO 79 STEP 6
  430.         IF cnt <= x AND x <= cnt + 5 THEN rtn = cnt
  431.     NEXT cnt
  432.     findBrickLeft = rtn
  433.  
  434. FUNCTION findBrickVertical (y)
  435.     rtn = 0
  436.     FOR cnt = 2 TO 47
  437.         IF cnt = y THEN rtn = cnt
  438.     NEXT
  439.     findBrickVertical = rtn
  440.  
  441. SUB PrintBall (onOrOff)
  442.     IF onOrOff = TRUE THEN
  443.         useColor = 11
  444.     ELSEIF onOrOff = FALSE THEN
  445.         useColor = 1
  446.     END IF
  447.     COLOR useColor, 1: LOCATE ball.y, ball.x: PRINT CHR$(254)
  448.     CALL PlaceChar(ball.x, ball.y, 254, useColor)
  449.  
  450. SUB PrintPaddle (onOrOff)
  451.     IF onOrOff = TRUE THEN
  452.         theColor = 15
  453.     ELSEIF onOrOf = FALSE THEN
  454.         theColor = 1
  455.     ELSE
  456.         COLOR 14, 1: LOCATE 40, 10: PRINT "error inside if block in PrintPaddle": CALL P: END
  457.     END IF
  458.     IF paddleLeft < 2 THEN paddleLeft = 2
  459.     IF paddleLeft > 72 THEN paddleLeft = 72
  460.     FOR across = 0 TO 7
  461.         LOCATE 48, INT(paddleLeft) + across: COLOR theColor: PRINT CHR$(219)
  462.         CALL PlaceChar(INT(paddleLeft) + across, 48, 219, theColor)
  463.     NEXT across
  464.  
  465. SUB PlaceChar (X, Y, char, clr)
  466.     board(X, Y, 1) = char
  467.     board(X, Y, 2) = clr
  468. SUB PrintBoard
  469.     FOR y = 2 TO 48: FOR x = 2 TO 79
  470.             this$ = CHR$(board(x, y, 1))
  471.             theColor = board(x, y, 2)
  472.             COLOR theColor, 1
  473.             IF theColor = 11 THEN
  474.                 LOCATE y, x
  475.                 PRINT CHR$(254)
  476.             ELSE 'IF theColor <> 1 THEN
  477.                 LOCATE y, x
  478.                 PRINT CHR$(219)
  479.             END IF
  480.     NEXT x: NEXT y
  481.     LOCATE 1, 3: COLOR 10, 12: PRINT "Men: " + S$(numberOfGuys);
  482.     PRINT "                    Bricks remaining: " + S$(numberOfBricks) + "     ";
  483.     PRINT "               Score: " + S$(score) + "  "
  484.  
  485. SUB CreationMain
  486.     penUp = TRUE
  487.     eraserOn = FALSE
  488.     currentColor = 0
  489.     brick.x = 2
  490.     brick.y = 2
  491.     FOR countX = 2 TO 79
  492.         FOR countY = 2 TO 46
  493.             creationBoard(countX, countY) = 1
  494.     NEXT: NEXT
  495.     CALL PrintBackdrop
  496.     CALL PrintCreationBoard
  497.     CALL PrintCreationBrick
  498.     printOutCurrentBoard = FALSE
  499.     DO
  500.         whichCommand$ = INKEY$
  501.         SELECT CASE whichCommand$
  502.             CASE "P", "p"
  503.                 IF penUp = TRUE THEN
  504.                     penUp = FALSE
  505.                     CALL PutBrickOnBoard
  506.                 ELSEIF penUp = FALSE AND eraserOn = FALSE THEN
  507.                     penUp = TRUE
  508.                 END IF
  509.                 CALL PrintBackdrop
  510.                 printOutCurrentBoard = TRUE
  511.             CASE "E", "e"
  512.                 IF eraserOn = FALSE THEN
  513.                     eraserOn = TRUE
  514.                     currentColor = 1
  515.                     penUp = FALSE
  516.                     CALL PutBrickOnBoard
  517.                 ELSEIF eraserOn = TRUE THEN
  518.                     eraserOn = FALSE
  519.                     currentColor = 0
  520.                     penUp = TRUE
  521.                 END IF
  522.                 printOutCurrentBoard = TRUE
  523.             CASE "C", "c"
  524.                 IF currentColor = 0 THEN
  525.                     currentColor = 2
  526.                 ELSEIF currentColor = 10 THEN
  527.                     currentColor = 13
  528.                 ELSEIF currentColor < 14 THEN
  529.                     currentColor = currentColor + 1
  530.                 ELSEIF currentColor = 14 THEN
  531.                     currentColor = 0
  532.                 END IF
  533.                 printOutCurrentBoard = TRUE
  534.                 IF penUp = FALSE THEN CALL PutBrickOnBoard
  535.             CASE "A", "a"
  536.                 penUp = TRUE
  537.                 eraserOn = FALSE
  538.                 currentColor = 0
  539.                 FOR xCount = 2 TO 79
  540.                     FOR yCount = 2 TO 46
  541.                         creationBoard(xCount, yCount) = 1
  542.                 NEXT: NEXT
  543.                 printOutCurrentBoard = TRUE
  544.             CASE "s", "S"
  545.                 CALL SaveBoard
  546.             CASE CHR$(0) + "H" ' up
  547.                 IF brick.y > 2 THEN
  548.                     brick.y = brick.y - 1
  549.                     IF penUp = FALSE THEN CALL PutBrickOnBoard
  550.                     printOutCurrentBoard = TRUE
  551.                 END IF
  552.             CASE CHR$(0) + "K" ' left
  553.                 IF brick.x > 7 THEN
  554.                     brick.x = brick.x - 6
  555.                     IF penUp = FALSE THEN CALL PutBrickOnBoard
  556.                     printOutCurrentBoard = TRUE
  557.                 END IF
  558.             CASE CHR$(0) + "P" ' down
  559.                 IF brick.y < 47 THEN
  560.                     brick.y = brick.y + 1
  561.                     IF penUp = FALSE THEN CALL PutBrickOnBoard
  562.                     printOutCurrentBoard = TRUE
  563.                 END IF
  564.             CASE CHR$(0) + "M" ' right
  565.                 IF brick.x < 73 THEN
  566.                     brick.x = brick.x + 6
  567.                     IF penUp = FALSE THEN CALL PutBrickOnBoard
  568.                     printOutCurrentBoard = TRUE
  569.                 END IF
  570.             CASE CHR$(27)
  571.                 EXIT DO
  572.         END SELECT
  573.         IF printOutCurrentBoard = TRUE THEN
  574.             CALL PrintBackdrop
  575.             CALL PrintCreationBoard
  576.             CALL PrintCreationBrick
  577.             printOutCurrentBoard = FALSE
  578.         END IF
  579.     LOOP
  580.  
  581. SUB SaveBoard
  582.     COLOR 10, 0
  583.     CLS
  584.     highestLevel = 0
  585.     DO
  586.         highestLevel = highestLevel + 1
  587.         filename$ = "Level " + LTRIM$(STR$(highestLevel))
  588.     LOOP UNTIL _FILEEXISTS(filename$) = 0
  589.     LOCATE 10, 10
  590.     PRINT "The game board has been saved as " + CHR$(34) + "Level " + LTRIM$(STR$(highestLevel)) + CHR$(34)
  591.     PRINT
  592.     PRINT "It will automatically be loaded after Level " + S$(highestLevel - 1) + " is completed."
  593.     PRINT
  594.     OPEN filename$ FOR OUTPUT AS #1
  595.     FOR x = 2 TO 79
  596.         FOR y = 2 TO 46
  597.             COLOR 10, 0: LOCATE 15, 15: PRINT "Working"
  598.             stringOut$ = LTRIM$(STR$(creationBoard(x, y)))
  599.             IF LEN(stringOut$) = 1 THEN stringOut$ = " " + stringOut$
  600.             PRINT #1, stringOut$
  601.             COLOR 0, 0: LOCATE 15, 15: PRINT "Working"
  602.     NEXT: NEXT
  603.     CLOSE #1
  604.     COLOR 10, 0
  605.  
  606.     a$ = "Push " + CHR$(34) + "A" + CHR$(34) + " to make (a)nother level"
  607.     LOCATE 30, (80 - LEN(a$)) / 2
  608.     PRINT a$
  609.     pause$ = INPUT$(1)
  610.     IF UCASE$(pause$) = "A" THEN
  611.         CALL CreationMain
  612.     ELSE
  613.         END
  614.     END IF
  615.  
  616. SUB PutBrickOnBoard
  617.     FOR counting = 0 TO 5
  618.         creationBoard(brick.x + counting, brick.y) = currentColor
  619.     NEXT counting
  620.  
  621. SUB PrintCreationBrick
  622.     IF penUp = TRUE AND eraserOn = FALSE THEN
  623.         LOCATE brick.y, brick.x
  624.         COLOR 15, 1
  625.         PRINT CHR$(219)
  626.     ELSEIF penUp = FALSE AND eraserOn = TRUE THEN
  627.         LOCATE brick.y, brick.x
  628.         COLOR 7, 1
  629.         PRINT "-"
  630.     ELSE
  631.         LOCATE brick.y, brick.x
  632.         COLOR currentColor, 1
  633.         PRINT CHR$(219)
  634.     END IF
  635.     FOR count = 1 TO 5
  636.         LOCATE brick.y, brick.x + count
  637.         IF eraserOn = TRUE THEN
  638.             COLOR 7, 1
  639.             PRINT "-"
  640.         ELSE
  641.             COLOR currentColor, 1
  642.             PRINT CHR$(219)
  643.         END IF
  644.     NEXT
  645.  
  646.  
  647. SUB PrintCreationBoard
  648.     CALL PrintBackdrop
  649.     FOR y = 2 TO 46
  650.         FOR x = 2 TO 79
  651.             COLOR creationBoard(x, y), 1
  652.             LOCATE y, x
  653.             PRINT CHR$(219)
  654.     NEXT: NEXT
  655.  
  656. SUB PrintBackdrop
  657.     COLOR 1, 1: CLS
  658.     COLOR 12, 1
  659.     FOR x = 1 TO 80
  660.         LOCATE 1, x: PRINT CHR$(219)
  661.     NEXT
  662.     FOR y = 1 TO 48
  663.         LOCATE y, 1: PRINT CHR$(219)
  664.         LOCATE y, 80: PRINT CHR$(219)
  665.     NEXT
  666.     COLOR 14, 1
  667.     LOCATE 47, 3
  668.     PRINT "Use the Arrow Keys To Move      P - Pen ";
  669.     IF penUp = TRUE THEN
  670.         COLOR 10, 1
  671.         PRINT "up";
  672.         COLOR 14, 1
  673.         PRINT "/";
  674.         COLOR 7, 1
  675.         PRINT "down";
  676.     ELSE
  677.         COLOR 7, 1
  678.         PRINT "up";
  679.         COLOR 14, 1
  680.         PRINT "/";
  681.         COLOR 10, 1
  682.         PRINT "down";
  683.     END IF
  684.     PRINT "       ";
  685.     IF currentColor = 0 THEN
  686.         COLOR 0, 7
  687.     ELSE
  688.         COLOR currentColor, 0
  689.     END IF
  690.     PRINT "C - Cycle colors"
  691.     LOCATE 48, 2
  692.     COLOR 14, 1: PRINT "E - Eraser ";
  693.     'LOCATE 20, 20: COLOR 15, 0: PRINT "eraserOn = " + S$(eraserOn): CALL P
  694.     '    LOCATE 48, 26
  695.     IF eraserOn = TRUE THEN
  696.         COLOR 10, 1
  697.         PRINT "On";
  698.         COLOR 14, 1: PRINT "/";
  699.         COLOR 7, 1
  700.         PRINT "Off";
  701.     ELSE
  702.         COLOR 7, 1
  703.         PRINT "On";
  704.         COLOR 14, 1
  705.         PRINT "/";
  706.         COLOR 10, 1
  707.         PRINT "Off";
  708.     END IF
  709.     COLOR 14, 1
  710.     PRINT "     A - Clear All";
  711.     PRINT "     S - Save     [ESC] - End Without Save"
  712.     CALL PrintCreationBrick
  713.     '    LOCATE 45, 10: COLOR 10, 1: PRINT "eraserOn = " + S$(eraserOn)
  714.  
  715.  
  716. SUB P
  717.     pause$ = INPUT$(1)
  718.     IF pause$ = CHR$(27) THEN END
  719.  
  720. FUNCTION S$ (number)
  721.     S$ = LTRIM$(STR$(number))

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
I have just copies the game and began "checking it out". One minor visual problem. Does not effect the play but, to me anyway, it was noticeable. The right-hand border did not appear.

Within the 'PrintLevel' subroutine, 'print left, right...', within the 'for y' loop, 'LOCATE 7, 80' should be "LOCATE y, 80". As I said, it is only minor, and does not effect the game play.

I have not finished playing so I have not, as yet,  come across the difficulty that you have stated. I like this version. Simple layout (default game) and "easy on the eye". A very good effort.

J
Logic is the beginning of wisdom.

Marked as best answer by Jaze on July 04, 2021, 08:24:33 am

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi Jaze
fine implementation of Bricks!
Also Johnno56 have told you about the right border glitch and how to fix it
1. interesting the customized delay routine
2. main out of main interesting
3. I cannot reproduce your experience (repeat twice the first level and then go on) but I got code stucking after finished the first level.. in debugging mode I find that CountBrick leads the level up together to lastLevelCreated.
But also if I MOD a Jump command I cannot the next level....
IMHO it is related to
Code: QB64: [Select]
  1. If lastlevelcreated <>0 then
in which you don't manage
Code: QB64: [Select]
  1.  filename$= level+str$(lastlevelcreated)
  2. Open filename$ for input as #1
if this operation works or not... with my debug code it seems not to work both if  I set currentlevel both if I set currentlevel and lastlevelcreated.

Good Luck
Here your code with my debug
Code: QB64: [Select]
  1. Width 80, 50
  2.  
  3. Type coordinate
  4.     x As Integer
  5.     y As Integer
  6.  
  7. '_FullScreen
  8.  
  9. GoTo beginning
  10. crap:
  11. Print "Error, error line"
  12.  
  13. beginning:
  14. Const TRUE = 1
  15. Const FALSE = 0
  16. Const leftDirection = 19200 '   to make the code more readable with the left and right arrowkeys
  17. Const rightDirection = 19712 '
  18. Const upAndLeft = 1 'the ball always moves in one diagonal direction
  19. Const upAndRight = 2
  20. Const downAndLeft = 3
  21. Const downAndRight = 4
  22. Const theBackground = 0 '           to determine what the ball hit
  23. Const theBrick = 1
  24. Const theWall = 2
  25. Const thePaddle = 3
  26. Const hitWallSound$ = "O2L30G"
  27. Const hitPaddleSound$ = "O3L30C"
  28. Const hitBrickSound$ = "O3L30G"
  29. Const deathSound$ = "O4L15CO3CO2CO1L10C"
  30. Const successBeep = "L9O2DL15DL9DL4F"
  31.  
  32. Dim Shared creationBoard(2 To 79, 2 To 46) As Integer
  33. For initializeArrayX = 2 To 79
  34.     For initializeArrayY = 2 To 46
  35.         creationBoard(initializeArrayX, initializeArrayY) = 1
  36. Dim Shared brick As coordinate: brick.x = 2: brick.y = 2
  37. Dim Shared ball As coordinate
  38. Dim Shared ballDirection As Integer
  39. Dim Shared paddleLeft As Single
  40. Dim Shared board(1 To 80, 1 To 48, 1 To 2) As Integer
  41. Dim Shared numberOfBricks As Integer
  42. Dim Shared currentLevel As Integer: currentLevel = 1
  43. Dim Shared numberOfGuys As Integer: numberOfGuys = 5
  44. Dim Shared score As Integer: scorte = 0
  45. Dim Shared manDied As Integer: manDied = FALSE
  46. Dim Shared spaceToStart: spaceToStart = FALSE
  47. Dim Shared debugMode As Integer: debugMode = FALSE
  48. Dim Shared penUp, currentColor, eraserOn As Integer: penUp = TRUE: currentColor = 0: eraserOn = FALSE
  49. Dim Shared mute As Integer: mute = -1 ' -1 mute is off (sound on)
  50. Dim Shared lastLevelCreated As Integer: lastLevelCreated = 0
  51. Dim levelChecking As Integer: levelChecking = 0
  52.     levelChecking = levelChecking + 1
  53. Loop Until _FileExists("Level " + LTrim$(Str$(levelChecking))) = 0
  54. lastLevelCreated = levelChecking - 1
  55.  
  56. Width 80, 50
  57.  
  58. Call Menu
  59.  
  60. Sub Menu
  61.     printTheMenu = TRUE
  62.     Do
  63.         If printTheMenu = TRUE Then
  64.             Color 14, 1
  65.             Cls
  66.             Locate 20, 1
  67.             Print CenterText$("Menu")
  68.             Print CenterText$("------")
  69.             Print
  70.             Print CenterText$("1.) Instructions")
  71.             Print
  72.             Print CenterText$("2.) Create A Level")
  73.             Print
  74.             Print CenterText$("3.) Play The Game")
  75.             Print
  76.             Print CenterText$("4.) Exit")
  77.             printTheMenu = FALSE
  78.         End If
  79.         menuCommand$ = InKey$
  80.         Select Case menuCommand$
  81.             Case "1"
  82.                 Call Instructions
  83.                 printTheMenu = TRUE
  84.             Case "2"
  85.                 Call CreationMain
  86.                 printTheMenu = TRUE
  87.             Case "3"
  88.                 Call Main
  89.                 printTheMenu = TRUE
  90.         End Select
  91.     Loop Until menuCommand$ = "4"
  92.     End
  93.  
  94. Sub Instructions
  95.     Color 10, 1
  96.     Cls
  97.     Locate 2
  98.     Print CenterText$("Level Creator")
  99.     Print CenterText$("---------------")
  100.     Print
  101.     Print CenterText$("The available commands are listed at the bottom of the screen.")
  102.     Print CenterText$("When the pen is up, the cursor moves without adding to the level")
  103.     Print CenterText$("being created. When the pen is down the current brick is added")
  104.     Print CenterText$("to the level being created. The other commands are self explanatory.")
  105.     Color 12, 1
  106.     Print
  107.     Print CenterText$("When the created level is saved, it is saved under " + Chr$(34) + "Level #" + Chr$(34) + ".")
  108.     Print CenterText$("If no levels have been created, the default level is used.")
  109.     Print CenterText$("If you progress beyond all levels created, the last level created repeats.")
  110.     Print
  111.     Print
  112.     Print
  113.     Color 14, 1
  114.     Print CenterText$("Game")
  115.     Print CenterText$("------")
  116.     Print
  117.     Print CenterText$("The object of the game is to progress to the next level by")
  118.     Print CenterText$("destroying all of the bricks. To do this you must position the")
  119.     Print
  120.     Color 15, 1
  121.     Print CenterText$(Chr$(219) + Chr$(219) + Chr$(219) + Chr$(219) + Chr$(219) + Chr$(219) + Chr$(219) + Chr$(219))
  122.     Print
  123.     Color 14, 1
  124.     Print CenterText$("under the square so that it is reflected toward the bricks using.")
  125.     Print CenterText$("the left and right arrow keys.")
  126.     Locate 29, 21
  127.     Print "Push ";: Color 13, 1: Print "[SPACEBAR]";: Color 14, 1: Print " to set the square in motion."
  128.     Print: Print
  129.     Color 13, 1: Print CenterText$("Available Commands:"): Color 14, 1: Print
  130.     Locate , 10: Print "+ to increase the game speed": Print
  131.     Locate , 10: Print "- to decrease the game speed": Print
  132.     Locate , 10: Print "P to pause and wait for any key to be pressed": Print
  133.     Locate , 10: Print "M to toggle mute on and off": Print
  134.     Locate , 10: Print "[ESC] to return to the main menu"
  135.     Print
  136.     Print CenterText$("Push C to turn on debug mode and O to turn it off.")
  137.     Locate 48
  138.     Color 15, 1
  139.     Print CenterText$("Push any key to continue.")
  140.  
  141.  
  142.     Call P
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. Function CenterText$ (thisString$)
  150.  
  151.     returnString$ = Space$(Int((80 - Len(thisString$)) / 2)) + thisString$
  152.     CenterText = returnString$
  153.  
  154. Sub PrintBrick (x, y, onOrOff)
  155.     If onOrOff = TRUE Then
  156.         useColor = board(x, y, 2) 'print the brick instead of erasing it, pick up the color there
  157.     ElseIf onOrOff = FALSE Then
  158.         useColor = 1
  159.     End If
  160.     For across = 0 To 5
  161.         Locate y, x + across
  162.         Color useColor
  163.         Print Chr$(219)
  164.         board(x + across, y, 1) = 219
  165.         board(x + across, y, 2) = useColor
  166.     Next across
  167.     If onOrOff = FALSE Then
  168.         numberOfBricks = numberOfBricks - 1
  169.         score = score + 5
  170.     End If
  171.  
  172. Sub PrintLevel
  173.     For x = 1 To 80: For y = 1 To 48 'make the board blue
  174.             board(x, y, 1) = 219 '    ASCII code of board space
  175.             board(x, y, 2) = 1 '      color of board space
  176.     Next y: Next x
  177.     Color 1, 1: Cls
  178.     'print left, right and top borders
  179.     For y = 1 To 48
  180.         Locate y, 1: Color 12, 1: Print Chr$(219) 'print the left border to the screen
  181.         Locate y, 80: Color 12, 1: Print Chr$(219) 'print the right border to the screen
  182.         Call PlaceChar(1, y, 219, 12) ' put the position, ASCII code and color on the board to make the left border
  183.         Call PlaceChar(80, y, 219, 12) ' same to make the left border on the board
  184.     Next y
  185.     For x = 1 To 80
  186.         Locate 1, x: Color 12, 1: Print Chr$(219) 'print the top border to the screen
  187.         Call PlaceChar(x, 1, 219, 12) ' put the top border on the board
  188.     Next x
  189.     paddleLeft = 36
  190.     Call PrintPaddle(TRUE) ' put the paddle on the board with the left at x coordinate 36
  191.     ball.x = 40: ball.y = 47 ' position the ball/moving square
  192.     Call PrintBall(TRUE) ' put the ball on the board
  193.     ballDirection = Int(Rnd * 2) + 1 'randomize the starting direction of the ball as up and left or up and right
  194.  
  195.     If lastLevelCreated <> 0 Then
  196.         filename$ = "Level " + S$(currentLevel)
  197.         If _FileExists(filename$) = 0 Then filename$ = "Level " + S$(lastLevelCreated)
  198.         ' If _FileExists(filename$) = 0 Then Print " no level file found": End ' <---- DEBUG
  199.         Open filename$ For Input As #1 'open the file with the level information
  200.         For xCounting = 2 To 79 ' x coordinate of the board
  201.             For yCounting = 2 To 46 ' y coordinate of the board
  202.                 Input #1, boardColor$ ' get the color of the board at xCounting, yCounting
  203.                 board(xCounting, yCounting, 2) = Val(boardColor$) 'color the board at xCounting, yCounting
  204.         Next: Next
  205.         '        LOCATE 20, 20: COLOR 15, 0: PRINT "Filename: " + filename$: CALL P
  206.         Close #1
  207.     Else ' if no levels were created
  208.         For x = 2 To 79
  209.             Call PlaceChar(x, 2, 219, 14) 'put a yellow (14) block (219) at x, y on the board
  210.             Call PlaceChar(x, 4, 219, 14)
  211.             Call PlaceChar(x, 6, 219, 14)
  212.         Next x
  213.         lastLevelCreated = 1 'DEBUG CODE
  214.     End If
  215.     numberOfBricks = 0 ' after a level has been put on the board, count the number of bricks on it
  216.     For brickCountingY = 2 To 47
  217.         For brickCountingX = 2 To 79 Step 6 'each brick is 6 blocks long
  218.             If board(brickCountingX, brickCountingY, 2) <> 1 Then numberOfBricks = numberOfBricks + 1
  219.     Next: Next
  220.     Call PrintBoard 'print the board to the screen
  221.     spaceToStart = FALSE 'spacebar has yet to be puched to start the current level
  222.  
  223. Sub Main
  224.  
  225.     Call PrintLevel 'put the current level on the board and print it out
  226.     printOutBoard = FALSE ' to avoid flicker of the board being constantly printed, the need to print it is false
  227.     delaySpeed = 0.029 'delay to use inside the main DO-LOOP
  228.     Do
  229.         Locate 48, 1: Print "Level"; currentLevel; ' DEBUG OUTPUT
  230.         debugging$ = InKey$ 'using strings instead of _KEYDOWN(code) is easier. i initially added INKEY$ for debugging
  231.         If spaceToStart = TRUE Then ' the spacebar has been hit to start the game
  232.             Call PrintBall(FALSE) 'remove the ball from the board
  233.             Call MoveBall 'move the ball in the current direction
  234.             Call PrintBall(TRUE) 'put the ball on the board at the new position
  235.             If _KeyDown(leftDirection) = -1 Then 'the left arrow key had been pushed
  236.                 Call PrintPaddle(FALSE) ' remove the paddle from the board
  237.                 paddleLeft = paddleLeft - 1.5 ' move the paddle leftward
  238.                 Call PrintPaddle(TRUE) ' put the paddle on the board at the new position
  239.                 printOutBoard = TRUE 'it is now needed to print the board to the screen
  240.             ElseIf _KeyDown(rightDirection) = -1 Then
  241.                 Call PrintPaddle(FALSE)
  242.                 paddleLeft = paddleLeft + 1.5
  243.                 Call PrintPaddle(TRUE)
  244.                 printOutBoard = TRUE
  245.             End If
  246.             If printOutBoard = TRUE Then 'print the board to the screen
  247.                 Call PrintBoard ' print the board to the screen
  248.                 printOutBoard = FALSE 'stop the board from being printed now
  249.             End If
  250.             If numberOfBricks = 0 Then 'there are no bricks left on the board
  251.                 ' lastLevelCreated = currentLevel  ' DEBUG SETTING
  252.                 currentLevel = currentLevel + 1 'increment the current level
  253.                 Call PrintLevel 'put the newest level on the board
  254.                 spaceToStart = FALSE 'the spacebar has not been pressed since the new level has been output
  255.                 score = score + 200 '200 points for finishing a level
  256.                 If mute = -1 Then Play successBeep '1 is mute, -1 is note muted
  257.             End If
  258.         End If
  259.         If debugging$ = "C" Then debugMode = TRUE
  260.         If debugging$ = "O" Then debugMode = FALSE
  261.         If _KeyDown(32) = -1 Then ' the spacebar has been pushed
  262.             spaceToStart = TRUE ' the spacebar has been pushed
  263.             printOutBoard = TRUE ' it is needed to print the board to the screen
  264.         End If
  265.         _Delay (delaySpeed) ' insert a delay to make the movement playable and visible
  266.         If numberOfGuys = 0 Then Call EndOfGame
  267.         If score Mod 2000 = 0 And score <> 0 Then numberOfGuys = numberOfGuys + 1 ' new guy every 2,000 points
  268.         If debugging$ = "+" Then 'increase the game speed
  269.             delaySpeed = delaySpeed - 0.01
  270.             If delaySpeed < 0 Then delaySpeed = 0 'don't allow a negative delay
  271.         End If
  272.         If debugging$ = "-" Then delaySpeed = delaySpeed + 0.005 'decrease the game speed
  273.         If debugging$ = "m" Or debugging$ = "M" Then mute = mute * -1 'turn mute on or off
  274.         If debugging$ = "p" Or debugging$ = "P" Then pause$ = Input$(1) 'pause the game
  275.         If debugging$ = Chr$(27) Then Exit Do 'end the game and return to the main menu
  276.         ' DEBUG CODE TO JUMP LEVEL
  277.         If debugging$ = "j" Then
  278.             numberOfBricks = 0 ' jump to the next level
  279.             '            lastLevelCreated = lastelevelcreated + 1
  280.         End If
  281.         ' END DEBUG CODE
  282.     Loop
  283.  
  284. Sub EndOfGame
  285.  
  286. Sub MoveBall
  287.     Select Case ballDirection
  288.         Case upAndRight
  289.             Call MoveUpAndRight
  290.         Case upAndLeft
  291.             Call MoveUpAndLeft
  292.         Case downAndRight
  293.             Call MoveDownAndRight
  294.         Case downAndLeft
  295.             Call MoveDownAndLeft
  296.     End Select
  297.  
  298. Function Report (x, y)
  299.     'if 12 then border, if 1 then background, if 15 then paddle, else brick
  300.     rtn = -1
  301.     If board(x, y, 2) = 12 Then
  302.         rtn = theWall
  303.     ElseIf board(x, y, 2) = 1 Then
  304.         rtn = theBackground
  305.     ElseIf board(x, y, 2) = 15 Then
  306.         rtn = thePaddle
  307.     Else
  308.         rtn = theBrick
  309.     End If
  310.     Report = rtn
  311.  
  312. Sub MoveUpAndRight
  313.     Select Case Report(ball.x + 1, ball.y - 1)
  314.         Case theBackground
  315.             ball.x = ball.x + 1
  316.             ball.y = ball.y - 1
  317.         Case theWall
  318.             If mute = -1 Then Play hitWallSound
  319.             If ball.x + 1 = 80 Then
  320.                 'hit the right wall
  321.                 ballDirection = upAndLeft
  322.             ElseIf ball.y - 1 = 1 Then
  323.                 'hit top wall
  324.                 ballDirection = downAndRight
  325.             End If
  326.         Case theBrick
  327.             If mute = -1 Then Play hitBrickSound
  328.             leftSideOfBrick = findBrickLeft(ball.x + 1)
  329.             vertOfBrick = findBrickVertical(ball.y - 1)
  330.             Call PrintBrick(leftSideOfBrick, vertOfBrick, FALSE)
  331.             ballDirection = downAndRight
  332.     End Select
  333.  
  334. Sub MoveUpAndLeft
  335.     Select Case Report(ball.x - 1, ball.y - 1)
  336.         Case theBackground
  337.             ball.x = ball.x - 1
  338.             ball.y = ball.y - 1
  339.         Case theWall
  340.             If mute = -1 Then Play hitWallSound
  341.             If ball.x - 1 = 1 Then
  342.                 ballDirection = upAndRight
  343.             ElseIf ball.y - 1 = 1 Then 'hit the top
  344.                 ballDirection = downAndLeft
  345.             End If
  346.         Case theBrick
  347.             If mute = -1 Then Play hitBrickSound
  348.             leftSideOfBrick = findBrickLeft(ball.x - 1)
  349.             vertOfBrick = findBrickVertical(ball.y - 1)
  350.             Call PrintBrick(leftSideOfBrick, vertOfBrick, FALSE)
  351.             ballDirection = downAndLeft
  352.     End Select
  353.  
  354. Sub MoveDownAndRight
  355.     Select Case Report(ball.x + 1, ball.y + 1)
  356.         Case theBackground
  357.             If ball.y + 1 = 48 Then
  358.                 If debugMode = FALSE Then
  359.                     manDied = TRUE
  360.                     score = score - 50
  361.                     spaceToStart = FALSE
  362.                     Call PrintPaddle(FALSE)
  363.                     Call PrintBall(FALSE)
  364.                     ball.x = 40: ball.y = 47
  365.                     Call PrintBall(TRUE)
  366.                     paddleLeft = 36
  367.                     Call PrintPaddle(TRUE)
  368.                     ballDirection = Int(Rnd * 2) + 1
  369.                     If mute = -1 Then Play deathSound
  370.                     numberOfGuys = numberOfGuys - 1
  371.                     Call PrintBoard
  372.                 Else
  373.                     If mute = -1 Then Play hitWallSound
  374.                     ballDirection = upAndRight
  375.                     ball.x = ball.x + 1
  376.                     ball.y = ball.y - 1
  377.                 End If
  378.             Else
  379.                 ball.x = ball.x + 1
  380.                 ball.y = ball.y + 1
  381.             End If
  382.         Case theWall
  383.             If mute = -1 Then Play hitWallSound
  384.             If ball.x + 1 = 80 Then ballDirection = downAndLeft
  385.         Case theBrick
  386.             If mute = -1 Then Play hitBrickSound
  387.             leftSideOfBrick = findBrickLeft(ball.x + 1)
  388.             vertOfBrick = findBrickVertical(ball.y + 1)
  389.             Call PrintBrick(leftSideOfBrick, vertOfBrick, FALSE)
  390.             ballDirection = upAndRight
  391.         Case thePaddle
  392.             If mute = -1 Then Play hitPaddleSound
  393.             ballDirection = upAndRight
  394.     End Select
  395.  
  396. Sub MoveDownAndLeft
  397.     Select Case Report(ball.x - 1, ball.y + 1)
  398.         Case theBackground
  399.             If ball.y + 1 = 48 Then
  400.                 If debugMode = FALSE Then
  401.                     manDied = TRUE
  402.                     score = score - 50
  403.                     spaceToStart = FALSE
  404.                     Call PrintPaddle(FALSE)
  405.                     Call PrintBall(FALSE)
  406.                     ball.x = 40: ball.y = 47
  407.                     Call PrintBall(TRUE)
  408.                     paddleLeft = 36
  409.                     Call PrintPaddle(TRUE)
  410.                     ballDirection = Int(Rnd * 2) + 1
  411.                     If mute = -1 Then Play deathSound
  412.                     numberOfGuys = numberOfGuys - 1
  413.                     Call PrintBoard
  414.                 Else
  415.                     If mute = -1 Then Play hitWallSound
  416.                     ballDirection = upAndLeft
  417.                     ball.x = ball.x - 1
  418.                     ball.y = ball.y - 1
  419.                 End If
  420.             Else
  421.                 ball.x = ball.x - 1
  422.                 ball.y = ball.y + 1
  423.             End If
  424.         Case theWall
  425.             If mute = -1 Then Play hitWallSound
  426.             If ball.x - 1 = 1 Then ballDirection = downAndRight
  427.         Case theBrick
  428.             If mute = -1 Then Play hitBrickSound
  429.             leftSideOfBrick = findBrickLeft(ball.x - 1)
  430.             vertOfBrick = findBrickVertical(ball.y + 1)
  431.             Call PrintBrick(leftSideOfBrick, vertOfBrick, FALSE)
  432.             ballDirection = upAndLeft
  433.         Case thePaddle
  434.             If mute = -1 Then Play hitPaddleSound
  435.             ballDirection = upAndLeft
  436.     End Select
  437.  
  438. Function findBrickLeft (x)
  439.     rtn = 0
  440.     For cnt = 2 To 79 Step 6
  441.         If cnt <= x And x <= cnt + 5 Then rtn = cnt
  442.     Next cnt
  443.     findBrickLeft = rtn
  444.  
  445. Function findBrickVertical (y)
  446.     rtn = 0
  447.     For cnt = 2 To 47
  448.         If cnt = y Then rtn = cnt
  449.     Next
  450.     findBrickVertical = rtn
  451.  
  452. Sub PrintBall (onOrOff)
  453.     If onOrOff = TRUE Then
  454.         useColor = 11
  455.     ElseIf onOrOff = FALSE Then
  456.         useColor = 1
  457.     End If
  458.     Color useColor, 1: Locate ball.y, ball.x: Print Chr$(254)
  459.     Call PlaceChar(ball.x, ball.y, 254, useColor)
  460.  
  461. Sub PrintPaddle (onOrOff)
  462.     If onOrOff = TRUE Then
  463.         theColor = 15
  464.     ElseIf onOrOf = FALSE Then
  465.         theColor = 1
  466.     Else
  467.         Color 14, 1: Locate 40, 10: Print "error inside if block in PrintPaddle": Call P: End
  468.     End If
  469.     If paddleLeft < 2 Then paddleLeft = 2
  470.     If paddleLeft > 72 Then paddleLeft = 72
  471.     For across = 0 To 7
  472.         Locate 48, Int(paddleLeft) + across: Color theColor: Print Chr$(219)
  473.         Call PlaceChar(Int(paddleLeft) + across, 48, 219, theColor)
  474.     Next across
  475.  
  476. Sub PlaceChar (X, Y, char, clr)
  477.     board(X, Y, 1) = char
  478.     board(X, Y, 2) = clr
  479. Sub PrintBoard
  480.     For y = 2 To 48: For x = 2 To 79
  481.             this$ = Chr$(board(x, y, 1))
  482.             theColor = board(x, y, 2)
  483.             Color theColor, 1
  484.             If theColor = 11 Then
  485.                 Locate y, x
  486.                 Print Chr$(254)
  487.             Else 'IF theColor <> 1 THEN
  488.                 Locate y, x
  489.                 Print Chr$(219)
  490.             End If
  491.     Next x: Next y
  492.     Locate 1, 3: Color 10, 12: Print "Men: " + S$(numberOfGuys);
  493.     Print "                    Bricks remaining: " + S$(numberOfBricks) + "     ";
  494.     Print "               Score: " + S$(score) + "  "
  495.  
  496. Sub CreationMain
  497.     penUp = TRUE
  498.     eraserOn = FALSE
  499.     currentColor = 0
  500.     brick.x = 2
  501.     brick.y = 2
  502.     For countX = 2 To 79
  503.         For countY = 2 To 46
  504.             creationBoard(countX, countY) = 1
  505.     Next: Next
  506.     Call PrintBackdrop
  507.     Call PrintCreationBoard
  508.     Call PrintCreationBrick
  509.     printOutCurrentBoard = FALSE
  510.     Do
  511.         whichCommand$ = InKey$
  512.         Select Case whichCommand$
  513.             Case "P", "p"
  514.                 If penUp = TRUE Then
  515.                     penUp = FALSE
  516.                     Call PutBrickOnBoard
  517.                 ElseIf penUp = FALSE And eraserOn = FALSE Then
  518.                     penUp = TRUE
  519.                 End If
  520.                 Call PrintBackdrop
  521.                 printOutCurrentBoard = TRUE
  522.             Case "E", "e"
  523.                 If eraserOn = FALSE Then
  524.                     eraserOn = TRUE
  525.                     currentColor = 1
  526.                     penUp = FALSE
  527.                     Call PutBrickOnBoard
  528.                 ElseIf eraserOn = TRUE Then
  529.                     eraserOn = FALSE
  530.                     currentColor = 0
  531.                     penUp = TRUE
  532.                 End If
  533.                 printOutCurrentBoard = TRUE
  534.             Case "C", "c"
  535.                 If currentColor = 0 Then
  536.                     currentColor = 2
  537.                 ElseIf currentColor = 10 Then
  538.                     currentColor = 13
  539.                 ElseIf currentColor < 14 Then
  540.                     currentColor = currentColor + 1
  541.                 ElseIf currentColor = 14 Then
  542.                     currentColor = 0
  543.                 End If
  544.                 printOutCurrentBoard = TRUE
  545.                 If penUp = FALSE Then Call PutBrickOnBoard
  546.             Case "A", "a"
  547.                 penUp = TRUE
  548.                 eraserOn = FALSE
  549.                 currentColor = 0
  550.                 For xCount = 2 To 79
  551.                     For yCount = 2 To 46
  552.                         creationBoard(xCount, yCount) = 1
  553.                 Next: Next
  554.                 printOutCurrentBoard = TRUE
  555.             Case "s", "S"
  556.                 Call SaveBoard
  557.             Case Chr$(0) + "H" ' up
  558.                 If brick.y > 2 Then
  559.                     brick.y = brick.y - 1
  560.                     If penUp = FALSE Then Call PutBrickOnBoard
  561.                     printOutCurrentBoard = TRUE
  562.                 End If
  563.             Case Chr$(0) + "K" ' left
  564.                 If brick.x > 7 Then
  565.                     brick.x = brick.x - 6
  566.                     If penUp = FALSE Then Call PutBrickOnBoard
  567.                     printOutCurrentBoard = TRUE
  568.                 End If
  569.             Case Chr$(0) + "P" ' down
  570.                 If brick.y < 47 Then
  571.                     brick.y = brick.y + 1
  572.                     If penUp = FALSE Then Call PutBrickOnBoard
  573.                     printOutCurrentBoard = TRUE
  574.                 End If
  575.             Case Chr$(0) + "M" ' right
  576.                 If brick.x < 73 Then
  577.                     brick.x = brick.x + 6
  578.                     If penUp = FALSE Then Call PutBrickOnBoard
  579.                     printOutCurrentBoard = TRUE
  580.                 End If
  581.             Case Chr$(27)
  582.                 Exit Do
  583.         End Select
  584.         If printOutCurrentBoard = TRUE Then
  585.             Call PrintBackdrop
  586.             Call PrintCreationBoard
  587.             Call PrintCreationBrick
  588.             printOutCurrentBoard = FALSE
  589.         End If
  590.     Loop
  591.  
  592. Sub SaveBoard
  593.     Color 10, 0
  594.     Cls
  595.     highestLevel = 0
  596.     Do
  597.         highestLevel = highestLevel + 1
  598.         filename$ = "Level " + LTrim$(Str$(highestLevel))
  599.     Loop Until _FileExists(filename$) = 0
  600.     Locate 10, 10
  601.     Print "The game board has been saved as " + Chr$(34) + "Level " + LTrim$(Str$(highestLevel)) + Chr$(34)
  602.     Print
  603.     Print "It will automatically be loaded after Level " + S$(highestLevel - 1) + " is completed."
  604.     Print
  605.     Open filename$ For Output As #1
  606.     For x = 2 To 79
  607.         For y = 2 To 46
  608.             Color 10, 0: Locate 15, 15: Print "Working"
  609.             stringOut$ = LTrim$(Str$(creationBoard(x, y)))
  610.             If Len(stringOut$) = 1 Then stringOut$ = " " + stringOut$
  611.             Print #1, stringOut$
  612.             Color 0, 0: Locate 15, 15: Print "Working"
  613.     Next: Next
  614.     Close #1
  615.     Color 10, 0
  616.  
  617.     a$ = "Push " + Chr$(34) + "A" + Chr$(34) + " to make (a)nother level"
  618.     Locate 30, (80 - Len(a$)) / 2
  619.     Print a$
  620.     pause$ = Input$(1)
  621.     If UCase$(pause$) = "A" Then
  622.         Call CreationMain
  623.     Else
  624.         End
  625.     End If
  626.  
  627. Sub PutBrickOnBoard
  628.     For counting = 0 To 5
  629.         creationBoard(brick.x + counting, brick.y) = currentColor
  630.     Next counting
  631.  
  632. Sub PrintCreationBrick
  633.     If penUp = TRUE And eraserOn = FALSE Then
  634.         Locate brick.y, brick.x
  635.         Color 15, 1
  636.         Print Chr$(219)
  637.     ElseIf penUp = FALSE And eraserOn = TRUE Then
  638.         Locate brick.y, brick.x
  639.         Color 7, 1
  640.         Print "-"
  641.     Else
  642.         Locate brick.y, brick.x
  643.         Color currentColor, 1
  644.         Print Chr$(219)
  645.     End If
  646.     For count = 1 To 5
  647.         Locate brick.y, brick.x + count
  648.         If eraserOn = TRUE Then
  649.             Color 7, 1
  650.             Print "-"
  651.         Else
  652.             Color currentColor, 1
  653.             Print Chr$(219)
  654.         End If
  655.     Next
  656.  
  657.  
  658. Sub PrintCreationBoard
  659.     Call PrintBackdrop
  660.     For y = 2 To 46
  661.         For x = 2 To 79
  662.             Color creationBoard(x, y), 1
  663.             Locate y, x
  664.             Print Chr$(219)
  665.     Next: Next
  666.  
  667. Sub PrintBackdrop
  668.     Color 1, 1: Cls
  669.     Color 12, 1
  670.     For x = 1 To 80
  671.         Locate 1, x: Print Chr$(219)
  672.     Next
  673.     For y = 1 To 48
  674.         Locate y, 1: Print Chr$(219)
  675.         Locate y, 80: Print Chr$(219)
  676.     Next
  677.     Color 14, 1
  678.     Locate 47, 3
  679.     Print "Use the Arrow Keys To Move      P - Pen ";
  680.     If penUp = TRUE Then
  681.         Color 10, 1
  682.         Print "up";
  683.         Color 14, 1
  684.         Print "/";
  685.         Color 7, 1
  686.         Print "down";
  687.     Else
  688.         Color 7, 1
  689.         Print "up";
  690.         Color 14, 1
  691.         Print "/";
  692.         Color 10, 1
  693.         Print "down";
  694.     End If
  695.     Print "       ";
  696.     If currentColor = 0 Then
  697.         Color 0, 7
  698.     Else
  699.         Color currentColor, 0
  700.     End If
  701.     Print "C - Cycle colors"
  702.     Locate 48, 2
  703.     Color 14, 1: Print "E - Eraser ";
  704.     'LOCATE 20, 20: COLOR 15, 0: PRINT "eraserOn = " + S$(eraserOn): CALL P
  705.     '    LOCATE 48, 26
  706.     If eraserOn = TRUE Then
  707.         Color 10, 1
  708.         Print "On";
  709.         Color 14, 1: Print "/";
  710.         Color 7, 1
  711.         Print "Off";
  712.     Else
  713.         Color 7, 1
  714.         Print "On";
  715.         Color 14, 1
  716.         Print "/";
  717.         Color 10, 1
  718.         Print "Off";
  719.     End If
  720.     Color 14, 1
  721.     Print "     A - Clear All";
  722.     Print "     S - Save     [ESC] - End Without Save"
  723.     Call PrintCreationBrick
  724.     '    LOCATE 45, 10: COLOR 10, 1: PRINT "eraserOn = " + S$(eraserOn)
  725.  
  726.  
  727. Sub P
  728.     pause$ = Input$(1)
  729.     If pause$ = Chr$(27) Then End
  730.  
  731. Function S$ (number)
  732.     S$ = LTrim$(Str$(number))
  733.  

PS You have duplicated
Code: QB64: [Select]
  1. width 80,50
at start of program.
Programming isn't difficult, only it's  consuming time and coffee

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
While I was typing my response, I was saying the same thing as @TempodiBasic in that I cannot repeat the issue (Running in both the stable and current development release of v1.5) you are having.

I have noticed a pattern to the way the ball bounces around, and ran into an issue where it does not get to all the bricks, no matter how long you play (see screen capture below).

Also, these patterns are predictable, which means that I was able to place the paddle in certain spots and keep the game going.

I would add a random bounce generator and vary the speed of the ball either coming off the paddle or the tiles. If I remember the game from back then, I believe you could impart spin or something similar (like changing the direction of the ball's travels, not quite spin, but mimics it).

All in all, I was transported back in time, and the game has promise.

The three bricks (it says 4, but I wonder if that is an error) I could never reach due to the limited way the ball bounces:
  [ You are not allowed to view this attachment ]  
« Last Edit: July 04, 2021, 09:48:20 am by George McGinn »
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Another method to alter the 'predictable' problem... When a level starts, don't put the paddle and ball in the middle. Randomize the starting position of the paddle. It doesn't need to be much or you can make it as wild as you like. Just remember to place the ball in the middle of the paddle. You could also randomly choose which direction the ball will start moving.
Logic is the beginning of wisdom.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
George McGinn, I have never had the problem where all the bricks do not get destroyed. To test it I put it in debug mode and ramped the speed to max. Nevertheless, putting the paddle in a random starting position is a good idea. To everyone else who looked into the code, thank you. I look forward to finishing the program. I intend to add a falling letter from a brick at random when it is destroyed, For example a falling "E" could expand the length of the paddle while an "S" would shrink it. I've got some other ideas about that too. Not sure how difficult it will be but I do want to get the program completed in its current form without errors before upgrading it. Thanks again.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
TempodiBasic, I added the
Code: QB64: [Select]
  1. lastLevelCreated = 1
and it fixed the problem but I have no idea why. Thanks.