formatted file to pep8 and changed readme

This commit is contained in:
Michał Starski 2019-03-21 01:25:19 +01:00
parent 59f9eaae36
commit c537d6c581
10 changed files with 183 additions and 158 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
VENV
env
**/__pycache__
linux_env
linux_env
.vscode

View File

@ -2,6 +2,7 @@
## Lekki notateczki
```yaml
Modele:
Smieciarka:
- Zbiera smieci
@ -14,31 +15,31 @@ Modele:
- Ma swoj typ
- Moze byc zebrany
Dom:
- Produkuje smieci roznych typow
- Liczba ludzi (produkuje smieci w zalezonsci od populacji)
- Produkuje smieci roznych typow
- Liczba ludzi (produkuje smieci w zalezonsci od populacji)
Smietnik:
- Ma swoj typ
- {{ LICZNIK ? }}
- { { LICZNIK ? } }
Mapa:
Blok:
- Dom
- Droga
- Smietnik
- Dom
- Droga
- Smietnik
Rozmiar: Moze byc skalowalna
Opis:
- Kwadrat o boku X na ktorym ukladamy obiekty
- Obiektow innych niz droga moze byc x-1
```
Smieciarz ma sie nauczyc jezdzic po mapie i prawidolowo wywozic rozne typy smieci
=================
## Uruchamianie
Aby uruchomić grę należy podać do skryptu argument __home-count__, który ustala liczbę domków do wyrenderowania na mapie:
Aby uruchomić grę należy podać do skryptu argument **home-count**, który ustala liczbę domków do wyrenderowania na mapie:
```shell
game.py --home-count=<amount>
$ game.py --home-count=<amount>
```

View File

@ -1,23 +1,26 @@
import sys, getopt
import sys
import getopt
from sprites.cell import CELL_SIZE
def set_home_amount():
arguments = sys.argv[1:]
try:
optlist, args = getopt.getopt(arguments, '', ['home-count='])
for o, amount in optlist:
if o == '--home-count':
if int(amount) < 2:
print('Home count too low - must be higher than 2')
sys.exit(2)
return int(amount)
print('Missing argument: --home-count <amount>')
sys.exit(2)
except getopt.GetoptError as err:
print(err)
sys.exit(2)
arguments = sys.argv[1:]
try:
optlist, args = getopt.getopt(arguments, '', ['home-count='])
for o, amount in optlist:
if o == '--home-count':
if int(amount) < 2:
print('Home count too low - must be higher than 2')
sys.exit(2)
return int(amount)
print('Missing argument: --home-count <amount>')
sys.exit(2)
except getopt.GetoptError as err:
print(err)
sys.exit(2)
home_amount = set_home_amount()
PLAY_WIDTH = home_amount*CELL_SIZE
PLAY_HEIGHT = PLAY_WIDTH
PLAY_HEIGHT = PLAY_WIDTH

58
game.py
View File

@ -1,12 +1,13 @@
from pygame import *
import sys, random
from config import PLAY_WIDTH,PLAY_HEIGHT,home_amount
import sys
import random
from config import PLAY_WIDTH, PLAY_HEIGHT, home_amount
from sprites.house import House
from pygame.locals import *
import utils
##INITIALIZE STATIC VARIABLES#########
FPS = 60
FPS = 60
all_sprites = sprite.Group()
fps_clock = time.Clock()
@ -24,37 +25,36 @@ display.set_caption('Smieciarz WMI')
######################################
##
#Generate level
utils.generate_grass( all_sprites )
utils.generate_houses( all_sprites, obstacles_coords )
utils.generate_landfills( all_sprites, obstacles_coords )
gc = utils.generate_garbage_collector( all_sprites, obstacles_coords )
# Generate level
utils.generate_grass(all_sprites)
utils.generate_houses(all_sprites, obstacles_coords)
utils.generate_landfills(all_sprites, obstacles_coords)
gc = utils.generate_garbage_collector(all_sprites, obstacles_coords)
##
##GAME LOOP#######################################################################
while(1):
for e in event.get():
if e.type == QUIT:
quit()
sys.exit()
if e.type == KEYUP:
if e.key == K_UP:
gc.move('up', obstacles_coords)
if e.key == K_DOWN:
gc.move('down', obstacles_coords)
if e.key == K_RIGHT:
gc.move('right', obstacles_coords)
if e.key == K_LEFT:
gc.move('left', obstacles_coords)
for e in event.get():
if e.type == QUIT:
quit()
sys.exit()
if e.type == KEYUP:
if e.key == K_UP:
gc.move('up', obstacles_coords)
if e.key == K_DOWN:
gc.move('down', obstacles_coords)
if e.key == K_RIGHT:
gc.move('right', obstacles_coords)
if e.key == K_LEFT:
gc.move('left', obstacles_coords)
all_sprites.update()
all_sprites.draw(GAMEWINDOW)
all_sprites.update()
all_sprites.draw(GAMEWINDOW)
for item in all_sprites:
if( type(item) == House ):
item.generate_rubbish()
for item in all_sprites:
if(type(item) == House):
item.generate_rubbish()
display.flip()
fps_clock.tick(FPS)
display.flip()
fps_clock.tick(FPS)
##################################################################################

