Merge pull request 'Refactor and sprites' (#7) from refactor into main
Reviewed-on: s473601/Machine_learning_2023#7
This commit is contained in:
commit
a65b07727b
@ -21,28 +21,53 @@ def default_color(func):
|
|||||||
|
|
||||||
return wrap
|
return wrap
|
||||||
|
|
||||||
|
class GridDraw():
|
||||||
class GridDraw:
|
def __init__(
|
||||||
def __init__(self, width=None, height=None, background=None):
|
self,
|
||||||
|
width = None,
|
||||||
|
height = None,
|
||||||
|
tiles_x = None,
|
||||||
|
tiles_y = 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/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()
|
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
|
||||||
|
|
||||||
@ -53,7 +78,17 @@ class GridDraw:
|
|||||||
self.line(0, tiles_height * i, self.width, tiles_height * i, color=color)
|
self.line(0, tiles_height * i, self.width, tiles_height * i, color=color)
|
||||||
|
|
||||||
@default_color
|
@default_color
|
||||||
def circle(self, x, y, radius, color=None):
|
def circle(self, x, y, radius, color = None):
|
||||||
pygame.draw.circle(
|
pygame.draw.circle(self.screen, color, (x, y), radius)
|
||||||
self.screen, color, (x, y), radius
|
|
||||||
) # TODO calculate radius (now 20) in terms of window size.
|
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)
|
||||||
|
|
||||||
|
@ -1,34 +1,50 @@
|
|||||||
from Interface.grid_draw import GridDraw, Colors
|
from Interface.grid_draw import GridDraw, Colors
|
||||||
import sys
|
import sys
|
||||||
import pygame
|
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
|
# window name
|
||||||
pygame.display.set_caption("AI Vacuum Cleaner")
|
pygame.display.set_caption("AI Vacuum Cleaner")
|
||||||
|
|
||||||
# define array for grid
|
grid = GridDraw(800, 800, GRID_SIZE_X, GRID_SIZE_Y)
|
||||||
border_array = [["" for j in range(board_size)] for i in range(board_size)]
|
|
||||||
|
|
||||||
# set window dimension
|
x = 2
|
||||||
grid_width = grid_dimensions
|
y = 2
|
||||||
grid_height = grid_dimensions
|
|
||||||
|
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:
|
while True:
|
||||||
grid.start_draw()
|
grid.start_draw()
|
||||||
grid.board(board_size, board_size)
|
grid.fill_grid_with_sprite('TILE')
|
||||||
(x, y) = robot_movement(grid_width, grid_height, tile_width, tile_height, x, y)
|
grid.board()
|
||||||
grid.circle(x, y, radius, color=Colors.RED)
|
|
||||||
grid.end_draw()
|
for peel in peels:
|
||||||
pygame.time.delay(10)
|
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)
|
||||||
|
2
main.py
2
main.py
@ -1,3 +1,3 @@
|
|||||||
from Interface.vacuum_render import initial_draw
|
from Interface.vacuum_render import initial_draw
|
||||||
|
|
||||||
initial_draw(500, 10)
|
initial_draw()
|
||||||
|
BIN
media/sprites/peel.webp
Normal file
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
BIN
media/sprites/tile.jpeg
Normal file
Binary file not shown.
After Width: | Height: | Size: 87 KiB |
BIN
media/sprites/vacuum.png
Normal file
BIN
media/sprites/vacuum.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 122 KiB |
BIN
media/sprites/wall.png
Normal file
BIN
media/sprites/wall.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
Loading…
Reference in New Issue
Block a user