SZI2019SmieciarzWmi/game.py

108 lines
3.3 KiB
Python
Raw Normal View History

2019-03-19 10:08:38 +01:00
from pygame import *
2019-03-19 11:47:23 +01:00
import sys, random
from sprites.cell import CELL_SIZE
2019-03-19 10:08:38 +01:00
from sprites.grass import Grass
2019-03-19 11:47:23 +01:00
from sprites.house import House
2019-03-19 18:29:18 +01:00
from sprites.landfill import Landfill
2019-03-20 11:20:10 +01:00
from sprites.garbage_collector import Garbage_collector
2019-03-19 11:47:23 +01:00
from pygame.locals import *
import utils
##INITIALIZE STATIC VARIABLES#########
FPS = 5
2019-03-19 11:47:23 +01:00
all_sprites = sprite.Group()
fps_clock = time.Clock()
######################################
2019-03-19 11:47:23 +01:00
##INITIALIZE DYNAMIC VARIABLES########
home_amount = utils.set_home_amount()
obstacles_coords = []
2019-03-19 11:47:23 +01:00
PLAY_WIDTH = home_amount*CELL_SIZE
2019-03-19 11:47:23 +01:00
PLAY_HEIGHT = PLAY_WIDTH
2019-03-19 10:08:38 +01:00
WINDOW_WIDTH = PLAY_WIDTH
WINDOW_HEIGHT = PLAY_HEIGHT
######################################
##GAMEWINDOW##########################
2019-03-19 11:47:23 +01:00
GAMEWINDOW = display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
display.set_caption('Śmieciarz WMI')
######################################
2019-03-19 10:08:38 +01:00
##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)
##################################################################################
2019-03-19 11:47:23 +01:00
##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
2019-03-19 11:47:23 +01:00
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)
##################################################################################
2019-03-19 11:47:23 +01:00
##GAME LOOP#######################################################################
2019-03-19 11:47:23 +01:00
while(1):
for e in event.get():
if e.type == QUIT:
quit()
sys.exit()
2019-03-20 11:20:10 +01:00
if e.type == KEYUP:
if e.key == K_UP:
gc.move('up', obstacles_coords)
2019-03-20 11:20:10 +01:00
if e.key == K_DOWN:
gc.move('down', obstacles_coords)
2019-03-20 11:20:10 +01:00
if e.key == K_RIGHT:
gc.move('right', obstacles_coords)
2019-03-20 11:20:10 +01:00
if e.key == K_LEFT:
gc.move('left', obstacles_coords)
2019-03-19 11:47:23 +01:00
all_sprites.update()
all_sprites.draw(GAMEWINDOW)
2019-03-19 18:37:48 +01:00
for item in all_sprites:
if( type(item) == House ):
item.generate_rubbish()
2019-03-19 11:47:23 +01:00
display.flip()
fps_clock.tick(FPS)
##################################################################################