LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

BG Collision For a Beginner, I need help!

Root / Programming Questions / [.]

Tiger2Created:
I have two questions: The most important question that I've been struggling with is getting my sprite to stop when there is a bg tile in the way. I have succeeded finally somewhat, but there are still some angles that cause the sprite to be inside of the bg tile a bit, especially when the sprite is on top of the bgtile and you fall off and immediately go the opposite direction. It is like there are holes after I use BGGET. I've tried reading examples, but I'm not that bright, apparently. I need to see a program example if someone could please help me. The program needs to be very simple, with only the function of a sprite and bg tile so it is easy to understand. I tried another program called platform that was supposed to be an example, but there is too much involved in the program to understand easily which parts correlate, and it seems like the example took the longest way possible to achieve it. About a hundred lines of code just for bg collision functions, and I don't think it is the beginner friendly way, The other question is, how do I get a bg tile to react the way I want it to when it hits another bg tile if their is no bgcol functions?

I wonder, does the platform game you referred to have a sine wave plasma on the bottom screen? If so feel free to ask me questions about it. But yeah while I could have made it a bit shorter a couple hundred lines isn't really extreme. I also have "Find the Exit" which is smaller but not all that much. Anyway, on to the topic at hand. The BG layers are for background, and shouldn't really collide so to speak with each other, that is why they don't have collision functions. In fact you should think of the bg layers like a fancy text screen because that is pretty much what they are. I could start talking about Sprite to Background collision here, but I think you need some different advice. A platform Mario style game is actually fairly advanced. Your last post here a year ago implied you didn't understand what arrays are for. I think you need to back up, and get some more practice and experience in before you try a platform game. I made a big list of things to try for someone about a week ago, maybe it can help you too. Try seeing how much of that list you can manage. 1. Guess the number - Basic looping and code constructs 2. Hangman - Data vs code, string manipulation. 3. Simon - Arrays, real time input handling 4. Tic Tac Toe or Connect Four with a computer opponent. AI 5. Text adventure (get lamp type, not just a branching story) Build a virtual world and manipulate it 6. Pong, Beakout or Arkanoid - Realtime graphics, basic physics simulation 7. Tetris or Columns or Puyo Pop. Dynamic world state 8. Space Invaders or Galaga or Galaxian - Multiple enemies 9. Asteroids (use vector graphics for this one, not sprites) - 2d math and geometry 10. Pac Man or top down dungeon crawler like say original Gauntlet - Movement on a 2d grid with tile map and diverse enemy AI 11. Mario-ish side scrolling platformer. Goal!

Thank you. However, to do anything on that list I would still need to have some idea of what I was doing. I can think of a couple ways to use arrays, but when I look into other codes--simple ones too--they use them in ways I can't understand, like for scores, sprite setting, map setting, all kinds of things, and I just don't know where to start. I tried doing tic tac toe once, but still, you need to have an understanding of how to use arrays. I know what an array is, basically, I just don't know how to effectively use them in a program.

Let's try to motivate why and when you would want to use an array. We will touch on the tic tac toe board but let's start with something simpler. Suppose you are making a program to find the average score for a test. You also want to save and load the scores (later) so we can't just do a simple running average, we need to keep the scores in memory. If this were a very small class with just three students you might get code like this.
VAR SCORE1, SCORE2,  SCORE3
VAR AVERAGE_SCORE

INPUT "SCORE #1"; SCORE1
INPUT "SCORE #2"; SCORE2
INPUT "SCORE #3"; SCORE3

AVERAGE_SCORE = (SCORE1 + SCORE2 + SCORE3) / 3
PRINT "AVERAGE SCORE:  ";
PRINT AVERAGE_SCORE

END
There are a number of problems with this code. The first is that we basically repeat the same INPUT code for each student. Repeating code makes your program bigger, and leaves more places to introduce bugs. We don't like that, don't repeat yourself if you can reasonably help it. We also have a number of redundant variable names SCORE1, SCORE2, and SCORE3 that only differ by the number at the end. This is cumbersome. This solution also doesn't scale. What if you instead need to grade a larger class of say 30 students, painful but doable. What if it was suddenly thousands of students, you would be coding for a very long time if that happened. If you find yourself repeating code or having a bunch of nearly identical variables chances are you want an Array. An array gives you a variable that can hold multiple copies of the same type of data. To get or set the right one you just pass it a number. The first one is at zero and it counts forward from there. If we use an array our code suddenly gets easier.
VAR NUM_STUDENTS = 30
VAR SCORES[NUM_STUDENTS]
VAR I, AVERAGE_SCORE

FOR I = 0 TO NUM_STUDENTS - 1
 INPUT "SCORE #" + STR$(I + 1);SCORES[I]
NEXT I

AVERAGE_SCORE = 0
FOR I = 0 TO NUM_STUDENTS - 1
 AVERAGE_SCORE = AVERAGE_SCORE + SCORES[I]
NEXT I 
AVERAGE_ SCORE = AVERAGE_SCORE / NUM_STUDENTS
PRINT "AVERAGE SCORE: ";
PRINT AVERAGE_SCORE
END
Now on first glance, this code is longer and more complicated. However we can change between 3 students and 3,000 student by changing one number on the first line. Very nice, and we can use FOR loops to check each piece of data instead of checking each one manually. More complicated but much better. Now onto the tic tac toe board we could have variables like row1col1, row1col2, all the way to row3col3, but again we are repeating variable names. Checking for a three in a row requires distinct code for every column and row. This screams out for an array. We could simply make an array like VAR BOARD[9], which would work very well. However we could also make one like VAR BOARD[3, 3]. This is a two dimensional array. You can think of it like a Excel spreadsheet that only allows one type of data. Now if you want the square in the middle it is BOARD[1, 1] . You can use one for loop to check for any three in a row values and another loop for the columns. Your code gets more maintainable and easier to handle and for large arrays vastly smaller. I hope that all makes sense. Please let me know if my code has bugs or typeos. Arrays are fundamental, you can't get too far without them. In the C language strings are just arrays of the char data type. If you think of data in you program as a list of, set of, a bunch of, a grid of, or block of something it is usually best to put it in an array. It can be a list of enemy fighters or a grid of tiles on a map, or just letters in a word.

Sorry it took me so long to reply, I wasn't used to a quick reply. Anyway, it says subscript out of range. I don't think so, but correct me if I'm wrong, cannot all the areas where you put var=var+/*something be replaced by a more compact inc and or dec?

Hmmm, finally got home and typed it in. I didn't get any errors. If it is subscript out of range you probably forgot the - 1 in FOR I = 0 TO NUM_STUDENTS - 1. The indexes run from zero to one less than the length. That trips up a lot of people. Hopefully it was useful either with or without an error. You are right about INC and DEC, I was just trying to keep the code as simple as I could.

Thank you.