def new_game(self): self.vivant = True self.win = False self.started = False self.openfile() self.minesLeft = self.nbrMines self.nbrBouttons = self.width*self.height self.updateLabel_MinesLeft() if len(self.liste_bouttons) != 0: for i in range(self.height): for j in range(self.width): self.liste_bouttons[i][j].destroy() self.liste_bouttons.clear() self.field = Field(self.height, self.width, self.nbrMines) labelHeight = 6 labelWidth = 8 for i in range(self.height): liste_ligne = [] for j in range(self.width): if self.field[i,j] == 0: lab = Etiquette(self, i, j, 0, " ") elif self.field[i,j] == 1: lab = Etiquette(self, i, j, 1, "1") elif self.field[i,j] == 2: lab = Etiquette(self, i, j, 2, "2", "forest green") elif self.field[i,j] == 3: lab = Etiquette(self, i, j, 3, "3", "orange red") elif self.field[i,j] == 4: lab = Etiquette(self, i, j, 4, "4", "navy") elif self.field[i,j] == 5: lab = Etiquette(self, i, j, 5, "5", "brown") elif self.field[i,j] == 6: lab = Etiquette(self, i, j, 6, "6", "light sea green") elif self.field[i,j] == 7: lab = Etiquette(self, i, j, 7, "7", "black") elif self.field[i,j] == 8: lab = Etiquette(self, i, j, 8, "8", "gray") else: lab = Etiquette(self, i, j, -1, "x", "black") lab.grid(row=i,column=j,padx=labelWidth,pady=labelHeight) bt = Case(self, i, j, " ") bt.grid(row=i, column=j) bt["relief"] = tk.RAISED bt["borderwidth"]=3 bt.grid_propagate(0) #bt.config(height = 30) liste_ligne.append(bt) self.liste_bouttons.append(liste_ligne) self.label_Time["text"] = "0" if self.chrono == None: self.chrono = Chrono() else: self.chrono.reset() self.root.after(1,self.updateLabel_Time)
def __init__(self, app): gobject.GObject.__init__(self) self.app = app self.blocks = {} self.wcf_uid_list = [] self.chrono_times = {} self.chrono_day = time.localtime().tm_wday if not os.path.exists(BLOCK_DB): try: os.makedirs(os.path.dirname(BLOCK_DB)) except: pass else: try: db = open(BLOCK_DB, 'rb') p = pickle.load(db) self.blocks = p[0] self.wcf_uid_list = p[1] self.chrono_times = p[2] self.chrono_day = p[3] db.close() except: print "Something wrong unpickling" self.__next_update_info = None self.usersmanager = UsersManager() if os.name == "nt": self.win32top = Win32Top(self) self.chrono = Chrono(self) self.firewall_filter = FirewallFilter(self) self.filter_manager = FilterManager(self) self.webcontent_filter = WebContentFilter(self, app) self.session_filter = SessionFilter(self) gobject.timeout_add(1000, self.__polling_cb)
end_sound = mixer.Sound(END_SOUND) goal_sound = mixer.Sound(GOAL_SOUND) os.environ['SDL_VIDEO_CENTERED'] = '1' icon = pygame.image.load(ICON) pygame.display.set_icon(icon) pygame.display.set_caption(TITLE) screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32) bg = pygame.image.load(BG_PATH).convert() bg_instruction = pygame.image.load(BG_INSTRUCTION_PATH).convert() clock = pygame.time.Clock() chrono = Chrono(0, 0, 0) player1 = Mallet(PLAYER1_START, MALLET_MASS, MALLET_RED_PATH, MALLET_MAX_SPEED, MALLET_ACCELERATION, MALLET_FRICTION, 1) player2 = Mallet(PLAYER2_START, MALLET_MASS, MALLET_BLUE_PATH, MALLET_MAX_SPEED, MALLET_ACCELERATION, MALLET_FRICTION, 2) disc = Disc(DISC_START_POS, DISC_START_ANGLE, DISC_START_SPEED, DISC_MASS, DISC_PATH, DISC_MAX_SPEED, DISC_FRICTION) posts = [] posts.append( Goalpost((BORDER_LEFT, GOAL_START), (-1, -1), GOALPOST_MASS, GOALPOST_PATH)) posts.append( Goalpost((BORDER_LEFT, GOAL_END), (-1, 1), GOALPOST_MASS, GOALPOST_PATH))
def __init__(self, app): gobject.GObject.__init__(self) self.app = app self.blocks = {} self.wcf_uid_list = [] self.chore_settings = {} self.chrono_times = {} self.chrono_day = (datetime.today() - datetime.utcfromtimestamp(0)).days if not os.path.exists(BLOCK_DB): try: os.makedirs(os.path.dirname(BLOCK_DB)) except: pass else: try: db = open(BLOCK_DB, 'rb') p = pickle.load(db) self.blocks = p[0] self.chore_settings = p[1] self.wcf_uid_list = p[2] self.chrono_times = p[3] self.chrono_day = p[4] db.close() except: print "Something wrong unpickling, trying older format (2.31.1)..." # old format (2.31.1) support try: db = open(BLOCK_DB, 'rb') p = pickle.load(db) self.blocks = p[0] self.wcf_uid_list = p[1] self.chrono_times = p[2] self.chrono_day = p[3] db.close() except: print "No success, something just went wrong unpickling" # more old format (2.31.1) support for t in self.chrono_times: announced = False for c in self.chrono_times[t]: if len(self.chrono_times[t] [c].items()) == 2: # 2.31.1 had 2 fields if not announced: print 'Old format (2.31.1) support: adding extra fields to user', t announced = True self.chrono_times[t][c]["extra_time"] = 0 self.chrono_times[t][c]["mercy_count"] = 0 self.chrono_times[t][c]["force_close"] = 0 self.chrono_times[t][c]["last_active"] = -1 self.__next_update_info = None self.usersmanager = UsersManager() if os.name == "nt": self.win32top = Win32Top(self) self.chrono = Chrono(self) self.firewall_filter = FirewallFilter(self) self.filter_manager = FilterManager(self) self.chore_manager = ChoreManager(self) self.webcontent_filter = WebContentFilter(self, app) self.session_filter = SessionFilter(self) gobject.timeout_add(1000, self.__polling_cb)
class Minesweeper(): def __init__(self, width, height, mines): self.width = width self.height = height self.nbrMines = mines self.minesLeft = mines self.chrono = None self.liste_bouttons = [] self.root = tk.Tk() self.root.title('Minesweeper') self.cadreMenu = tk.Frame(self.root) self.cadreMenu.grid(row=0, column=0) self.mb = tk.Menubutton(self.cadreMenu,anchor=tk.W,text="Game") self.mb.grid(row=0, column=0) self.mb.menu = tk.Menu(self.mb,tearoff=0) self.mb["menu"] = self.mb.menu self.mb.menu.add_command(command=self.new_game,label="New Game") self.mb.menu.add_command(command=self.statistics,label="Statistics") self.mb.menu.add_command(command=self.options,label="Options") self.mb.menu.add_command(command=self.exit,label="Exit") self.smiley = tk.PhotoImage(file="smiley.gif").subsample(2, 2) self.cadre1 = tk.Frame(self.root, height = 48, width = 250) self.cadre1.grid(row = 1, padx = 10, pady = 1) self.cadre1.grid_propagate(0) self.cadre1['relief']=tk.SUNKEN self.cadre1["borderwidth"]=5 self.frame1 = tk.Frame(self.cadre1, height = 21, width = 80) self.frame1.grid(row=0,column=0) self.frame1.grid_propagate(0) self.frameSmiley = tk.Frame(self.cadre1) self.frameSmiley.grid(row=0,column=3) self.frameTime = tk.Frame(self.cadre1) self.frameTime.grid(row=0,column=5) self.label_MinesLeft = tk.Label(self.frame1) self.label_MinesLeft.grid(padx=10) self.label_Time = tk.Label(self.frameTime) self.label_Time.grid(padx=30) self.smileyButton = tk.Button(self.frameSmiley,image=self.smiley, command=self.new_game, height=32, width=32) self.smileyButton.grid(padx=22) self.cadre2 = tk.Frame(self.root, height = 347, width = 277) self.cadre2.grid(padx=6,pady=4) self.cadre2['relief']=tk.SUNKEN self.cadre2["borderwidth"]=5 self.cadre2.grid_propagate(0) self.new_game() self.root.mainloop() #while self.vivant == True: # self.root.update() # self.updateLabel_Time() def new_game(self): self.vivant = True self.win = False self.started = False self.openfile() self.minesLeft = self.nbrMines self.nbrBouttons = self.width*self.height self.updateLabel_MinesLeft() if len(self.liste_bouttons) != 0: for i in range(self.height): for j in range(self.width): self.liste_bouttons[i][j].destroy() self.liste_bouttons.clear() self.field = Field(self.height, self.width, self.nbrMines) labelHeight = 6 labelWidth = 8 for i in range(self.height): liste_ligne = [] for j in range(self.width): if self.field[i,j] == 0: lab = Etiquette(self, i, j, 0, " ") elif self.field[i,j] == 1: lab = Etiquette(self, i, j, 1, "1") elif self.field[i,j] == 2: lab = Etiquette(self, i, j, 2, "2", "forest green") elif self.field[i,j] == 3: lab = Etiquette(self, i, j, 3, "3", "orange red") elif self.field[i,j] == 4: lab = Etiquette(self, i, j, 4, "4", "navy") elif self.field[i,j] == 5: lab = Etiquette(self, i, j, 5, "5", "brown") elif self.field[i,j] == 6: lab = Etiquette(self, i, j, 6, "6", "light sea green") elif self.field[i,j] == 7: lab = Etiquette(self, i, j, 7, "7", "black") elif self.field[i,j] == 8: lab = Etiquette(self, i, j, 8, "8", "gray") else: lab = Etiquette(self, i, j, -1, "x", "black") lab.grid(row=i,column=j,padx=labelWidth,pady=labelHeight) bt = Case(self, i, j, " ") bt.grid(row=i, column=j) bt["relief"] = tk.RAISED bt["borderwidth"]=3 bt.grid_propagate(0) #bt.config(height = 30) liste_ligne.append(bt) self.liste_bouttons.append(liste_ligne) self.label_Time["text"] = "0" if self.chrono == None: self.chrono = Chrono() else: self.chrono.reset() self.root.after(1,self.updateLabel_Time) def updateLabel_MinesLeft(self): self.label_MinesLeft['text'] = "Mines : {}".format(str(self.minesLeft)) def updateLabel_Time(self): if self.vivant == True: if self.started == True: if self.win == False: self.label_Time["text"] = "{}".format(str(int(self.chrono.get()))) self.root.after(1,self.updateLabel_Time) else: time = int(self.chrono.get()) ecrire_stats = open("stats.txt", "w") if time < self.highScore: ecrire_stats.write(str(time)+"\n") else: ecrire_stats.write(str(self.highScore)+"\n") ecrire_stats.write(str(self.nbrGames)+"\n") self.nbrWins += 1 ecrire_stats.write(str(self.nbrWins)+"\n") ecrire_stats.close() else: self.root.after(1,self.updateLabel_Time) def openfile(self): fich = open("stats.txt", "r") liste = fich.readlines() fich.close() print(liste) #self.stats = {"easy":{}, "medium":{}, "hard":{}} self.highScore = int(liste[0]) self.nbrGames = int(liste[1]) + 1 self.nbrWins = int(liste[2]) fich = open("stats.txt", "w") fich.write(str(self.highScore)+"\n") fich.write(str(self.nbrGames)+"\n") fich.write(str(self.nbrWins)) fich.close() def statistics(self): fen_statistics = tk.Tk() fen_statistics.title("statistics") tableFrame = tk.Frame(fen_statistics, height = 30, width = 30) tableFrame.grid(row=1, pady=1) tableFrame['relief']=tk.SUNKEN tableFrame["borderwidth"]=5 tk.Label(tableFrame, text="Difficulty").grid(row=1,column=1,padx=5,pady=2) tk.Label(tableFrame, text="Easy").grid(row=1,column=2,padx=5,pady=2) tk.Label(tableFrame, text="Medium").grid(row=1,column=3,padx=5,pady=2) tk.Label(tableFrame, text="Hard").grid(row=1,column=4,padx=5,pady=2) tk.Label(tableFrame, text="Game Started").grid(row=2,column=1,padx=5,pady=2) tk.Label(tableFrame, text=str(self.nbrGames)).grid(row=2,column=2,padx=5,pady=2) tk.Label(tableFrame, text="N/A").grid(row=2,column=3,padx=5,pady=5) tk.Label(tableFrame, text="N/A").grid(row=2,column=4,padx=5,pady=2) tk.Label(tableFrame, text="Game Won").grid(row=3,column=1,padx=5,pady=2) tk.Label(tableFrame, text=str(self.nbrWins)).grid(row=3,column=2,padx=5,pady=2) tk.Label(tableFrame, text="N/A").grid(row=3,column=3,padx=5,pady=2) tk.Label(tableFrame, text="N/A").grid(row=3,column=4,padx=5,pady=2) tk.Label(tableFrame, text="% Game Won").grid(row=4,column=1,padx=5,pady=2) tk.Label(tableFrame, text=str(int(100*self.nbrWins/self.nbrGames))+"%").grid(row=4,column=2,padx=5,pady=2) tk.Label(tableFrame, text="N/A").grid(row=4,column=3,padx=5,pady=2) tk.Label(tableFrame, text="N/A").grid(row=4,column=4,padx=5,pady=2) tk.Label(tableFrame, text="Best Time").grid(row=5, column=1,padx=5,pady=2) tk.Label(tableFrame, text=str(self.highScore)).grid(row=5,column=2,padx=5,pady=2) tk.Label(tableFrame, text="N/A").grid(row=5,column=3,padx=5,pady=2) tk.Label(tableFrame, text="N/A").grid(row=5,column=4,padx=5,pady=2) close = tk.Button(fen_statistics, command=fen_statistics.destroy, text="close") close.grid(row=2, padx=100, pady=25) def options(self): fen_options = tk.Tk() fen_options.title("options") close = tk.Button(fen_options, command=fen_options.destroy, text="close") close.grid(padx=100, pady=50) def exit(self): self.root.quit()