def stop_timer(self): if self.timing_mode == False: return # Do nothing if already stopped self.stop_time = pysweep.time() self.timing_mode = False curtime = pysweep.time() elapsed = curtime - self.start_time sincelasttick = curtime - self.previous_tick_time self.previous_tick_time = curtime self.next_tick_time = 0 self.stop_time = curtime self.callback(elapsed, sincelasttick)
def set_tile_number(self, row, col, number): # number: 0..8 if type(number) == int and 0 <= number and number < 9: self.send_event(GameDisplayEvent("TileNumber", pysweep.time(), number, row, col)) self.set_tile(row, col, "tile_{}".format(number)) else: raise ValueError('Tile number {} does not exist'.format(number))
def generate_mines(self, hn, e): width, height = self.gamedisplay.board_size area = width*height self.minecount = 0 self.rng = HashRandom() self.rng.update("TIME {}\n".format(pysweep.time())) self.rng.update("RANDOM {}\n".format(random.random())) mines_left = 99 squares_left = area for row in range(height): for col in range(width): # NB: No need to put in self-generated entropy per square we calculate. # There is no actual new entropy here between each cycle. This is handled # in the HashRandom class # self.rng.update("GEN {} {}\n".format(row, col)) ismine = self.rng.random(mines_left, squares_left) squares_left -= 1 if ismine: self.minecount += 1 mines_left -= 1 self.gamedisplay.set_tile_mine(row, col) else: self.gamedisplay.set_tile_unopened(row, col) self.gamedisplay.set_mine_counter(self.minecount)
def send_event(self, hn, e): e = ClickerEvent(self.gamedisplay.display, hn[1], pysweep.time(), self.currentposition[0], self.currentposition[1], self.currentrootposition[0], self.currentrootposition[1], self.lmb, self.rmb) # e.widget = self.gamedisplay.display # e.x, e.y = self.currentposition # e.lmb = self.lmb # e.rmb = self.rmb self.pysweep.handle_event(hn, e)
def poll_timer(self): if self.timing_mode == True: curtime = pysweep.time() elapsed = curtime - self.start_time - self.discounted_time sincelasttick = curtime - self.previous_tick_time if curtime > self.next_tick_time: self.previous_tick_time = curtime self.next_tick_time += self.period self.callback(elapsed, sincelasttick) self.master.after(self.poll_freq, self.poll_timer)
def start_timer(self): self.start_time = pysweep.time() self.previous_tick_time = self.start_time self.pause_time = 0 self.discounted_time = 0 self.next_tick_time = self.start_time # Used to have + self.period, but have it send two callbacks now so pysweeper can ceil the timer nicely :) self.callback(0, 0) if self.timing_mode == False: self.timing_mode = True self.poll_timer()
def pause_timer(self): if self.timing_mode == False: return self.timing_mode = False curtime = pysweep.time() self.pause_time = curtime elapsed = curtime - self.start_time - self.discounted_time sincelasttick = curtime - self.previous_tick_time self.previous_tick_time = curtime self.next_tick_time = self.period - sincelasttick self.callback(elapsed, sincelasttick)
def resume_timer(self): if self.timing_mode == True: return curtime = pysweep.time() self.discounted_time += curtime - self.pause_time elapsed = curtime - self.start_time - self.discounted_time self.previous_tick_time = curtime self.next_tick_time += curtime sincelasttick = 0 self.callback(elapsed, sincelasttick) self.poll_timer()
def determine_tile(self, row, col): width, height = self.gamedisplay.board_size if (not (0 <= row < height and 0 <= col < width) or (row, col) in self.determined): return numspaces = len(self.notdetermined) nummines = self.num_mines - len(self.mines) ismine = self.rng.random(nummines, numspaces) self.determined.append((row, col)) self.notdetermined.remove((row, col)) if ismine: self.mines.append((row, col)) curtime = pysweep.time() self.vid.add_command(["GENERATE", curtime, row, col, ismine]) self._add_seed(["GENERATE", curtime // 1000, row, col, ismine])
def set_mine_counter(self, t): self.send_event(GameDisplayEvent("Counter", pysweep.time(), t)) self.mine_counter.set_value(t)
def set_timer(self, t): self.send_event(GameDisplayEvent("Timer", pysweep.time(), t)) self.timer.set_value(t)
def set_face(self, face): if self.current_face != face: self.current_face = face self.send_event(GameDisplayEvent("Face", pysweep.time(), face)) self.face_button.set_face(face)
def set_tile_other(self, row, col, tile_type): self.send_event(GameDisplayEvent("TileOther", pysweep.time(), tile_type, row, col)) self.set_tile(row, col, tile_type)
def _time(): return ["TIME", pysweep.time()]