added garbage collector

This commit is contained in:
s1202077 2019-03-20 11:20:10 +01:00
parent 08b529761b
commit 50bda0aff9
6 changed files with 43 additions and 13 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
VENV
env
**/__pycache__
linux_env

30
game.py
View File

@ -3,6 +3,7 @@ import sys, random
from sprites.grass import Grass
from sprites.house import House
from sprites.landfill import Landfill
from sprites.garbage_collector import Garbage_collector
from pygame.locals import *
import utils
@ -37,22 +38,25 @@ for x in range(PLAY_HEIGHT//64):
#Losowanie domków i dodawanie je do mapy
home_len = home_amount
trash_count = 2
while( home_len > 0 ):
#Sprawdzenie, czy istnieje już domek na danej pozycji, jeżeli tak to losuj ponownie
x = random.randint(0, (PLAY_WIDTH//64)-1)
y = random.randint(0, (PLAY_WIDTH//64)-1)
x, y = utils.generate_rand_coordinates(PLAY_WIDTH, PLAY_HEIGHT)
if( type(cells[x][y]) == Grass ):
cells[x][y] = House(x,y, 10, 10, 10)
home_len = home_len - 1
for trash_type in range(3):
x = random.randint(0, (PLAY_WIDTH//64)-1)
y = random.randint(0, (PLAY_WIDTH//64)-1)
while(trash_count >= 0):
x, y = utils.generate_rand_coordinates(PLAY_WIDTH, PLAY_HEIGHT)
if( type(cells[x][y]) == Grass ):
cells[x][y] = Landfill(x, y, trash_type)
cells[x][y] = Landfill(x, y, trash_count)
trash_count -= 1
while(True):
x, y = utils.generate_rand_coordinates(PLAY_WIDTH, PLAY_HEIGHT)
if type(cells[x][y]) is Grass:
cells[x][y] = Garbage_collector(x,y)
break
#Dodawanie wszystkich spritow do grupy spritow
for x in range(len(cells)):
@ -65,6 +69,16 @@ while(1):
if e.type == QUIT:
quit()
sys.exit()
if e.type == KEYUP:
if e.key == K_UP:
print('up')
if e.key == K_DOWN:
print('down')
if e.key == K_RIGHT:
print('right')
if e.key == K_LEFT:
print('left')
all_sprites.update()
all_sprites.draw(GAMEWINDOW)

View File

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -0,0 +1,15 @@
import pygame
from sprites.cell import Cell
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: self.y + 1 if (self.x, self.y + 1) not in forbidden else self.y,
"down": lambda forbidden: self.y - 1 if (self.x, self.y - 1) not in forbidden else self.y,
"left": lambda forbidden: self.x - 1 if (self.x - 1, self.y) not in forbidden else self.x,
"right": lambda forbidden: self.x + 1 if (self.x + 1, self.y) not in forbidden else self.x
}

View File

@ -10,7 +10,7 @@ class House(Cell):
def __init__(self,x,y, max_plastic, max_glass, max_metal):
Cell.__init__(self,x,y)
self.image = pygame.image.load("images/house.png")
self.rubbish = [0,0,0] #plastic, glass, metal
self.rubbish = [random.randint(0, max_plastic), random.randint(0, max_glass), random.randint(0, max_metal)] #plastic, glass, metal
self.max_plastic = max_plastic
self.max_glass = max_glass
@ -23,13 +23,10 @@ class House(Cell):
if( self.rubbish[PLASTIC] > self.max_plastic ):
self.image = pygame.image.load("images/house_plastic.png")
print("PLASTIC alert")
if( self.rubbish[GLASS] > self.max_glass ):
self.image = pygame.image.load("images/house_glass.png")
print("GLASS alert")
if( self.rubbish[METAL] > self.max_metal ):
self.image = pygame.image.load("images/house_metal.png")
print("METAL alert")
def check_rubbish_status(self):
print( "plastic: " + str(self.rubbish[PLASTIC]) + " glass: " + str(self.rubbish[GLASS]) + " metal: " + str(self.rubbish[METAL]) )

View File

@ -1,4 +1,4 @@
import sys, getopt
import sys, getopt, random
def set_home_amount():
arguments = sys.argv[1:]
@ -15,3 +15,6 @@ def set_home_amount():
except getopt.GetoptError as err:
print(err)
sys.exit(2)
def generate_rand_coordinates(PLAY_WIDTH, PLAY_HEIGHT):
return (random.randint(0, (PLAY_WIDTH//64)-1), random.randint(0, (PLAY_WIDTH//64)-1))