Merge pull request 'Refactor and sprites' (#7) from refactor into main

Reviewed-on: #7
This commit is contained in:
Veronika Polevara 2023-03-21 20:45:49 +01:00
commit a65b07727b
7 changed files with 82 additions and 31 deletions

View File

@ -21,28 +21,53 @@ def default_color(func):
return wrap
class GridDraw:
def __init__(self, width=None, height=None, background=None):
class GridDraw():
def __init__(
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/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)),
'TILE': pygame.transform.scale(pygame.image.load('media/sprites/tile.jpeg'), (self.tile_width, self.tile_height)),
'PEEL': pygame.transform.scale(pygame.image.load('media/sprites/peel.webp'), (self.tile_width, self.tile_height)),
}
pygame.init()
self.screen = pygame.display.set_mode((self.width, self.height))
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,6 +79,16 @@ class GridDraw:
@default_color
def circle(self, x, y, radius, color = None):
pygame.draw.circle(
self.screen, color, (x, y), radius
) # TODO calculate radius (now 20) in terms of window size.
pygame.draw.circle(self.screen, color, (x, y), radius)
def draw_sprite(self, tile_x, tile_y, sprite):
self.screen.blit(
self.sprites[sprite],
(tile_x * self.tile_width, tile_y * self.tile_height)
)
def fill_grid_with_sprite(self, sprite):
for tile_x in range(self.tiles_x):
for tile_y in range(self.tiles_y):
self.draw_sprite(tile_x, tile_y, sprite)

View File

@ -1,34 +1,50 @@
from Interface.grid_draw import GridDraw, Colors
import sys
import pygame
from Interface.movement import robot_movement
from random import randint
GRID_SIZE_X = 10
GRID_SIZE_Y = 10
def initial_draw(grid_dimensions, board_size):
# dummy function
def initial_draw():
# 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)]
grid = GridDraw(800, 800, GRID_SIZE_X, GRID_SIZE_Y)
# set window dimension
grid_width = grid_dimensions
grid_height = grid_dimensions
x = 2
y = 2
peels = []
for _ in range(10):
temp_x = randint(0, GRID_SIZE_X)
temp_y = randint(0, GRID_SIZE_Y)
peels.append((temp_x, temp_y))
# 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 = 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, radius, color=Colors.RED)
grid.end_draw()
pygame.time.delay(10)
grid.fill_grid_with_sprite('TILE')
grid.board()
for peel in peels:
grid.draw_sprite(peel[0], peel[1], "PEEL")
grid.draw_sprite(x, y, "VACUUM")
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
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)

View File

@ -1,3 +1,3 @@
from Interface.vacuum_render import initial_draw
initial_draw(500, 10)
initial_draw()

BIN
media/sprites/peel.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

BIN
media/sprites/tile.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

BIN
media/sprites/vacuum.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

BIN
media/sprites/wall.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB