def test_pen_do_something_else_working(self): import sys from io import StringIO saved_stdout = sys.stdout pen = Pen(color='red') try: out = StringIO() sys.stdout = out pen.do_something_else() output = out.getvalue().strip() self.assertEqual('red', output) finally: sys.stdout = saved_stdout
def __init__(self, game, screen, rows, cols, model): self.wn = screen self.num_rows = rows self.num_cols = cols self.model = model self.pixels = [[] for row in range(rows)] self.pixel_gap = 560 / self.num_cols self.get_x = lambda col: self.pixel_gap * col + 10 self.get_y = lambda row: -self.pixel_gap * row + 270 for row in range(rows): for col in range(cols): pixel = Pixel(self, row, col) self.pen = Pen(screen=self.wn, grid=self) self.game = game
class Pattern: def __init__(self): self.pen = Pen() self.pen.callback = self.refresh pygame.init() self.size = 1200, 1080 self.screen = pygame.display.set_mode(self.size) def render(self): self.screen.fill([25, 23, 28]) self.pen.get_state().position = [200, 1000] self.pen.draw([ 'F[+F]F[-F]F', 'F[+F]F', 'F[-F]F', ], 5, np.deg2rad(25.7)) self.pen.get_state().position = [550, 1000] self.pen.draw('F[+F]F[-F][F]', 5, np.deg2rad(20)) self.pen.get_state().position = [850, 1000] self.pen.draw('FF-[-F+F+F]+[+F-F-F]', 4, np.deg2rad(22.5)) pygame.display.flip() while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() def refresh(self, prev_state, state): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() pygame.draw.line(self.screen, self.colorize(prev_state, state), tuple(prev_state.position), tuple(state.position)) #pygame.display.flip() def colorize(self, prev_state, state): position = state.position col_part = [ self.clamp(255 * np.sin(position[0]/self.size[0]), 0, 255), self.clamp(255 * np.cos(position[1]/self.size[1]), 0, 255), ] return col_part[1], col_part[0], 55 def clamp(self, n, minn, maxn): return max(min(maxn, n), minn)
def test_pen_write_word_with_float_ink_result(self): pen = Pen(ink_container_value=10, size_letter=0.7) self.assertEqual('word', pen.write('word')) # 10 ink - 4 letters * (with size 0.7) = 7.2 self.assertEqual(7.2, int(pen.ink_container_value)) # simulating java int type
def test_pen_write_no_word_with_negative_size_letter(self): pen = Pen(ink_container_value=100, size_letter=-2.0) self.assertEqual('', pen.write('word'))
def test_pen_write_whitespaces_do_not_use_ink(self): pen = Pen(ink_container_value=2000) text = 'A basic test runner implementation that outputs results to a stream.' text_without_whitespaces = text.replace(' ', '') self.assertEqual(text, pen.write(text)) self.assertEqual(2000 - len(text_without_whitespaces), pen.ink_container_value)
def test_pen_write_no_word_with_no_ink(self): pen = Pen(ink_container_value=0) self.assertEqual('', pen.write('school'))
def test_pen_init_no_parameters(self): pen = Pen() self.assertEqual(1000, pen.ink_container_value) self.assertEqual(1.0, pen.size_letter) self.assertEqual('blue', pen.color)
def test_pen_write_part_of_word_with_not_enough_ink(self): pen = Pen(ink_container_value=5, size_letter=2.5) self.assertEqual('sc', pen.write('school')) # 5 ink - 2 letters * (with size 2.5) = 0 self.assertEqual(0, pen.ink_container_value)
from Pen import Pen p1 = Pen('Parker', 10, 'Black') p1.write() p2 = Pen('Cello', 20, 'blue') p2.write() p3 = Pen('Raynolds', 0, 'Green') p3.write()
def test_pen_get_color_working(self): pen = Pen(color='green') self.assertEqual('green', pen.get_color())
def test_pen_check_pen_state_working(self): pen = Pen(ink_container_value=10) self.assertTrue(pen.check_pen_state())
def test_pen_check_pen_state_failing(self): pen = Pen(ink_container_value=0) self.assertFalse(pen.check_pen_state())
def test_pen_init_three_parameters(self): pen = Pen(ink_container_value=1, size_letter=0.9, color='green') self.assertEqual(1, pen.ink_container_value) self.assertEqual(0.9, pen.size_letter) self.assertEqual('green', pen.color)
def test_pen_init_two_parameters(self): pen = Pen(ink_container_value=3000, size_letter=2.5) self.assertEqual(3000, pen.ink_container_value) self.assertEqual(2.5, pen.size_letter) self.assertEqual('blue', pen.color)
def test_pen_init_one_parameter(self): pen = Pen(ink_container_value=90) self.assertEqual(90, pen.ink_container_value) self.assertEqual(1.0, pen.size_letter) self.assertEqual('blue', pen.color)
class Grid: def __init__(self, game, screen, rows, cols, model): self.wn = screen self.num_rows = rows self.num_cols = cols self.model = model self.pixels = [[] for row in range(rows)] self.pixel_gap = 560 / self.num_cols self.get_x = lambda col: self.pixel_gap * col + 10 self.get_y = lambda row: -self.pixel_gap * row + 270 for row in range(rows): for col in range(cols): pixel = Pixel(self, row, col) self.pen = Pen(screen=self.wn, grid=self) self.game = game def initialize_all_pixels(self): ''' One-time only ''' for row in self.pixels: for pixel in row: pixel.neighbors = pixel.get_neighbors() def listen_to_user(self): ''' Use the implemented pen. ''' onmove(self.pen.wn, self.pen.move_handler) self.pen.onclick(self.pen.click_handler) t.listen() def color_clicked_pixel(self, x, y): ''' Using the mouse cord, find the pixel that is clicked, and color it (and it's neighbors). ''' col = int(x - 280) // int(self.pixel_gap) row = int(y) // int(self.pixel_gap) # Convert to correct values row = -row + 13 col = col + 14 if row < 0 or col < 0: # Avoid errors and wrong indexing return self.pixels[row][col].draw(x, y) def convert_location(self, row, col): x = self.get_x(col) y = self.get_y(row) return (x, y) def export_grid(self): ''' Export the grid to a numpy array that will be feeded to the Neural Network. ''' all_pixels = [[] for row in range(self.num_rows)] for row in range(self.num_rows): for col in range(self.num_cols): if self.pixels[row][col].is_colored: all_pixels[row].append(1) else: all_pixels[row].append(0) pixel_array = np.array(all_pixels) pixel_array = pixel_array.reshape(1, 28, 28, 1) pixel_array = pixel_array.astype('float32') return pixel_array def make_a_prediction(self): input_grid = self.export_grid() prediction = self.model.predict(input_grid) if max(prediction[0]) < 0.25: # Don't take bad decisions result = None else: result = np.argmax(prediction[0]) if result != None: for balloon in self.game.balloons: if balloon.value == result: balloon.kill() #print("Prediction: {}".format(result)) # Get ready for the next drawing self.reset() def reset(self): ''' Uncolor all pixels. ''' for row in self.pixels: for pixel in row: pixel.is_colored = False pixel.ht()
def test_pen_write_full_word_with_enough_ink(self): pen = Pen() self.assertEqual('school', pen.write('school')) # default 1000 ink - 6 letters * (with default size 1.0) = 994 self.assertEqual(994, pen.ink_container_value)
print("Hari/Tgl: Kamis, 14 Januari 2021 ") print("#" * 34) # main program print("\n-> Create Object Backpack") item_list = Backpack(owner="Bintang", capacity=3) item_list.detail() print("\n-> Backpack.remove()") item_list.remove() print("\n-> Backpack.add() menambahkan Object Eraser") item_list.add(Eraser(owner="Bintang")) print("\n-> Backpack.add() menambahkan Object Pen") item_list.add(Pen(owner="Agastya")) print("\n-> Backpack.add() menambahkan Object Notebook") item_list.add(Notebook(owner="Bintang")) print("\n-> Backpack.add() menambahkan Object Ruler") item_list.add(Ruler(owner="Agastya")) print("\n-> Backpack.remove(4) pada index = 4") item_list.remove(4) print("\n-> Backpack.remove(1) pada index = 1") item_list.remove(1) print("\n-> Backpack.add() menambahkan Object Pencil") item_list.add(Pencil(owner="Bintang"))
def __init__(self): self.pen = Pen() self.pen.callback = self.refresh pygame.init() self.size = 1200, 1080 self.screen = pygame.display.set_mode(self.size)