add sprites and refactor

This commit is contained in:
Veronika Polevara 2023-03-21 20:29:35 +01:00
parent 2ea0e3b27d
commit ff3f344fca
6 changed files with 25 additions and 11 deletions

View File

@ -38,8 +38,10 @@ class GridDraw():
self.background = background if background != None else Colors.BLACK self.background = background if background != None else Colors.BLACK
self.sprites = { self.sprites = {
'VACUUM': pygame.transform.scale(pygame.image.load('media/sprites/robot_vacuum.png'), (self.tile_width, self.tile_height)), '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)), '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()
@ -75,14 +77,14 @@ class GridDraw():
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): def draw_sprite(self, tile_x, tile_y, sprite):
self.screen.blit( self.screen.blit(
self.sprites['VACUUM'], self.sprites[sprite],
(tile_x * self.tile_width, tile_y * self.tile_height) (tile_x * self.tile_width, tile_y * self.tile_height)
) )
def draw_wall(self, tile_x, tile_y): def fill_grid_with_sprite(self, sprite):
self.screen.blit( for tile_x in range(self.tiles_x):
self.sprites['WALL'], for tile_y in range(self.tiles_y):
(tile_x * self.tile_width, tile_y * self.tile_height) self.draw_sprite(tile_x, tile_y, sprite)
)

View File

@ -1,23 +1,35 @@
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
GRID_SIZE_X = 10 GRID_SIZE_X = 10
GRID_SIZE_Y = 10 GRID_SIZE_Y = 10
# dummy function # dummy function
def initial_draw(): def initial_draw():
grid = GridDraw(500, 500, GRID_SIZE_X, GRID_SIZE_Y) grid = GridDraw(800, 800, GRID_SIZE_X, GRID_SIZE_Y)
x = 2 x = 2
y = 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))
while True: while True:
grid.start_draw() grid.start_draw()
# grid.draw_vacuum(x, y)
grid.fill_grid_with_sprite('TILE')
grid.board() grid.board()
grid.draw_wall(0, 0) for peel in peels:
grid.draw_vacuum(x, y) grid.draw_sprite(peel[0], peel[1], "PEEL")
grid.draw_sprite(x, y, "VACUUM")
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:

BIN
media/sprites/peel.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 328 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