未加星标

Chess Game Using Racket's Pasteboard

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

The Racket GUI library provides an “editor toolkit” which can be used to implement programs that use an interactive graphical canvas where objects can be moved around with the mouse. This toolkit has good reference documentation , however this documentation can be somewhat overwhelming, and it is not always clear how to begin writing such interactive application, or how to achieve some basic functionality, so I wrote a tutorial on how to implement a chess board game in Racket using the pasteboard% and snip% classes which are part of the GUI library.

This tutorial presents a step-by-step guide on how to create a chess board application using only the Racket GUI toolkit. While it uses chess as an example application, it is meant to illustrate how to create new snip% objects and how to manage them inside pasteboard% objects, and rather than enumerating all the features of the pasteboard% class, it just shows the ones that are needed to solve the immediate problems of implementing the game. The tutorial will not implement a complete chess game, as in you will not be able to play a game of chess against the computer with this program ― it will only deal with the graphical interface of the chess game.

The topics that I wanted to cover turned out to be too long for a single blog post, so the final program does not provide a payable game, I plan to write another blog post covering some more topics related to this.


Chess Game Using Racket's Pasteboard
Chess pieces as snip objects

The first thing we need in a chess game are the chess pieces, and to keep the application simple, we will use the Unicode characters for the chess pieces, so each piece will actually be displayed as text. There are 12 chess pieces in total listed below with their name, mnemonic letter and Unicode code point. The Unicode glyphs, as displayed below, are too small to be used in the game, but they can be rendered using a larger font to make them bigger.

Name Mnemonic Unicode Character Code Unicode Character White King K #\u2654 White Queen Q #\u2655 White Rook R #\u2656 White Bishop B #\u2657 White Knight N #\u2658 White Pawn P #\u2659 Black King k #\u265A Black Queen q #\u265B Black Rook r #\u265C Black Bishop b #\u265D Black Pawn p #\u265F

Before we can write the snip% for the chess pieces, we need to define a “snip class”. This name is misleading, as we are not defining a class% , but an instance of the snip-class% object. This “snip class” is used when serializing snips from the pasteboard. We won’t use this functionality here, but we still need to define one. Since we don’t use serialization facilities the definition is very simple, and shown below. The class name, as passed to set-classname needs to be unique between all the snip classes used by the application:

(define chess-piece-snip-class (make-object (class snip-class% (super-new) (send this set-classname "chess-piece-snip"))))

The snip class object needs to be registered with the Racket editor gui, and this is done using the call below:

(send (get-the-snip-class-list) add chess-piece-snip-class)

We will use a single snip class for all chess pieces, since the only difference between them is how they are displayed. To have a minimal working snip, three things need to be present in the derived class:

a call to set-snipclass which associates the snip instance with the snip class that was previously defined a get-extent method which the pasteboard% uses to determine the size of the snip a draw method, which is used to draw the snip onto the pasteboard canvas.

Our chess-piece% snip class receives three arguments in the constructor: a glyph , which is a string representing the Unicode character for the piece, a font used to render the glyph and a size which is the size in pixels of the chess piece (since the piece is a square it will have the same width and height). The font object could already determine the size of the piece, but a separate size parameter allows us to define chess pieces that are larger than the glyph that they display (as an exercise, you can experiment with different variations of font and size in the make-chess-piece defined later).

The get-extent method is used by the pasteboard% to obtain the dimensions of the snip. The implementation is straightforward, as it just reports the size as both the width and height , but the actual method call is somewhat unusual: a device context, dc is passed in, together with the position of the snip on the canvas as the x and y coordinates, in return, the pasteboard% expects the width , height and some other parameters to be filled in by our objec

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

tags: snip,chess,class,piece,The,pasteboard
分页:12
转载请注明
本文标题:Chess Game Using Racket's Pasteboard
本站链接:https://www.codesec.net/view/604935.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(12)