From ee58257d54dd9ec3520f07ad7239bb8b07c7052e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20Pierzy=C5=84ski?= Date: Wed, 20 Mar 2019 23:33:22 +0100 Subject: [PATCH] Dodano podzial projektu na pliki + plik configuracyjnu --- config.py | 23 +++++++++++++++++ game.py | 70 +++++++++------------------------------------------- utils.py | 74 +++++++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 92 insertions(+), 75 deletions(-) create mode 100644 config.py diff --git a/config.py b/config.py new file mode 100644 index 0000000..aa39d75 --- /dev/null +++ b/config.py @@ -0,0 +1,23 @@ +import sys, 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 ') + 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 \ No newline at end of file diff --git a/game.py b/game.py index 111604b..7f55480 100644 --- a/game.py +++ b/game.py @@ -1,10 +1,7 @@ from pygame import * import sys, random -from sprites.cell import CELL_SIZE -from sprites.grass import Grass +from config import PLAY_WIDTH,PLAY_HEIGHT,home_amount from sprites.house import House -from sprites.landfill import Landfill -from sprites.garbage_collector import Garbage_collector from pygame.locals import * import utils @@ -16,68 +13,23 @@ fps_clock = time.Clock() ###################################### ##INITIALIZE DYNAMIC VARIABLES######## -home_amount = utils.set_home_amount() obstacles_coords = [] - -PLAY_WIDTH = home_amount*CELL_SIZE -PLAY_HEIGHT = PLAY_WIDTH - -WINDOW_WIDTH = PLAY_WIDTH -WINDOW_HEIGHT = PLAY_HEIGHT ###################################### ##GAMEWINDOW########################## +WINDOW_WIDTH = PLAY_WIDTH +WINDOW_HEIGHT = PLAY_HEIGHT GAMEWINDOW = display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32) -display.set_caption('Śmieciarz WMI') +display.set_caption('Smieciarz WMI') ###################################### -##GENERATE GRASS################################################################## -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################################################################# -houses = [] -home_counter = home_amount -while( home_counter != 0 ): - x,y = utils.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############################################################## -landfills = [] -landfill_counter = 3 -while( landfill_counter != 0): - x,y = utils.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###################################################### -while( True ): - x,y = utils.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) -################################################################################## +## +#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): diff --git a/utils.py b/utils.py index f9e35c3..e4c98e3 100644 --- a/utils.py +++ b/utils.py @@ -1,21 +1,63 @@ import sys, getopt, random - -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 ') - sys.exit(2) - except getopt.GetoptError as err: - print(err) - sys.exit(2) +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 ): + 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) +################################################################################## + +##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) +################################################################################## + +##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 +################################################################################## \ No newline at end of file