From 6f31a587b93b9375664591e7f1cf0d4b67207456 Mon Sep 17 00:00:00 2001 From: countingthedots Date: Sun, 12 Mar 2023 18:11:31 +0100 Subject: [PATCH 01/10] Moving circle added --- Interface/grid_draw.py | 2 -- Interface/interface.py | 40 -------------------------------------- Interface/vacuum_render.py | 29 +++++++++++++++++++++------ main.py | 2 +- 4 files changed, 24 insertions(+), 49 deletions(-) delete mode 100644 Interface/interface.py diff --git a/Interface/grid_draw.py b/Interface/grid_draw.py index 92471f9..0f74e86 100644 --- a/Interface/grid_draw.py +++ b/Interface/grid_draw.py @@ -32,8 +32,6 @@ class GridDraw(): self.screen = pygame.display.set_mode((self.width, self.height)) def start_draw(self): - for event in pygame.event.get(): - if event.type == pygame.QUIT: sys.exit() self.screen.fill(Colors.BLACK) def end_draw(self): diff --git a/Interface/interface.py b/Interface/interface.py deleted file mode 100644 index 8fc8971..0000000 --- a/Interface/interface.py +++ /dev/null @@ -1,40 +0,0 @@ -import sys -import pygame -from .helpers import * - - -def initialize_interface(tiles_x, tiles_y): - - size = width, height = 600, 600 - screen = create_screen(size) - pygame.display.set_caption('Epic AI Vacuum Cleaner') - tile_width = width / tiles_x - tile_height = height / tiles_y - x = tile_width / 2 - y = tile_height / 2 - radius = 15 - - while True: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - sys.exit() - userInput = pygame.key.get_pressed() - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_LEFT: - x -= tile_width - if event.key == pygame.K_RIGHT: - x += tile_width - if event.key == pygame.K_UP: - y -= tile_height - if event.key == pygame.K_DOWN: - y += tile_height - - pygame.time.delay(10) - pygame.display.update() - screen.fill(BLACK) - # rect(screen, WHITE, 50,50,90,90) - - draw_board(screen, WHITE, width, height, tiles_x, tiles_y) - pygame.draw.circle(screen, (178, 172, 136), (int(x), int(y)), radius) - - pygame.display.flip() diff --git a/Interface/vacuum_render.py b/Interface/vacuum_render.py index 29f1b52..3732c3e 100644 --- a/Interface/vacuum_render.py +++ b/Interface/vacuum_render.py @@ -1,14 +1,31 @@ from Interface.grid_draw import GridDraw, Colors +import sys +import pygame # dummy function def initial_draw(): grid = GridDraw(500, 500) - + tile_width = 500 / 10 + tile_height = 500 / 10 + x = tile_width / 2 + y = tile_height / 2 + radius = 15 while True: grid.start_draw() - grid.board(10, 10) - grid.circle(75, 75, 20, color=Colors.RED) - grid.circle(225, 175, 20, color=Colors.GREEN) - - grid.end_draw() + for event in pygame.event.get(): + if event.type == pygame.QUIT: + sys.exit() + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_LEFT: + x -= tile_width + if event.key == pygame.K_RIGHT: + x += tile_width + if event.key == pygame.K_UP: + y -= tile_height + if event.key == pygame.K_DOWN: + y += tile_height + grid.circle(x, y, 20, color=Colors.RED) + grid.end_draw() + pygame.time.delay(10) + diff --git a/main.py b/main.py index 45c51de..a857b41 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,3 @@ from Interface.vacuum_render import initial_draw -initial_draw() \ No newline at end of file +initial_draw() From 63912b9b246712ee0c14b110044d2d523652b1e8 Mon Sep 17 00:00:00 2001 From: countingthedots Date: Sun, 12 Mar 2023 20:10:18 +0100 Subject: [PATCH 02/10] movement as a seperate function --- Interface/vacuum_render.py | 22 ++++++---------------- main.py | 2 +- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/Interface/vacuum_render.py b/Interface/vacuum_render.py index 3732c3e..e19baac 100644 --- a/Interface/vacuum_render.py +++ b/Interface/vacuum_render.py @@ -1,30 +1,20 @@ from Interface.grid_draw import GridDraw, Colors import sys import pygame +from Interface.movement import moving_cleaner # dummy function -def initial_draw(): - grid = GridDraw(500, 500) - tile_width = 500 / 10 - tile_height = 500 / 10 +def initial_draw(grid_width, grid_height): + grid = GridDraw(grid_width, grid_height) + tile_width = grid_width / 10 + tile_height = grid_height / 10 x = tile_width / 2 y = tile_height / 2 radius = 15 while True: grid.start_draw() grid.board(10, 10) - for event in pygame.event.get(): - if event.type == pygame.QUIT: - sys.exit() - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_LEFT: - x -= tile_width - if event.key == pygame.K_RIGHT: - x += tile_width - if event.key == pygame.K_UP: - y -= tile_height - if event.key == pygame.K_DOWN: - y += tile_height + (x,y) = moving_cleaner(grid_width,grid_height,tile_width,tile_height, x, y) grid.circle(x, y, 20, color=Colors.RED) grid.end_draw() pygame.time.delay(10) diff --git a/main.py b/main.py index a857b41..2b8ee4a 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,3 @@ from Interface.vacuum_render import initial_draw -initial_draw() +initial_draw(500, 500) From 9db0cc3cfdb3b584c4f2b0ff9d7dd682dc8fad44 Mon Sep 17 00:00:00 2001 From: countingthedots Date: Sun, 12 Mar 2023 20:13:00 +0100 Subject: [PATCH 03/10] movement as a seperate function --- Interface/movement.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Interface/movement.py diff --git a/Interface/movement.py b/Interface/movement.py new file mode 100644 index 0000000..cbcd2c0 --- /dev/null +++ b/Interface/movement.py @@ -0,0 +1,17 @@ +import pygame +import sys + +def moving_cleaner(grid_width, grid_height,tile_width,tile_height, x, y): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + sys.exit() + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_LEFT and x>(tile_width / 2): + x -= tile_width + if event.key == pygame.K_RIGHT and x<(grid_width-(tile_width / 2)): + x += tile_width + if event.key == pygame.K_UP and y>(tile_height / 2): + y -= tile_height + if event.key == pygame.K_DOWN and y<(grid_height-(tile_height / 2)): + y += tile_height + return(x,y) From d080b047633cb91e7b220e252cea3dcb07536ecf Mon Sep 17 00:00:00 2001 From: Mateusz Dokowicz Date: Thu, 16 Mar 2023 20:33:22 +0100 Subject: [PATCH 04/10] formatting and comments --- Interface/grid_draw.py | 34 +++++++++++++++++----------------- Interface/movement.py | 20 ++++++++++++++------ Interface/vacuum_render.py | 11 ++++++----- 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/Interface/grid_draw.py b/Interface/grid_draw.py index 0f74e86..e3900e7 100644 --- a/Interface/grid_draw.py +++ b/Interface/grid_draw.py @@ -1,33 +1,33 @@ import sys import pygame -class Colors(): + +class Colors: BLACK = 0, 0, 0 WHITE = 255, 255, 255 RED = 255, 0, 0 GREEN = 0, 255, 0 + DEFAULT_COLOR = Colors.WHITE -def default_color(func): + +def default_color(func): def wrap(*args, **kwargs): - if 'color' not in kwargs: - kwargs['color'] = DEFAULT_COLOR + if "color" not in kwargs: + kwargs["color"] = DEFAULT_COLOR result = func(*args, **kwargs) return result + return wrap -class GridDraw(): - def __init__( - self, - width = None, - height = None, - background = None - ): + +class GridDraw: + def __init__(self, width=None, height=None, background=None): self.width = width if width != None else 100 self.height = height if height != None else 100 self.background = background if background != None else Colors.BLACK - + pygame.init() self.screen = pygame.display.set_mode((self.width, self.height)) @@ -38,11 +38,11 @@ class GridDraw(): pygame.display.flip() @default_color - def line(self, x_1, y_1, x_2, y_2, color = None): + def line(self, x_1, y_1, x_2, y_2, color=None): pygame.draw.line(self.screen, color, (x_1, y_1), (x_2, y_2)) - + @default_color - def board(self, tiles_x, tiles_y, color = None): + def board(self, tiles_x, tiles_y, color=None): tiles_width = self.width / tiles_x tiles_height = self.height / tiles_y @@ -53,5 +53,5 @@ class GridDraw(): self.line(0, tiles_height * i, self.width, tiles_height * i, color=color) @default_color - def circle(self, x, y, radius, color = None): - pygame.draw.circle(self.screen, color, (x, y), radius) \ No newline at end of file + def circle(self, x, y, radius, color=None): + pygame.draw.circle(self.screen, color, (x, y), radius) diff --git a/Interface/movement.py b/Interface/movement.py index cbcd2c0..a984e56 100644 --- a/Interface/movement.py +++ b/Interface/movement.py @@ -1,17 +1,25 @@ import pygame import sys -def moving_cleaner(grid_width, grid_height,tile_width,tile_height, x, y): + +def moving_cleaner(grid_width, grid_height, tile_width, tile_height, x, y): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: - if event.key == pygame.K_LEFT and x>(tile_width / 2): + # go left + if event.key == pygame.K_LEFT and x > (tile_width / 2): x -= tile_width - if event.key == pygame.K_RIGHT and x<(grid_width-(tile_width / 2)): + + # go right + if event.key == pygame.K_RIGHT and x < (grid_width - (tile_width / 2)): x += tile_width - if event.key == pygame.K_UP and y>(tile_height / 2): + + # go up + if event.key == pygame.K_UP and y > (tile_height / 2): y -= tile_height - if event.key == pygame.K_DOWN and y<(grid_height-(tile_height / 2)): + + # go down + if event.key == pygame.K_DOWN and y < (grid_height - (tile_height / 2)): y += tile_height - return(x,y) + return (x, y) diff --git a/Interface/vacuum_render.py b/Interface/vacuum_render.py index e19baac..7e28bd8 100644 --- a/Interface/vacuum_render.py +++ b/Interface/vacuum_render.py @@ -3,7 +3,7 @@ import sys import pygame from Interface.movement import moving_cleaner -# dummy function + def initial_draw(grid_width, grid_height): grid = GridDraw(grid_width, grid_height) tile_width = grid_width / 10 @@ -11,11 +11,12 @@ def initial_draw(grid_width, grid_height): x = tile_width / 2 y = tile_height / 2 radius = 15 + + # rendering loop while True: grid.start_draw() grid.board(10, 10) - (x,y) = moving_cleaner(grid_width,grid_height,tile_width,tile_height, x, y) + (x, y) = moving_cleaner(grid_width, grid_height, tile_width, tile_height, x, y) grid.circle(x, y, 20, color=Colors.RED) - grid.end_draw() - pygame.time.delay(10) - + grid.end_draw() + pygame.time.delay(10) From 3696636245a60d447ec03e588a4546722a40e8d3 Mon Sep 17 00:00:00 2001 From: Mateusz Dokowicz Date: Thu, 16 Mar 2023 21:38:42 +0100 Subject: [PATCH 05/10] Code readability --- Interface/vacuum_render.py | 19 +++++++++++++++---- main.py | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Interface/vacuum_render.py b/Interface/vacuum_render.py index 7e28bd8..d4d743a 100644 --- a/Interface/vacuum_render.py +++ b/Interface/vacuum_render.py @@ -4,10 +4,21 @@ import pygame from Interface.movement import moving_cleaner -def initial_draw(grid_width, grid_height): +def initial_draw(grid_dimensions, board_size): + # window name + pygame.display.set_caption("AI Vacuum Cleaner") + + # define array for grid + border_array = [["" for j in range(board_size)] for i in range(board_size)] + + # set window dimension + grid_width = grid_dimensions + grid_height = grid_dimensions + + # FIXME @countingthedots: please tell me what is going on there and why??? grid = GridDraw(grid_width, grid_height) - tile_width = grid_width / 10 - tile_height = grid_height / 10 + tile_width = grid_width / board_size + tile_height = grid_height / board_size x = tile_width / 2 y = tile_height / 2 radius = 15 @@ -15,7 +26,7 @@ def initial_draw(grid_width, grid_height): # rendering loop while True: grid.start_draw() - grid.board(10, 10) + grid.board(board_size, board_size) (x, y) = moving_cleaner(grid_width, grid_height, tile_width, tile_height, x, y) grid.circle(x, y, 20, color=Colors.RED) grid.end_draw() diff --git a/main.py b/main.py index 2b8ee4a..a9f53f8 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,3 @@ from Interface.vacuum_render import initial_draw -initial_draw(500, 500) +initial_draw(500, 10) From 257f0f7d1ddb5b4f6f52577b33d3ff369a136061 Mon Sep 17 00:00:00 2001 From: Mateusz Dokowicz Date: Thu, 16 Mar 2023 21:49:06 +0100 Subject: [PATCH 06/10] TODO calculate radius --- Interface/grid_draw.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Interface/grid_draw.py b/Interface/grid_draw.py index e3900e7..fb47608 100644 --- a/Interface/grid_draw.py +++ b/Interface/grid_draw.py @@ -53,5 +53,7 @@ class GridDraw: self.line(0, tiles_height * i, self.width, tiles_height * i, color=color) @default_color - def circle(self, x, y, radius, color=None): - pygame.draw.circle(self.screen, color, (x, y), radius) + def circle(self, x, y, color=None): + pygame.draw.circle( + self.screen, color, (x, y), 20 + ) # TODO calculate radius (now 15) in terms of window size. From a5e38a707b6c453cdb9b30256f06af7770175291 Mon Sep 17 00:00:00 2001 From: Mateusz Dokowicz Date: Thu, 16 Mar 2023 21:50:27 +0100 Subject: [PATCH 07/10] calculate radius prep --- Interface/vacuum_render.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Interface/vacuum_render.py b/Interface/vacuum_render.py index d4d743a..4c305e6 100644 --- a/Interface/vacuum_render.py +++ b/Interface/vacuum_render.py @@ -21,13 +21,13 @@ def initial_draw(grid_dimensions, board_size): tile_height = grid_height / board_size x = tile_width / 2 y = tile_height / 2 - radius = 15 + radius = 20 # rendering loop while True: grid.start_draw() grid.board(board_size, board_size) (x, y) = moving_cleaner(grid_width, grid_height, tile_width, tile_height, x, y) - grid.circle(x, y, 20, color=Colors.RED) + grid.circle(x, y, color=Colors.RED) grid.end_draw() pygame.time.delay(10) From 9b7bb1a62c025387ba3db24c94c01058f912e36e Mon Sep 17 00:00:00 2001 From: Mateusz Dokowicz Date: Thu, 16 Mar 2023 21:55:05 +0100 Subject: [PATCH 08/10] rename movingCleaner to robot_movement --- Interface/movement.py | 2 +- Interface/vacuum_render.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Interface/movement.py b/Interface/movement.py index a984e56..546c8f5 100644 --- a/Interface/movement.py +++ b/Interface/movement.py @@ -2,7 +2,7 @@ import pygame import sys -def moving_cleaner(grid_width, grid_height, tile_width, tile_height, x, y): +def robot_movement(grid_width, grid_height, tile_width, tile_height, x, y): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() diff --git a/Interface/vacuum_render.py b/Interface/vacuum_render.py index 4c305e6..7de34a4 100644 --- a/Interface/vacuum_render.py +++ b/Interface/vacuum_render.py @@ -1,7 +1,7 @@ from Interface.grid_draw import GridDraw, Colors import sys import pygame -from Interface.movement import moving_cleaner +from Interface.movement import robot_movement def initial_draw(grid_dimensions, board_size): @@ -27,7 +27,7 @@ def initial_draw(grid_dimensions, board_size): while True: grid.start_draw() grid.board(board_size, board_size) - (x, y) = moving_cleaner(grid_width, grid_height, tile_width, tile_height, x, y) + (x, y) = robot_movement(grid_width, grid_height, tile_width, tile_height, x, y) grid.circle(x, y, color=Colors.RED) grid.end_draw() pygame.time.delay(10) From 2200cf15d75cd5385389d0cd383c0cda45f7aca5 Mon Sep 17 00:00:00 2001 From: Mateusz Dokowicz Date: Thu, 16 Mar 2023 22:08:47 +0100 Subject: [PATCH 09/10] typo 15->20 --- Interface/grid_draw.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Interface/grid_draw.py b/Interface/grid_draw.py index fb47608..ab878a2 100644 --- a/Interface/grid_draw.py +++ b/Interface/grid_draw.py @@ -56,4 +56,4 @@ class GridDraw: def circle(self, x, y, color=None): pygame.draw.circle( self.screen, color, (x, y), 20 - ) # TODO calculate radius (now 15) in terms of window size. + ) # TODO calculate radius (now 20) in terms of window size. From 8d9c4146a4991b605a16506ef9177073fe448756 Mon Sep 17 00:00:00 2001 From: countingthedots Date: Sun, 19 Mar 2023 19:53:54 +0100 Subject: [PATCH 10/10] Fixed radius value --- Interface/grid_draw.py | 4 ++-- Interface/vacuum_render.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Interface/grid_draw.py b/Interface/grid_draw.py index ab878a2..5335dee 100644 --- a/Interface/grid_draw.py +++ b/Interface/grid_draw.py @@ -53,7 +53,7 @@ class GridDraw: self.line(0, tiles_height * i, self.width, tiles_height * i, color=color) @default_color - def circle(self, x, y, color=None): + def circle(self, x, y, radius, color=None): pygame.draw.circle( - self.screen, color, (x, y), 20 + self.screen, color, (x, y), radius ) # TODO calculate radius (now 20) in terms of window size. diff --git a/Interface/vacuum_render.py b/Interface/vacuum_render.py index 7de34a4..b5007fe 100644 --- a/Interface/vacuum_render.py +++ b/Interface/vacuum_render.py @@ -16,18 +16,19 @@ def initial_draw(grid_dimensions, board_size): grid_height = grid_dimensions # FIXME @countingthedots: please tell me what is going on there and why??? + # grid = GridDraw(grid_width, grid_height) tile_width = grid_width / board_size tile_height = grid_height / board_size x = tile_width / 2 y = tile_height / 2 - radius = 20 + radius = tile_height/3 # rendering loop while True: grid.start_draw() grid.board(board_size, board_size) (x, y) = robot_movement(grid_width, grid_height, tile_width, tile_height, x, y) - grid.circle(x, y, color=Colors.RED) + grid.circle(x, y, radius, color=Colors.RED) grid.end_draw() pygame.time.delay(10)