Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RCBasic question: this program does not work.
#1
Hi...I hope that some members can help me to make the program work. Thanks first!!!!!

Code:
CONST boardWidth = 40
CONST boardHeight = 20
DIM snakeX[100]
DIM snakeY[100]
snakeLength = 3
direction = 0  ' 0 = right, 1 = left, 2 = up, 3 = down
' Set initial snake positions (head is segment 1)
snakeX[1] = 20
snakeY[1] = 10
snakeX[2] = 19
snakeY[2] = 10
snakeX[3] = 18
snakeY[3] = 10
' Initialize the randomizer using the correct syntax.
Randomize(Timer)
' Position the food randomly within the boundaries using Rand(n).
foodX = INT( Rand(boardWidth - 2) ) + 2
foodY = INT( Rand(boardHeight - 2) ) + 2
DO
  ' Input handling: Read keystrokes.
  k$ = INKEY$
  IF k$ <> "" THEN
      IF k$ = "w" OR k$ = "W" THEN
        direction = 2    ' Move up.
      ELSEIF k$ = "s" OR k$ = "S" THEN
        direction = 3    ' Move down.
      ELSEIF k$ = "a" OR k$ = "A" THEN
        direction = 1    ' Move left.
      ELSEIF k$ = "d" OR k$ = "D" THEN
        direction = 0    ' Move right.
      ENDIF
  ENDIF
  ' Move snake body: Update segments from tail to head.
  FOR i = snakeLength TO 2 STEP -1
      snakeX[i] = snakeX[i - 1]
      snakeY[i] = snakeY[i - 1]
  NEXT i
  ' Update the snake's head according to the current direction.
  IF direction = 0 THEN
      snakeX[1] = snakeX[1] + 1
  ELSEIF direction = 1 THEN
      snakeX[1] = snakeX[1] - 1
  ELSEIF direction = 2 THEN
      snakeY[1] = snakeY[1] - 1
  ELSEIF direction = 3 THEN
      snakeY[1] = snakeY[1] + 1
  ENDIF
  ' Collision detection: Wall collisions.
  IF snakeX[1] < 1 OR snakeX[1] > boardWidth OR snakeY[1] < 1 OR snakeY[1] > boardHeight THEN
      GOTO GameOver
  ENDIF
  ' Collision detection: Self-collision.
  FOR i = 2 TO snakeLength
      IF snakeX[1] = snakeX[i] AND snakeY[1] = snakeY[i] THEN
        GOTO GameOver
      ENDIF
  NEXT i
  ' Food consumption: Increase snake's length and reposition the food.
  IF snakeX[1] = foodX AND snakeY[1] = foodY THEN
      snakeLength = snakeLength + 1
      snakeX[snakeLength] = snakeX[snakeLength - 1]
      snakeY[snakeLength] = snakeY[snakeLength - 1]
      foodX = INT( Rand(boardWidth - 2) ) + 2
      foodY = INT( Rand(boardHeight - 2) ) + 2
  ENDIF
  ' Render the game:
  CLS
  ' Draw top and bottom boundaries.
  FOR x = 1 TO boardWidth
      LOCATE 1, x
      PRINT "-";
      LOCATE boardHeight, x
      PRINT "-";
  NEXT x
  ' Draw left and right boundaries.
  FOR y = 2 TO boardHeight - 1
      LOCATE y, 1
      PRINT "|";
      LOCATE y, boardWidth
      PRINT "|";
  NEXT y
  ' Draw the snake segments.
  FOR i = 1 TO snakeLength
      LOCATE snakeY[i], snakeX[i]
      PRINT "O";
  NEXT i
  ' Draw the food.
  LOCATE foodY, foodX
  PRINT "*";
  ' Pause to control game speed (adjust delay if needed).
  SLEEP 100
LOOP
GameOver:
  CLS
  PRINT "Game Over! Your Score: "; snakeLength - 3
  END
Reply


Messages In This Thread
RCBasic question: this program does not work. - by luwal - 05-30-2025, 12:52 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)