View File

@ -5,12 +5,14 @@ from pygame.locals import *
CELL_SIZE = 64
class Cell(pygame.sprite.Sprite):
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.update()
def update(self):
self.rect = pygame.Rect(self.x*CELL_SIZE,self.y*CELL_SIZE, CELL_SIZE, CELL_SIZE)
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.update()
def update(self):
self.rect = pygame.Rect(
self.x*CELL_SIZE, self.y*CELL_SIZE, CELL_SIZE, CELL_SIZE)

View File

@ -1,24 +1,23 @@
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: ('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)
}
def move(self, direction, forbidden):
(destination, value) = self.move_options[direction](forbidden)
if( destination is 'x'):
self.x = value
elif( destination is 'y'):
self.y = value
self.update()
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)
}
def move(self, direction, forbidden):
(destination, value) = self.move_options[direction](forbidden)
if(destination is 'x'):
self.x = value
elif(destination is 'y'):
self.y = value
self.update()

View File

@ -2,7 +2,8 @@ import pygame
import sys
from sprites.cell import Cell
class Grass(Cell):
def __init__(self, x, y ):
Cell.__init__(self,x,y)
def __init__(self, x, y):
Cell.__init__(self, x, y)
self.image = pygame.image.load("images/grass.png")

View File

@ -1,32 +1,36 @@
import pygame
import sys, random
import sys
import random
from sprites.cell import Cell
PLASTIC = 0 #blue
GLASS = 1 #green
METAL = 2 #yellow
PLASTIC = 0 # blue
GLASS = 1 # green
METAL = 2 # yellow
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 = [random.randint(0, max_plastic), random.randint(0, max_glass), random.randint(0, max_metal)] #plastic, glass, metal
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 = [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
self.max_metal = max_metal
self.max_plastic = max_plastic
self.max_glass = max_glass
self.max_metal = max_metal
def generate_rubbish(self):
if( random.randint(0, 5) == 1 ): #1/5 szansa na wyrzucenie śmiecia w klatce
thrash_type = random.randint(0,2)
self.rubbish[thrash_type] = self.rubbish[thrash_type] + 1
def generate_rubbish(self):
if(random.randint(0, 5) == 1): # 1/5 szansa na wyrzucenie śmiecia w klatce
thrash_type = random.randint(0, 2)
self.rubbish[thrash_type] = self.rubbish[thrash_type] + 1
if( self.rubbish[PLASTIC] > self.max_plastic ):
self.image = pygame.image.load("images/house_plastic.png")
if( self.rubbish[GLASS] > self.max_glass ):
self.image = pygame.image.load("images/house_glass.png")
if( self.rubbish[METAL] > self.max_metal ):
self.image = pygame.image.load("images/house_metal.png")
if(self.rubbish[PLASTIC] > self.max_plastic):
self.image = pygame.image.load("images/house_plastic.png")
if(self.rubbish[GLASS] > self.max_glass):
self.image = pygame.image.load("images/house_glass.png")
if(self.rubbish[METAL] > self.max_metal):
self.image = pygame.image.load("images/house_metal.png")
def check_rubbish_status(self):
print( "plastic: " + str(self.rubbish[PLASTIC]) + " glass: " + str(self.rubbish[GLASS]) + " metal: " + str(self.rubbish[METAL]) )
def check_rubbish_status(self):
print("plastic: " + str(self.rubbish[PLASTIC]) + " glass: " + str(
self.rubbish[GLASS]) + " metal: " + str(self.rubbish[METAL]))

View File

@ -2,9 +2,10 @@ import pygame
import sys
from sprites.cell import Cell
class Landfill(Cell):
def __init__(self, x,y, type):
Cell.__init__(self,x,y)
types = ["plastic", "glass", "metal"]
self.type = types[type]
self.image = pygame.image.load("images/landfill_%s.png" %(self.type))
def __init__(self, x, y, type):
Cell.__init__(self, x, y)
types = ["plastic", "glass", "metal"]
self.type = types[type]
self.image = pygame.image.load("images/landfill_%s.png" % (self.type))

103
utils.py
View File

@ -1,63 +1,76 @@
import sys, getopt, random
from config import PLAY_WIDTH,PLAY_HEIGHT,home_amount
import sys
import getopt
import random
from config import PLAY_WIDTH, PLAY_HEIGHT, home_amount
from sprites.cell import CELL_SIZE
from sprites.grass import Grass
from sprites.house import House
from sprites.landfill import Landfill
from sprites.garbage_collector import Garbage_collector
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)))
return (random.randint(0, max_x), random.randint(0, (max_y)))
##GENERATE GRASS##################################################################
def generate_grass( all_sprites ):
grass = []
for k in range(0,(PLAY_WIDTH//CELL_SIZE)*(PLAY_HEIGHT//CELL_SIZE)):
x,y = (int(k%(PLAY_WIDTH//CELL_SIZE)), int(k/(PLAY_WIDTH//CELL_SIZE)))
grass.append( Grass(x,y) )
for item in grass:
all_sprites.add(item)
def generate_grass(all_sprites):
grass = []
for k in range(0, (PLAY_WIDTH//CELL_SIZE)*(PLAY_HEIGHT//CELL_SIZE)):
x, y = (int(k % (PLAY_WIDTH//CELL_SIZE)),
int(k/(PLAY_WIDTH//CELL_SIZE)))
grass.append(Grass(x, y))
for item in grass:
all_sprites.add(item)
##################################################################################
##GENERATE HOUSES#################################################################
def generate_houses( all_sprites, obstacles_coords ):
houses = []
home_counter = home_amount
while( home_counter != 0 ):
x,y = generate_rand_coordinates((PLAY_WIDTH//CELL_SIZE)-1,(PLAY_HEIGHT//CELL_SIZE)-1)
if( (x,y) not in obstacles_coords ):
houses.append( House(x,y, 10, 10, 10) )
obstacles_coords.append((x,y))
home_counter = home_counter - 1
for item in houses:
all_sprites.add(item)
def generate_houses(all_sprites, obstacles_coords):
houses = []
home_counter = home_amount
while(home_counter != 0):
x, y = generate_rand_coordinates(
(PLAY_WIDTH//CELL_SIZE)-1, (PLAY_HEIGHT//CELL_SIZE)-1)
if((x, y) not in obstacles_coords):
houses.append(House(x, y, 10, 10, 10))
obstacles_coords.append((x, y))
home_counter = home_counter - 1
for item in houses:
all_sprites.add(item)
##################################################################################
##GENERATE LANDFILLS##############################################################
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
for item in landfills:
all_sprites.add(item)
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
for item in landfills:
all_sprites.add(item)
##################################################################################
##GENERATE GARBAGE COLLECTOR######################################################
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 ):
gc = Garbage_collector(x,y)
break
all_sprites.add(gc)
return gc
##################################################################################
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):
gc = Garbage_collector(x, y)
break
all_sprites.add(gc)
return gc
##################################################################################