Chess

Lesson 20: Starting chess

from fltk import *


class piece(Fl_Box):
    def __init__(self,pic, x, y, w, h):
        Fl_Box.__init__(self, x, y, w, h)
        self.box(FL_NO_BOX) # default
        self.image(pic.copy(w,h))
        self.dx=0 #offsets to top corner of box
        self.dy=0

    def handle(self, event):
        r = super().handle(event)
        if event==FL_PUSH:
            self.dx= Fl.event_x() - self.x()
            self.dy= Fl.event_y() - self.y()
            return 1
        elif event==FL_DRAG:
            X= Fl.event_x()
            Y= Fl.event_y()
            self.position(X-self.dx, Y-self.dy)
            self.parent().redraw()
            return 1
        elif event==FL_RELEASE:
            X= Fl.event_x()
            Y= Fl.event_y()
            self.position(X//100*100,Y//100*100)
            self.parent().redraw()

            return 1
        else:
            return r


class Board(Fl_Group):
    def __init__(self, x,y, w=800, h=800, label=None):
        super().__init__(x, y, w, h, label)
        self.box(FL_FLAT_BOX)
        self.sqrsiz=100
        self.end()

    def draw(self):
        super().draw()
        color=[FL_WHITE,FL_DARK2]
        for y in range(0,self.w(),self.sqrsiz):
            for x in range(0, self.h(), self.sqrsiz):
                c=color[(x+y)//self.sqrsiz%2]
                fl_rectf(x,y,self.sqrsiz,self.sqrsiz,c)


class chess(Fl_Double_Window):
    def __init__(self, x,y,w=800,h=800,label=None):
        super().__init__(x,y,w,h,label)
        self.board= Board(0,0) # create Board before pawn so pawn is on top
        self.bpawn_img = Fl_PNG_Image('bpwn.png')
        self.bpawn = piece(self.bpawn_img,0,0,100,100)
        self.end()

app = chess(0,0)
app.show()
Fl.run()

Alternate easier solution:

Here is another way to accomplish the same result using a simpler approach. We only need two classes and we don’t even need to overide draw(). We simple create a Board class which contains a 2D grid of colored Fl_Box’s and then add the chess Piece, so it’s drawn on top. Note: the Piece class is exactly the same.

from fltk import *

class Piece(Fl_Box):
    def __init__(self,pic, x, y, w, h):
        Fl_Box.__init__(self, x, y, w, h)
        self.box(FL_NO_BOX) # default: box is not drawn
        self.image(pic.copy(w,h))
        self.dx=0 #offsets to top corner of box
        self.dy=0

    def handle(self, event):
        r = super().handle(event)
        if event==FL_PUSH:
            self.dx= Fl.event_x() - self.x()
            self.dy= Fl.event_y() - self.y()
            return 1
        elif event==FL_DRAG:
            X= Fl.event_x()
            Y= Fl.event_y()
            self.position(X-self.dx, Y-self.dy)
            self.parent().redraw()
            return 1
        elif event==FL_RELEASE:
            X= Fl.event_x()
            Y= Fl.event_y()
            self.position(X//100*100,Y//100*100)
            self.parent().redraw()

            return 1
        else:
            return r

class Board(Fl_Double_Window):
    def __init__(self, x, y, w=800, h=800, label=None):
        super().__init__(x,y,w,h,label)
        c=False
        WB=[FL_WHITE, FL_DARK3]
        self.sqrs=[]
        for y in range(9):
            for x in range(9):
                square=Fl_Box(x*100,y*100, 100, 100)
                square.box(FL_FLAT_BOX)
                square.color(WB[int(c)])
                c= not c

        img=Fl_PNG_Image('bpwn.png')
        self.pawn=Piece(img, 0,0, 100, 100)

app=Board(0,0)
app.show()
Fl.run()