Redesigned project architecture to be a little bit more modular #12

Merged
s473555 merged 10 commits from sandbox into main 2023-03-30 18:29:26 +02:00
10 changed files with 144 additions and 0 deletions
Showing only changes of commit 28af8048b9 - Show all commits

0
domain/__init__.py Normal file
View File

5
domain/entity.py Normal file
View File

@ -0,0 +1,5 @@
class Entity:
def __init__(self, x: int, y: int, type: str):
self.x = x
self.y = y
self.type = type

8
domain/vacuum.py Normal file
View File

@ -0,0 +1,8 @@
from domain.entity import Entity
class Vacuum(Entity):
def __init__(self, x: int, y: int):
super().__init__(x, y, 'VACUUM')
self.battery = 100
# TODO add more properties

19
domain/world.py Normal file
View File

@ -0,0 +1,19 @@
from domain.entity import Entity
from domain.vacuum import Vacuum
class World:
def __init__(self, width: int, height: int):
self.width = width
self.height = height
self.grid = [
[[] for j in range(height)] for i in range(width)
]
self.entities = []
self.cleaner = Vacuum(0, 0)
self.add(self.cleaner)
def add(self, entity: Entity):
self.entities.append(entity)
self.grid[entity.x][entity.y].append(entity)

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

Binary file not shown.

After

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

112
view/user_interface.py Normal file
View File

@ -0,0 +1,112 @@
from random import randint
import pygame
from pygame import Color
from domain.world import World, Entity
class UserInterface:
def __init__(
self,
width=800,
height=800,
tiles_x=10,
tiles_y=10,
):
self.width = width
self.height = height
self.tiles_x = tiles_x
self.tiles_y = tiles_y
self.tile_width = self.width / self.tiles_x
self.tile_height = self.height / self.tiles_y
self.world = World(tiles_x, tiles_y)
for _ in range(10):
temp_x = randint(0, tiles_x - 1)
temp_y = randint(0, tiles_y - 1)
self.world.add(Entity(temp_x, temp_y, 'PEEL'))
pygame.init()
pygame.display.set_caption('AI Vacuum Cleaner')
self.screen = pygame.display.set_mode((self.width, self.height))
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_cropped.jpeg'),
(self.tile_width, self.tile_height)),
'PEEL': pygame.transform.scale(pygame.image.load('../media/sprites/peel.webp'),
(self.tile_width, self.tile_height)),
}
self.clock = pygame.time.Clock()
self.running = True
self.fps = 60
def process_input(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and self.world.cleaner.x > 0:
self.world.cleaner.x -= 1
if event.key == pygame.K_RIGHT and self.world.cleaner.x < self.tiles_x - 1:
self.world.cleaner.x += 1
if event.key == pygame.K_UP and self.world.cleaner.y > 0:
self.world.cleaner.y -= 1
if event.key == pygame.K_DOWN and self.world.cleaner.y < self.tiles_y - 1:
self.world.cleaner.y += 1
def update(self):
pass
def render(self):
self.render_floor()
self.render_board()
for entity in self.world.entities:
self.draw_sprite(entity.x, entity.y, entity.type)
self.draw_sprite(self.world.cleaner.x, self.world.cleaner.y, self.world.cleaner.type)
pygame.display.update()
def run(self):
while self.running:
self.process_input()
self.update()
self.render()
self.clock.tick(self.fps)
pygame.quit()
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))
def render_board(self, color=Color('black')):
for i in range(1, self.tiles_x):
self.line(self.tile_width * i, 0, self.tile_width * i, self.height, color=color)
for i in range(1, self.tiles_y):
self.line(0, self.tile_height * i, self.width, self.tile_height * i, color=color)
def draw_sprite(self, x, y, sprite):
self.screen.blit(
self.sprites[sprite],
(x * self.tile_width, 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)
def render_floor(self):
self.fill_grid_with_sprite('TILE')
UserInterface(800, 800, 10, 10).run()