![]() ( define chess-board% ( class pasteboard% rest of the chess-board% definition remains unchanged. The on-paint method does not know if a mouse drag operation is in progress and where it is at, so the chess-board% object will hold a highlight-location value, which will be managed by methods that do have access to the mouse position and drag operation status: Since the highlighted square is not part of the interactive section of the pasteboard% it needs to be drawn as part of the on-paint method, together with the chess board background. ( define ( highlight-square dc location color-name border-color-name ) ( define-values ( rank file ) ( location->rank-file location )) ( define brush ( if color-name ( let* (( base ( send the-color-database find-color color-name )) ( color ( make-object color% ( send base red ) ( send base green ) ( send base blue ) 0.3 ))) ( send the-brush-list find-or-create-brush color ' solid )) ( send the-brush-list find-or-create-brush "black" ' transparent ))) ( define pen ( if border-color-name ( send the-pen-list find-or-create-pen border-color-name 2 ' solid ) ( send the-pen-list find-or-create-pen "black" 1 ' transparent ))) ( send dc set-pen pen ) ( send dc set-brush brush ) ( define-values ( dc-width dc-height ) ( send dc get-size )) ( define-values ( cell-width cell-height ) ( values ( / dc-width 8 ) ( / dc-height 8 ))) ( send dc draw-rectangle ( * file cell-width ) ( * rank cell-height ) cell-width cell-height )) #Glyph chess rulebook how to#So far, however, the program allows any move to be made, and we will fix that, but in the next section we’ll look at how to provide better visual feedback when moving pieces. Note how the chess pieces snap to the closest square when they are moved with the mouse and any piece at the destination square is removed from the board. You can find the updated program in this GitHub Gist and the result of running it is shown below. It is best to only override this method to implement functionality that does not require further moving of snips. If snips are moved inside an after-move-to method they will trigger another call to the after-move-to method, and this needs to be handled carefully otherwise after-move-to will be called recursively in an infinite sequence of calls. However, this method is invoked every time a snip is moved, including as a result of a call to the move method. NOTE: If you read the pasteboard% documentation, you might notice that this class also has an after-move-to method, which can also be overridden. ( define ( piece-at-location board location ) ( let loop (( snip ( send board find-first-snip ))) ( if snip ( if ( equal? location ( send snip get-location )) snip ( loop ( send snip next ))) #f ))) We can use these coordinates to determine the square location for the destination: this is done in the xy->location function, as with other functions, this function queries the size of the board, so that the location is always determined correctly: The method receives a mouse-event% object for the last event of the drag operation and this event object contains the mouse coordinates for where the chess piece was moved to. ![]() To address that, we need to override the after-interactive-move method of the pasteboard%, which called every time the user completes a snip drag operation. In the last version of the chess board program could we could place any inserted piece in its correct location, and pieces were also repositioned if the board size changed, however, the user can still drag pieces around with the mouse to any place on the board. Place the piece on a square after it is moved In this blog post we look at how to restrict piece movements to chess board squares, permit only valid moves and implement turn based game play. This is a continuation of the previous blog post, where the racket pasteboard% features are explored by implementing a Chess Game Board. Chess Game Using Racket’s Pasteboard (part 2) ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |