diff --git a/config.py b/config.py index 6fac672..ca6e693 100644 --- a/config.py +++ b/config.py @@ -22,6 +22,6 @@ def set_home_amount(): home_amount = set_home_amount() -PLAY_WIDTH = (home_amount+2)*CELL_SIZE +PLAY_WIDTH = (home_amount + 4)*CELL_SIZE PLAY_HEIGHT = PLAY_WIDTH HUD_HEIGHT = int(home_amount*CELL_SIZE/4) diff --git a/game.py b/game.py index 384f503..f692ce0 100644 --- a/game.py +++ b/game.py @@ -15,9 +15,11 @@ fps_clock = time.Clock() ###################################### -##INITIALIZE DYNAMIC VARIABLES######## -obstacles_coords = [] -###################################### +interactables = { + "homes": [], + "landfills": [] +} + ##GAMEWINDOW########################## WINDOW_WIDTH = PLAY_WIDTH @@ -32,10 +34,9 @@ display.set_icon(icon) ## # Generate level utils.generate_grass(all_sprites) -utils.generate_landfills(all_sprites, obstacles_coords) -utils.generate_houses(all_sprites, obstacles_coords) -utils.add_frame_as_obstacles(obstacles_coords) -gc = utils.generate_garbage_collector(all_sprites, obstacles_coords) +utils.generate_landfills(all_sprites, interactables) +utils.generate_houses(all_sprites, interactables) +gc = utils.generate_garbage_collector(all_sprites, interactables) ## ##GAME LOOP####################################################################### @@ -46,13 +47,13 @@ while(1): sys.exit() if e.type == KEYUP: if e.key == K_UP: - gc.move('up', obstacles_coords) + gc.move('up', interactables["homes"] + interactables["landfills"]) if e.key == K_DOWN: - gc.move('down', obstacles_coords) + gc.move('down', interactables["homes"] + interactables["landfills"]) if e.key == K_RIGHT: - gc.move('right', obstacles_coords) + gc.move('right', interactables["homes"] + interactables["landfills"]) if e.key == K_LEFT: - gc.move('left', obstacles_coords) + gc.move('left', interactables["homes"] + interactables["landfills"]) all_sprites.update() all_sprites.draw(GAMEWINDOW) diff --git a/sprites/garbage_collector.py b/sprites/garbage_collector.py index 1185e59..229b16c 100644 --- a/sprites/garbage_collector.py +++ b/sprites/garbage_collector.py @@ -1,6 +1,6 @@ import pygame -from sprites.cell import Cell - +from sprites.cell import Cell, CELL_SIZE +from config import PLAY_HEIGHT, PLAY_WIDTH class Garbage_collector(Cell): def __init__(self, x, y): @@ -9,10 +9,10 @@ class Garbage_collector(Cell): Cell.__init__(self, x, y) self.image = pygame.image.load("images/garbage_collector.png") self.move_options = { - "up": lambda forbidden: ('y', self.y - 1) if (self.x, self.y - 1) not in forbidden else ('y', self.y), - "down": lambda forbidden: ('y', self.y + 1) if (self.x, self.y + 1) not in forbidden else ('y', self.y), - "left": lambda forbidden: ('x', self.x - 1) if (self.x - 1, self.y) not in forbidden else ('x', self.x), - "right": lambda forbidden: ('x', self.x + 1) if (self.x + 1, self.y) not in forbidden else ('x', self.x) + "up": lambda forbidden: ('y', self.y - 1) if (self.x, self.y - 1) not in forbidden and self.y - 1 >= 0 else ('y', self.y), + "down": lambda forbidden: ('y', self.y + 1) if (self.x, self.y + 1) not in forbidden and self.y + 1 < PLAY_HEIGHT // CELL_SIZE else ('y', self.y), + "left": lambda forbidden: ('x', self.x - 1) if (self.x - 1, self.y) not in forbidden and self.x - 1 >= 0 else ('x', self.x), + "right": lambda forbidden: ('x', self.x + 1) if (self.x + 1, self.y) not in forbidden and self.x + 1 < PLAY_WIDTH // CELL_SIZE else ('x', self.x) } self.trash_space_taken = { "plastic": 0, @@ -27,7 +27,6 @@ class Garbage_collector(Cell): self.x = value elif(destination is 'y'): self.y = value - self.update() def get_collect_data(self): diff --git a/utils.py b/utils.py index 46cc6f8..f066433 100644 --- a/utils.py +++ b/utils.py @@ -12,13 +12,6 @@ from sprites.garbage_collector import Garbage_collector def generate_rand_coordinates(max_x, max_y): return (random.randint(0, max_x), random.randint(0, (max_y))) -def add_frame_as_obstacles(obstacles_coords): - for x in range(0, PLAY_WIDTH//CELL_SIZE): - obstacles_coords.append((x,-1)) - obstacles_coords.append((-1,x)) - obstacles_coords.append((PLAY_WIDTH//CELL_SIZE,x)) - obstacles_coords.append((x,PLAY_HEIGHT//CELL_SIZE)) - ##GENERATE GRASS################################################################## def generate_grass(all_sprites): grass = [] @@ -40,9 +33,9 @@ def generate_houses(all_sprites, obstacles_coords): while(home_counter != 0): x, y = generate_rand_coordinates( (PLAY_WIDTH//CELL_SIZE)-1, (PLAY_HEIGHT//CELL_SIZE)-1) - if(((x, y) or (x+1,y)) not in obstacles_coords): + if(((x, y)) not in obstacles_coords["homes"] and ((x, y)) not in obstacles_coords["landfills"]): houses.append(House(x, y, 10, 10, 10)) - obstacles_coords.append((x, y)) + obstacles_coords["homes"].append((x, y)) home_counter = home_counter - 1 for item in houses: @@ -55,21 +48,10 @@ def generate_houses(all_sprites, obstacles_coords): def generate_landfills(all_sprites, obstacles_coords): landfills = [] landfill_counter = 3 - -# while(landfill_counter != 0): -# x, y = generate_rand_coordinates( -# (PLAY_WIDTH//CELL_SIZE)-1, (PLAY_HEIGHT//CELL_SIZE)-1) -# if((x, y) not in obstacles_coords): -# landfills.append(Landfill(x, y, landfill_counter-1)) -# obstacles_coords.append((x, y)) -# landfill_counter = landfill_counter - 1 y=0 for x in range(landfill_counter): landfills.append(Landfill(x,y,x)) - obstacles_coords.append((x,y)) - - - + obstacles_coords["landfills"].append((x,y)) for item in landfills: all_sprites.add(item) ################################################################################## @@ -81,7 +63,7 @@ def generate_garbage_collector(all_sprites, obstacles_coords): while(True): x, y = generate_rand_coordinates( (PLAY_WIDTH//CELL_SIZE)-1, (PLAY_HEIGHT//CELL_SIZE)-1) - if((x, y) not in obstacles_coords): + if((x, y) not in obstacles_coords["landfills"] and (x, y) not in obstacles_coords["homes"]): gc = Garbage_collector(x, y) break all_sprites.add(gc)