Frozone212 Posted October 20, 2022 Share Posted October 20, 2022 I'm following this guy on youtube called the coding train and was wondering: how do I check for collisions without resorting to a subroutine? I want to structure the code somewhat without making it too unreadable on the off chance that I decide to make my own game. This will be an awesome opportunity for me to move from making text adventures to actual games. I can get movement well enough but collision is never checked and because of that, the position of the fruit (he was doing snake) never changes. can someone post an example of the code? I've checked google and archive.org but no luck. I even RTFM but no dice Quote Link to comment Share on other sites More sharing options...
TheDevil'sCompass Posted October 21, 2022 Share Posted October 21, 2022 I have not watched the video, but I see it is on the text screen. At the simplest level, collision between the snake and food can be checked like this: (Assuming FX, FY are the X, Y coordinates for the food and SX, SY are the coordinates for the snake head) IF SY=FY AND SX=FX THEN SC=SC+10 : REM CHECK FOR COLLISION, IF DETECTED ADD 10 TO THE SCORE VARIABLE, SC Quote Link to comment Share on other sites More sharing options...
jhd Posted October 21, 2022 Share Posted October 21, 2022 I am not familiar with Applesoft BASIC, specifically, but in other versions of BASIC it is possible to use a PEEK command to read a value directly from the screen RAM. For example, I once wrote a Centipede clone using text mode. As the projectile travelled up the screen, the code would PEEK the byte on the line immediately above it. If it was empty (blank space), then the projectile would continue to advance upwards. If the byte contained a character (a mushroom, part of the centipede) then other things would happen accordingly. Quote Link to comment Share on other sites More sharing options...
TheDevil'sCompass Posted October 21, 2022 Share Posted October 21, 2022 Yes, doing PEEKs is also an option. You'll have to keep track of more things though because of how the screen is addressed. The screen is divided into 3 vertical sections: Lines 0-7, 8-15, 16-23. Line (Hex) (Decimal) 00 $400-427 1024-1063 01 $480-4A7 1152-1191 02 $500-527 1280-1319 03 $580-5A7 1408-1447 04 $600-627 1536-1575 05 $680-6A7 1664-1703 06 $700-727 1792-1831 07 $780-7A7 1920-1959 08 $428-44F 1064-1103 09 $4A8-4CF 1192-1231 10 $528-54F 1320-1359 11 $5A8-5CF 1448-1487 12 $628-64F 1576-1615 13 $6A8-6CF 1704-1743 14 $728-74F 1832-1871 15 $7A8-7CF 1960-1999 16 $450-477 1104-1143 17 $4D0-4F7 1232-1271 18 $550-577 1360-1399 19 $5D0-5F7 1488-1527 20 $650-677 1616-1655 21 $6D0-6F7 1744-1783 22 $750-777 1872-1911 23 $7D0-7F7 2000-2039 Example: 0,0 - Check for <space> character X=PEEK(1024) IF X-128<>32 THEN POKE 1024,207 : REM IF IT'S A SPACE, POKE AN "O" FOR THE SNAKE CHARACTER Another way to do it is to setup a two-dimensional array and use that to check for collisions. You'd essentially update the array (and check for collisions) at the same time you update the screen. DIM PF$(23,39) : PLAYFIELD ARRAY IS SAME SIZE AS TEXT SCREEN IF PF$(SY,SX)=32 THEN PF$(SY,SX)="O" : HTAB SX : VTAB SY : PRINT "O"; : REM IF A SPACE IS FOUND IN THE ARRAY, PRINT AN "O" FOR THE SNAKE CHARACTER 1 Quote Link to comment Share on other sites More sharing options...
The Usotsuki Posted October 21, 2022 Share Posted October 21, 2022 Applesoft also has SCRN() which is intended for reading off the LRG display but can be used to pick nibbles off video memory. 1 Quote Link to comment Share on other sites More sharing options...
Frozone212 Posted October 21, 2022 Author Share Posted October 21, 2022 ok so I have the base playfield set, with J as the bit flag to check for collisions. Thanks for the help guys. I really appreciate it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.