Re: Chess GUI with wxPython Phoenix

Chris Barker <[email protected]>
Newsgroups gmane.comp.python.wxpython.devel
Message-ID <CALGmxEKe3FN9PjmeRagqh0Cpk6a2qSs8XNiDHVBNi7Cb2BCSPw@mail.gmail.com>
I few thoughts:

I'd probably have your chess pieces pre-rendered as bitmaps (images)

You _could_ use a Grid sizer, and wxButtons for the board, but I wouldn't
do that...

Rather, I'd create a custom widget, probably derived from wx.Window (or
wx.Panel, but I think what a wx.Panel buys you is stuff for putting other
widgets on it (like tab navigation), so you don't need that.

Then I'd draw the board with a few basic draw calls -- 64 squares, drawn in
a loop.

You could double buffer the board, so that you would never need to re-draw
it -- though drawing 64 squares is pretty darn fast these days.

Then you draw the pieces on top of the board.

When there is a move, you can just re-draw the whole thing -- you could be
really smart and jsut re-draw what's changed, but again, drawing is fast.

Double buffer the whole thing, so you dont get flicker at all.

Call the drawing code when somethign has changed.

The Paint evt handler should just blit the buffer -- easy if you use a
wxBufferedPAintDC:


def OnPaint(self, event):
    dc = wx.BufferedPaintDC(self, self.buffer)

These pages:

https://wiki.wxpython.org/RecipesImagesAndGraphics

Should get you going.

To handle events, bind the mouse events on your Window and use math to
figure out which square was clicked on.

For a similar example, see my Sudoko Board example:

https://github.com/PythonCHB/wxPythonDemos/blob/master/sudoku-chb.py

You could probably start with that and edit it to suite your needs.

-CHB





On Mon, Oct 16, 2017 at 5:48 AM, Boštjan Mejak <[email protected]>
wrote:

> I am creating a chess GUI with wxPython Phoenix (wxPython-4.0.0rc1.dev3436+
> efbe850, the latest wheel build as of 10/16/2017 for Python 3.6 on
> Windows 10 64-bit).
>
>
> My backend is going to be python-chess, a lovely chess library with all
> the bells and whistles, like legal move detection and all that stuff.
>
>
> And now to the question. How on Earth can I incorporate a chess board onto
> the panel of my main window? python-chess provides a way to display the
> chess board, even in SVG, but I can't seem to do this. I've looked many
> solutions, and the one I come up with is by using the
> wx.AutoBufferedPaintDC. That'll create sort sort of a canvas on the panel
> for me to paint the chess board to. Also, the chess board itself is static,
> but the pieces must be interactive. I just can't make this work.
>
>
> Do I need to create a custom wx.Panel class or something else? Also, I
> must make sure that wx.Window.SetBackgroundStyle is called with
> wx.BG_STYLE_PAINT somewhere in the class initialization code to avoid
> flickering when a chess piece moves. Further, this wx.DC derivative can be
> used inside of an EVT_PAINT() event handler to achieve double-buffered
> drawing. How can this be coded?
>
> I just don't have enough knowledge to do this. Does anyone know how can I
> do this? Some code examples would be nice, because that'll give me a
> clearer picture on the task at hand.
>
> --
> You received this message because you are subscribed to the Google Groups
> "wxPython-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[email protected]

-- 
You received this message because you are subscribed to the Google Groups "wxPython-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.