Machine_learning_2023/Interface/grid_draw.py

95 lines
3.0 KiB
Python
Raw Normal View History

2023-03-12 02:07:20 +01:00
import sys
import pygame
2023-03-16 20:33:22 +01:00
class Colors:
2023-03-12 02:07:20 +01:00
BLACK = 0, 0, 0
WHITE = 255, 255, 255
RED = 255, 0, 0
GREEN = 0, 255, 0
2023-03-16 20:33:22 +01:00
2023-03-12 02:07:20 +01:00
DEFAULT_COLOR = Colors.WHITE
2023-03-16 20:33:22 +01:00
def default_color(func):
2023-03-12 02:07:20 +01:00
def wrap(*args, **kwargs):
2023-03-16 20:33:22 +01:00
if "color" not in kwargs:
kwargs["color"] = DEFAULT_COLOR
2023-03-12 02:07:20 +01:00
result = func(*args, **kwargs)
return result
2023-03-16 20:33:22 +01:00
2023-03-12 02:07:20 +01:00
return wrap
class GridDraw():
def __init__(
self,
width = None,
height = None,
2023-03-12 19:33:57 +01:00
tiles_x = None,
tiles_y = None,
2023-03-12 02:07:20 +01:00
background = None
):
self.width = width if width != None else 100
self.height = height if height != None else 100
2023-03-12 19:33:57 +01:00
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
2023-03-12 02:07:20 +01:00
self.background = background if background != None else Colors.BLACK
2023-03-12 19:33:57 +01:00
self.sprites = {
2023-03-21 20:29:35 +01:00
'VACUUM': pygame.transform.scale(pygame.image.load('media/sprites/vacuum.png'), (self.tile_width, self.tile_height)),
2023-03-12 19:33:57 +01:00
'WALL': pygame.transform.scale(pygame.image.load('media/sprites/wall.png'), (self.tile_width, self.tile_height)),
2023-03-21 20:29:35 +01:00
'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)),
2023-03-12 19:33:57 +01:00
}
2023-03-12 02:07:20 +01:00
pygame.init()
self.screen = pygame.display.set_mode((self.width, self.height))
def start_draw(self):
self.screen.fill(Colors.BLACK)
2023-03-12 19:33:57 +01:00
def end_draw(self, delay = None):
2023-03-12 02:07:20 +01:00
pygame.display.flip()
2023-03-12 19:33:57 +01:00
delay = delay if delay != None else 10
pygame.time.delay(delay)
2023-03-12 02:07:20 +01:00
@default_color
2023-03-16 20:33:22 +01:00
def line(self, x_1, y_1, x_2, y_2, color=None):
2023-03-12 02:07:20 +01:00
pygame.draw.line(self.screen, color, (x_1, y_1), (x_2, y_2))
2023-03-16 20:33:22 +01:00
2023-03-12 02:07:20 +01:00
@default_color
2023-03-12 19:33:57 +01:00
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
2023-03-12 02:07:20 +01:00
tiles_width = self.width / tiles_x
tiles_height = self.height / tiles_y
for i in range(1, tiles_x):
self.line(tiles_width * i, 0, tiles_width * i, self.height, color=color)
for i in range(1, tiles_y):
self.line(0, tiles_height * i, self.width, tiles_height * i, color=color)
@default_color
def circle(self, x, y, radius, color = None):
2023-03-12 19:33:57 +01:00
pygame.draw.circle(self.screen, color, (x, y), radius)
2023-03-21 20:29:35 +01:00
def draw_sprite(self, tile_x, tile_y, sprite):
2023-03-12 19:33:57 +01:00
self.screen.blit(
2023-03-21 20:29:35 +01:00
self.sprites[sprite],
2023-03-12 19:33:57 +01:00
(tile_x * self.tile_width, tile_y * self.tile_height)
)
2023-03-21 20:29:35 +01:00
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)