From 7c31461da65bb81ec9ae263b5f55689b490017be Mon Sep 17 00:00:00 2001 From: jedryb Date: Tue, 22 Mar 2022 21:34:50 +0100 Subject: [PATCH] added tile class, refactor some other classes --- .idea/ai-project.iml | 2 +- .idea/misc.xml | 2 +- main.py | 27 +++++++++++++++------------ src/plant.py | 6 +++--- src/settings.py | 10 ++++++---- src/tile.py | 24 ++++++++++++++++++++++++ src/tractor.py | 23 +++++++++++++---------- src/{world_creator.py => world.py} | 19 +++++++++++-------- 8 files changed, 74 insertions(+), 39 deletions(-) create mode 100644 src/tile.py rename src/{world_creator.py => world.py} (71%) diff --git a/.idea/ai-project.iml b/.idea/ai-project.iml index a23e703..0c80114 100644 --- a/.idea/ai-project.iml +++ b/.idea/ai-project.iml @@ -5,7 +5,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index dc9ea49..3fc3916 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/main.py b/main.py index 201b42e..995594b 100644 --- a/main.py +++ b/main.py @@ -1,37 +1,40 @@ import pygame -from src.world_creator import World -from settings import Settings +from src.world import World +from src.tractor import Tractor +from src.settings import Settings -def draw_line(screen: pygame.display, tile_size, settings): +def draw_line(screen: pygame.display, settings): for line in range(0, 11): - pygame.draw.line(screen, (255, 255, 255), (0, line * tile_size), (settings.screen_width, line*tile_size)) - pygame.draw.line(screen, (255, 255, 255), (line * tile_size, 0), (line * tile_size, settings.screen_height)) + pygame.draw.line(screen, (255, 255, 255), (0, line * settings.tile_size), + (settings.screen_width, line*settings.tile_size)) + pygame.draw.line(screen, (255, 255, 255), (line * settings.tile_size, 0), + (line * settings.tile_size, settings.screen_height)) +# TODO: add Sprite to every class and store Tile objects as Sprite group + def main(): - print('siema') pygame.init() settings = Settings() + world = World(settings) + tractor = Tractor("Spalinowy", "Nawóz 1", settings) + screen = pygame.display.set_mode((settings.screen_width, settings.screen_height)) - world = World(settings.screen_title_size) pygame.display.set_caption('TRAKTOHOLIK') background_image = pygame.image.load('assets/images/sky.png') background_image = pygame.transform.scale(background_image, (settings.screen_width, settings.screen_height)) - tractor_image = pygame.transform.scale(pygame.image.load('assets/images/tractor.png'), - (0.9*settings.screen_title_size, 0.9*settings.screen_title_size)) run = True while run: - screen.blit(background_image, (0, 0)) world.draw(screen) - draw_line(screen, settings.screen_title_size, settings) - screen.blit(tractor_image, (6*settings.screen_title_size, 5*settings.screen_title_size)) + draw_line(screen, settings) + screen.blit(tractor.image, (6*settings.tile_size, 5*settings.tile_size)) for event in pygame.event.get(): if event.type == pygame.QUIT: diff --git a/src/plant.py b/src/plant.py index 7e12859..a247d2c 100644 --- a/src/plant.py +++ b/src/plant.py @@ -1,5 +1,5 @@ class Plant: - """class representing plants""" + """ Class representing plants """ def __init__(self, species, tile): self.species = species @@ -29,8 +29,8 @@ class Plant: self.growth = 0 # value between 0 and 1 self.wilted = False - def remove(self): - self.tile.planted = False + def remove(self): # to chyba do usunięcia, bo to tile zawiera planta, a nie na odwrót + self.tile.plant = None def grow(self): return True diff --git a/src/settings.py b/src/settings.py index a3cbbcd..c091421 100644 --- a/src/settings.py +++ b/src/settings.py @@ -1,8 +1,10 @@ class Settings: - """ A class to represent all settings for the game, maybe later as .cfg """ + """ Class to represent all settings for the app, maybe later as .cfg """ def __init__(self): # Screen settings - self.screen_width = 1100 - self.screen_height = 1100 - self.screen_title_size = 110 \ No newline at end of file + self.screen_width = 800 + self.screen_height = 800 + + # Tile settings + self.tile_size = 80 diff --git a/src/tile.py b/src/tile.py new file mode 100644 index 0000000..a59be55 --- /dev/null +++ b/src/tile.py @@ -0,0 +1,24 @@ +class Tile: + """ Class to represent single board tile """ + + def __init__(self, row_id, col_id, image, rect): + self.row_id = row_id + self.col_id = col_id + self.position = (row_id, col_id) + self.image = image + self.rect = rect + self.plant = None + self.hydration = 0 + self.fertilizer = None + self.is_fertilized = False + + def add_plant(self, plant): + self.plant = plant + + def remove_plant(self): + self.plant = None + + + + + diff --git a/src/tractor.py b/src/tractor.py index 0f4be36..07f6406 100644 --- a/src/tractor.py +++ b/src/tractor.py @@ -1,11 +1,14 @@ +import pygame + + class Tractor: - """ A class to represent our agent """ - - def __init__(self, engine, addition, trajectory): + """ Class to represent our agent """ + def __init__(self, engine, fertilizer, settings): + self.image = pygame.transform.scale(pygame.image.load('assets/images/tractor.png'), + (0.9*settings.tile_size, 0.9*settings.tile_size)) self.engine = engine - self.addition = addition - self.trajectory = trajectory + self.fertilizer = fertilizer def hydrate(self, tile): if tile.hydration < tile.plant.min_hydration: @@ -18,11 +21,11 @@ class Tractor: def cut(self, tile): tile.remove_plant() - def plant(self, tile): - if not tile.planted: - tile.plant() + def plant(self, tile, plant): + if not tile.plant: + tile.add_plant(plant) def fertilize(self, tile): - if not tile.isfertilized: - tile.isFertilized = True + if not tile.is_fertilized: + tile.is_fertilized = True tile.fertilizer = tile.plant.fertilizer diff --git a/src/world_creator.py b/src/world.py similarity index 71% rename from src/world_creator.py rename to src/world.py index f445e57..5cb96c5 100644 --- a/src/world_creator.py +++ b/src/world.py @@ -1,8 +1,9 @@ import pygame +from src.tile import Tile class World: - """ A class to represent game board """ + """ Class to represent complete game board """ world_data = [ [1, 1, 1, 1, 0, 0, 1, 1, 1, 1], @@ -17,27 +18,29 @@ class World: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ] # it will be changed when miguel sends his code - def __init__(self, tile_size): + def __init__(self, settings): self.tile_list = [] self.dirt_image = pygame.image.load('assets/images/dirt.jpeg') self.gravel_image = pygame.image.load('assets/images/gravel.jpeg') + self.settings = settings row_count = 0 for row in self.world_data: col_count = 0 + for tile in row: if tile == 1: - img = pygame.transform.scale(self.dirt_image, (tile_size, tile_size)) + img = pygame.transform.scale(self.dirt_image, (settings.tile_size, settings.tile_size)) elif tile == 0: - img = pygame.transform.scale(self.gravel_image, (tile_size, tile_size)) + img = pygame.transform.scale(self.gravel_image, (settings.tile_size, settings.tile_size)) img_rect = img.get_rect() - img_rect.x = col_count * tile_size - img_rect.y = row_count * tile_size - tile = (img, img_rect) + img_rect.x = col_count * settings.tile_size + img_rect.y = row_count * settings.tile_size + tile = Tile(row_count, col_count, img, img_rect) self.tile_list.append(tile) col_count += 1 row_count += 1 def draw(self, screen): for tile in self.tile_list: - screen.blit(tile[0], tile[1]) + screen.blit(tile.image, tile.rect)