diff --git a/__pycache__/agent.cpython-38.pyc b/__pycache__/agent.cpython-38.pyc new file mode 100644 index 0000000..22b0b52 Binary files /dev/null and b/__pycache__/agent.cpython-38.pyc differ diff --git a/__pycache__/common.cpython-38.pyc b/__pycache__/common.cpython-38.pyc new file mode 100644 index 0000000..acf04c8 Binary files /dev/null and b/__pycache__/common.cpython-38.pyc differ diff --git a/__pycache__/field.cpython-38.pyc b/__pycache__/field.cpython-38.pyc new file mode 100644 index 0000000..ce5e88c Binary files /dev/null and b/__pycache__/field.cpython-38.pyc differ diff --git a/__pycache__/settings.cpython-38.pyc b/__pycache__/settings.cpython-38.pyc new file mode 100644 index 0000000..9c65d0b Binary files /dev/null and b/__pycache__/settings.cpython-38.pyc differ diff --git a/agent.py b/agent.py new file mode 100644 index 0000000..bf1f8ab --- /dev/null +++ b/agent.py @@ -0,0 +1,39 @@ +import pygame +import settings + + +class Instance: + def __init__(self): + size = settings.Field.size + self.rect = pygame.Rect(0, 0, size(), size()) + self.action = '' + + def coordinates(self): + return { + 'x': self.x(), + 'y': self.y() + } + + def x(self): + return int(self.rect.x / settings.Field.size()) + + def y(self): + return int(self.rect.y / settings.Field.size()) + + def move(self): + key_pressed = pygame.key.get_pressed() + height = settings.Pygame.height() + width = settings.Pygame.width() + tile_size = settings.Field.size() + + if key_pressed[pygame.K_LEFT] and self.rect.x > 0: + self.rect.x -= tile_size + elif key_pressed[pygame.K_RIGHT] and self.rect.x < width - tile_size: + self.rect.x += tile_size + elif key_pressed[pygame.K_UP] and self.rect.y > 0: + self.rect.y -= tile_size + elif key_pressed[pygame.K_DOWN] and self.rect.y < height - tile_size: + self.rect.y += tile_size + elif key_pressed[pygame.K_SPACE]: + return 'open_window' + return 'none' diff --git a/assets/fields/default/grass.jpg b/assets/fields/default/grass.jpg new file mode 100644 index 0000000..15f3c02 Binary files /dev/null and b/assets/fields/default/grass.jpg differ diff --git a/assets/fields/default/grass_block.jpg b/assets/fields/default/grass_block.jpg new file mode 100644 index 0000000..9d9f797 Binary files /dev/null and b/assets/fields/default/grass_block.jpg differ diff --git a/sand.jpeg b/assets/fields/default/sand.jpg similarity index 100% rename from sand.jpeg rename to assets/fields/default/sand.jpg diff --git a/sand_block.jpg b/assets/fields/default/sand_block.jpg similarity index 100% rename from sand_block.jpg rename to assets/fields/default/sand_block.jpg diff --git a/dirt.jpg b/assets/fields/plant/dirt.jpg similarity index 100% rename from dirt.jpg rename to assets/fields/plant/dirt.jpg diff --git a/dirt_block.jpg b/assets/fields/plant/dirt_block.jpg similarity index 100% rename from dirt_block.jpg rename to assets/fields/plant/dirt_block.jpg diff --git a/cobble.jpeg b/assets/fields/special/cobble.jpg similarity index 100% rename from cobble.jpeg rename to assets/fields/special/cobble.jpg diff --git a/cobble_block.jpeg b/assets/fields/special/cobble_block.jpg similarity index 100% rename from cobble_block.jpeg rename to assets/fields/special/cobble_block.jpg diff --git a/common.py b/common.py new file mode 100644 index 0000000..9339b88 --- /dev/null +++ b/common.py @@ -0,0 +1,9 @@ +class Instance: + def __init__(self): + self.vars = {} + + def set(self, key: str, value): + self.vars[key] = value + + def get(self, key: str): + return self.vars[key] \ No newline at end of file diff --git a/field.py b/field.py new file mode 100644 index 0000000..484505b --- /dev/null +++ b/field.py @@ -0,0 +1,87 @@ +from enum import Enum +import pygame +import settings + + +class Type(Enum): + DEFAULT = 0 + PLANT = 1 + SPECIAL = 2 + + @staticmethod + def get_string(_type): + return { + Type.DEFAULT: 'default', + Type.PLANT: 'plant', + Type.SPECIAL: 'special', + }[_type] + + +class Object: + height = width = 0 + name = 'default' + type = Type.DEFAULT + + def create(self): + field_string = Type.get_string(self.type) + + img = pygame.image.load("./assets/fields/" + field_string + "/" + self.name + ".jpg") + return pygame.transform.scale(img, (self.width, self.height)) + + +class Tile(Object) : + def __init__(self, name: str, _type: Type): + self.name = name + self.type = _type + self.width = self.height = settings.Field.size() + self.object = self.create() + + +class Block(Object) : + def __init__(self, name: str, _type: Type): + self.name = name + self.type = _type + self.width = settings.Pygame.width() + self.height = settings.Pygame.height() + self.object = self.create() + + +class Field: + type = Type.DEFAULT + name = 'default' + + def __init__(self): + self.tile = Tile(self.name, self.type) + self.block = Block(self.name + "_block", self.type) + + +class Dirt(Field): + type = Type.PLANT + name = 'dirt' + + def __init__(self): + super(Dirt, self).__init__() + + +class Cobble(Field): + type = Type.SPECIAL + name = 'cobble' + + def __init__(self): + super(Cobble, self).__init__() + + +class Grass(Field): + type = Type.DEFAULT + name = 'grass' + + def __init__(self): + super(Grass, self).__init__() + + +class Sand(Field): + type = Type.DEFAULT + name = 'sand' + + def __init__(self): + super(Sand, self).__init__() diff --git a/grass.png b/grass.png deleted file mode 100644 index eaa5a7d..0000000 Binary files a/grass.png and /dev/null differ diff --git a/grass_block.jpeg b/grass_block.jpeg deleted file mode 100644 index a57efd9..0000000 Binary files a/grass_block.jpeg and /dev/null differ diff --git a/main.py b/main.py index 0df1e06..f087020 100644 --- a/main.py +++ b/main.py @@ -1,100 +1,101 @@ import pygame import random -WIDTH, HEIGHT = 800, 800 -WIN = pygame.display.set_mode((WIDTH, HEIGHT)) -pygame.display.set_caption("Sztuczna Inteligencja") -FPS = 30 +import field +import settings +import common +import agent + AGENT_IMG = pygame.image.load("dot-22-433567.png") -AGENT = pygame.transform.scale(AGENT_IMG, (50, 50)) -DIRT_IMG = pygame.image.load("dirt.jpg") -DIRT = pygame.transform.scale(DIRT_IMG, (50, 50)) -GRASS_IMG = pygame.image.load("grass.png") -GRASS = pygame.transform.scale(GRASS_IMG, (50, 50)) -SAND_IMG = pygame.image.load("sand.jpeg") -SAND = pygame.transform.scale(SAND_IMG, (50, 50)) -COBBLE_IMG = pygame.image.load("cobble.jpeg") -COBBLE = pygame.transform.scale(COBBLE_IMG, (50, 50)) -DIRT_BLOCK_IMG = pygame.image.load("dirt_block.jpg") -DIRT_BLOCK = pygame.transform.scale(DIRT_BLOCK_IMG, (800, 800)) -GRASS_BLOCK_IMG = pygame.image.load("grass_block.jpeg") -GRASS_BLOCK = pygame.transform.scale(GRASS_BLOCK_IMG, (800, 800)) -SAND_BLOCK_IMG = pygame.image.load("sand_block.jpg") -SAND_BLOCK = pygame.transform.scale(SAND_BLOCK_IMG, (800, 800)) -COBBLE_BLOCK_IMG = pygame.image.load("cobble_block.jpeg") -COBBLE_BLOCK = pygame.transform.scale(COBBLE_BLOCK_IMG, (800, 800)) +AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size())) + +possibleFields = { + 'dirt': field.Dirt(), + 'grass': field.Grass(), + 'cobble': field.Cobble(), + 'sand': field.Sand(), +} def randomize_map(): - possible_fields = [DIRT, GRASS, SAND, COBBLE] + fields_array = [] + for i in possibleFields: + fields_array.append(possibleFields[i].tile.object) + field_array_big = [] field_array_small = [] - for i in range(16): - for j in range(16): - field_array_small.append(random.choice(possible_fields)) + for i in range(settings.Field.horizontal_count()): + for j in range(settings.Field.vertical_count()): + field_array_small.append(random.choice(fields_array)) field_array_big.append(field_array_small) field_array_small = [] return field_array_big def read_img(agent, fields): - if fields[int(agent.x / 50)][int(agent.y / 50)] == GRASS: - WIN.blit(GRASS_BLOCK, (0, 0)) - elif fields[int(agent.x / 50)][int(agent.y / 50)] == DIRT: - WIN.blit(DIRT_BLOCK, (0, 0)) - elif fields[int(agent.x / 50)][int(agent.y / 50)] == SAND: - WIN.blit(SAND_BLOCK, (0, 0)) - elif fields[int(agent.x / 50)][int(agent.y / 50)] == COBBLE: - WIN.blit(COBBLE_BLOCK, (0, 0)) + window = common.get('window') + current_field = fields[agent.x()][agent.y()] + + if current_field == possibleFields['grass'].tile.object: + window.blit(possibleFields['grass'].block.object, (0, 0)) + elif current_field == possibleFields['dirt'].tile.object: + window.blit(possibleFields['dirt'].block.object, (0, 0)) + elif current_field == possibleFields['sand'].tile.object: + window.blit(possibleFields['sand'].block.object, (0, 0)) + elif current_field == possibleFields['cobble'].tile.object: + window.blit(possibleFields['cobble'].block.object, (0, 0)) pygame.display.update() pygame.time.delay(2000) + common.set('state_imgShown', False) -def draw_window(agent, fields, image_shown): - if image_shown: - for i in range(16): - for j in range(16): - WIN.blit(fields[i][j], (i * 50, j * 50)) - WIN.blit(AGENT, (agent.x, agent.y)) - pygame.display.update() - else: - read_img(agent, fields) +def agent_action(action: str): + if action == 'open_window': + common.set('state_imgShown', True) -def agent_movement(keys_pressed, agent): - if keys_pressed[pygame.K_LEFT] and agent.x > 0: - agent.x -= 50 - return False - elif keys_pressed[pygame.K_RIGHT] and agent.x < 750: - agent.x += 50 - return False - elif keys_pressed[pygame.K_UP] and agent.y > 0: - agent.y -= 50 - return False - elif keys_pressed[pygame.K_DOWN] and agent.y < 750: - agent.y += 50 - return False - else: - return True +def draw_window(agent, fields): + window = common.get('window') + rect = agent.rect + for i in range(settings.Field.horizontal_count()): + for j in range(settings.Field.vertical_count()): + window.blit(fields[i][j], (i * settings.Field.size(), j * settings.Field.size())) + window.blit(AGENT, (rect.x, rect.y)) + pygame.display.update() + + +common = common.Instance() +agent = agent.Instance() def main(): - clock = pygame.time.Clock() - run = True - agent = pygame.Rect(0, 0, 50, 50) + common.set('game_running', True) + common.set('state_imgShown', False) + common.set( + 'window', + pygame.display.set_mode(( + settings.Pygame.width(), + settings.Pygame.height()))) + + pygame.display.set_caption(settings.Pygame.display_name()) + fields = randomize_map() - image_shown = True - while run: - clock.tick(FPS) + + while common.get('game_running'): + pygame.time.Clock().tick(settings.Pygame.fps()) for event in pygame.event.get(): if event.type == pygame.QUIT: - run = False - keys_pressed = pygame.key.get_pressed() - draw_window(agent, fields, image_shown) - image_shown = agent_movement(keys_pressed, agent) + common.set('game_running', False) + + if common.get('state_imgShown'): + read_img(agent, fields) + else: + draw_window(agent, fields) + + agent_action(agent.move()) pygame.quit() if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..e9c2c4b --- /dev/null +++ b/settings.py @@ -0,0 +1,36 @@ +class Field: + @staticmethod + def size(): + return 50 + + @staticmethod + def horizontal_count(): + return 24 + + @staticmethod + def vertical_count(): + return 16 + + +class Pygame: + @staticmethod + def display_name(): + return 'Szybcior Sztucznie Inteligentny Traktor' + + @staticmethod + def fps(): + return 10 + + @staticmethod + def height(): + return Field.vertical_count() * Field.size() + + @staticmethod + def width(): + return Field.horizontal_count() * Field.size() + + +class Setting: + def __init__(self, key, default_value): + self.key = key + self.default_value = default_value