|
-
May 19th, 2003, 02:31 PM
#1
Thread Starter
New Member
Help with Picture Boxes?
I have a chess program...i want it so that when i click on a piece, it will go through an If statement and determine which type of piece it is that was clicked on, but im having some problems. Here is what i have so far inside of my function
If Square.Picture = ("Z:\Pawn.bmp") Then
MovePawn()
ElseIf Square.picture = ("Z:\Rook.bmp") Then
MoveRook()
ElseIf ....
I think you can get the picture of what im trying to do...but the problem is that when i run the program, i get a
Run-Time error '13':
Type Mismatch
When i hit the debug button it highlights the line of the if statement that says
If Square.Picture = ("Z:\Pawn.bmp") Then
I have no idea what is wrong. With my logic, it seems like it should work...any ideas?
-
May 19th, 2003, 02:53 PM
#2
Fanatic Member
.Picture is a property of the picture box, which is a StdPicture object, not a string. you need to use the .LoadPicture(filename) method to load a picture from a hard drive.
what i would do would be to load all the piece pictures that i need in an array of PictureBox controls and play with them. to make a picture show on your chess board, i would use then
VB Code:
Set ChessBoard(8 * letter + digit).Picture = Pieces(INDEX_PAWN).Picture
there are 2 reasons why i leave my work unfinished:
(1) i'm getting old.
-
May 19th, 2003, 08:55 PM
#3
Thread Starter
New Member
i dont think you get what im saying here...i have all the pieces on the board already, first i click on the piece i want to move, then i click on the square that i want the piece to go to...But it can't be just any piece that moves. I need to make sure that if i clicked on a pawn, that a pawn will appear in the square i clicked on the second time, and then the first pawn disappears. So what it's saying is, if the first square that you clicked on had the Pawn as the .picture property, then it should go to the MovePawn function. Then the same thing happens when you click on any other piece, it just goes to the corresponding function to move that piece. I hope this is making sense, because I've been working on this for quite awhile and having no luck. I know there are probably much easier ways to make this game, but im trying a different approach.
-
May 19th, 2003, 09:11 PM
#4
One way to do it would be to use the tag property of the picturebox to hold the piece type. Set it when you move the piece to the square and clear it in the square that you moved from. When you click on the piece, use a select case on the tag property to call the needed function.
-
May 19th, 2003, 09:53 PM
#5
Fanatic Member
well, yes, i understand, but you cant just ask a square on your chess board 'does your picture looks like a pawn?'. i mean in theory you could, but it's not the best to compare pictures instead of comparing simple numeric values...
i would start by declaring a piece as:
VB Code:
Type Piece
Figure As Integer
Color As Integer
End Type
for the main app i would use 2 variables. one of them would be a 2-dimensional array that would store what figure is in every square on the chess board. lets name it ChessBoard, it store the virtual layout of the pieces on the chessboard.
VB Code:
Dim ChessBoard (1 To 8, 1 To 8) As Piece
another one would be the image of the chess board itself, built up of 64 squares represented by picture boxes. Lets name the array of 64 picture box controls ChessBoardImage (indexes 0 to 63). (unfortunately, you cannot declare a 2-dimensional array of objects, because this would have looked nicer.) separately from that, i would declare another set of preloaded picture boxes (PieceImages), each of them preloaded with the image of every piece.
on the Form_Load event i would:
(1) alternatively set the background colors of the ChessBoardImage picture boxes to black and white
(2) load the images in the Pieces() array of preloaded images:
VB Code:
PieceImages(0).LoadPicture App.Path & "\PAWNWHITE.BMP"
PieceImages(1).LoadPicture App.Path & "\TOWERWHITE.BMP"
'...
PieceImages(6).LoadPicture App.Path & "\PAWNBLACK.BMP"
PieceImages(7).LoadPicture App.Path & "\TOWERBLACK.BMP"
'...
(3) would SetPiece() on the first 2 rows and last 2 rows of the chess board, to obtain the initial layout.
now lets define the SetPiece and MovePiece functions:
VB Code:
Sub SetPiece( _
ByVal p As Piece, _
ByVal ToX As Integer, ByVal ToY As Integer _
)
ChessBoard(ToX, ToY).Figure = p.Figure
ChessBoard(ToX, ToY).Color = p.Color
Set ChessBoardImage(8 * (ToY - 1) + ToX - 1).Picture = _
PieceImages(p.Figure + 6 * Iif(p.Color = COLOR_WHITE, 0, 1)).Picture
End Sub
Sub MovePiece( _
ByVal FromX As Integer, ByVal FromY As Integer, _
ByVal ToX As Integer, ByVal ToY As Integer _
)
'make the destination show the piece
ChessBoard(ToX, ToY).Figure = ChessBoard(FromX, FromY).Figure
ChessBoard(ToX, ToY).Color = ChessBoard(FromX, FromY).Color
Set ChessBoardImage(8 * (ToY - 1) + ToX - 1).Picture = _
ChessBoardImage(8 * (FromY - 1) + FromX - 1).Picture
'delete from the origin
ChessBoard(FromX, FromY).Figure = FIG_NONE
ChessBoard(FromX, FromY).Color = COLOR_NONE
Set ChessBoardImage(8 * (FromY - 1) + FromX - 1).Picture = Nothing
End Sub
the problem that you have, i.e. what piece do i have in the square X and Y, is simply checking the values for ChessBoard(X, Y).Figure and ChessBoard(X, Y).Color. the .Figure will take values like FIG_NONE, FIG_PAWN, FIG_TOWER and so on (constants from 0 to 6) and .Color will be COLOR_NONE, COLOR_WHITE or COLOR_BLACK (constants from 0 to 2).
Last edited by radum; May 19th, 2003 at 09:58 PM.
there are 2 reasons why i leave my work unfinished:
(1) i'm getting old.
-
May 19th, 2003, 10:15 PM
#6
Thread Starter
New Member
I understand what you guys are saying...im gonna try the tag thing first...ive never even heard of that before, but it sounds useful as hell...and if that doesnt work then im gonna try out what radum says...thanks!
-
May 20th, 2003, 02:52 PM
#7
Thread Starter
New Member
ahhhhh yes...the .tag property works miracles! thank you very much for all your help!
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
|