Hmm... I don't have code errors! I just don't know how to do this:
- Fix so the random number is 1 number.
- Fix so the square stays at its new place.
Please help me!
Hmm... I don't have code errors! I just don't know how to do this:
- Fix so the random number is 1 number.
- Fix so the square stays at its new place.
Please help me!
Last edited by Livijn; 05-16-2007 at 12:47 AM.
EDIT:
And if someone have a link to a game or something like this so i can learn. POST IT! This is going to be a silly gift to my girlfriend on here 15 anniversary day (in 19 days) so i've gotta hurry!
> This is going to be a silly gift to my girlfriend on here 15 anniversary day (in 19 days) so i've > gotta hurry!
Yeah, you'd better. You wouldn't want to disappoint her with a softwareless anniversary, girls hate that.
It's too bad you continue to bump your threads. I personally have reached the point of not even looking at the problem anymore. I'm guessing I'm not the only one. Maybe practice a little more patience next time.
I'm a neard but she aint! It's a cool game where you walk with her face. And when you've reached 30 p you get to see me kissing her.
Please, don't make me dissapoint her!
PLEASE! HELP ME!
What do you wan't me to do? :s
May-be showing what you have done with the code and describing the coding problem, not why you need the code written.
If the problem is that something doesn't stay in the new place, it is entirely possible that the question has already been answered: the function does not update the values in the calling code. The change is local to the function and when the function exits everything is back to as it was.
If that is the cause of the problem, looking at other people's code cannot help very much. It might be that your problem is basically the same as to why in the following code the numbers are not swapped after the call to the swap function.
You might also try to find some tutorials on collision detection. I think what you need is "bounding box collision" or something of the kind and you should be able to find a good description at GameDev pages.Code:#include <iostream> using std::cout; void swap(int a, int b) { int temp = a; a = b; b = temp; } int main() { int x = 1, y = 2; cout << x << " " << y << "\n"; //1 2 swap(x, y); cout << x << " " << y << "\n"; //1 2 noooo! }
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
I it's like that example! But how do i solve it?
In this particular example, by passing a reference (a.k.a the real thing) not a copy.
Code:#include <iostream> using std::cout; void swap(int & a, int & b) { int temp = a; a = b; b = temp; } int main() { int x = 1, y = 2; cout << x << " " << y << "\n"; //1 2 swap(x, y); cout << x << " " << y << "\n"; //2 1 alright! }
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Okey! Thanks for trying helping me!
I changed 1 line but it didn't work.
Code://Check if there is a collision void check_coll( int sX, int sY, int pX, int pY ) { //Square's position int & sXl = sX; int sXr = sX + 29; int sYl = sY; int sYr = sY + 29; //Piece's position int pXl = pX; int pXr = pX + 75; int pYl = pY; int pYr = pY + 148; //Check if you hit the square if ((pXr >= sXl && pXr < sXr) || (pXl <= sXr && pXl > sXl) || (sXl >= pXl && sXl <= pXr) || (sXr <= pXr && sXr >= pXl)) { //New position for the square sXl = 50; SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00) ); apply_surface( 0, 0, background, screen ); piece = load_image( "piece.png" ); apply_surface( pXl, pYl, piece, screen ); } //Apply the square apply_surface( sXl, sYl, square, screen ); }
sX1 is a reference of sX which is still just a local variable and a copy of the value that you pass to the function.
The whole point, if you want the function to modify the value of the parameter that is passed to the function, is to declare a reference in the argument list, as in the swap example.
From a design point of view, the problem with the code could be broader. The function's name is check_coll, but this is not what the function actually does: in addition it does some drawing and updates the game state. This violates one of the main guidelines of good code design: each function should do just one well-defined task.
In this case the function should only check for collision (as the name suggests) and return a bool (true if collides, otherwise false). Then it is up to the caller to decide what to do with the result (may-be call another function whose task is to choose a new position for the object).
The whole graphical part should also probably be handled by a special function which just draws stuff where it happens to be currently. Generally it is advised to keep graphics (updating screen display) and logic (deciding where to move stuff) separate.
(You'll still need to know about references: they are a key tool in C++ and in situations like that preferable over pointers.)
Last edited by anon; 05-17-2007 at 01:49 PM.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
I seriously can't do this.
Code:
Code://Move square if there is a collision void move_square( int pXl, int pYl ) { //Variables int sXl = 400; //New position for the square sXl = 50; SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00) ); apply_surface( 0, 0, background, screen ); piece = load_image( "piece.png" ); apply_surface( pXl, pYl, piece, screen ); //Apply the square apply_surface( sXl, 300, square, screen ); } //Check if there is a collision void check_coll( int sX, int sY, int pX, int pY ) { //Square's position int sXl = sX; int sXr = sX + 29; int sYl = sY; int sYr = sY + 29; //Piece's position int pXl = pX; int pXr = pX + 75; int pYl = pY; int pYr = pY + 148; //Check if you hit the square if ((pXr >= sXl && pXr < sXr) || (pXl <= sXr && pXl > sXl) || (sXl >= pXl && sXl <= pXr) || (sXr <= pXr && sXr >= pXl)) { move_square( pXl, pYl ); } }
If you want to do it this way, you need to use references. Study the working swap function carefully.
But really, you should structure your program better:
I'm not very familiar with the graphics API, but it also looks suspicious that you keep loading the same image over and over - possibly without ever releasing the memory.Code:bool is_colliding(Coordinates a, Coordinates b); void move_square(Coordinates & a); void draw_object(what); which could be used like: if (is_colliding(a, b)) { move_square(a); draw_object(my_game_object); //that is, if drawing is not done in a loop and only if the object moves }
Whatever piece is, you could probably load it just once and you could probably also obtain the image dimensions from it instead of hardcoding them.
By the way, have you heard at least about structs, as they are also very good for structuring code like that.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Same problem:
Code://Check if there is a collision bool check_coll( int sX, int sY, int pX, int pY ) { //Square's position int sXl = sX; int sXr = sX + 29; int sYl = sY; int sYr = sY + 29; //Piece's position int pXl = pX; int pXr = pX + 75; int pYl = pY; int pYr = pY + 148; return false; //Check if you hit the square if ((pXr >= sXl && pXr < sXr) || (pXl <= sXr && pXl > sXl) || (sXl >= pXl && sXl <= pXr) || (sXr <= pXr && sXr >= pXl)) { return true; } } //Move square if there is a collision void move_square( int sX, int sY, int pX, int pY ) { //Variables int sXl = 400; //If there is a collision if (check_coll( sX, sY, pX, pY ) == true) { //New position for the square sXl = 50; //Make the square move SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00) ); apply_surface( 0, 0, background, screen ); piece = load_image( "piece.png" ); apply_surface( pX, pY, piece, screen ); } //Apply the square apply_surface( sXl, 300, square, screen ); }
I must say your program is getting worse
Now the function move_square doesn't do what it says: it will only move square if there is a collision...
Why don't you just make 3 separate functions that don't call each other, so you'll be able to check collisions, move squares and update display as you wish (by calling the functions one by one the way you choose in a fourth functions)?
As the only thing that move_square does (to move the square) is set sX1 equal to 50, you have two more choices: 1) as you change only one value you can make it the return value of the function; 2) you can omit this function altogether and set the x_pos variable to 50 in the main code.
By the way, check_coll returns false always.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.