diff --git a/ProjTemplates/u1battleship/finalproduct.py b/ProjTemplates/u1battleship/finalproduct.py deleted file mode 100644 index b78bb89..0000000 --- a/ProjTemplates/u1battleship/finalproduct.py +++ /dev/null @@ -1,108 +0,0 @@ -from random import randint -enem_board = [[u"\U0001F30A"]*5 for n in range(5)] -frnd_board = [[u"\U0001F30A"]*5 for n in range(5)] -name = "Sa'ar" -def print_board(frnd, enem): - print("CPU") - print(end=" ") - for i in range(1, len(enem[0])+1): - print(i, end=" ") - print() - for i in range(1, len(enem[0])+1): - row = enem[i-1] - print(i," ".join(row)) - print("-".join(["--"]*5)) - print(name) - print(end=" ") - for i in range(1, len(frnd[0])+1): - print(i, end=" ") - print() - for i in range(1, len(frnd)+1): - row = frnd[i-1] - print(i," ".join(row)) - print() -#computer hide -row = randint(0, len(enem_board)-1) -col = randint(0, len(enem_board[0])-1) -#player hide -pr = None -pc = None -while pr == None and pc == None: - if pr == None: - pr = randint(1, len(frnd_board)) - pr -= 1 - if pc == None: - pc = randint(1, len(frnd_board)) - pc -= 1 - if pc < 0 or pc >= len(frnd_board[0]): - pc = None - if pr < 0 or pr > len(frnd_board): - pr = None -else: - print(pr+1,pc+1) - frnd_board[pr][pc] = u"\U0001F6A2" - -won = False -lost = False -while (not won) and (not lost): - print_board(frnd_board, enem_board) - print("CPU: ") - t = False - while not t: - gr = randint(0, len(frnd_board)-1) - gc = randint(0, len(frnd_board[0])-1) - if frnd_board[gr][gc] != u"\U0001F4A3": - t = True - print("Row: ",gr+1) - print("Column: ",gc+1) - print() - if frnd_board[gr][gc] == u"\U0001F6A2": - lost = True - frnd_board[gr][gc] = u"\U0001F4A5" - print("Hit!") - break - else: - frnd_board[gr][gc] = u"\U0001F4A3" - print("Miss!") - print() - print_board(frnd_board, enem_board) - print(name+":") - gr = randint(1, len(enem_board)) - gr -= 1 - print("Row:",gr) - gc = randint(1, len(enem_board[0])) - gc -= 1 - print("Column:",gc) - print() - if gr == row and gc == col: - print("Hit!") - enem_board[gr][gc] = u"\U0001F4A5" - won = True - elif gr < 0 or gr >= len(enem_board) or gc < 0 or gc >= len(enem_board[0]): - print("That's not even in the ocean ):") - elif enem_board[gr][gc] == u"\U0001F4A3": - print("You already guessed that, stupid! DX") - else: - print("Miss!") - print() - enem_board[gr][gc] = u"\U0001F4A3" -if won: - for i in range(len(enem_board)): - for j in range(len(enem_board[i])): - if not enem_board[i][j] == u"\U0001F4A3": - enem_board[i][j] = u"\U0001F525" - else: - enem_board[i][j] = u"\u2620" - enem_board[row][col] = u"\U0001F4A5" - print("Congrats on winning!") -if lost: - for i in range(len(frnd_board)): - for j in range(len(frnd_board[i])): - if not frnd_board[i][j] == u"\U0001F4A3": - frnd_board[i][j] = u"\U0001F525" - else: - frnd_board[i][j] = u"\u2620" - frnd_board[pr][pc] = u"\U0001F4A5" - enem_board[row][col] = u"\U0001F6A2" - print("You sax. Better luck next time!") -print_board(frnd_board, enem_board) diff --git a/Project templates/u1_battleship/01.py b/Project templates/u1_battleship/01.py new file mode 100644 index 0000000..7b701a6 --- /dev/null +++ b/Project templates/u1_battleship/01.py @@ -0,0 +1 @@ +board = [] diff --git a/Project templates/u1_battleship/02.py b/Project templates/u1_battleship/02.py new file mode 100644 index 0000000..c3b13e6 --- /dev/null +++ b/Project templates/u1_battleship/02.py @@ -0,0 +1 @@ +board = [["O"]*5 for _ in range 5] diff --git a/Project templates/u1_battleship/03.py b/Project templates/u1_battleship/03.py new file mode 100644 index 0000000..fa22453 --- /dev/null +++ b/Project templates/u1_battleship/03.py @@ -0,0 +1,5 @@ +board = [["O"]*5 for _ in range 5] + +def print_board: + for row in board: + " ".join(row) diff --git a/Project templates/u1_battleship/04.py b/Project templates/u1_battleship/04.py new file mode 100644 index 0000000..880ec8f --- /dev/null +++ b/Project templates/u1_battleship/04.py @@ -0,0 +1,9 @@ +from random import randint +board = [["O"]*5 for _ in range 5] + +def print_board: + for row in board: + print(" ".join(row)) + +col = randint(0, len(board[0])) +row = randint(0, len(board)) diff --git a/Project templates/u1_battleship/05.py b/Project templates/u1_battleship/05.py new file mode 100644 index 0000000..3f30012 --- /dev/null +++ b/Project templates/u1_battleship/05.py @@ -0,0 +1,12 @@ +from random import randint +board = [["O"]*5 for _ in range 5] + +def print_board: + for row in board: + print(" ".join(row)) + +col = randint(0, len(board[0])) +row = randint(0, len(board)) + +row_guess = input("Guess row: ") +col_guess = input("Guess column: ") diff --git a/Project templates/u1_battleship/06.py b/Project templates/u1_battleship/06.py new file mode 100644 index 0000000..37739f4 --- /dev/null +++ b/Project templates/u1_battleship/06.py @@ -0,0 +1,14 @@ +from random import randint +board = [["O"]*5 for _ in range 5] + +def print_board: + for row in board: + print(" ".join(row)) + +col = randint(0, len(board[0])) +row = randint(0, len(board)) + +row_guess = input("Guess row: ") +col_guess = input("Guess column: ") + +print(row, col) diff --git a/Project templates/u1_battleship/07.py b/Project templates/u1_battleship/07.py new file mode 100644 index 0000000..326d8bf --- /dev/null +++ b/Project templates/u1_battleship/07.py @@ -0,0 +1,17 @@ +from random import randint +board = [["O"]*5 for _ in range 5] + +def print_board: + for row in board: + print(" ".join(row)) + +col = randint(0, len(board[0])) +row = randint(0, len(board)) + +print(row, col) + +row_guess = input("Guess row: ") +col_guess = input("Guess column: ") + +if row_guess == row and col_guess == col: + print("something like \"We have a winner\"") diff --git a/Project templates/u1_battleship/08.py b/Project templates/u1_battleship/08.py new file mode 100644 index 0000000..29f278e --- /dev/null +++ b/Project templates/u1_battleship/08.py @@ -0,0 +1,19 @@ +from random import randint +board = [["O"]*5 for _ in range 5] + +def print_board: + for row in board: + print(" ".join(row)) + +col = randint(0, len(board[0])) +row = randint(0, len(board)) + +print(row, col) + +row_guess = input("Guess row: ") +col_guess = input("Guess column: ") + +if row_guess == row and col_guess == col: + print("something like \"We have a winner\"") +else: + print("YOU SAX!!!") diff --git a/Project templates/u1_battleship/09.py b/Project templates/u1_battleship/09.py new file mode 100644 index 0000000..f6dd80c --- /dev/null +++ b/Project templates/u1_battleship/09.py @@ -0,0 +1,21 @@ +from random import randint +board = [["O"]*5 for _ in range 5] + +def print_board: + for row in board: + print(" ".join(row)) + +col = randint(0, len(board[0])) +row = randint(0, len(board)) + +print(row, col) + +row_guess = input("Guess row: ") - 1 +col_guess = input("Guess column: ") - 1 + +if row_guess == row and col_guess == col: + print("something like \"We have a winner\"") +elif row_guess > len(board) or col_guess > len(board[0]) or row_guess < 0 or col_guess < 0: + print("Next time, try to aim *inside* the ocean") +else: + print("YOU SAX!!!") diff --git a/Project templates/u1_battleship/10.py b/Project templates/u1_battleship/10.py new file mode 100644 index 0000000..2725e43 --- /dev/null +++ b/Project templates/u1_battleship/10.py @@ -0,0 +1,25 @@ +from random import randint +board = [["O"]*5 for _ in range 5] + +def print_board(): + for row in board: + print(" ".join(row)) + +col = randint(0, len(board[0])) +row = randint(0, len(board)) + +print(row, col) + +row_guess = input("Guess row: ") - 1 +col_guess = input("Guess column: ") - 1 + +print_board() +if row_guess == row and col_guess == col: + print("something like \"We have a winner\"") +elif row_guess > len(board) or col_guess > len(board[0]) or row_guess < 0 or col_guess < 0: + print("Next time, try to aim *inside* the ocean") +elif board[row_guess][col_guess] == 'x': + print("Insanity: doing the same thing over and over again and expecting different results.") +else: + board[row_guess][col_guess] = 'x' + print("YOU SAX!!!") diff --git a/__pycache__/mainloop.cpython-34.pyc b/__pycache__/mainloop.cpython-34.pyc index 028bd23..7d35f66 100644 Binary files a/__pycache__/mainloop.cpython-34.pyc and b/__pycache__/mainloop.cpython-34.pyc differ diff --git a/__pycache__/screens.cpython-34.pyc b/__pycache__/screens.cpython-34.pyc index 768bd68..34dd491 100644 Binary files a/__pycache__/screens.cpython-34.pyc and b/__pycache__/screens.cpython-34.pyc differ diff --git a/__pycache__/window.cpython-34.pyc b/__pycache__/window.cpython-34.pyc index 21849c1..e1d0044 100644 Binary files a/__pycache__/window.cpython-34.pyc and b/__pycache__/window.cpython-34.pyc differ diff --git a/dist/library.zip b/dist/library.zip index 0ee22fe..43fb9f0 100644 Binary files a/dist/library.zip and b/dist/library.zip differ diff --git a/screens.py b/screens.py index 1763004..74705fb 100644 --- a/screens.py +++ b/screens.py @@ -2,10 +2,17 @@ from window import Window class Screen: + width = 300 def __init__(self, title, showAs, func): self.title = title self.showAs = showAs self.func = func + + def __repr__(self): + return self.showAs + + def __str__(self): + return self.__repr__() class LessonScreen(Screen): def __init__(self, i, text, screenFunc): @@ -22,6 +29,15 @@ def __init__(self, unit, proj, partTitle, screenFunc): super().__init__(text, showAs, func) class Screens(Window): + def scrnBtn(self, screenbtn): + text = screenbtn.title + func = screenbtn.func + self.projBtn(text, func) + + def projBtn(self, text, func): + btn = self.cenBtn(text, func) + btn.config(width=self.scrnWidth) + #screenNum: 1, 2, 3, 4, 5, etc. def new_lesson(self, unit, screenNum): screenNum -= 1 @@ -50,6 +66,7 @@ def new_proj(self, unit, partNum): def __init__(self, master=None): self.homeBounds = "800x1000" self.screenBounds = "800x400" + self.scrnWidth = 100 self.u1 = [ LessonScreen(1, "Setting up the IDE", self.idle), LessonScreen(2, "Basic syntax", self.syn), @@ -77,12 +94,21 @@ def __init__(self, master=None): ProjectScreen(1, PROJ, "YOU SAX!!!", None), ProjectScreen(1, PROJ, "Next time, try to aim *inside* the ocean", None), ProjectScreen(1, PROJ, "\"Insanity: doing the same thing over and over and expecting different results\" - Albert Einstein", None), + ProjectScreen(1, PROJ, "Game loop-da-loops", None), ProjectScreen(1, PROJ, "Testing, one, two, three", None), - ProjectScreen(1, PROJ, "Loop. Loop-edy loop loop la-loop loop", None), ProjectScreen(1, PROJ, "Are you sure you won?", None), ProjectScreen(1, PROJ, "Stop cheating!", None), - ProjectScreen(1, PROJ, "Extra credit!", None) + ProjectScreen(1, PROJ, "Adding the other board", None), + ProjectScreen(1, PROJ, "Now, hide your ships", None), + ProjectScreen(1, PROJ, "Pirates of the Caribbean", None), + ProjectScreen(1, PROJ, "You are terminated!", None) ] + self.units = [ + {"name": "The fundmentals of Python programming", "started": True}, + {"name": "Turtle graphics", "started": False}, + {"name": "GUI with Tkinter", "started": False} + ] + self.pr1 = PROJ super().__init__(master) self.master = master self.master.title("LearnPythonWithPython") @@ -90,8 +116,11 @@ def __init__(self, master=None): def s_init(self, unitNum): self.new(0) - s = "self.unit_"+str(unitNum)+"()" - exec(s) + for s in eval("self.u"+str(unitNum)): + self.scrnBtn(s) + self.projBtn("Unit project: "+eval("self.pr"+str(unitNum)), self.p_init(unitNum)) + if self.units[unitNum]["started"]: + Button(self, text="Unit"+str(unitNum+1)+self.units[unitNum].text, command=eval("self.u"+str(unitNum+1))).place(relx=1.0, rely=1.0, anchor=SE) self.master.geometry(self.homeBounds) def p_init(self, unitNum): @@ -99,18 +128,12 @@ def p_init(self, unitNum): s = "self.proj_"+str(unitNum)+"()" exec(s) self.master.geometry(self.homeBounds) - - def unit_1(self): - for s in self.u1: - self.cenBtn(s.showAs, s.func) - self.cenBtn("Unit project: Battleship", lambda: self.p_init(1)) - Button(self, text="Unit 2: Turtle graphics", command=None).place(relx=1.0, rely=1.0, anchor=SE) def proj_1(self): for s in self.p1: self.cenBtn(s.showAs, s.func) self.cenBtn("Back to unit 1", lambda: self.s_init(1)) - self.cenBtn("Unit 2: Turtle graphics", None) #s_init(2) + self.cenBtn("Unit 2: Turtle graphics", self.s_init(2)) def idle(self): self.new_lesson(1, 1) @@ -221,15 +244,15 @@ def bs_board_list(self): def bs_print_board(self): self.new_proj(1, 3) - self.multiLbl("""We need a method to print out the board good because we don't want it looking like [['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O']]. We want to join it. So the function called print_board which will take one argument, board and will have a for loop in which we are doing a command *for row in board*. The command is a method I haven't told you about yet. It's called join. The syntax is like this: str.join(list). For example, we want to print O O O O O where it's currently printing ['O', 'O', 'O', 'O', 'O'] so we do " ".join(board) to join the board with spaces. That is our command in the for loop.""") + self.multiLbl("""We need a method to print out the board good because we don't want it looking like [['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O']]. We want to join it. So the function called print_board which will take no arguments and will have a for loop in which we are doing a command *for row in board*. The command is a method I haven't told you about yet. It's called join. The syntax is like this: str.join(list). For example, we want to print O O O O O where it's currently printing ['O', 'O', 'O', 'O', 'O'] so we do " ".join(board) to join the board with spaces. We surround that with a print( and ) and then we have our print.""") def bs_hide(self): self.new_proj(1, 4) - self.multiLbl("""Let's "hide" the ship. To do this, we need to have a few lines of code. First we need to do this thing called import-ing. To import, you can either do import module_name or from module_name import what_you_want_from_the_module (or you can add as what_you_want_to_called). So we need to do a from module_name import what_you_want. We want to import randint from random. Soooo, just use your logic and from random import randint. Then you need to set a variable *col =* to a *randint* from *(0,* to *len(boardp[0])-1)* and set *row =* to a *randint* from *(0,* to *len(board)-1)*. Then you have the locations of the ship""") + self.multiLbl("""Let's "hide" the ship. To do this, we need to have a few lines of code. First we need to do this thing called import-ing. To import, you can either do import module_name or from module_name import what_you_want_from_the_module (or you can add as what_you_want_to_call_it). So we need to do a from module_name import what_you_want. We want to import randint from random. Soooo, just use your logic and from random import randint. Then you need to set a variable *col =* to a *randint* from *(0,* to *len(boarp[0])-1)* and set *row =* to a *randint* from *(0,* to *len(board)-1)*. Then you have the locations of the ship""") def bs_seek(self): self.new_proj(1, 5) - self.multiLbl("""Let's "seek" the ship. To do this, we need to ask the user for *input()*. Let's set a variable called *row_guess =* to *input("Guess row: ") and set *col_guess =* to *input("Guess column")*. Then we have a guess that literally does nothing (so far).""") + self.multiLbl("""Let's "seek" the ship. To do this, we need to ask the user for *input()*. Let's set a variable called *row_guess =* to *input("Guess row: ") and set *col_guess =* to *input("Guess column: ")*. Then we have a guess that literally does nothing (so far).""") def bs_debug(self): self.new_proj(1, 6) @@ -241,4 +264,4 @@ def bs_win(self): def bs_lose(self): self.new_proj(1, 8) - self.multiLbl("""To make a losing condition, we have to add an else. The else co""") \ No newline at end of file + self.multiLbl("""To make a losing condition, we have to add an else. The else condition will be if the player gets it wrong.""") diff --git a/window.py b/window.py index 59e9672..6217afd 100644 --- a/window.py +++ b/window.py @@ -9,13 +9,11 @@ def close_app(self): self.master.destroy() def cenBtn(self, txt, func): - b = Button(self, text=txt, command=func) - b.pack() - + btn = Button(self, text=txt, command=func) + btn.pack() + return btn def choBtn(self, txt, func, x, y): - b = Button(self, text=txt, command=func) - b.place(x=x, y=y) - return b + return Button(self, text=txt, command=func).place(x=x, y=y) def btnQuit(self): return self.choBtn("Quit", self.close_app, 0, 0) @@ -24,10 +22,14 @@ def btnBack(self, unit): return self.choBtn("Back", lambda: self.s_init(unit), 0, 0) def btnNext(self, nextScreen): - return Button(self, text="Next", command=nextScreen).place(relx=1.0, rely=1.0, anchor=SE) + btn = Button(self, text="Next", command=nextScreen) + btn.place(relx=1.0, rely=1.0, anchor=SE) + return btn def btnPrev(self, prevScreen): - return Button(self, text="Previous", command=prevScreen).place(relx=0.0, rely=1.0, anchor=SW) + btn = Button(self, text="Previous", command=prevScreen) + btn.place(relx=0.0, rely=1.0, anchor=SW) + return btn def choLbl(self, txt, x, y): lbl = Label(self, text=txt) @@ -35,10 +37,14 @@ def choLbl(self, txt, x, y): return lbl def cenLbl(self, txt): - return Label(self, text=txt).pack() - + lbl = Label(self, text=txt) + lbl.pack() + return lbl + def multiLbl(self, txt): - return Label(self, text=txt, wraplength=750).pack(anchor=W) + lbl = Label(self, text=txt, wraplength=750) + lbl.pack(anchor=W) + return lbl def new(self, unit): for widget in self.winfo_children():