19

The function detectCollision() returns NULL if there's no collision happened. You have to check whether your object is equal to NULL before you pass it to getType().


12

Pay close attention to what detectCollision() returns. What happens when there is no collision? (which will be most of the time). Are you handling that? It appears you are not, hence your segfault.


11

What really causes this problem is that the ball hits one of the paddle's sides (or corners) and the bottom of the ball actually passes the top of the paddle. let's call the distance between the bottom of the ball and the top of the paddle x. at this point, a collision with the paddle is detected and the ball goes up with a velocity on the y-axis whose ...


8

The Idea Behind Collision: There's a function namely detectCollision() that's already written for you and ready to use. This function, when it's called, checks whether any of the sides of the ball collides with any GObject (e.g., GRect). If so, it returns that GObject immediately. The simple idea behind collision is that whether any of the points on the ...


8

You may also have to make sure it's not null before you try to pass it into getType(). detectCollision will return null when there are no collisions.


6

Your first assumption was not wrong. The bricks are not returned to the main function, so you need to find another way to refer to them while the game is running. The bricks do definitely exist as GRect objects attached to the window. 'detectCollision' will return an object which may be a brick, the paddle, the label, or whatever other object the ball is ...


6

You have to check whether object is equal to NULL before passing it to getType(). detectCollision() might return NULL in case there's no collision happened.


6

The function removeGWindow() receives a GWindow and a GObject not a GWindow and a string. You should be removing object when it's a GRect other than the paddle!


6

Instead of removing the ball and paddle from the window with removeGWindow, why not use another command (hint, hint) to change their positions back to where they began? Then you can allow normal play to resume with WaitForClick(), which is definitely a good plan to use. Also, you may want to think about simply using lives--; instead the self-refence you have ...


5

You get a segmentation fault when you touch locations in memory that you shouldn't! Make sure your initPaddle() function doesn't return NULL and returns the paddle you created within it instead!


5

Converting the code to pseudo code might help to reveal the problem: loop while lives > 0 and bricks > 0 set velocity to -0.5 move the ball in the y axis by the velocity if the ball touches the top edge or bottom edge reverse the velocity handle mouse input etc Note that regardless of what happens to the velocity in ...


5

Yes. Usually there's a better approach to do something specially when you find yourself repeating code or checking every possible case manually to do something different in each situation. I prefer the idea of storing all the color names I want in an array of strings, specify the size of that array and maybe hard-code it (you can calculate it either) and ...


4

This is a great question because it illustrates a very common mistake. Here is an excerpt from your code: for (int i = 0; i < ROWS; i++) { x = 2; for (int j = 0; i < COLS; i++) { ... You have a loop within a loop. The outer loop uses the variable i as an iterator while the inner loop uses j. However, the inner loop is also (...


4

As such, the Stanford Portable Library is under a GNU license, which makes it legal for us (or in fact, me) to redistribute it to you, or anyone else, as stated by the license: /*********************************************************************/ /* Stanford Portable Library / / Copyright (C) 2013 by Eric Roberts / / / / This program is free software: ...


4

Thankfully I found where I have done a mistake! It's in the initScoreboard . I didn't add return scoreboard to be used in updateScoreboard because in the beginning of main there's GLabel label=initScoreboard and that must have a return value! Thank you for your help ^^


4

You need to add if(object != NULL) before your if (object == paddle) Your program stops because of "Segmentation Fault". The Detect-Collision function returns a NULL if no object is detected. You cannot compare a NULL with anything.


4

It returns an int that represents a color. See http://www.programmingsimplified.com/c/graphics.h/colors for more info. Also google getColor for more explanation. You could use the actual color numbers in your code, or you could add something like #define RED 4 #define YELLOW 14 at the top of your program (and remove the quotes in the if tests). [edit/...


3

there is an updateScoreboard() function already implemented. You just need to call it with the parameters specified.


3

Here is a bit of psuedocode to help you define detectCollision: If object is not equal to NULL then check if: object == paddle, if it is, flip the y-velocity accordingly else if, object == GRect (which in this case is a brick) then: flip the y-velocity, remove the object (removeGWindow), update the points and update the scoreboard (UpdateScoreboard)


3

Just for the record, I had the same problem of breakout getting "laggy" after running (in the appliance) for a few seconds. Staff's implementation would run properly, so it wasn't strictly a hardware (or a VM) issue. I found this useful answer in /r/cs50, that, basically, states that the pause() statement is mandatory for the program to run properly. As @...


3

The zip file you linked contains folders for breakout and find, as specified in the problem set. There does not appear to be any fifteen content. Note that find was moved to accompany breakout, as stated in the notes detailing the changes between the 2014 and 2015 course offerings. The fifteen project and files appear to have been discarded entirely. It also ...


3

If you're following the distribution code, you're likely to have a variable named paddle that stores an instance of your paddle. If the ball collides with the paddle, detectCollision returns an instance of the same paddle (the one stored in paddle). You may think of the value that detect collision returns as an alias to the value stored in paddle. At this ...


3

I was having a similar problem when I moved from: if (object == paddle) to: if (strcmp(getType(object), "GRect") == 0) After reading your question, I realized I was probably not handling NULL correctly, and so I went back and did a check to make sure the object being returned by getCollision was not equal to NULL. Then, things worked better. In your ...


3

It sounds like you've hit a known issue with this program. Is this what's happening? The ball hits the side of the paddle or a brick and it starts bouncing inside the paddle or brick until it emerges from the other end, or forever. Here's what's happening. Because the ball hits the side of the paddle or brick, it is able to get inside of the other object. ...


3

look at your 2nd while loop that moves the ball, think about when your code will exit that and move on to the part of the code that moves the paddle


3

Not sure what you mean by "many" if statements, but most people will use two nested for loops to create the bricks - an inner for loop to create each row and an outer for loop to do multiple rows. Then, you only need to create one set of statements to create each brick. [EDIT - addition] There's nothing wrong with what you have done. This is a perfect ...


3

My guess is that the score is being displayed slightly low and slightly right of center. This is because setLocation() will place the object based on the upper left corner of the object, not the center of the object. You have to account for the width and height of the label, the same as you had to for the ball, the paddle and the bricks. I'll leave it to ...


3

the problem is obviously that you're not updating event. that is, you're getting the next event only once. if it doesn't happen to be a mouse click, the value of event never changes and your program is stuck in an infinite loop as control never leaves the nested while loop.


2

On the main routine, you have a call to a function that detect collisions. The ball is the only moving object. What you need to do is checking if the ball "touches" an object or the walls. Detecting wall hitting is easy: you only need to compare the position of the ball with the border of the window, and invert the xvelocity in case the position is on the ...


2

You don't really need to have an array to store bricks. Once you create and add a brick to the window, it stays there until you remove it using the function named removeGWindow() from the SPL library passing to it a reference to that brick after you get that reference returned by detectCollisio(). I guess the error you're getting is because you're declaring ...


Only top voted, non community-wiki answers of a minimum length are eligible