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

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ VENV
env
**/__pycache__
linux_env
.vscode

View File

@ -2,6 +2,7 @@
## Lekki notateczki
```yaml
Modele:
Smieciarka:
- Zbiera smieci
@ -18,8 +19,7 @@ Modele:
- Liczba ludzi (produkuje smieci w zalezonsci od populacji)
Smietnik:
- Ma swoj typ
- {{ LICZNIK ? }}
- { { LICZNIK ? } }
Mapa:
Blok:
@ -30,15 +30,16 @@ Mapa:
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,6 +1,8 @@
import sys, getopt
import sys
import getopt
from sprites.cell import CELL_SIZE
def set_home_amount():
arguments = sys.argv[1:]
try:
@ -17,6 +19,7 @@ def set_home_amount():
print(err)
sys.exit(2)
home_amount = set_home_amount()
PLAY_WIDTH = home_amount*CELL_SIZE

18
game.py
View File

@ -1,6 +1,7 @@
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
@ -24,11 +25,11 @@ 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#######################################################################
@ -47,12 +48,11 @@ while(1):
if e.key == K_LEFT:
gc.move('left', obstacles_coords)
all_sprites.update()
all_sprites.draw(GAMEWINDOW)
for item in all_sprites:
if( type(item) == House ):
if(type(item) == House):
item.generate_rubbish()
display.flip()

View File

@ -5,12 +5,14 @@ from pygame.locals import *
CELL_SIZE = 64
class Cell(pygame.sprite.Sprite):
def __init__(self,x,y):
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)
self.rect = pygame.Rect(
self.x*CELL_SIZE, self.y*CELL_SIZE, CELL_SIZE, CELL_SIZE)

View File

@ -1,9 +1,10 @@
import pygame
from sprites.cell import Cell
class Garbage_collector(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/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),
@ -14,11 +15,9 @@ class Garbage_collector(Cell):
def move(self, direction, forbidden):
(destination, value) = self.move_options[direction](forbidden)
if( destination is 'x'):
if(destination is 'x'):
self.x = value
elif( destination is 'y'):
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)
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.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
def generate_rubbish(self):
if( random.randint(0, 5) == 1 ): #1/5 szansa na wyrzucenie śmiecia w klatce
thrash_type = random.randint(0,2)
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 ):
if(self.rubbish[PLASTIC] > self.max_plastic):
self.image = pygame.image.load("images/house_plastic.png")
if( self.rubbish[GLASS] > self.max_glass ):
if(self.rubbish[GLASS] > self.max_glass):
self.image = pygame.image.load("images/house_glass.png")
if( self.rubbish[METAL] > self.max_metal ):
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]) )
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)
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))
self.image = pygame.image.load("images/landfill_%s.png" % (self.type))

View File

@ -1,35 +1,42 @@
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
def generate_rand_coordinates(max_x, max_y):
return (random.randint(0, max_x), random.randint(0, (max_y)))
##GENERATE GRASS##################################################################
def generate_grass( all_sprites ):
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 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 ):
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))
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:
@ -37,14 +44,17 @@ def generate_houses( all_sprites, obstacles_coords ):
##################################################################################
##GENERATE LANDFILLS##############################################################
def generate_landfills( 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))
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:
@ -52,11 +62,14 @@ def generate_landfills( all_sprites, obstacles_coords ):
##################################################################################
##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)
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