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

This reverts commit a65b07727b, reversing
changes made to 4ec034d548.
This commit is contained in:
Mateusz Dokowicz 2023-03-21 21:15:08 +01:00
parent a65b07727b
commit 51a629285d
7 changed files with 31 additions and 82 deletions

View File

@ -21,53 +21,28 @@ def default_color(func):
return wrap return wrap
class GridDraw():
def __init__( class GridDraw:
self, def __init__(self, width=None, height=None, background=None):
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, delay = None): def end_draw(self):
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 = None, tiles_y = None, color = None): def board(self, tiles_x, tiles_y, 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
@ -78,17 +53,7 @@ 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(self.screen, color, (x, y), radius) pygame.draw.circle(
self.screen, color, (x, y), radius
def draw_sprite(self, tile_x, tile_y, sprite): ) # TODO calculate radius (now 20) in terms of window size.
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,50 +1,34 @@
from Interface.grid_draw import GridDraw, Colors from Interface.grid_draw import GridDraw, Colors
import sys import sys
import pygame import pygame
from random import randint from Interface.movement import robot_movement
GRID_SIZE_X = 10
GRID_SIZE_Y = 10
# dummy function def initial_draw(grid_dimensions, board_size):
def initial_draw():
# window name # window name
pygame.display.set_caption("AI Vacuum Cleaner") pygame.display.set_caption("AI Vacuum Cleaner")
grid = GridDraw(800, 800, GRID_SIZE_X, GRID_SIZE_Y) # define array for grid
border_array = [["" for j in range(board_size)] for i in range(board_size)]
x = 2 # set window dimension
y = 2 grid_width = grid_dimensions
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.fill_grid_with_sprite('TILE') grid.board(board_size, board_size)
grid.board() (x, y) = robot_movement(grid_width, grid_height, tile_width, tile_height, x, y)
grid.circle(x, y, radius, color=Colors.RED)
for peel in peels: grid.end_draw()
grid.draw_sprite(peel[0], peel[1], "PEEL") pygame.time.delay(10)
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 from Interface.vacuum_render import initial_draw
initial_draw() initial_draw(500, 10)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 122 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB