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,27 +22,46 @@ class GridDraw():
self, self,
width = None, width = None,
height = None, height = None,
tiles_x = None,
tiles_y = None,
background = None background = None
): ):
self.width = width if width != None else 100 self.width = width if width != None else 100
self.height = height if height != 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.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() pygame.init()
self.screen = pygame.display.set_mode((self.width, self.height)) self.screen = pygame.display.set_mode((self.width, self.height))
def start_draw(self): def start_draw(self):
self.screen.fill(Colors.BLACK) self.screen.fill(Colors.BLACK)
def end_draw(self): def end_draw(self, delay = None):
pygame.display.flip() pygame.display.flip()
delay = delay if delay != None else 10
pygame.time.delay(delay)
@default_color @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)) pygame.draw.line(self.screen, color, (x_1, y_1), (x_2, y_2))
@default_color @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_width = self.width / tiles_x
tiles_height = self.height / tiles_y tiles_height = self.height / tiles_y
@ -55,3 +74,15 @@ class GridDraw():
@default_color @default_color
def circle(self, x, y, radius, color = None): 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 sys
import pygame import pygame
GRID_SIZE_X = 10
GRID_SIZE_Y = 10
# dummy function # dummy function
def initial_draw(): def initial_draw():
grid = GridDraw(500, 500) grid = GridDraw(500, 500, GRID_SIZE_X, GRID_SIZE_Y)
tile_width = 500 / 10
tile_height = 500 / 10 x = 2
x = tile_width / 2 y = 2
y = tile_height / 2
radius = 15
while True: while True:
grid.start_draw() 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(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
sys.exit() sys.exit()
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT: match event.key:
x -= tile_width case pygame.K_LEFT:
if event.key == pygame.K_RIGHT: x = (x - 1 + GRID_SIZE_X) % GRID_SIZE_X
x += tile_width case pygame.K_RIGHT:
if event.key == pygame.K_UP: x = (x + 1 + GRID_SIZE_X) % GRID_SIZE_X
y -= tile_height case pygame.K_UP:
if event.key == pygame.K_DOWN: y = (y - 1 + GRID_SIZE_Y) % GRID_SIZE_Y
y += tile_height case pygame.K_DOWN:
grid.circle(x, y, 20, color=Colors.RED) y = (y + 1 + GRID_SIZE_Y) % GRID_SIZE_Y
grid.end_draw()
pygame.time.delay(10) 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