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()