refactor 1

This commit is contained in:
Veronika Polevara 2023-03-12 19:33:57 +01:00
parent 9c4b811c98
commit 2ea0e3b27d
4 changed files with 59 additions and 21 deletions

View File

@ -22,11 +22,25 @@ class GridDraw():
self,
width = None,
height = None,
tiles_x = None,
tiles_y = None,
background = None
):
self.width = width if width != None else 100
self.height = height if height != None else 100
self.tiles_x = tiles_x if tiles_x != None else 10
self.tiles_y = tiles_y if tiles_y != None else 10
self.tile_width = self.width / self.tiles_x
self.tile_height = self.height / self.tiles_y
self.background = background if background != None else Colors.BLACK
self.sprites = {
'VACUUM': pygame.transform.scale(pygame.image.load('media/sprites/robot_vacuum.png'), (self.tile_width, self.tile_height)),
'WALL': pygame.transform.scale(pygame.image.load('media/sprites/wall.png'), (self.tile_width, self.tile_height)),
}
pygame.init()
self.screen = pygame.display.set_mode((self.width, self.height))
@ -34,15 +48,20 @@ class GridDraw():
def start_draw(self):
self.screen.fill(Colors.BLACK)
def end_draw(self):
def end_draw(self, delay = None):
pygame.display.flip()
delay = delay if delay != None else 10
pygame.time.delay(delay)
@default_color
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 = None, tiles_y = None, color = None):
tiles_x = tiles_x if tiles_x != None else self.tiles_x
tiles_y = tiles_y if tiles_y != None else self.tiles_y
tiles_width = self.width / tiles_x
tiles_height = self.height / tiles_y
@ -54,4 +73,16 @@ class GridDraw():
@default_color
def circle(self, x, y, radius, color = None):
pygame.draw.circle(self.screen, color, (x, y), radius)
pygame.draw.circle(self.screen, color, (x, y), radius)
def draw_vacuum(self, tile_x, tile_y):
self.screen.blit(
self.sprites['VACUUM'],
(tile_x * self.tile_width, tile_y * self.tile_height)
)
def draw_wall(self, tile_x, tile_y):
self.screen.blit(
self.sprites['WALL'],
(tile_x * self.tile_width, tile_y * self.tile_height)
)

View File

@ -2,30 +2,37 @@ from Interface.grid_draw import GridDraw, Colors
import sys
import pygame
GRID_SIZE_X = 10
GRID_SIZE_Y = 10
# 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
grid = GridDraw(500, 500, GRID_SIZE_X, GRID_SIZE_Y)
x = 2
y = 2
while True:
grid.start_draw()
grid.board(10, 10)
grid.board()
grid.draw_wall(0, 0)
grid.draw_vacuum(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:
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)
match event.key:
case pygame.K_LEFT:
x = (x - 1 + GRID_SIZE_X) % GRID_SIZE_X
case pygame.K_RIGHT:
x = (x + 1 + GRID_SIZE_X) % GRID_SIZE_X
case pygame.K_UP:
y = (y - 1 + GRID_SIZE_Y) % GRID_SIZE_Y
case pygame.K_DOWN:
y = (y + 1 + GRID_SIZE_Y) % GRID_SIZE_Y
grid.end_draw(delay=10)

Binary file not shown.

After

Width:  |  Height:  |  Size: 328 KiB

BIN
media/sprites/wall.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB