|
-
Jan 18th, 2010, 02:32 PM
#1
Thread Starter
Member
[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.
-
Jan 18th, 2010, 03:17 PM
#2
Member
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.
-
Jan 18th, 2010, 03:27 PM
#3
Thread Starter
Member
Re: PictureBoxes in a checkered colouration?
 Originally Posted by miker00lz
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
-
Jan 18th, 2010, 05:41 PM
#4
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.
-
Jan 19th, 2010, 07:41 AM
#5
Thread Starter
Member
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?
-
Jan 19th, 2010, 08:40 AM
#6
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.
-
Jan 19th, 2010, 01:02 PM
#7
Thread Starter
Member
-
Jan 19th, 2010, 01:32 PM
#8
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.
-
Jan 19th, 2010, 02:15 PM
#9
Thread Starter
Member
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?

-
Jan 19th, 2010, 02:18 PM
#10
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?
-
Jan 19th, 2010, 02:27 PM
#11
Thread Starter
Member
-
Jan 19th, 2010, 02:39 PM
#12
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.
-
Jan 19th, 2010, 02:57 PM
#13
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|