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 VENV
env env
**/__pycache__ **/__pycache__
linux_env linux_env
.vscode

View File

@ -2,6 +2,7 @@
## Lekki notateczki ## Lekki notateczki
```yaml
Modele: Modele:
Smieciarka: Smieciarka:
- Zbiera smieci - Zbiera smieci
@ -14,31 +15,31 @@ Modele:
- Ma swoj typ - Ma swoj typ
- Moze byc zebrany - Moze byc zebrany
Dom: Dom:
- Produkuje smieci roznych typow - Produkuje smieci roznych typow
- Liczba ludzi (produkuje smieci w zalezonsci od populacji) - Liczba ludzi (produkuje smieci w zalezonsci od populacji)
Smietnik: Smietnik:
- Ma swoj typ - Ma swoj typ
- {{ LICZNIK ? }} - { { LICZNIK ? } }
Mapa: Mapa:
Blok: Blok:
- Dom - Dom
- Droga - Droga
- Smietnik - Smietnik
Rozmiar: Moze byc skalowalna Rozmiar: Moze byc skalowalna
Opis: Opis:
- Kwadrat o boku X na ktorym ukladamy obiekty - Kwadrat o boku X na ktorym ukladamy obiekty
- Obiektow innych niz droga moze byc x-1 - Obiektow innych niz droga moze byc x-1
```
Smieciarz ma sie nauczyc jezdzic po mapie i prawidolowo wywozic rozne typy smieci Smieciarz ma sie nauczyc jezdzic po mapie i prawidolowo wywozic rozne typy smieci
================= =================
## Uruchamianie ## 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 ```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 from sprites.cell import CELL_SIZE
def set_home_amount(): def set_home_amount():
arguments = sys.argv[1:] arguments = sys.argv[1:]
try: try:
optlist, args = getopt.getopt(arguments, '', ['home-count=']) optlist, args = getopt.getopt(arguments, '', ['home-count='])
for o, amount in optlist: for o, amount in optlist:
if o == '--home-count': if o == '--home-count':
if int(amount) < 2: if int(amount) < 2:
print('Home count too low - must be higher than 2') print('Home count too low - must be higher than 2')
sys.exit(2) sys.exit(2)
return int(amount) return int(amount)
print('Missing argument: --home-count <amount>') print('Missing argument: --home-count <amount>')
sys.exit(2) sys.exit(2)
except getopt.GetoptError as err: except getopt.GetoptError as err:
print(err) print(err)
sys.exit(2) sys.exit(2)
home_amount = set_home_amount() home_amount = set_home_amount()
PLAY_WIDTH = home_amount*CELL_SIZE 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 * from pygame import *
import sys, random import sys
from config import PLAY_WIDTH,PLAY_HEIGHT,home_amount import random
from config import PLAY_WIDTH, PLAY_HEIGHT, home_amount
from sprites.house import House from sprites.house import House
from pygame.locals import * from pygame.locals import *
import utils import utils
##INITIALIZE STATIC VARIABLES######### ##INITIALIZE STATIC VARIABLES#########
FPS = 60 FPS = 60
all_sprites = sprite.Group() all_sprites = sprite.Group()
fps_clock = time.Clock() fps_clock = time.Clock()
@ -24,37 +25,36 @@ display.set_caption('Smieciarz WMI')
###################################### ######################################
## ##
#Generate level # Generate level
utils.generate_grass( all_sprites ) utils.generate_grass(all_sprites)
utils.generate_houses( all_sprites, obstacles_coords ) utils.generate_houses(all_sprites, obstacles_coords)
utils.generate_landfills( all_sprites, obstacles_coords ) utils.generate_landfills(all_sprites, obstacles_coords)
gc = utils.generate_garbage_collector( all_sprites, obstacles_coords ) gc = utils.generate_garbage_collector(all_sprites, obstacles_coords)
## ##
##GAME LOOP####################################################################### ##GAME LOOP#######################################################################
while(1): while(1):
for e in event.get(): for e in event.get():
if e.type == QUIT: if e.type == QUIT:
quit() quit()
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', obstacles_coords)
if e.key == K_DOWN: if e.key == K_DOWN:
gc.move('down', obstacles_coords) gc.move('down', obstacles_coords)
if e.key == K_RIGHT: if e.key == K_RIGHT:
gc.move('right', obstacles_coords) gc.move('right', obstacles_coords)
if e.key == K_LEFT: if e.key == K_LEFT:
gc.move('left', obstacles_coords) gc.move('left', obstacles_coords)
all_sprites.update() all_sprites.update()
all_sprites.draw(GAMEWINDOW) all_sprites.draw(GAMEWINDOW)
for item in all_sprites: for item in all_sprites:
if( type(item) == House ): if(type(item) == House):
item.generate_rubbish() item.generate_rubbish()
display.flip() display.flip()
fps_clock.tick(FPS) fps_clock.tick(FPS)
################################################################################## ##################################################################################

View File

@ -5,12 +5,14 @@ from pygame.locals import *
CELL_SIZE = 64 CELL_SIZE = 64
class Cell(pygame.sprite.Sprite): class Cell(pygame.sprite.Sprite):
def __init__(self,x,y): def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.x = x self.x = x
self.y = y self.y = y
self.update() self.update()
def update(self): 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,24 +1,23 @@
import pygame import pygame
from sprites.cell import Cell from sprites.cell import Cell
class Garbage_collector(Cell): class Garbage_collector(Cell):
def __init__(self, x,y): def __init__(self, x, y):
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 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 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 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 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 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 import sys
from sprites.cell import Cell from sprites.cell import Cell
class Grass(Cell): class Grass(Cell):
def __init__(self, x, y ): def __init__(self, x, y):
Cell.__init__(self,x,y) Cell.__init__(self, x, y)
self.image = pygame.image.load("images/grass.png") self.image = pygame.image.load("images/grass.png")

View File

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

103
utils.py
View File

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