Results 1 to 13 of 13

Thread: [RESOLVED] PictureBoxes in a checkered colouration?

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    40

    Resolved [RESOLVED] PictureBoxes in a checkered colouration?

    I have a game that uses a chessboard but I can't figure out a way to get the pictureboxes that represent the squares to have the colours of a chess board (i.e. checkered black and white). I want to be able to do this at runtime: when the piece is moved, the previous square turns red - I have a "reset" button that should put everything back to black and white, and the piece back to its starting position. The picboxes are in a control array. Can anyone suggest an algorithm that could do this?
    Sorry if I haven't been clear enough.

  2. #2
    Member
    Join Date
    Jan 2010
    Posts
    34

    Re: PictureBoxes in a checkered colouration?

    maybe something like this?

    Code:
    totalboxes = whatever number of boxes you used
    
    curcolor = 0 ' probably not actually needed, but why not
    For n = 0 to totalboxes - 1
      paintcolor = curcolor * &HFFFFFF
      curcolor = (curcolor + 1) Mod 2
      MyPictureBox(n).Line (0, 0)-(MyPictureBox(n).Width, MyPictureBox(n).Height), paintcolor, BF
    Next n
    EDIT: change curcolor = 0 to curcolor = 1 if you wanna start with a white box.
    Last edited by miker00lz; Jan 18th, 2010 at 03:22 PM.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    40

    Re: PictureBoxes in a checkered colouration?

    Quote Originally Posted by miker00lz View Post
    maybe something like this?

    Code:
    totalboxes = whatever number of boxes you used
    
    curcolor = 0 ' probably not actually needed, but why not
    For n = 0 to totalboxes - 1
      paintcolor = curcolor * &HFFFFFF
      curcolor = (curcolor + 1) Mod 2
      MyPictureBox(n).Line (0, 0)-(MyPictureBox(n).Width, MyPictureBox(n).Height), paintcolor, BF
    Next n
    EDIT: change curcolor = 0 to curcolor = 1 if you wanna start with a white box.
    I tried code like this, but it doesn't work - the way i have numbered my boxes (0 to 8 from left to right, then on the next line 8 to 15 from left to right, etc.) means that it's not a simple case of painting the even boxes one colour and the odd boxes another. Also, I need the boxes numbered that way and I can't change it.
    Thanks anyway though

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: PictureBoxes in a checkered colouration?

    You wrote that the top line is 0 to 8, but you meant 0 to 7, correct?
    Try this. Change picGrid to your picturebox's name.
    Assumption is that your pictureboxes are indexed 0 to 63 inclusively. If not, you'll need to give a better description.
    Code:
    Dim X As Long, Y As Long, paintColor As Long
    ' start with either White or Black; change as needed
    paintColor = vbWhite ' change to vbBlack if needed
    For Y = 0 To 7
        For X = 0 To 7
            picGrid(Y * 8 + X).BackColor = paintColor
            paintColor = paintColor Xor vbWhite
        Next
        paintColor = paintColor Xor vbWhite
    Next
    End Sub
    Last edited by LaVolpe; Jan 18th, 2010 at 05:44 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    40

    Re: PictureBoxes in a checkered colouration?

    Thanks LaVolpe - the code works well However, I need X and Y as Integers in the rest of my program - is there anyway I can have them declared as long in one Sub and integer in the others?

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: PictureBoxes in a checkered colouration?

    Yes. Unless they are form/module-level declarations, each are separate entities within individual subs. You can also just simply declare them as Integers in the sample code I provided.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    40

    Re: PictureBoxes in a checkered colouration?

    Thanks a lot for that Having X and Y declared as long had stopped the rest of the program from working, but It's fixed now The algorithm for painting the chessboard is intriguing - having used visual basic/programmed for less than 2 years, I have never had to use the Xor statement, so I would never have thought of doing that I know your code works, but I'm still trying to figure out WHY it works...

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: PictureBoxes in a checkered colouration?

    Well, quickly. XOR adds bits to a value if they don't exist or removes them if they do exist.

    vbBlack = hex 000000, or all zeros for 1st 24 bits of the Long value
    vbWhite = hex FFFFFF, or all ones for 1st 24 bits of the Long value

    What it is doing is simply toggling from black to white. When color is all zeros then XORing all ones, changes them to all ones. And when all ones then XORing all ones, changes them all zeros.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    40

    Re: PictureBoxes in a checkered colouration?

    Thanks for the explanation, but there is still something that I'm wondering:
    When it gets to square 7, it paints it black, and on square 8 (the first square of the next row) it paints it black again (which is correct). However, from what I understand of your code, it should be painting alternately white then black. How does it know to paint two black squares in a row? Is there something I am missing here?

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: PictureBoxes in a checkered colouration?

    The Y variable is for the rows if you will. The X is for the columns in that row.

    For the first row, the color is white to start, then each time a picbox is colored, the XOR, within the X loop, changes color from white to black and vice versa. I think you got that part.

    Now when the row finishes, you exit the X loop and what is waiting there? Another XOR function that changes the color one more time. Niffty, huh?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    40

    Re: PictureBoxes in a checkered colouration?

    I never noticed that
    Now, one (hopefully last) question: is there any way I could alter the code to alternate other colours (say black/red or purple/orange)? I had the idea to include in my program an option to change the colour scheme and just wondered if I could maybe have an abnormally coloured board.
    Your help is very much appreciated Is there any way I can give "reputation points", "thanks", etc. on this forum? // Edit: I found it :P

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: PictureBoxes in a checkered colouration?

    Yes, very easily in fact. The beauty of XOR allows us to do this easily. Just need the difference (XORed) of the two colors. Here is a more generic routine.
    Code:
    Private Sub PaintBoard(colorLight As Long, colorDark As Long)
    
        Dim X As Long, Y As Long, paintColor As Long, xorDiff As Long
        
        paintColor = colorLight ' or change to colorDark if wanting to start with dark square first
        xorDiff = colorLight Xor colorDark
        
        For Y = 0 To 7
           X = Y * 8  ' tweaked a bit for optimizing; don't need to do multiplication in the X loop
            For X = X To X + 7
                picGrid(X).BackColor = paintColor
                paintColor = paintColor Xor xorDiff
            Next
            paintColor = paintColor Xor xorDiff
        Next
    End Sub
    Examples of calling it
    :: PaintBoard vbWhite, vbBlack
    :: PaintBoard vbCyan, vbBlue
    And you can even use VB system colors
    :: PaintBoard vbInfoBackground, vbRed

    Edited: Change Long to Integer for X,Y if necessary. The colors & xorDiff must be Long
    Last edited by LaVolpe; Jan 19th, 2010 at 02:43 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  13. #13

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    40

    Re: PictureBoxes in a checkered colouration?

    Thank you so much for this Works like a charm

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