This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:cprog:spring2025:projects:mtb3 [2025/03/05 03:41] – [brick ball collision detection] tstrickl | notes:cprog:spring2025:projects:mtb3 [2025/03/05 18:48] (current) – [array of bricks] bdildine | ||
---|---|---|---|
Line 17: | Line 17: | ||
Game where you have a set of bricks that are breakable by the ball. You also have a paddle to bounce the ball towards the bricks. If the ball falls under the paddle, end game or lose lives. | Game where you have a set of bricks that are breakable by the ball. You also have a paddle to bounce the ball towards the bricks. If the ball falls under the paddle, end game or lose lives. | ||
====brick field==== | ====brick field==== | ||
+ | You can save yourself a lot of time and effort by using a for loop for the brick field. This way you can check for bounds and draw each box and then loop, rather than write the same code with a few numbers changed for as many times as you have boxes. The basic structure of a for loop in C is as follows: | ||
+ | < | ||
+ | for ( int counter = 0; counter < 10; counter++ ) | ||
+ | { | ||
+ | //code | ||
+ | } | ||
+ | </ | ||
+ | This tell the loop to initialize a counter variable as 0 in this case. It will check if the counter is less than 10 and continue with the code. The counter++ tells the loop to add one to the counter variable every time it loops, so eventually, counter will be 10 and the loop will stop. | ||
====brick structure==== | ====brick structure==== | ||
====array of bricks==== | ====array of bricks==== | ||
+ | You can use an array of boolean values to check if the box has had a collision or not. This would look something like: | ||
+ | < | ||
+ | bool[# | ||
+ | </ | ||
+ | You can make use of for loops to initialize every value in the array to true or false. | ||
+ | Then when drawing the boxes, use a for loop and only draw each box if the corresponding value in the boolean array is true or not | ||
+ | ( This would assume when the ball collides with the box you set that boxes boolean value to false ) | ||
====brick ball collision detection==== | ====brick ball collision detection==== | ||
There are many ways to check for collisions. First, an object that needs to be collided with has to have a hitbox. This means there needs to be a variable representation of an object' | There are many ways to check for collisions. First, an object that needs to be collided with has to have a hitbox. This means there needs to be a variable representation of an object' | ||
====pointers==== | ====pointers==== | ||
+ | Pointers are simply a variable that stores a memory address of other variables. pointers themselves have their own data types aswell, like an int datatype to the number 52. The pointer datatype has the goal of formatting data to represent a memory address of some given variable. Because of this, the pointer also needs to know to what type of variable it is pointing to. There could be a pointer that points to a memory address of an integer, or a pointer that points to the memory address of a string. In order to represent a memory address of a variable, it needs to know what type of variable it is pointing to. | ||
+ | Pointers don't just point to the memory address of something, they can be dereferenced and reveal the value at which they are pointing at. to dereference a pointer, and reveal the value at the memory address that it is pointing at, there are many techniques. First let's define a pointer. To define a pointer, memory needs to be allocated manually within c, but only to the data that it is pointing to. | ||
+ | |||
+ | int *hi; | ||
+ | |||
+ | This is how you define a pointer. By itself, it is saying make a new pointer type of thing, that points to an integer. But in this format, the computer only allocates the memory for a pointer type, and not the thing being pointed to. The integer that is being pointed to has not yet had memory allocated for it. Normally behind the scenes, when you create a variable of some type of thing, memory is allocated automatically, | ||
+ | |||
+ | malloc( [bytes] ) is a function that returns a pointer to the first memory address of a section of memory that is allocated based on the parameter of how big you want that space to be (in bytes). | ||
+ | |||
+ | one would need to allocate enough memory to represent an integer, if a pointer is pointing to that type. | ||
+ | |||
+ | There is a function that returns how big a datatype is in bytes: sizeof([type]) | ||
+ | |||
+ | so to allocated enough memory for an integer manually you would do: malloc(sizeof(int)), | ||
+ | to summarize it returns the pointer to the first address of a block of memory that is the size of an integer. | ||
+ | |||
+ | the pointer that is return though is a pointer that points to nothing, aka a null pointer. the pointer needs to be casted to change it's type to a int pointer. | ||
+ | |||
+ | (*int)malloc(sizeof(int)) -returns an int pointer | ||
+ | |||
+ | the variable that was first created can now be assigned to this | ||
+ | |||
+ | int *hi = (*int)malloc(sizeof(int)); | ||
+ | |||
+ | now after all that, it can now be understood that a memory block that represents an integer has been allocated, and the starting address of that memory is now stored as a value to a pointer type, which when called, will give the starting address of the integer that it points to. |