diff --git a/blocks.py b/blocks.py index 7dfa349f..eb38ca2c 100644 --- a/blocks.py +++ b/blocks.py @@ -21,7 +21,6 @@ class Blocks: self.soil = soil.Soil() - def locate_blocks(self, blocks_number, cell_number, body): for i in range(blocks_number): self.x = random.randint(0, cell_number-1) @@ -30,7 +29,6 @@ class Blocks: body.append(self.pos) #entire_block.update({self.x : 1}) # for now it may lay on each other, #print(entire_block) - def place_blocks(self, parent_screen, cell_size, body, color): #drawing blocks for block in body: diff --git a/land.py b/land.py index 89f6bf0a..557940a8 100644 --- a/land.py +++ b/land.py @@ -2,24 +2,71 @@ import pygame import random class Land: - def __init__(self, cell_size, cell_number, grass_body): + def __init__(self, parent_screen, cell_size, cell_number, all_soil_body, irrigation): + self.parent_screen = parent_screen + self.cell_number = cell_number + self.cell_size = cell_size + self._irrigation = irrigation + self.all_soil_body = all_soil_body + self.grass_image = pygame.image.load(r'resources/grass.png').convert() self.grass_image = pygame.transform.scale(self.grass_image, (cell_size, cell_size)) self.bad_grass_image = pygame.image.load(r'resources/bad_grass.png').convert() self.bad_grass_image = pygame.transform.scale(self.bad_grass_image, (cell_size, cell_size)) - for i in range(0, cell_number): - for k in range(0, cell_number): - grass_body.append([i,k]) - + self.black_earth_image = pygame.image.load(r'resources/black_earth.png').convert() + self.black_earth_image = pygame.transform.scale(self.black_earth_image, (cell_size, cell_size)) - def place_grass(self, parent_screen, cell_number, cell_size, grass_body, name): - for body in grass_body: - x = int(body[0] * cell_size) - y = int(body[1] * cell_size) - if(name == 'good'): - parent_screen.blit(self.grass_image, (x, y)) # to-check: () redundant parentheses - if(name == 'bad'): - parent_screen.blit(self.bad_grass_image, (x, y)) - \ No newline at end of file + self.brown_earth_image = pygame.image.load(r'resources/brown_earth.png').convert() + self.brown_earth_image = pygame.transform.scale(self.brown_earth_image, (cell_size, cell_size)) + + self.fawn_soil_image = pygame.image.load(r'resources/fawn_soil.png').convert() + self.fawn_soil_image = pygame.transform.scale(self.fawn_soil_image, (cell_size, cell_size)) + + self.fen_soil_image = pygame.image.load(r'resources/fen_soil.png').convert() + self.fen_soil_image = pygame.transform.scale(self.fen_soil_image, (cell_size, cell_size)) + + # for i in range(0, cell_number): + # for k in range(0, cell_number): + # all_soil_body.append([i, k]) + + def locate_soil(self, soil_body): # finds free places(coordinates) for soil and adds them to soil_body[] + number_of_blocs_for_each_soil = 50 + + if number_of_blocs_for_each_soil > (self.cell_number * self.cell_number) // 4: + number_of_blocs_for_each_soil = (self.cell_number * self.cell_number) // 4 + print('Number of soil blocks exceeds the number of fields!') + + for i in range(number_of_blocs_for_each_soil): # can't be more than: (cell_number * cell_number) // soil_types + while True: + rand_x = random.randint(0, self.cell_number - 1) # to-check + rand_y = random.randint(0, self.cell_number - 1) + if [rand_x, rand_y] not in self.all_soil_body: + self.all_soil_body.append([rand_x, rand_y]) + soil_body.append([rand_x, rand_y]) + break + + def place_soil(self, soil_body, soil_name): + for body in soil_body: + x = int(body[0] * self.cell_size) + y = int(body[1] * self.cell_size) + if soil_name == 'black_earth': + self.parent_screen.blit(self.black_earth_image, (x, y)) + if soil_name == 'brown_earth': + self.parent_screen.blit(self.brown_earth_image, (x, y)) + if soil_name == 'fawn_soil': + self.parent_screen.blit(self.fawn_soil_image, (x, y)) + if soil_name == 'fen_soil': + self.parent_screen.blit(self.fen_soil_image, (x, y)) + + def set_and_place_block_of_grass(self, name): + for i in range(0, self.cell_number): + for k in range(0, self.cell_number): + if [k, i] not in self.all_soil_body: + x = int(k * self.cell_size) + y = int(i * self.cell_size) + if name == 'good': + self.parent_screen.blit(self.grass_image, (x, y)) + if name == 'bad': + self.parent_screen.blit(self.bad_grass_image, (x, y)) diff --git a/main.py b/main.py index efed0767..426b65c1 100644 --- a/main.py +++ b/main.py @@ -11,8 +11,8 @@ from datetime import datetime class Game: cell_size = 50 - cell_number = 15 #horizontally - blocks_number = 15 # to-check sth is wrong: duplicate(?) and overlap + cell_number = 15 # horizontally + blocks_number = 15 def __init__(self): self.dead_leaf_body = [] @@ -22,18 +22,30 @@ class Game: self.dead_grass_body = [] self.grass_body = [] - self.potato_field = [] - + self.black_earth_body = [] + self.brown_earth_body = [] + self.fawn_soil_body = [] + self.fen_soil_body = [] + self.allBodyPos = [] self.entire_block = {} + # initialize a window pygame.init() - self.surface = pygame.display.set_mode((self.cell_size*self.cell_number, self.cell_size*self.cell_number)) # initialize a window + self.surface = pygame.display.set_mode((self.cell_size*self.cell_number, self.cell_size*self.cell_number)) - self.land = land.Land(self.cell_size, self.cell_number, self.grass_body) - - self.blocks = blocks.Blocks(self.surface,self.cell_size) + # finds places for every type soil and grass + self.black_earth = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100) + self.black_earth.locate_soil(self.black_earth_body) + self.brown_earth = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100) + self.brown_earth.locate_soil(self.brown_earth_body) + self.fawn_soil = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100) + self.fawn_soil.locate_soil(self.fawn_soil_body) + self.fen_soil = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100) + self.fen_soil.locate_soil(self.fen_soil_body) + self.grass = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100) + self.blocks = blocks.Blocks(self.surface, self.cell_size) self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.dead_leaf_body) self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.stone_body) self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.flower_body) @@ -45,16 +57,16 @@ class Game: self.tractor.draw() def run(self): - print(self.potato.get_soil_info().get_name()) - print(self.potato.get_soil_info().get_acidity()) - print(self.potato.get_soil_info().get_irrigation()) + # print(self.potato.get_soil_info().get_name()) + # print(self.potato.get_soil_info().get_acidity()) + # print(self.potato.get_soil_info().get_irrigation()) running = True clock = pygame.time.Clock() - last_time = datetime.now() + # last_time = datetime.now() while running: clock.tick(60) # manual fps control not to overwork the computer - time_now = datetime.now() + # time_now = datetime.now() for event in pygame.event.get(): if event.type == KEYDOWN: @@ -62,39 +74,35 @@ class Game: running = False # in case we want to use keyboard if pygame.key.get_pressed()[K_UP]: - self.tractor.move('up',self.cell_size, self.cell_number) + self.tractor.move('up', self.cell_size, self.cell_number) if pygame.key.get_pressed()[K_DOWN]: - self.tractor.move('down',self.cell_size, self.cell_number) + self.tractor.move('down', self.cell_size, self.cell_number) if pygame.key.get_pressed()[K_LEFT]: - self.tractor.move('left',self.cell_size, self.cell_number) + self.tractor.move('left', self.cell_size, self.cell_number) if pygame.key.get_pressed()[K_RIGHT]: - self.tractor.move('right',self.cell_size, self.cell_number) + self.tractor.move('right', self.cell_size, self.cell_number) if pygame.key.get_pressed()[K_SPACE]: - self.tractor.water(self.dead_leaf_body, self.green_leaf_body, self.cell_size) - #self.tractor.water(self.grass_body, self.dead_grass_body, self.cell_size) + self.tractor.water(self.dead_leaf_body, self.green_leaf_body, self.cell_size) + # self.tractor.water(self.grass_body, self.dead_grass_body, self.cell_size) elif event.type == QUIT: running = False self.surface.fill((140, 203, 97)) # background color - self.land.place_grass(self.surface, self.cell_number, self.cell_size, self.grass_body, 'good') - #self.land.place_grass(self.surface, self.cell_number, self.cell_size, self.dead_grass_body, 'bad') + self.grass.set_and_place_block_of_grass('good') + self.black_earth.place_soil(self.black_earth_body, 'black_earth') + self.brown_earth.place_soil(self.brown_earth_body, 'brown_earth') + self.fawn_soil.place_soil(self.fawn_soil_body, 'fawn_soil') + self.fen_soil.place_soil(self.fen_soil_body, 'fen_soil') self.blocks.place_blocks(self.surface, self.cell_size, self.dead_leaf_body, 'leaf') self.blocks.place_blocks(self.surface, self.cell_size, self.green_leaf_body, 'alive') self.blocks.place_blocks(self.surface, self.cell_size, self.stone_body, 'stone') self.blocks.place_blocks(self.surface, self.cell_size, self.flower_body, 'flower') - self.tractor.draw() pygame.display.update() - - - # if (time_now - last_time).total_seconds() > 1: # tractor moves every 1 sec - #last_time = datetime.now() - #self.tractor.walk() - #print(f'x, y = ({int(self.tractor.x / 50)}, {int(self.tractor.y / 50)})') if __name__ == '__main__': diff --git a/resources/black_earth.png b/resources/black_earth.png new file mode 100644 index 00000000..694f5ccc Binary files /dev/null and b/resources/black_earth.png differ diff --git a/resources/earth.png b/resources/earth.png new file mode 100644 index 00000000..e2c92023 Binary files /dev/null and b/resources/earth.png differ diff --git a/resources/fawn_soil.png b/resources/fawn_soil.png new file mode 100644 index 00000000..cafaeba1 Binary files /dev/null and b/resources/fawn_soil.png differ diff --git a/resources/fen_soil.png b/resources/fen_soil.png new file mode 100644 index 00000000..d2f5bd8c Binary files /dev/null and b/resources/fen_soil.png differ diff --git a/resources/grass copy.png b/resources/grass copy.png new file mode 100644 index 00000000..35b48386 Binary files /dev/null and b/resources/grass copy.png differ diff --git a/resources/grassTwo.png b/resources/grassTwo.png new file mode 100644 index 00000000..f3c3f03d Binary files /dev/null and b/resources/grassTwo.png differ diff --git a/resources/seed.png b/resources/seed.png new file mode 100644 index 00000000..44b6e99f Binary files /dev/null and b/resources/seed.png differ diff --git a/resources/wheat.png b/resources/wheat.png new file mode 100644 index 00000000..9b75ec51 Binary files /dev/null and b/resources/wheat.png differ diff --git a/resources/wheatFour.png b/resources/wheatFour.png new file mode 100644 index 00000000..f3b3d2d2 Binary files /dev/null and b/resources/wheatFour.png differ diff --git a/resources/wheatOne.png b/resources/wheatOne.png new file mode 100644 index 00000000..a61e9666 Binary files /dev/null and b/resources/wheatOne.png differ diff --git a/resources/wheatThree.png b/resources/wheatThree.png new file mode 100644 index 00000000..3d77fe23 Binary files /dev/null and b/resources/wheatThree.png differ diff --git a/resources/wheatTwo.png b/resources/wheatTwo.png new file mode 100644 index 00000000..3d77fe23 Binary files /dev/null and b/resources/wheatTwo.png differ diff --git a/tractor.py b/tractor.py index aaab31a4..727dd3d3 100644 --- a/tractor.py +++ b/tractor.py @@ -4,6 +4,7 @@ import random class Tractor: def __init__(self, parent_screen, cell_size): self.parent_screen = parent_screen + self.lastVisitedBlocks = [] # tractor stores last 3 visited blocks self.up = pygame.image.load(r'resources/up.png').convert_alpha() self.down = pygame.image.load(r'resources/down.png').convert_alpha() @@ -22,6 +23,8 @@ class Tractor: self.direction = 'up' self.image = self.down + self.lastVisitedBlocks = [] # as tractor moves it stores last 3 coordinates + def draw(self): self.parent_screen.blit(self.image, (self.x, self.y)) # rotate tractor