Thread: Class/Struct Question

  1. #1
    Akilla
    Guest

    Question Class/Struct Question

    I switched my interest towards writing a Text Based
    pong game.

    here's the code which I wrote so far :
    Code:
    #include <stdio.h>
    
    class tag_ball
    {
    	public:
    	int x, y;
    	int oldx, oldy;
    } ball;
    
    class tag_paddle
    {
    	public:
    	int x, y
    	int oldx;
    } paddle;
    
    main()
    {
    }
    This is the most basic code. (Just did 1 minute of work on this.)

    I want to make this program as simple as possible (I wrote this
    game in Qbasic, but in graphics, but didn't program it really
    well. Now that C programs are structured, I want to make the code simple more readable.

    Ok, here comes the question.

    Whenever I say
    paddle.x = 5

    I want the paddle class' x variable to be 5. And I want it to
    automatically call a function (say, placepaddle, or whatever name)
    and run that function, how do I do that ?

    in other words, whenever I set the variable, it should automatically call a function that does a particular job.

    I know this could be done in classes (that's the reason
    why I changed my struct to class), but I don't remember how.

    CHECK OUT www.akilla.tk

  2. #2
    TK
    Guest
    C++ers make the data of the class private rather than public, and they use a constructor to initialize the class object when it is instanciated.

  3. #3
    Akilla
    Guest

    er

    Could you be more detailed?

    If I make it private.. how can I access the variables
    from other functions or from main ?

    COOL PROGRAMS @ www.akilla.tk

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    I want the paddle class' x variable to be 5. And I want it to automatically call a function (say, placepaddle, or whatever name) and run that function, how do I do that ?
    Code:
    class paddle {
    
    public:
    
      bool PlacePaddle(int xcoord);
    
      .
      .
      .
    
    private:
    
      COORD paddleCoords;
    
      .
      .
      .
    
    };
    
    paddle::PlacePaddle(int xcoord)
    {
        paddleCoords.X = xcoord
        ChangePosition(this);
    }
    There are many ways you could do this.. thats one that comes to mind.
    Last edited by Dual-Catfish; 07-19-2002 at 09:15 AM.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Dual Catfish solution looks good..I would also recomend that you don't create an object of your class until you use it
    Code:
    class tag_ball
    {
    	public:
    	int x, y;
    	int oldx, oldy;
    } ball; //DON"T USE BALL HERE
    It will make your code less flexible. Your ball and paddle class are almost identical so you probably could get away with using one class and create two objects. One ball object and one paddle object.

    Code:
    class tag
    {
        //Methods and stuff goes here
    }
    Then create two objects
    Code:
    tag ball;
    tag paddle;
    Last edited by Barjor; 07-19-2002 at 09:50 AM.

  6. #6
    Akilla
    Guest

    Smile phew

    Ok.. I am getting a little confused but I'm finally learning
    something... cool..

    here's what I came up with :
    PHP Code:
    #include <stdio.h>

    class gamecoords
    {
        public:
        
    int bufxbufy;
        
    int oldxoldy;
        
    int newxnewy;
        
    void resetcords();
    };

    void gamecoords :: resetcords()
    {
        
    oldx newx;
        
    oldy newy;
        
    newx bufx;
        
    newy bufy;
    }


    main()
    {
        
    gamecoords ballpaddle;
        

    Here're my questions

    1) Does it matter if I say gamecoords ball, paddle in main() or if I decalre it right after the class (near }; )

    2) If I make them private/public, does it mean I can't access
    them via main() or other functions (if I create any) ? .. If so,
    I am more comfortable with public.

    3) How do I make the resetcoords() function work whenever I
    say.. for example..
    paddle.bufx = 9

    by the way, oldx and oldy are old x and y coordinates .. newx and newy are the new ones... bufx and bufy are the variables thru which I introduce the new values ... and I will draw the paddle and ball using newx and newy once these variables are with resetcoords.

    4) How am I doing till now ? The program looks screwy My first C game... yoohooo

    HOMEPAGE @ www.akilla.tk

  7. #7
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Despite their similarites, the ball and paddle are somewhat different. How about making an abstract base class and deriving your paddle and ball from that?

    class pong_object {

    .
    .
    .

    };


    class ball : public pong_object {

    .
    .
    .

    };

    class paddle : public pong_object {

    .
    .
    .

    };

    About your questions...
    1) It's preferred if you declare your object only in the scope which it's needed. If you declare it right after the class declaration.. that would make it global (wouldn't it?)

    2) If you make it private, usually you have get and set methods for manipulating it. You cannot access the variable from outside the class, only the class can manipulate it.

    3) Instead of directly setting the value of a public variable, make it private. Then you can use your set method you can call the resetcoords method/function.

    By set and get methods (also called accessor methods) I mean, for example GetXCoord(); SetXCoord(); etc. These would be methods of your class that you would use to manipluate its data.

    4) Well, by introducing classes, this is no longer your first C game try C++

  8. #8
    Akilla
    Guest

    Unhappy new

    I am getting confused with classes, so I made it a struct.

    here's what I came up with so far:
    PHP Code:
    #include <stdio.h>
    #include <conio.h>

    struct gamecomponent
    {
        
    int bufxbufy;
        
    int oldxoldy;
        
    int newxnewy;
        
    int speed;
        
    void resetcoords();
    ballpaddle;

    void gamecomponent :: resetcoords()
    {
        
    oldx bufx;
        
    oldy bufy;
        
    bufx newx;
        
    bufy newy;
    }

    void changecoordball()
    {
        
    int xaddyadd;

        if (
    ball.newx >= 80)
        {
            
    xadd = -ball.speed;
        }

        if (
    ball.newx <= 0)
        {
            
    xadd ball.speed;
        }

        if (
    ball.newy >= 25)
        {
            
    yadd = -ball.speed;
        }

        if (
    ball.newy <= 0)
        {
            
    yadd ball.speed;
        }

        
    ball.newx ball.newx xadd;
        
    ball.newy ball.newy yadd;
    }

    void initialize()
    {
        
    ball.bufx 1;
        
    ball.bufy 1;
        
    ball.newx 1;
        
    ball.newy 1;
        
    ball.oldx 1;
        
    ball.oldy 1;
        
    ball.speed 1;

        
    paddle.bufx 1;
        
    paddle.bufy 25;
        
    paddle.newx 1;
        
    paddle.newy 25;
        
    paddle.oldx 1;
        
    paddle.oldy 25;
        
    paddle.speed 1;
    }

    void placeball()
    {
        
    gotoxy(ball.oldxball.oldy);
        
    printf(" ");

        
    gotoxy(ball.newxball.newy);
        
    printf("*");
    }

    void placepaddle()
    {
        
    gotoxy(paddle.oldxpaddle.oldy)
        
    printf("       ");
        
        
    gotoxy(paddle.newxpaddle.newy)
        
    printf("=======");
    }

    main()
    {
        
    clrscr();
        
    initialize();
        
    placeball();

        while (!
    kbhit())
        {
        }

    SUMMARY :

    1) Defined structs for ball and paddle which contain all the related variables for each "gamecomponent"

    2) Write gamecomponent :: resetcoords ... I am not sure if this works. Now that I wrote the code like this, can I now say

    ball.resetcoords

    ? Does it reset the coordinates ?

    3) Place ball will place the ball at the newx and newy coordinates of the ball and puts a blank space " " at the previous coordinates.

    QUESTIONS

    1) Can I still keep these things public and use it ? Because I want
    to write different functions that can access these variables.

    2) I want to write a function that keeps running with time (say every 1/100th of a second or something like that) and keeps changing the ballcoordinates ... i.e. in other words, runs the changecoordball() function.

    At the same time, this function should get input from user,
    (i.e. left arrow, or right arrow) and change the coordinates of the paddle

    It should also check if the ball fell down (I have an idea of how this one works)

    So how do I write this function which takes arrow keys and changes the coordinates of the paddle (with input) and of the
    ball (with time) and of also checks if it has fallen down ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. class/struct question
    By darksaidin in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2003, 08:13 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM