refactor, optymalizacja

This commit is contained in:
michalStarski 2019-03-25 16:00:01 +01:00
parent 04927589c2
commit af1f91c4f0
4 changed files with 24 additions and 41 deletions

View File

@ -22,5 +22,5 @@ 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

23
game.py
View File

@ -14,9 +14,11 @@ fps_clock = time.Clock()
######################################
##INITIALIZE DYNAMIC VARIABLES########
obstacles_coords = []
######################################
interactables = {
"homes": [],
"landfills": []
}
##GAMEWINDOW##########################
WINDOW_WIDTH = PLAY_WIDTH
@ -30,10 +32,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#######################################################################
@ -44,13 +45,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)

View File

@ -1,16 +1,16 @@
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):
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)
}
def move(self, direction, forbidden):
@ -19,5 +19,5 @@ class Garbage_collector(Cell):
self.x = value
elif(destination is 'y'):
self.y = value
self.update()

View File

@ -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)