![]() |
|
RCBasic question: this program does not work. - Printable Version +- NaaLaa (https://www.naalaa.com/forum) +-- Forum: Other (https://www.naalaa.com/forum/forum-8.html) +--- Forum: Programming (https://www.naalaa.com/forum/forum-9.html) +--- Thread: RCBasic question: this program does not work. (/thread-262.html) Pages:
1
2
|
RCBasic question: this program does not work. - luwal - 05-30-2025 Hi...I hope that some members can help me to make the program work. Thanks first!!!!! Code: CONST boardWidth = 40RE: RCBasic question: this program does not work. - johnno56 - 05-30-2025 I am curious as to where you got this version of "Snake"? As it is, it will not run using RCBasic, without some modification. Are you wanting it to run with RCB or N7? RE: RCBasic question: this program does not work. - luwal - 05-30-2025 (05-30-2025, 05:45 AM)johnno56 Wrote: I am curious as to where you got this version of "Snake"? I really want to try to make some money by selling awesome games. I try to learn. Now I don't understand the keyboard section of the RCBasic manual. RE: RCBasic question: this program does not work. - johnno56 - 05-30-2025 Ok. I will attempt to convert the game to RCBasic and Naalaa... The basic listing that you provided seems to be for a "text-based" game (no graphics). The problem is, both Naalaa and RCBasic, do not have a "text mode" for a display. Yes. Both will display text but they display the text at graphics coordinates. Usually, a screen, will be configured to display either 40 or 80 characters across the screen and either 15 or 25 rows of characters down the screen. For example: I the letter "A" is to be displayed in the top left corner of the screen, then the locate command, will be Locate 1, 1 (the first position on the fist line). Middle of the screen; Locate 20, 12 (the 20th position on the 12th line) Naalaa "Draw"'s the text at a graphics coordinate. Locate 1, 1 is the same as drawing text at coordinates 0, 0. Locate 20, 12 will probably be at coordinate 160, 96. This is just one problem. There will be more. Give me time to convert then I can go into each problem. I need to know which "basic" was used to create the game. I need to know 'if' the games actually works. If it works then I can run it using the original Basic and use it as a comparison for conversion. As it stands, the RCBasic conversion, does not work. I need to know if it is the problem is the listing or me... lol RE: RCBasic question: this program does not work. - luwal - 05-30-2025 (05-30-2025, 08:02 AM)johnno56 Wrote: You will not believe: I plan to upgrade my game programmng skills in BASIC by developing very simple classic games in 70's or 80's. Thus I asked Copliiot to write a tutorial of a simple Snake game in RCBasic for me. Copliot really wrote a tutorial with source code of a simple Snake game. However, some syntax is not syntax of RCBasic. The current version of Snake is not the original version: more Syntax mistakes in the original version of Snake. I also asked Copliot to write a Pong tutorial in RCBasic for me. Copliot's Pong tutorial is here: In this tutorial, we will cover: 1. Setting up the game area and global variables 2. Handling input for moving the human paddle 3. Updating the ball’s movement and detecting collisions (including paddle bounces and top/bottom rebounds) 4. Implementing a very simple computer AI that moves its paddle toward the ball 5. Detecting when a score occurs, resetting the ball, and updating scores 6. Rendering the updated frame each loop iteration ## 1. Setting Up the Game Area and Global Variables We first declare constants for the board dimensions and the paddle height. The human paddle is placed near the left side of the screen, while the computer paddle is near the right side. We also initialize the ball’s starting position (center of the playfield) and its movement direction (using variables `ballDX` and `ballDY`). Score variables for each side are initialized to zero. Below is the code for this section: ```basic ' Pong Game in RCBasic – Human vs Computer ' Define the playfield dimensions and paddle size. CONST boardWidth = 80 CONST boardHeight = 25 CONST paddleHeight = 4 ' Height of each paddle ' Initialize human paddle (left side). humanPaddleX = 2 humanPaddleY = INT(boardHeight / 2) ' Initialize computer paddle (right side). computerPaddleX = boardWidth - 1 computerPaddleY = INT(boardHeight / 2) ' Initialize ball position at the center and its velocity. ballX = INT(boardWidth / 2) ballY = INT(boardHeight / 2) ballDX = 1 ' Horizontal speed factor (+1 moves right, -1 moves left) ballDY = 1 ' Vertical speed factor (+1 moves down, -1 moves up) ' Initialize scores. scoreHuman = 0 scoreComputer = 0 CLS ``` ## 2. Handling Player Input Within the game’s main loop the human player controls their paddle with the keyboard—using “W” to move up and “S” to move down. We also ensure that the paddle remains within the top and bottom boundaries. The following snippet handles keyboard input and keeps the paddle within the screen’s limits: ```basic DO ' Check keyboard input. k$ = INKEY$ IF k$ <> "" THEN IF k$ = "w" OR k$ = "W" THEN humanPaddleY = humanPaddleY - 1 ELSEIF k$ = "s" OR k$ = "S" THEN humanPaddleY = humanPaddleY + 1 ENDIF ENDIF ' Prevent the human paddle from leaving the playfield. IF humanPaddleY < 2 THEN humanPaddleY = 2 IF humanPaddleY > boardHeight - paddleHeight THEN humanPaddleY = boardHeight - paddleHeight ``` --- ## 3. Updating the Ball’s Movement and Handling Collisions Each iteration of the main loop first moves the ball by adding its directional factors to its current coordinates. Next, we check for collisions with the top and bottom boundaries—reversing the vertical direction (by negating `ballDY`) when needed. We then check whether the ball meets one of the paddles. If the ball’s horizontal coordinate is just past a paddle edge and its vertical coordinate falls within that paddle’s range, we reverse the horizontal direction (by negating `ballDX`). Finally, if the ball moves past the left or right boundaries, a score is recorded for the opposing side and the ball is reset to the center with an appropriate starting direction. Below is the ball update and collision detection code: ```basic ' Update ball position. ballX = ballX + ballDX ballY = ballY + ballDY ' Bounce the ball off the top and bottom boundaries. IF ballY < 2 THEN ballY = 2 ballDY = -ballDY ENDIF IF ballY > boardHeight - 1 THEN ballY = boardHeight - 1 ballDY = -ballDY ENDIF ' Check collision with the human paddle. ' When the ball reaches one column to the right of the human paddle. IF ballX = humanPaddleX + 1 THEN IF ballY >= humanPaddleY AND ballY < humanPaddleY + paddleHeight THEN ballDX = -ballDX ENDIF ENDIF ' Check collision with the computer paddle. IF ballX = computerPaddleX - 1 THEN IF ballY >= computerPaddleY AND ballY < computerPaddleY + paddleHeight THEN ballDX = -ballDX ENDIF NEXTi ' Note: For RCBasic, end the FOR loop with NEXT [variable], as shown later. ' Check for scoring. IF ballX < 1 THEN ' The ball passed the left edge: Computer scores. scoreComputer = scoreComputer + 1 ballX = INT(boardWidth / 2) ballY = INT(boardHeight / 2) ballDX = 1 ' Relaunch ball toward the human side. ballDY = 1 ENDIF IF ballX > boardWidth THEN ' The ball passed the right edge: Human scores. scoreHuman = scoreHuman + 1 ballX = INT(boardWidth / 2) ballY = INT(boardHeight / 2) ballDX = -1 ' Relaunch ball toward the computer side. ballDY = 1 ENDIF ``` --- ## 4. Implementing the Computer Opponent (AI) A very simple AI is used for the computer paddle. In each frame the computer paddle “chases” the ball by checking whether its center is above or below the ball’s vertical position. It moves one step per frame, creating a challenge that is not perfect—allowing the human player a chance to score. We also make sure the computer paddle stays within the boundaries. Here’s the computer paddle update code: ```basic ' Move the computer paddle toward the ball. compCenter = computerPaddleY + (paddleHeight / 2) IF ballY < compCenter THEN computerPaddleY = computerPaddleY - 1 ELSEIF ballY > compCenter THEN computerPaddleY = computerPaddleY + 1 ENDIF ' Keep the computer paddle within the top and bottom boundaries. IF computerPaddleY < 2 THEN computerPaddleY = 2 IF computerPaddleY > boardHeight - paddleHeight THEN computerPaddleY = boardHeight - paddleHeight ENDIF ``` --- ## 5. Rendering the Game After updating the game state (paddle and ball positions, scores, etc.), we clear the screen and redraw the entire game frame. This includes: - Drawing horizontal boundaries at the top and bottom (using the “-” character) - Drawing the human paddle (a vertical column of “|” near the left) - Drawing the computer paddle (a vertical column of “|” near the right) - Drawing the ball (using an “O”) - Displaying the current score near the top center Below is the rendering code: ```basic ' Clear the screen. CLS ' Draw the top border. FOR x = 1 TO boardWidth LOCATE 1, x PRINT "-"; NEXT x ' Draw the bottom border. FOR x = 1 TO boardWidth LOCATE boardHeight, x PRINT "-"; NEXT x ' Draw the human paddle. FOR i = 0 TO paddleHeight - 1 LOCATE humanPaddleY + i, humanPaddleX PRINT "|"; NEXT i ' Draw the computer paddle. FOR i = 0 TO paddleHeight - 1 LOCATE computerPaddleY + i, computerPaddleX PRINT "|"; NEXT i ' Draw the ball. LOCATE ballY, ballX PRINT "O"; ' Display the scores. LOCATE 1, INT(boardWidth / 2 - 10) PRINT "You:"; scoreHuman; " Computer:"; scoreComputer; ' Delay to control the game speed. SLEEP 50 LOOP ``` --- ## 6. Complete Code Listing Combining all the sections above, here is the full Pong game code in RCBasic: ```basic ' Pong Game in RCBasic – Human vs Computer CONST boardWidth = 80 CONST boardHeight = 25 CONST paddleHeight = 4 ' Initialize human paddle (left side). humanPaddleX = 2 humanPaddleY = INT(boardHeight / 2) ' Initialize computer paddle (right side). computerPaddleX = boardWidth - 1 computerPaddleY = INT(boardHeight / 2) ' Initialize ball position at the center and its velocity. ballX = INT(boardWidth / 2) ballY = INT(boardHeight / 2) ballDX = 1 ballDY = 1 ' Initialize scores. scoreHuman = 0 scoreComputer = 0 CLS DO ' ============ Input Handling ============ k$ = INKEY$ IF k$ <> "" THEN IF k$ = "w" OR k$ = "W" THEN humanPaddleY = humanPaddleY - 1 ELSEIF k$ = "s" OR k$ = "S" THEN humanPaddleY = humanPaddleY + 1 ENDIF ENDIF ' Keep the human paddle within bounds. IF humanPaddleY < 2 THEN humanPaddleY = 2 IF humanPaddleY > boardHeight - paddleHeight THEN humanPaddleY = boardHeight - paddleHeight ' ============ Ball Movement & Collision ============ ballX = ballX + ballDX ballY = ballY + ballDY ' Bounce off the top and bottom edges. IF ballY < 2 THEN ballY = 2 ballDY = -ballDY ENDIF IF ballY > boardHeight - 1 THEN ballY = boardHeight - 1 ballDY = -ballDY ENDIF ' Collision with the human paddle. IF ballX = humanPaddleX + 1 THEN IF ballY >= humanPaddleY AND ballY < humanPaddleY + paddleHeight THEN ballDX = -ballDX ENDIF ENDIF ' Collision with the computer paddle. IF ballX = computerPaddleX - 1 THEN IF ballY >= computerPaddleY AND ballY < computerPaddleY + paddleHeight THEN ballDX = -ballDX ENDIF ENDIF ' Check for scoring. IF ballX < 1 THEN scoreComputer = scoreComputer + 1 ballX = INT(boardWidth / 2) ballY = INT(boardHeight / 2) ballDX = 1 ballDY = 1 ENDIF IF ballX > boardWidth THEN scoreHuman = scoreHuman + 1 ballX = INT(boardWidth / 2) ballY = INT(boardHeight / 2) ballDX = -1 ballDY = 1 ENDIF ' ============ Computer AI ============ ' Calculate computer paddle center. compCenter = computerPaddleY + (paddleHeight / 2) IF ballY < compCenter THEN computerPaddleY = computerPaddleY - 1 ELSEIF ballY > compCenter THEN computerPaddleY = computerPaddleY + 1 ENDIF IF computerPaddleY < 2 THEN computerPaddleY = 2 IF computerPaddleY > boardHeight - paddleHeight THEN computerPaddleY = boardHeight - paddleHeight ENDIF ' ============ Rendering ============ CLS ' Draw top boundary. FOR x = 1 TO boardWidth LOCATE 1, x PRINT "-"; NEXT x ' Draw bottom boundary. FOR x = 1 TO boardWidth LOCATE boardHeight, x PRINT "-"; NEXT x ' Draw human paddle. FOR i = 0 TO paddleHeight - 1 LOCATE humanPaddleY + i, humanPaddleX PRINT "|"; NEXT i ' Draw computer paddle. FOR i = 0 TO paddleHeight - 1 LOCATE computerPaddleY + i, computerPaddleX PRINT "|"; NEXT i ' Draw the ball. LOCATE ballY, ballX PRINT "O"; ' Display scores. LOCATE 1, INT(boardWidth / 2 - 10) PRINT "You:"; scoreHuman; " Computer:"; scoreComputer; ' Delay to control game speed. SLEEP 50 LOOP ``` --- ## Final Thoughts and Enhancements - **Difficulty Adjustments:** The simple computer AI presented here moves one step per game loop iteration. To adjust difficulty, you might add randomness or further limit its speed. - **Game Improvements:** You could add a winning score, sound effects (if your RCBasic version supports audio), or even a start/pause menu. - **Additional Controls:** Consider enhancing paddle movement (for example, supporting arrow keys if available) or allowing the game speed to ramp up as points accumulate. This tutorial should give you a solid starting point for a Pong game in RCBasic with a human vs computer opponent. Experiment with the code and adjust parameters to suit your style. Happy coding! The same syntax error happens in the code of Pong! RE: RCBasic question: this program does not work. - johnno56 - 05-30-2025 Ok. Snake AND pong are AI generated... Even though the results are impressive, I have concerns. (not with you... The AI lol) The AI has a long way to go before it can produce an error free listing. I could not get Snake to run on RCBasic yet... I had to use QB64 before I could get it to kind of work... The AI is "unaware" of the fact that RCBasic needs to setup and define the main display. RCBasic uses key() and inkey() to scan the keyboard... That being said, the AI would need to import all the commands and be able to determine what each command does. This would need to be done for each basic-like language used. I suppose all of that will improve over time. Ok. Enough about errors etc. My main concern is that, once the AI has learnt how to create error free code, what then? Anybody (programmer or not) will simply say, "Create a game of chess". The AI will create a chess game that runs flawlessly. That program is exported to run on various platforms and is sold (at a fair price of course... lol) but who gets the money? The one giving the order to make the game or the one that actually wrote the game. Let's take this example further. What happens when the AI becomes so smart that "it" requests a game of chess and produces a flawless game. The AI that "we" have created will simply remove people from the equation... Who gets the money then? Another side effect... Because the AI builds this game or that game, we learn nothing about programming. We don't have to because the AI did it... It won't happen? Look at our history. eg: When locomotives were first considered for public transport, scientists concluded that, the train be limited to 19mph because, if it went any faster, the air would be sucked out of the compartments... Now we have trains that travels at 431kph (268mph)... Bill Gates said that 640k of ram should be enough. In 2019 the largest PC memory was 1.5terrabytes... These examples have nothing to do with AI... but there is a pattern... It's in our nature to advance... My concern is that we need to be careful that our "things" do not advance so far that "we" are no longer required. AI will satisfy our current generation of "I want it and I want it now" (immediate gratification) My suggestion... For the time being, forget about AI producing games. Do yourself a favour and learn how to code so that "you" will produce the next awesome game. "You" will keep all the rewards of the sales. Sure, it's going to take a lot of time, but knowing that "you" put in all the effort to create the game will definitely be worth it. I need a coffee.... RE: RCBasic question: this program does not work. - luwal - 05-30-2025 (05-30-2025, 09:47 PM)johnno56 Wrote: Ok. Snake AND pong are AI generated... Even though the results are impressive, I have concerns. (not with you... The AI lol) Ha.....Copliot is actually very clever. Furthermore, Copliot can help Marcus or NaaLaa a lot. How? Copliot is able to help Marcus to promote NaaLaa. In my view, NaaLaa seriously lacks text tutorials. Marcus can ask Copliot to write many short tutorials for classic games with source codes in NaaLaa. Then Marcus correct and expand Copliot's tutorials and source codes. In conclusion, Marcus will be able to release many good text tutorials with source codes within a short period of time. RE: RCBasic question: this program does not work. - johnno56 - 05-31-2025 Ok. Pong has been translated "as is" to run with RCBasic. The following are the major errors: ' ------------------------------------------------------------------------------- Error on Line 29 in pong.bas: Must set string variable to string expression Use KEY() or INKEY() not INKEY$ All ENDIF's should be END IF LOCATE(x, y) must use parentheses (all locate coorinates are reversed. The listing coordinates locate(down, across) : RCBasic uses locate(across, down) PRINT " " PRINT is for the console PRINTS(" ") is for the screen Error in line 115: Expected end of line in NEXT NEXT does not use a variable name Error line line 144: SLEEP was not found in scope. WAIT(SECONDS) is used After all errors corrected. Console displayed "Window Index Out of Range RCBasic requires the window and canvas to be defined. RCBasic does not have a "text only" display. ' ------------------------------------------------------------------------------- See image of a screen shot. Note: I had to remove the "SLEEP" / WAIT() statements (far too much lag). Still too much lag. The game is not playable. The game is 'printing' strings, 80 characters per row; 25 rows. The problem is that all 80x25 rows of text (containing visible characters or not) are being drawn up to 60 times per second. Possible solution: Display the "static" elements of the screen (boundaries and scores) only once (outside the main loop) and only display the paddles and ball within the loop. Jumping out of the loop only to update the scores. (theory... not tested yet) Update: The for next loop to print the top and bottom boundaries were replaced by a single string of 80 dashes. The game lagged ( but not as much) and the ball moved quicker. Still too slow to play. But getting better. If I can get it to a playable level then I will convert it to N7... This will probably require a "monospaced" font and a routine to simulate 'locate' and 'string' (which I have already ... lol) ... more coffee... RE: RCBasic question: this program does not work. - johnno56 - 05-31-2025 Ok. PongAI converted from RCBasic to N7 Firstly. I had to limit the frame rate to 30. Without limitation the game ran WAY too fast! Secondly. As N7 does not use "locate" to place text on the screen a function had to be created. There is a minor bug at the start of the game... it may be caused by the locate function but not certain. Anyway, it runs. It performs way better that the RCBasic version. Colours were added... white on black... is so... monochromatic... Overall, it took about a hour to convert from RCB to N7. Considerably longer to convert the AI listing to RCB ( its easier for me to convert RCB to N7) Note: Use up and down arrows to control computer paddle.
pongAI.n7 (Size: 4.25 KB / Downloads: 3)
RE: RCBasic question: this program does not work. - Marcus - 05-31-2025 Nicely done, johnno! |