<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Drawing :: CS2 course</title><link>https://robark.gitlab.io/drawing/index.html</link><description>Chapter 6 Drawing Things</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Wed, 06 Jan 2021 21:37:34 +0000</lastBuildDate><atom:link href="https://robark.gitlab.io/drawing/index.xml" rel="self" type="application/rss+xml"/><item><title>Lines</title><link>https://robark.gitlab.io/drawing/lines/index.html</link><pubDate>Thu, 17 Dec 2020 23:50:30 +0000</pubDate><guid>https://robark.gitlab.io/drawing/lines/index.html</guid><description>Lesson 18: Drawing Lines from fltk import * import time class MyApp(Fl_Window): def __init__(self, x,y, w, h, label=None): Fl_Window.__init__(self, x, y, w, h, label) self.color(FL_BLACK) self.end() def draw(self): Fl_Window.draw(self) #or the line below #super().draw() sep=100 fl_color(FL_RED) #set color before line style in Windows #fl_line_style(FL_DOT, 10) for x in range(0,self.w()+1, sep):# vertical lines fl_line(x,0,x,self.h()) fl_color(FL_YELLOW) for y in range(0,self.h()+1, sep):# horizontal lines fl_line(0,y,self.w(),y) size=601 app = MyApp(0,0, size, size) app.show() Fl.run() Second assignment overriding handle</description></item><item><title>Rectangles</title><link>https://robark.gitlab.io/drawing/rectangles/index.html</link><pubDate>Tue, 05 Jan 2021 19:41:51 +0000</pubDate><guid>https://robark.gitlab.io/drawing/rectangles/index.html</guid><description>Lesson 19: Drawing Rectangles with fill colors from fltk import * class MyApp(Fl_Window): def __init__(self, x,y, w, h, label=None): Fl_Window.__init__(self, x, y, w, h, label) self.color(FL_BLACK) self.end() self.sqrsiz=100 def handle(self, event): r=super().handle(event) if event==FL_RELEASE: x=Fl.event_x() y=Fl.event_y() if ((x//self.sqrsiz)+(y//self.sqrsiz))%2 == 1 : print('Black') else: print('White') return 1 return r def draw(self): Fl_Window.draw(self) color=[FL_WHITE,FL_DARK3] #color=[7,0] for x in range(0,self.w(),self.sqrsiz): for y in range(0, self.h(), self.sqrsiz): c=color[(x+y)//100%2] fl_rectf(x,y,self.sqrsiz,self.sqrsiz,c) height=500 app = MyApp(0,0, height, height) app.show() Fl.run()</description></item><item><title>Chess</title><link>https://robark.gitlab.io/drawing/chess/index.html</link><pubDate>Wed, 06 Jan 2021 21:37:34 +0000</pubDate><guid>https://robark.gitlab.io/drawing/chess/index.html</guid><description>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.</description></item></channel></rss>