CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2014
    Posts
    4

    Complete Beginner of C++ Graphics Programming(Help in simple checker game)

    Hi ! I am going to start making a simple checkers game. Could any one give an idea which part to begin with and any good resource that i can learn from? What actually i can do in the initialise and draw functions
    Thanks.

    Code:
    #include"ccc_win.h"
    // main function for the execution of a graphics program
    int ccc_win_main()
    {
      // piece positions on the board
    	const int numRows = 8;
        const int numCols = 8;
    	int board[8][8];  // rowsxcolumns 8x8 square of a checkers board
    	int new_game ;
    	int exit_program;
    
    	// result of the game
    	int result ;
    	do
    	{
          Initialise(board);  // calling Initialise function 
    	  Draw(board); // calling Draw function
    	  result = PlayGame(board); // calling play function 
    
    	  // wait for new game or exit game button click
    	  while (result != new_game && result != exit_program)
    	  {
    		  // get a mouse Click
    	  }
    
    	} while(result == new_game);
    
    	return 0 ;
    }
    
    // Function to initialize the board
    void Initialise(int board)
    {
    
    // to draw a base big 8x8 square with small square drawn alternatively on top of the board
    	PenWidth pen1 = PenWidth(5);
    	PenColour redPen = PenColour(255 , 0,0);
    	PenColour blackPen = PenColour(0,0,0);
    	PenWidth penB = PenWidth(5);
    	BrushColour blackBrush = BrushColour(0,0,0);
    
    
    // Line and points for the line
    	Point origin(0,0);
    	Point p(4 , 3);
    	Point p1(-7,7) , p2(7,7);
    	Line myLine(origin , p);
    	Rectangle square(p1 , p2);
    
    
    // to draw a  black border of the board draw rectangle
    	cwin<< penB << blackBrush << square ;
    
    
    }
    // Function to draw the board
    void Draw(int board)
    {
    
    }
    
    }
    Last edited by VictorN; October 8th, 2014 at 03:19 AM. Reason: Fixed the wrong [code=java] tag

  2. #2
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: Complete Beginner of C++ Graphics Programming(Help in simple checker game)

    gamedev.net http://www.gamedev.net/page/index.html
    is were i would start for simple tutorials and questions
    its a dedicated game dev site and forum, its been around for a long time

    though more specifically simply google-ing this
    "programming a checkers game" https://duckduckgo.com/?q=programming+a+checkers+game
    directly answers you question
    Last edited by willmotil; October 11th, 2014 at 12:18 AM.

  3. #3
    Join Date
    Oct 2014
    Posts
    4

    Re: Complete Beginner of C++ Graphics Programming(Help in simple checker game)

    Thank you so much . I checked it.. That is definitely going to be very helpful. Appreciate!

  4. #4
    Join Date
    Oct 2014
    Posts
    4

    Re: Complete Beginner of C++ Graphics Programming(Help in simple checker game)

    Hi!! So far I have got this in pic below .
    Can anyone please give an idea How to use the board[row][column] to check the the number of rows so I can draw two different colours of pieces(Circles) two opposite side and no pieces at centre based on that ???????????????
    const int numRows = 8;
    const int numCols = 8;
    int board[numRows][numCols];
    const Circle DARK_PIECE ;
    void Draw(int board[numRows][numCols])
    {

    for(int x = 0; x < 8; x++)
    {
    for(int y = 0; y < 8; y++)
    {
    if((x+y)%2 == 0)
    {
    Point p1 = Point(x+1,y+1) ;
    Point p2 = Point(x+2,y+2);
    //Point cP = Point ((x+1)/2 ,(y+1)/2) ;
    cwin<< BrushColour(205,205,193) << PenColour(0,0,0) << PenWidth(2) << Rectangle(Point(x+1 , y+1) , Point(x+2 , y+2 )) ;
    if((x+y)%2 == 0 && numRows >=0 && numRows<=3)
    {
    cwin<< BrushColour(255,0,0) << PenColour(0,0,0) << PenWidth(2) << Circle( Point(x+1.5 , y+1.5) , 0.4) ;
    }
    }
    else if ((x+y)%2 != 0 )
    {
    cwin<< BrushColour(34,139,34) << PenColour(0,0,0) << PenWidth(2) << Rectangle(Point(x+1,y+1) , Point(x+2,y+2));


    }
    Name:  P_20141019_102129.jpg
Views: 2000
Size:  30.1 KB
    Last edited by klamgade; October 18th, 2014 at 05:34 PM.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Complete Beginner of C++ Graphics Programming(Help in simple checker game)

    When posting code, please use code tags as you did in your post #1 so that the posted code is readable.

    What values can the elements of board have? Then check for these values in the loops and draw/not draw a circle as appropriate.

    You are defining the number of rows/columns as const variables but use 8 in the for loops?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Oct 2014
    Posts
    4

    Re: Complete Beginner of C++ Graphics Programming(Help in simple checker game)

    Thank you so much. I tried that and got it working. Now , My next goal is to make them the pieces move . please let me know if you have any ideas on that.
    Attached Images Attached Images  
    Last edited by klamgade; October 19th, 2014 at 05:00 AM.

  7. #7
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: Complete Beginner of C++ Graphics Programming(Help in simple checker game)

    Edit:
    i was half asleep waiting on a download , when i wrote that out,
    i should have waited ackk... let me clear it up

    consider the code at the bottom pesudo code if you don't have a vector2 class im sure c++ has a few
    i used vector2 instead of point cause its easier to not recast to point all the time


    for the basic steps in making a piece on a board game move, like a checker between two squares
    we need the following steps to be considered...

    1)how much time will it take to move
    (1.1)is it to be a time based animation or
    (1.2)simply a animation over a number of frames (easier interpolated, ill use this for the example)
    if we call this animation frame length
    we will also need a current animation frame count or counter

    2)can the move be canceled during the animation
    what will happen if you do and should some buttons be disabled during the animation

    3)is the move legal

    4)where is the piece moving from and where is it moving to (positionally row column or x,y )

    5)we need to calculate the motion per frame and apply it
    this is the moving steps and math part, in this case im using simple interpolation
    before we begin will denote row by x and column by y for simplicity's sake


    we start by getting the differences from position A to B or from to positions we intend to interpolate thru
    simply found by B-A = directional length as they are x and y positional comparison difference's we will denote that with ...
    dif x or y ...or just by dif in the case of a vector2 that has a x and y field

    the number of frames are divided into the differences to get a interpolation step-ing rate
    ill use the term motion per frame instead of rate as we will use that rate directly for our motion
    motion_per_frame_x = dif_x / the animations_frame_length
    motion_per_frame_y = dif_y / the animations_frame_length

    as the animation counter increases we multiply it by this motion that gives a directional distance
    if we take the original position A and add to that a amount of motion that is multiply by some time
    we have interpolated between position a and b by varing proportions according to time
    to say
    if we have 10 frames in a animation and we are on frame 4 of 10 that's our current animation frame
    and by the below interpolation we will be 4/10th's or 40% of the way from a to b

    the piece will be drawn to a position that must be found from ...
    position_x = Ax+ current_animation_frame * motion_per_frame_x
    position_y = Ay+ current_animation_frame * motion_per_frame_y

    this is done each frame and drawn each frame, until the last frame is reached, giving the illusion of motion
    at which point you clear the counter and or the bool that tells your app to draw the animation
    place the piece itself at B , do any necessary logic ect...
    then your done, the move has been completed with the animation
    ...
    keeping track of the frames is a matter of how you want to do it, so ill leave that out
    the above math is typically done in a single method that takes all the required parameters
    which looks something like the below example
    Code:
    //
    // in c# or if you are using a similar c++ Vector struct or class 
    // otherwise use the individual variables in place of vectors
    // the texture is just a placeholder for however you draw the image with a context or what not
    //
    public void DrawInterpolate(bitmap the_texture, float current_frame, float number_of_frames, Vector2 position_from, Vector2 position_to )
    {
       Vector2 dif = position_to - position_from;
       Vector2 rate = dif / number_of_frames;
       Vector2 position = position_from + rate * current_frame;
       //
       // same as above
       //Vector2 position = new Vector2(position_from.X + rate * current_frame, position_from.Y + rate * current_frame);
       
       // from here use some draw method you may want to make or already have 
       // to draw the texture i.e your checker at the specified position
       Draw(the_texture, position);
    }
    you should note that this is just for a simple game like checkers
    for most things that are real time basic interpolation is not sufficient
    beyond the scope of this topic er doesn't fit to the question here
    Last edited by willmotil; October 24th, 2014 at 12:28 PM. Reason: simplified

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured