User Tools

Site Tools


notes:cprog:spring2025:projects:mtb3

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
notes:cprog:spring2025:projects:mtb3 [2025/03/05 04:08] – [pointers] tstricklnotes: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:
 +<code>
 +for ( int counter = 0; counter < 10; counter++ ) 
 +{
 +//code
 +}
 +</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:
 +<code>
 +bool[#ofboxes] Boxes;
 +</code>
 +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====
Line 27: Line 42:
 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 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.  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;  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, which stores a memory address, 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, but for pointers, they need to manually allocate memory for the thing that the pointer is pointing to.+ 
 +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, but for pointers, they need to manually allocate memory for the thing that the pointer is pointing to.
  
 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). 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).
Line 47: Line 64:
 int *hi = (*int)malloc(sizeof(int)); 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 is points to.+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.
notes/cprog/spring2025/projects/mtb3.1741147739.txt.gz · Last modified: 2025/03/05 04:08 by tstrickl