Rewritten the login, desktop, jpad and also a new addition: minesweeper

This commit is contained in:
Jordon Brooks 2016-05-09 20:39:55 +01:00
parent 1d4f214e8a
commit 5154baacf3
18 changed files with 1131 additions and 0 deletions

213
Programs/jpad.py Normal file
View file

@ -0,0 +1,213 @@
import logging
from tkinter import *
from tkinter.filedialog import askopenfilename, asksaveasfilename
jpadLog = logging.getLogger("jpad.py")
jpadLog.debug("Atempting to read settings file")
try:
config = {}
exec(open("JDE/Settings/settings.conf").read(), config)
jpadLog.debug("System settings Detected!")
except Exception as e:
jpadLog.error(str(e))
class jpadEditor:
def minimize(self):
jpadLog.debug("Running minimize")
try:
self.appFocus = 0
except Exception as e:
jpadLog.error(str(e))
def closeApp(self):
jpadLog.debug("Running closeApp")
try:
# self.appIcon.destroy()
self.window.destroy()
except Exception as e:
jpadLog.error(str(e))
def focusApp(self):
jpadLog.debug("Running focusApp")
try:
if self.appFocus == 0:
self.window.focus_force()
self.appFocus = 1
else:
self.appFocus = 0
self.window.focus_set()
except Exception as e:
jpadLog.error(str(e))
def createMenu(self):
jpadLog.debug("Running createMenu")
try:
self.menu = Menu(self.window)
self.window.config(menu=self.menu)
self.filemenu = Menu(self.menu)
self.menu.add_cascade(label="File", menu=self.filemenu)
self.filemenu.add_command(label="New", command=self.new)
self.filemenu.add_command(label="Open...", command=self.open_command)
self.filemenu.add_command(label="Save", command=self.save_command)
self.filemenu.add_command(label="Save As", command=self.saveAs_command)
self.filemenu.add_separator()
self.filemenu.add_command(label="Exit", command=self.exit_command)
self.fontMenu = Menu(self.menu)
self.menu.add_cascade(label="Font", menu=self.fontMenu)
self.fontMenu.add_command(label="font", command=self.chfont)
except Exception as e:
jpadLog.error(str(e))
def new(self):
jpadLog.debug("Running new")
try:
self.window.title("Jpad Text Editor" + " File: New File")
self.textPad.delete(0.0, END)
except Exception as e:
jpadLog.error(str(e))
def exit_command(self):
jpadLog.debug("Running exit_command")
try:
self.closeApp()
except Exception as e:
jpadLog.error(str(e))
def saveAs_command(self):
jpadLog.debug("Running saveAs_command")
try:
self.jpadFile = asksaveasfilename(defaultextension=".txt",
filetypes=[("Text Files", ".txt"), ("Python .py", ".py"),
("Python .pyw", ".pyw"),
("All Files", ".*")])
self.window.title("Jpad Text Editor" + " File: " + str(self.jpadFile))
self.jpadFile = open(str(self.jpadFile), "w")
if self.jpadFile != None:
# slice off the last character from get, as an extra return is added
self.data = self.textPad.get(0.0, END)
self.jpadFile.write(self.data)
self.jpadFile.close()
except Exception as e:
jpadLog.error(str(e))
def save_command(self):
jpadLog.debug("Running save_command")
try:
self.jpadFile = open(str(self.jpadFile), "w")
if self.jpadFile != None:
# slice off the last character from get, as an extra return is added
self.data = self.textPad.get(0.0, END)
self.jpadFile.write(self.data)
self.jpadFile.close()
except Exception as e:
jpadLog.error(str(e))
def open_command(self):
jpadLog.debug("Running open_command")
try:
self.jpadFile = askopenfilename(defaultextension=".txt",
filetypes=[("Text Files", ".txt"), ("Python .py", ".py"),
("Python .pyw", ".pyw"),
("All Files", ".*")], )
self.window.title("Jpad Text Editor" + " File: " + str(self.jpadFile))
self.jpadFile = open(str(self.jpadFile), "r")
if self.jpadFile != None:
self.contents = self.jpadFile.read()
self.textPad.delete(0.0, END)
self.textPad.insert(0.0, self.contents)
self.jpadFile.close()
except Exception as e:
jpadLog.error(str(e))
def chfont(self):
def apply(self):
try:
self.textPad.configure(font=(self.font1.get(), int(self.font2.get()), self.font3.get()))
self.font[0] = self.font1.get()
self.font[1] = self.font2.get()
self.font[2] = self.font3.get()
self.rootFont.destroy()
except:
self.msg.configure(text="Whoops, something went wrong while applying your font!")
self.rootFont = Tk()
self.rootFont.title("Font")
self.rootFont.geometry("320x110")
self.font1 = Entry(self.rootFont)
self.font2 = Entry(self.rootFont)
self.font3 = Entry(self.rootFont)
self.font1.insert(0, self.font[0])
self.font2.insert(0, self.font[1])
self.font3.insert(0, self.font[2])
self.applyButton = Button(self.rootFont, text="Apply Font", command=apply)
self.msg = Label(self.rootFont)
self.font1.pack()
self.font2.pack()
self.font3.pack()
self.applyButton.pack()
self.msg.pack()
self.rootFont.mainloop()
def createWidgets(self):
self.textPad = Text(self.window)
self.textPad.configure(bg=self.menuColour)
self.textPad.configure(font=(self.font[0], int(self.font[1]), self.font[2]))
self.textPad.focus()
if self.jpadFile != None:
self.window.title("Jpad Text Editor" + " File: " + str(self.jpadFile))
self.jpadFile = open(str(self.jpadFile), "r")
self.contents = self.jpadFile.read()
self.textPad.delete(0.0, END)
self.textPad.insert(0.0, self.contents)
self.jpadFile.close()
self.textPad.pack(expand=YES, fill=BOTH)
def createWindow(self):
self.window = Tk()
self.sWidth = self.window.winfo_screenwidth()
self.sHeight = self.window.winfo_screenheight()
self.window.title("Jpad")
self.window.geometry(self.widthHeight)
self.window.maxsize(self.sWidth, self.sHeight)
self.window.minsize("200", "200")
self.createWidgets()
self.createMenu()
self.window.bind("<Unmap>", self.minimize)
self.window.protocol("WM_DELETE_WINDOW", self.closeApp)
self.window.mainloop()
def __init__(self, file=None):
self.recentAdd = open("JDE/Recent", "a")
self.recentAdd.write("\n")
self.recentAdd.write("jpad")
self.recentAdd.close()
self.jpadFile = file
self.width = "500"
self.height = "400"
self.appFocus = 1
self.widthHeight = self.width + "x" + self.height
self.menuColour = config["colour"].replace("\n", "")
self.font = ["Arial", "11", "normal"]
try:
self.appIcon = Button(self.toolbar, text="Jpad", command=self.focusApp)
self.appIcon.pack(side=LEFT, fill=Y)
except:
pass
self.createWindow()

