Fixed conflicts

This commit is contained in:
Magdalena Wilczyńska 2019-03-25 16:36:08 +01:00
commit 84d375e5dd
4 changed files with 23 additions and 41 deletions

View File

@ -22,6 +22,6 @@ def set_home_amount():
home_amount = 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 PLAY_HEIGHT = PLAY_WIDTH
HUD_HEIGHT = int(home_amount*CELL_SIZE/4) HUD_HEIGHT = int(home_amount*CELL_SIZE/4)

23
game.py
View File

@ -15,9 +15,11 @@ fps_clock = time.Clock()
###################################### ######################################
##INITIALIZE DYNAMIC VARIABLES######## interactables = {
obstacles_coords = [] "homes": [],
###################################### "landfills": []
}
##GAMEWINDOW########################## ##GAMEWINDOW##########################
WINDOW_WIDTH = PLAY_WIDTH WINDOW_WIDTH = PLAY_WIDTH
@ -32,10 +34,9 @@ display.set_icon(icon)
## ##
# Generate level # Generate level
utils.generate_grass(all_sprites) utils.generate_grass(all_sprites)
utils.generate_landfills(all_sprites, obstacles_coords) utils.generate_landfills(all_sprites, interactables)
utils.generate_houses(all_sprites, obstacles_coords) utils.generate_houses(all_sprites, interactables)
utils.add_frame_as_obstacles(obstacles_coords) gc = utils.generate_garbage_collector(all_sprites, interactables)
gc = utils.generate_garbage_collector(all_sprites, obstacles_coords)
## ##
##GAME LOOP####################################################################### ##GAME LOOP#######################################################################
@ -46,13 +47,13 @@ while(1):
sys.exit() sys.exit()
if e.type == KEYUP: if e.type == KEYUP:
if e.key == K_UP: if e.key == K_UP:
gc.move('up', obstacles_coords) gc.move('up', interactables["homes"] + interactables["landfills"])
if e.key == K_DOWN: if e.key == K_DOWN:
gc.move('down', obstacles_coords) gc.move('down', interactables["homes"] + interactables["landfills"])
if e.key == K_RIGHT: if e.key == K_RIGHT:
gc.move('right', obstacles_coords) gc.move('right', interactables["homes"] + interactables["landfills"])
if e.key == K_LEFT: if e.key == K_LEFT:
gc.move('left', obstacles_coords) gc.move('left', interactables["homes"] + interactables["landfills"])
all_sprites.update() all_sprites.update()
all_sprites.draw(GAMEWINDOW) all_sprites.draw(GAMEWINDOW)

View File

@ -1,6 +1,6 @@
import pygame 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): class Garbage_collector(Cell):
def __init__(self, x, y): def __init__(self, x, y):
@ -9,10 +9,10 @@ class Garbage_collector(Cell):
Cell.__init__(self, x, y) Cell.__init__(self, x, y)
self.image = pygame.image.load("images/garbage_collector.png") self.image = pygame.image.load("images/garbage_collector.png")
self.move_options = { self.move_options = {
"up": lambda forbidden: ('y', self.y - 1) if (self.x, self.y - 1) not in forbidden else ('y', self.y), "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 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 else ('x', self.x), "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 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 = { self.trash_space_taken = {
"plastic": 0, "plastic": 0,
@ -27,7 +27,6 @@ class Garbage_collector(Cell):
self.x = value self.x = value
elif(destination is 'y'): elif(destination is 'y'):
self.y = value self.y = value
self.update() self.update()
def get_collect_data(self): def get_collect_data(self):

View File

@ -12,13 +12,6 @@ from sprites.garbage_collector import Garbage_collector
def generate_rand_coordinates(max_x, max_y): def generate_rand_coordinates(max_x, max_y):
return (random.randint(0, max_x), random.randint(0, (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################################################################## ##GENERATE GRASS##################################################################
def generate_grass(all_sprites): def generate_grass(all_sprites):
grass = [] grass = []
@ -40,9 +33,9 @@ def generate_houses(all_sprites, obstacles_coords):
while(home_counter != 0): while(home_counter != 0):
x, y = generate_rand_coordinates( x, y = generate_rand_coordinates(
(PLAY_WIDTH//CELL_SIZE)-1, (PLAY_HEIGHT//CELL_SIZE)-1) (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)) houses.append(House(x, y, 10, 10, 10))
obstacles_coords.append((x, y)) obstacles_coords["homes"].append((x, y))
home_counter = home_counter - 1 home_counter = home_counter - 1
for item in houses: for item in houses:
@ -55,21 +48,10 @@ def generate_houses(all_sprites, obstacles_coords):
def generate_landfills(all_sprites, obstacles_coords): def generate_landfills(all_sprites, obstacles_coords):
landfills = [] landfills = []
landfill_counter = 3 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 y=0
for x in range(landfill_counter): for x in range(landfill_counter):
landfills.append(Landfill(x,y,x)) landfills.append(Landfill(x,y,x))
obstacles_coords.append((x,y)) obstacles_coords["landfills"].append((x,y))
for item in landfills: for item in landfills:
all_sprites.add(item) all_sprites.add(item)
################################################################################## ##################################################################################
@ -81,7 +63,7 @@ def generate_garbage_collector(all_sprites, obstacles_coords):
while(True): while(True):
x, y = generate_rand_coordinates( x, y = generate_rand_coordinates(
(PLAY_WIDTH//CELL_SIZE)-1, (PLAY_HEIGHT//CELL_SIZE)-1) (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) gc = Garbage_collector(x, y)
break break
all_sprites.add(gc) all_sprites.add(gc)