[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. :o
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.
Re: PictureBoxes in a checkered colouration?
Quote:
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 :)
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
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?
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.
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 :thumb: 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 :o I know your code works, but I'm still trying to figure out WHY it works... :ehh:
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.
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?
:):ehh:
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?
Re: PictureBoxes in a checkered colouration?
:bigyello: I never noticed that :lol:
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
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
Re: PictureBoxes in a checkered colouration?
Thank you so much for this :) Works like a charm ;)