def do_key(self,key): if key==pygame.K_o or key==265: self.do_button('new'); return if key==pygame.K_5 or key==263: self.do_button('reset'); return if not self.grid.complete(): buttons.clear() if key in self.keys.keys(): self.grid.colour_ind=self.keys[key] buttons.stay_down(str(self.grid.colour_ind))
def run(self): g.init() g.journal=self.journal if not self.journal: utils.load(); self.score=g.score; self.level=g.level else: g.score=self.score; g.level=self.level self.buttons_setup() if self.journal: # Sugar only a,b,c,d=pygame.cursors.load_xbm('my_cursor.xbm','my_cursor_mask.xbm') pygame.mouse.set_cursor(a,b,c,d) self.slider=slider.Slider(g.sx(16),g.sy(20.5),9,utils.GREEN) self.grid=ah.Grid() self.grid.new1(); self.grid.setup() going=True while going: # Pump GTK messages. while gtk.events_pending(): gtk.main_iteration() # Pump PyGame messages. for event in pygame.event.get(): if event.type==pygame.QUIT: if not self.journal: utils.save() going=False elif event.type == pygame.MOUSEMOTION: g.redraw=True if self.canvas<>None: self.canvas.grab_focus() elif event.type == pygame.MOUSEBUTTONDOWN: g.redraw=True if event.button==2: # centre button if not self.journal: g.version_display=not g.version_display if event.button==1: if self.grid.click(): pass elif self.slider.mouse(): self.grid.new1(); self.grid.setup(); buttons.clear() # level changed else: bu=buttons.check() if bu!='': self.do_button(bu) else: self.grid.colour_ind=0 elif event.type == pygame.KEYDOWN: self.do_key(event.key); g.redraw=True if not going: break if self.grid.complete(): buttons.clear() if g.redraw: self.display() if g.version_display: utils.version_display() pygame.display.flip() g.redraw=False self.score=g.score; self.level=g.level tf=False if pygame.mouse.get_focused(): tf=True pygame.mouse.set_visible(tf) g.clock.tick(40)
def do_button(self, bu): if bu == 'reset': self.grid.reset() buttons.clear() elif bu == 'new': self.grid.setup() buttons.clear() else: if not self.grid.complete(): self.grid.colour_ind = int(bu)
def do_key(self, key): if key in g.SQUARE: self.do_button('new'); return if key in g.CIRCLE: self.do_button('reset'); return if key in g.TICK: self.change_level(); return if key==pygame.K_v: g.version_display=not g.version_display; return if not self.grid.complete(): if key in g.NUMBERS: buttons.clear() self.grid.colour_ind=g.NUMBERS[key] buttons.stay_down(str(self.grid.colour_ind))
def display(self): if g.help_on: if g.help_img==None: g.help_img=utils.load_image('help.png') g.screen.blit(g.help_img,(0,0)) return g.screen.fill((70,0,70)) self.sim.draw() if not g.running: buttons.clear() buttons.draw() self.slider.draw() if g.load_n>0: utils.display_number(g.load_n,self.load_c,g.font1,utils.CYAN) utils.display_number(g.save_n,self.save_c,g.font1,utils.ORANGE)
def do_button(self,bu): if bu=='green': if g.running: g.running=False; buttons.clear() else: buttons.stay_down('green'); g.running=True elif bu=='yellow': g.running=False; buttons.clear(); self.sim.step() elif bu=='cream': g.running=False; buttons.clear(); self.sim.clear() elif bu=='cyan': g.running=False; buttons.clear(); self.sim.clear() self.sim.load() elif bu=='orange': g.running=False; buttons.clear() self.sim.save() if g.saved_n>0: buttons.on('cyan') elif bu=='help': g.help_on=True; buttons.off('help'); return
def cls(): g.tscreen.fill(utils.CREAM) for turtle in g.turtles: turtle.active=False g.running=False; buttons.clear() g.grid_on=False; g.turtles_on=True
def run(self): g.init() if not self.journal: utils.load() load_save.retrieve() self.buttons_setup() self.slider=slider.Slider(g.sx(16),g.sy(20.5),9,utils.GREEN) self.grid=ah.Grid() self.grid.new1(); self.grid.setup() if self.canvas!=None: self.canvas.grab_focus() ctrl=False pygame.key.set_repeat(600,120); key_ms=pygame.time.get_ticks() going=True while going: if self.journal: # Pump GTK messages. while Gtk.events_pending(): Gtk.main_iteration() # Pump PyGame messages. for event in pygame.event.get(): if event.type==pygame.QUIT: if not self.journal: utils.save() going=False elif event.type == pygame.MOUSEMOTION: g.pos=event.pos g.redraw=True if self.canvas!=None: self.canvas.grab_focus() elif event.type == pygame.MOUSEBUTTONDOWN: g.redraw=True if event.button==1: if self.grid.click(): pass elif self.slider.mouse(): self.grid.new1(); self.grid.setup(); buttons.clear() # level changed else: bu=buttons.check() if bu!='': self.do_button(bu) else: self.grid.colour_ind=0 self.flush_queue() elif event.button==3: self.do_button('reset') elif event.type == pygame.KEYDOWN: # throttle keyboard repeat if pygame.time.get_ticks()-key_ms>110: key_ms=pygame.time.get_ticks() if ctrl: if event.key==pygame.K_q: if not self.journal: utils.save() going=False; break else: ctrl=False if event.key in (pygame.K_LCTRL,pygame.K_RCTRL): ctrl=True; break self.do_key(event.key); g.redraw=True self.flush_queue() elif event.type == pygame.KEYUP: ctrl=False if not going: break if self.grid.complete(): buttons.clear() if g.redraw: self.display() if g.version_display: utils.version_display() g.screen.blit(g.pointer,g.pos) pygame.display.flip() g.redraw=False self.score=g.score; self.level=g.level g.clock.tick(40)
def change_level(self): g.level+=1 if g.level>self.slider.steps: g.level=1 self.grid.new1(); self.grid.setup(); buttons.clear()