Thread: In Need of Help with a few Minor C++ Problems

  1. #1
    Registered User Wilhelm's Avatar
    Join Date
    Nov 2010
    Location
    Louisiana, United States
    Posts
    3

    In Need of Help with a few Minor C++ Problems

    Alright. I have been working on two video games in C++: Pac-Man and Pong. I have been able to do most of it myself, but there are a few minor things that is giving me a hell of a time.

    The first issue is to run a loop of cycling images. I have the sprites for Pac-Man, and I want to have the mouth open and close while running the program, by looping the images. I can't seem to get it to work. All I can get it to do is show one image while running.

    The second issue is collisions. I can easily get images to collide with certain 'x' or 'y' points, but I can not get an image to collide with another image. This is the issue with Pong. I want the ball to collide with ONLY the paddle, and not a the entire side of the program screen. I will also need to apply this concept in Pac-Man with colliding with the maze walls.

    The last problem is automatic movement of images. In Pong, I will need the ball to bounce around the screen by itself, and I need the second paddle to be controlled by the program. In Pac-Man, I need the ghosts to automatically move around the maze. I feel like this is a simple concept, but I just can't figure it out.

    I don't think that this is anything major that I am asking help with, and I would greatly appreciate any help I can get on this. Also, I didn't think I needed to post any of my code, but if needed, I'll gladly post parts of it.

    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    You will have to be a lot more specific with what you are having problems with. What have you tried, how does it not work. E.g.

    [All I can get it to do is show one image while running.
    So, instead of showing the one, switch to the other the next frame, and the frame after that, you switch back again.

    I can easily get images to collide with certain 'x' or 'y' points, but I can not get an image to collide with another image.
    How is colliding with certain x or y points any different from colliding with an image which in the end resides at a certain x or y point too.

    And so on and so on.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  3. #3
    Registered User Wilhelm's Avatar
    Join Date
    Nov 2010
    Location
    Louisiana, United States
    Posts
    3
    Quote Originally Posted by MWAAAHAAA View Post
    You will have to be a lot more specific with what you are having problems with. What have you tried, how does it not work.
    I'll try to explain it more specifically.
    I also forgot to mention that it is an SDL project also, if it matters.

    Quote Originally Posted by MWAAAHAAA View Post
    So, instead of showing the one, switch to the other the next frame, and the frame after that, you switch back again.
    Hmm... I am not sure what you mean by having the image switch frames.

    I have all the images written in the code, I just don't know how to make it cycle the images(or switch frames). It only shows one or none when it is running.

    This is the part of the code for drawing Pac-Man:

    SDL_Surface* Pac[4];
    Pac[0] = IMG_Load("source_pictures/pac.png");
    Pac[1] = IMG_Load("source_pictures/pac2.png");
    Pac[2] = IMG_Load("source_pictures/pac3.png");
    Pac[3] = IMG_Load("source_pictures/pac4.png");
    DrawImage(Pac[4],pacx,pacy);
    I have tried random things such as while loops with the 'DrawImage' including Pac[0], Pac[1], Pac[2]... and so forth, but with no success. There is something I am doing wrong, or something I don't know how to do.

    Quote Originally Posted by MWAAAHAAA View Post
    How is colliding with certain x or y points any different from colliding with an image which in the end resides at a certain x or y point too.
    Well, for Pong, the paddles will be moving, so I am not sure how to get the collisions to only work on a moving paddle, if you know what I mean. And if it doesn't hit the moving paddle, it should be going of the screen, not hitting an invisible wall.

    As for Pac-Man, I see how I can do the maze wall collisions now. (A lot of work)
    I just wasn't thinking apparently.


    And the automatic movements being controlled by the program, I really don't have a clue how to make that work.

    I hope I explained it better.
    Last edited by Wilhelm; 11-21-2010 at 11:06 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    I don't know how to write games or anything like that, but I know some things about C++. In regards to the image collision, the images should have their own x,y coordinates no matter where they are, you just need to have a function keep track of all the current x,y coordinates of all the objects and ensure that they aren't colliding... I don't know how simple it really is but I would assume you wouldn't have problems doing it?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    DrawImage(Pac[4],pacx,pacy);
    I don't have any experience in SDL, however, aside from the fact that you loaded only four images, while you are trying to access the non-existent fifth image in the array with this call, what would be the problem in loading two images, one for mouth open, and one for mouth closed, for each monster. When drawing, you simply call DrawImage, alternating between the two images whenever you want the mouth two open/close.

    As for collisions, see also sudox' response. You know the position of the paddle, you know the position of the ball, so you can determine whether the two are colliding or not.

    Post your code if there is any problems.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Assuming your call to DrawImage is supposed to pass the SDL_Surface array you want to pass it as Pac instead of Pac[4]. Pac[4] would pass only the 5th (and non-existant) cell.

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You need to have some sort of status on the image you are displaying. There are variety of ways to do this.

    You could have a timer that sets a status flag and you draw different images based on the state of that flag(Look at SDL_SetTimer).

    You could implement some simple loop logic that changes the image state after a set period(Look at SDL_GetTicks)
    Woop?

  8. #8
    Registered User Wilhelm's Avatar
    Join Date
    Nov 2010
    Location
    Louisiana, United States
    Posts
    3
    Alright, I did some changes to the part of the code that loads the images, and got the images looping with some simple if statements.

    Quote Originally Posted by sudox View Post
    I don't know how to write games or anything like that, but I know some things about C++. In regards to the image collision, the images should have their own x,y coordinates no matter where they are, you just need to have a function keep track of all the current x,y coordinates of all the objects and ensure that they aren't colliding... I don't know how simple it really is but I would assume you wouldn't have problems doing it?
    Quote Originally Posted by MWAAAHAAA View Post
    As for collisions, see also sudox' response. You know the position of the paddle, you know the position of the ball, so you can determine whether the two are colliding or not.

    Post your code if there is any problems.
    I well understand what both of you are saying about the image collisions, but I still can't seem to get working properly. I have been trying many if statements and such, but the best results still come out as hitting an invisible wall. I guess I'll post part of the code for Pong for you to evaluate it, and see what can be done.

    SDL_Surface* Paddle;
    SDL_Surface* Paddle2;
    SDL_Surface* Ball;
    const int SWIDTH = 800;
    const int SHEIGHT = 600;
    bool moveup = false, movedown = false;
    bool moveup2 = false, movedown2 = false;
    bool bup = false, bdown = false;
    bool bleft = false, bright = false;
    int px = 10, py = 230;
    int p2x = 760, p2y = 230;
    int bx = 400, by = 300;
    //Paddle Movements.
    if(moveup == true && py >= 0)
    {
    py = py - 10;

    if(py < 0)
    {
    py = 0;
    }
    }

    if(movedown == true && py <= 500)
    {
    py = py + 10;

    if(py >= 500)
    {
    py = 500;
    }
    }

    if(moveup2 == true && p2y >= 0)
    {
    p2y = p2y - 10;

    if(p2y < 0)
    {
    p2y = 0;
    }
    }

    if(movedown2 == true && p2y <= 500)
    {
    p2y = p2y + 10;

    if(p2y >= 500)
    {
    p2y = 500;
    }
    }
    //Ball Collisions and Movement. Work in Progress.

    if(bright == true)
    {
    //Direction and Speed.
    bx = bx + 8;

    //Collision.
    if(bx >= 770)
    {
    bx = 400;
    by = 300;
    }
    }

    if(bleft == true)
    {
    //Direction and Speed.
    bx = bx - 8;

    //Collision.
    if(bx <= 0)
    {
    bx = 400;
    by = 300;
    }
    }

    if(bdown == true && by <= 570)
    {
    //Direction and Speed.
    by = by + 8;

    //Collision.
    if(by >= 570)
    {
    by = 570;
    }
    }

    if(bup == true && by >= 0)
    {
    //Direction and Speed.
    by = by - 8;

    //Collision.
    if(by <= 0)
    {
    by = 0;
    }
    }
    //Draw Images
    DrawImage(Paddle,px,py);
    DrawImage(Paddle2,p2x,p2y);
    DrawImage(Ball,bx,by);
    I tried to post as least code as possible, but with enough information. If you need more code or the entire code, I'll post it.

    I am pretty sure I know where to apply the code for the image collisions, maybe I'm wrong, but I am not figuring out the right code to do what I am trying to do.

    Also, I had set up some temporary key presses to control the ball to test the collisions since I don't know how to get the ball to randomly bounce around the screen.
    So I still need help with the random movement of the ball and the left paddle. I tried a few things like 'rand()', but no success there either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 06:38 PM
  2. tile colliision detection problems
    By werdy666 in forum Game Programming
    Replies: 1
    Last Post: 10-23-2002, 09:26 PM
  3. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM
  4. Problems with my RPG app
    By valar_king in forum Game Programming
    Replies: 1
    Last Post: 12-15-2001, 08:07 PM
  5. problems with too many warning messages?
    By Isometric in forum C Programming
    Replies: 9
    Last Post: 11-25-2001, 01:23 AM