270
Programs/minesweeper.py Normal file
View file

@ -0,0 +1,270 @@
import random
from tkinter import *
from tkinter import messagebox
def mine_sweeper(colour="#39d972"):
class MineSweeperCell(Label):
'''Creates a minesweeper square that is part of a grid'''
def __init__(self, master):
'''Makes an unmarked minesweeper grid square; 0 represents blank and 9 represents bomb'''
# create cell label
Label.__init__(self, master, height=1, width=2, text='', bg=colour, bd=5, font=('Arial', 10), relief=RAISED)
self.value = '' # store cell value
self.clicked = False # store whether it is clicked
self.flagged = False # store whether it is flagged
self.checked = False # store whether it has been checked (for 0's)
# color dictionary
self.colorMap = ['', 'blue', 'darkgreen', 'red', 'purple', 'maroon', 'cyan', 'black', 'gray']
# attaches mouse clicks to functions
self.bind('<Button-1>', self.reveal)
self.bind('<Button-3>', self.flag)
def get_value(self):
'''Returns value of cell'''
return self.value
def is_clicked(self):
'''Tells if cell has been clicked'''
return self.clicked
def is_flagged(self):
'''Tells if cell is flagged'''
return self.flagged
def flag(self, event):
'''Flags the square if player believes there is a bomb'''
if self.flagged == False and self.clicked == False and int(
self.master.flags) > 0 and self.master.winner == False:
# if no flag
self['text'] = '*'
self.master.subFlag() # subtract from remaining bombs
self.flagged = True # set self.flagged to true
elif self.flagged == True and self.clicked == False and int(self.master.flags) < int(
self.master.bombs) and self.master.winner == False: # if it is already flagged
self['text'] = '' # remove flag
self.master.addFlag() # add to remaining bombs
self.flagged = False # set self.flagged to False
def set_value(self, value):
'''Sets the value of the cell'''
self.value = value
def reveal(self, event):
'''Reveals the hidden cell value'''
if self.clicked == False:
if 0 < self.value < 9:
if self.flagged == False and self.master.loser == False: # checks if it is a valid click
self['relief'] = SUNKEN
# set text and color
self['text'] = str(self.value)
self['fg'] = self.colorMap[self.value]
self['bg'] = 'gray65'
self["bd"] = 5
self.clicked = True
self.master.CheckWin()
elif self.value == 9:
if self.master.winner == False and self.flagged == False: # if it is a bomb
self['text'] = '*' # make text a bomb
self['bg'] = 'red' # make background red
self["bd"] = 5
self['relief'] = SUNKEN
self.clicked = True
self.master.CheckLoss()
elif self.value == 0:
if self.flagged == False and self.master.loser == False:
self['text'] = ''
self.master.uncover_other_blank(self)
self['relief'] = SUNKEN
self['bg'] = 'gray65'
self["bd"] = 5
self.clicked = True
self.master.CheckWin()
def expose(self):
'''Exposes surrounding zeros'''
if self.clicked == False:
if 0 < self.value < 9:
if self.flagged == False and self.master.loser == False:
self['text'] = str(self.value) # set text and color
self['fg'] = self.colorMap[self.value]
self['relief'] = SUNKEN
self['bg'] = 'gray65'
self["bd"] = 5
self.clicked = True
self.master.CheckWin()
elif self.value == 9:
if self.flagged == False: # if it is a bomb
self['text'] = '*' # make text a bomb
self['bg'] = 'red' # make background red
self["bd"] = 5
self['relief'] = SUNKEN
self.clicked = True
elif self.value == 0:
if self.flagged == False and self.master.loser == False:
self['text'] = ''
self.master.uncover_other_blank(self)
self['relief'] = SUNKEN
self['bg'] = 'gray65'
self["bd"] = 5
self.clicked = True
self.master.CheckWin()
class MineSweeperGrid(Frame):
'''Creates a minesweeper grid to play the game on'''
def __init__(self, master, width, height, numBombs):
'''Creates a grid with the given width, height, and number of bombs'''
Frame.__init__(self, master, bg=colour, bd=5)
self.grid()
# sets the number of bombs
self.bombs = numBombs
# sets the number of flags
self.flags = numBombs
# create something to set the bombs up around each square
self.count = 0
self.winner = False
self.loser = False
# create the label that will keep track of flags
self.flagTrack = Label(self, text=self.flags, bg=colour, height=1, width=2, font=("Arial", 36))
self.flagTrack.grid(row=height, columnspan=width)
# create the bombs
self.cells = {}
for i in range(int(numBombs)):
y = random.randint(0, int(width) - 1)
x = random.randint(0, int(height) - 1)
while (x, y) in self.cells.keys():
y = random.randint(0, int(width) - 1)
x = random.randint(0, int(height) - 1)
self.cells[(x, y)] = MineSweeperCell(self)
self.cells[(x, y)].set_value(9)
self.cells[(x, y)].grid(row=x, column=y)
# create the other cells
for row in range(int(height)):
for column in range(int(width)):
if (row, column) not in self.cells.keys():
self.count = 0
if (row + 1, column) in self.cells.keys():
if self.cells[(row + 1, column)].get_value() == 9:
self.count = self.count + 1
if (row + 1, column + 1) in self.cells.keys():
if self.cells[(row + 1, column + 1)].get_value() == 9:
self.count = self.count + 1
if (row, column + 1) in self.cells.keys():
if self.cells[(row, column + 1)].get_value() == 9:
self.count = self.count + 1
if (row - 1, column + 1) in self.cells.keys():
if self.cells[(row - 1, column + 1)].get_value() == 9:
self.count = self.count + 1
if (row - 1, column) in self.cells.keys():
if self.cells[(row - 1, column)].get_value() == 9:
self.count = self.count + 1
if (row - 1, column - 1) in self.cells.keys():
if self.cells[(row - 1, column - 1)].get_value() == 9:
self.count = self.count + 1
if (row, column - 1) in self.cells.keys():
if self.cells[(row, column - 1)].get_value() == 9:
self.count = self.count + 1
if (row + 1, column - 1) in self.cells.keys():
if self.cells[(row + 1, column - 1)].get_value() == 9:
self.count = self.count + 1
self.cells[(row, column)] = MineSweeperCell(self)
self.cells[(row, column)].set_value(self.count)
self.cells[(row, column)].grid(row=row, column=column)
def subFlag(self):
'''Subtracts one from the flagcount'''
self.flags = int(self.flags) - 1
self.flagTrack['text'] = str(self.flags)
def addFlag(self):
'''Adds one from the flagcount'''
self.flags = int(self.flags) + 1
self.flagTrack['text'] = str(self.flags)
def uncover_other_blank(self, cell):
'''Uncovers all blank squares adjacent to the marked square'''
for coords, mine in self.cells.items():
if mine == cell:
c = coords
x = int(float(c[0]))
y = int(float(c[1]))
cell.checked = True
# checks all cells in radius
if (x + 1, y) in self.cells.keys():
if self.cells[(x + 1, y)].checked == False:
self.cells[(x + 1, y)].expose()
if (x + 1, y + 1) in self.cells.keys():
if self.cells[(x + 1, y + 1)].checked == False:
self.cells[(x + 1, y + 1)].expose()
if (x, y + 1) in self.cells.keys():
if self.cells[(x, y + 1)].checked == False:
self.cells[(x, y + 1)].expose()
if (x - 1, y + 1) in self.cells.keys():
if self.cells[(x - 1, y + 1)].checked == False:
self.cells[(x - 1, y + 1)].expose()
if (x - 1, y) in self.cells.keys():
if self.cells[(x - 1, y)].checked == False:
self.cells[(x - 1, y)].expose()
if (x - 1, y - 1) in self.cells.keys():
if self.cells[(x - 1, y - 1)].checked == False:
self.cells[(x - 1, y - 1)].expose()
if (x, y - 1) in self.cells.keys():
if self.cells[(x, y - 1)].checked == False:
self.cells[(x, y - 1)].expose()
if (x + 1, y - 1) in self.cells.keys():
if self.cells[(x + 1, y - 1)].checked == False:
self.cells[(x + 1, y - 1)].expose()
def CheckWin(self):
'''Checks if player won'''
doneList = []
for key in self.cells.keys():
if self.cells[key].clicked == True and self.cells[key].value != 9:
doneList.append(self.cells[key])
if len(doneList) == int(height) * int(width) - int(numBombs):
messagebox.showinfo('Minesweeper', 'Congratulations -- you won!', parent=self)
self.winner = True
playagain()
def CheckLoss(self):
'''Checks if player lost'''
self.loser = True
self.flagTrack['text'] = '0'
messagebox._show('Minesweeper', 'You Lose!', parent=self)
for key in self.cells.keys():
if self.cells[key].value == 9:
self.cells[key].flagged = False
self.cells[key].expose()
playagain()
def playagain():
again = messagebox.askyesno("Minesweeper", "Do you want to play again?")
if again == TRUE:
root.destroy()
__init__()
else:
root.destroy()
def __init__():
global width
global height
global numBombs
width = 10
height = 10
numBombs = 30
global root
root = Tk()
root.maxsize(270, 335)
root.minsize(270, 335)
root.title('Minesweeper')
global game
game = MineSweeperGrid(root, width, height, numBombs)
game.mainloop()
__init__()
mine_sweeper()