def test_find_complete_lines(self): tmp = tkinter.Frame(borderwidth=3, relief="solid") ttrs = CanvasGame(tmp, width=400, height=600, borderwidth=3, relief="solid", bg="white", rows=24, columns=16, cell=25) # fill line for j in range(ttrs.columns): ttrs.matrix[j][ttrs.rows - 1] = "blue" ttrs.matrix[j][ttrs.rows - 2] = "red" f = Figure() f.cells.append((1, ttrs.rows - 1)) f.cells.append((1, ttrs.rows - 2)) self.assertTrue(ttrs.line_complete(ttrs.rows - 1)) count = ttrs.find_complete_lines(f) self.assertEqual(count, 2)
def test_hold_figure(self): tmp = tkinter.Frame(borderwidth=3, relief="solid") ttrs = CanvasGame(tmp, width=400, height=600, borderwidth=3, relief="solid", bg="white", rows=24, columns=16, cell=25) f = Figure() ttrs.hold_figure(f) for cell in f.cells: self.assertEqual(ttrs.matrix[cell[0]][cell[1]], f.color)
def test_is_valid_coords(self): tmp = tkinter.Frame(borderwidth=3, relief="solid") ttrs = CanvasGame(tmp, width=400, height=600, borderwidth=3, relief="solid", bg="white", rows=24, columns=16, cell=25) valid_coords = [(1, 1), (2, 9), (3, 3)] not_valid_coords = valid_coords.copy() not_valid_coords.append((-1, -1)) self.assertTrue(ttrs.is_valid_coords(valid_coords)) self.assertFalse(ttrs.is_valid_coords(not_valid_coords)) not_valid_coords = valid_coords.copy() not_valid_coords.append((1, -1)) self.assertFalse(ttrs.is_valid_coords(not_valid_coords))
def test_draw(self): tmp = tkinter.Frame(borderwidth=3, relief="solid") ttrs = CanvasGame(tmp, width=400, height=600, borderwidth=3, relief="solid", bg="white", rows=24, columns=16, cell=25) old_value = ttrs.matrix[1][1] ttrs.matrix[1][1] = 'red' ttrs.draw() self.assertNotEqual(ttrs.rects[1, 1], old_value) ttrs.redraw() self.assertNotEqual(ttrs.rects[1, 1], old_value)
def first_screen(self): """Start screen control""" self.control = Frame(borderwidth=3, relief="solid", bg="white") self.control.grid(row=0, column=0, sticky=N + E + S + W) header_im = PhotoImage(file="tetris.png") self.control.header = Label(self.control, image=header_im, borderwidth=3, relief="solid", width=400, bg="white") self.control.header.image = header_im self.control.header.grid(row=0, columnspan=3, sticky=N + E + S + W) self.control.canvas = CanvasGame(self.control, width=400, height=600, borderwidth=3, relief="solid", bg="white", rows=24, columns=16, cell=25) self.control.canvas.grid(row=1, column=0, rowspan=7) self.control.canvas_next = Canvas(self.control, width=150, height=200, borderwidth=3, relief="solid", bg="white") self.control.canvas_next.grid(row=1, column=1, columnspan=2, sticky=N + E + S + W) self.control = Frame(borderwidth=3, relief="solid", bg="white") self.control.grid(row=0, column=0, sticky=N + E + S + W) self.control.best_string = StringVar() self.control.best_string.set(f"Best: {self.best}") self.control.score_string = StringVar() self.control.score_string.set(f"Score: {self.score}") header_im = PhotoImage(file="tetris.png") self.control.header = Label(self.control, image=header_im, borderwidth=3, relief="solid", width=615, bg="white") self.control.header.image = header_im self.control.header.grid(row=0, columnspan=4, sticky=N + E + S + W) self.control.score = Label(self.control, textvariable=self.control.score_string, borderwidth=3, relief="solid", font=("Liberation Sans", 14), bg="white") self.control.score.grid(row=2, column=1, columnspan=2, sticky=N + E + S + W) self.control.new_game = Button(self.control, text="New Game", command=self.second_screen, borderwidth=3, relief="solid", font=("Liberation Sans", 14), bg="white") self.control.new_game.grid(row=4, column=1, columnspan=2, sticky=N + E + S + W) self.control.best = Label(self.control, textvariable=self.control.best_string, borderwidth=3, relief="solid", font=("Liberation Sans", 14), bg="white") self.control.best.grid(row=6, column=1, columnspan=2, sticky=N + E + S + W) self.control.quit = Button(self.control, text="Quit", command=self.quit, borderwidth=3, relief="solid", font=("Liberation Sans", 14), bg="white") self.control.quit.grid(row=8, column=1, columnspan=2, sticky=N + E + S + W) self.control.grid_rowconfigure(1, minsize=100) self.control.grid_rowconfigure(3, minsize=50) self.control.grid_rowconfigure(5, minsize=50) self.control.grid_rowconfigure(7, minsize=50) self.control.grid_columnconfigure(0, minsize=35)
def second_screen(self): """Game screen control""" self.score = 0 self.lines = 0 self.control = Frame(borderwidth=3, relief="solid") self.control.grid(row=0, column=0, sticky=N + E + S + W) self.control.best_string = StringVar() self.control.best_string.set(f"Best: {self.best}") self.control.score_string = StringVar() self.control.score_string.set(f"Score: {self.score}") self.control.lines_string = StringVar() self.control.lines_string.set(f"Lines: {self.lines}") header_im = PhotoImage(file="tetris.png") self.control.header = Label(self.control, image=header_im, borderwidth=3, relief="solid", width=400, bg="white") self.control.header.image = header_im self.control.header.grid(row=0, columnspan=3, sticky=N + E + S + W) self.control.canvas = CanvasGame(self.control, width=400, height=600, borderwidth=3, relief="solid", bg="white", rows=24, columns=16, cell=25) self.control.canvas.grid(row=1, column=0, rowspan=7) self.control.canvas_next = CanvasGame(self.control, width=200, height=200, borderwidth=3, relief="solid", bg="white", rows=8, columns=8, cell=25) self.control.canvas_next.grid(row=1, column=1, columnspan=2, sticky=N + E + S + W) self.control.lines = Label(self.control, textvariable=self.control.lines_string, borderwidth=3, relief="solid", font=("Liberation Sans", 14), bg="white") self.control.lines.grid(row=3, column=1, columnspan=2, sticky=N + E + S + W) self.control.score = Label(self.control, textvariable=self.control.score_string, borderwidth=3, relief="solid", font=("Liberation Sans", 14), bg="white") self.control.score.grid(row=4, column=1, columnspan=2, sticky=N + E + S + W) self.control.best = Label(self.control, textvariable=self.control.best_string, borderwidth=3, relief="solid", font=("Liberation Sans", 14), bg="white") self.control.best.grid(row=5, column=1, columnspan=2, sticky=N + E + S + W) self.control.pause = Button(self.control, text="Pause", command=self.pause, borderwidth=3, relief="solid", font=("Liberation Sans", 14), bg="white") self.control.pause.grid(row=6, column=1, sticky=N + E + S + W) self.control.quit = Button(self.control, text="Quit", command=self.terminate, borderwidth=3, relief="solid", font=("Liberation Sans", 14), bg="white") self.control.quit.grid(row=6, column=2, sticky=N + E + S + W) self.control.canvas.draw() self.control.canvas_next.draw() self.next_figure = None self.create_figure() self._job = None self.bind_all("<Left>", self.move_left) self.bind_all("<Right>", self.move_right) self.bind_all("<Up>", self.rotate) self.bind_all("<Down>", self.gravity) self.tick()