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 ...
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
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 ...
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 ...
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
Note that regardless of what happens to the velocity in ...
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 ...
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 (...
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: ...
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 ^^
You need to add
if(object != NULL)
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.
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).
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)
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.
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 ...
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 ...
I was having a similar problem when I moved from:
if (object == paddle)
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 ...
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. ...
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 ...
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 ...
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.
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 ...
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 ...