Open Technical Blog
Wednesday, 8 March 2017
GTK3 Bouncing Ball
This script shows a basic structure for an animation using Python and GTK+3. The following techniques are adopted:
GTK+ 3 & Cairo for graphics.
Object-oriented style for the moving object.
Timer trigger for animation.
Source Code
#!/usr/bin/python # Title : GTK3 Cairo Animation # Date : 15/01/2017 from gi.repository import Gtk, Gdk, GObject import cairo, math class Background(): def draw(self,pCtx,pWidth,pHeight): # draw rectangle pCtx.set_source_rgb(0.4, 0.2, 0.4) pCtx.rectangle(0,0,pWidth,pHeight) pCtx.fill() class Ball(): def __init__(self): self.x = 100 self.y = 50 self.r = 30 # radius self.w = 10 # width of border self.sx = 2 # x speed self.sy = 2 # y speed def draw(self,pCtx,pWidth,pHeight): # check boundaries if self.x < self.r+self.w: # hit left boundary self.x = self.r+self.w self.sx = -self.sx elif self.x > pWidth-self.r-self.w: # hit right boundary self.x = pWidth-self.r-self.w self.sx = -self.sx if self.y < self.r+self.w: # hit top boundary self.y = self.r+self.w self.sy = -self.sy elif self.y > pHeight-self.r-self.w: # hit bottom boundary self.y = pHeight-self.r-self.w self.sy = -self.sy # draw circle pCtx.set_line_width(self.w) pCtx.set_source_rgb(0.7, 0.2, 0.0) pCtx.arc(self.x, self.y, self.r, 0, 2*math.pi) pCtx.stroke_preserve() pCtx.set_source_rgb(0.9, 0.9, 0.7) pCtx.fill() def move(self): self.x += self.sx self.y += self.sy class mainApp(): def __init__(self): self.win = Gtk.Window() self.win.connect('destroy', Gtk.main_quit) self.win.set_default_size(480,360) self.win.set_position(Gtk.WindowPosition.CENTER) self.win.set_title('GTK3 Bouncing Ball') mBox = Gtk.VBox(False,3) # not equal height, space # drawing area self.drawFlag = False self.drawArea = Gtk.DrawingArea() self.drawArea.connect('draw', self.onDraw) mBox.pack_start(self.drawArea,True,True,2) # control panel self.but01 = Gtk.Button('Go') self.but01.connect("clicked", self.callback, "fireGo") mBox.pack_start(self.but01,False,True,2) # message label self.msg = Gtk.Label("Message") mBox.pack_end(self.msg,False,True,2) self.win.add(mBox) self.win.show_all() # define drawing objects self.bgn = Background() self.ball01 = Ball() # define a timer: trigger at each 1/20 s self.timer = GObject.timeout_add(50, self.timeOut) # timeout trigger def timeOut(self): if not self.drawFlag: return True # change something self.ball01.move() # redraw window self.win.queue_draw() # keep timer running return True # stop timer running # return False def onDraw(self, pDrawArea, pCtx): if not self.drawFlag: return # get the dimensions of the draw area mw = pDrawArea.get_allocated_width() mh = pDrawArea.get_allocated_height() # draw objects self.bgn.draw(pCtx,mw,mh) self.ball01.draw(pCtx,mw,mh) def callback(self, widget, data): if data == 'fireGo': self.drawFlag = not(self.drawFlag) if self.drawFlag: self.but01.set_label('Stop') else: self.but01.set_label('Go') # redraw drawing area self.win.queue_draw() mApp = mainApp() Gtk.main()
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment