Poprawiono kod + oskryptowano poruszanie sie smieciarza
This commit is contained in:
parent
b43fd1f878
commit
ebab21363d
117
game.py
117
game.py
@ -1,5 +1,6 @@
|
||||
from pygame import *
|
||||
import sys, random
|
||||
from sprites.cell import CELL_SIZE
|
||||
from sprites.grass import Grass
|
||||
from sprites.house import House
|
||||
from sprites.landfill import Landfill
|
||||
@ -7,63 +8,78 @@ from sprites.garbage_collector import Garbage_collector
|
||||
from pygame.locals import *
|
||||
import utils
|
||||
|
||||
##INITIALIZE STATIC VARIABLES#########
|
||||
FPS = 5
|
||||
|
||||
all_sprites = sprite.Group()
|
||||
cells = []
|
||||
FPS = 5
|
||||
cell_size = 64
|
||||
fps_clock = time.Clock()
|
||||
######################################
|
||||
|
||||
#Tu będzie zmienna do wybrania przez użytkownika na start/ do zmiany w trakcie "gry"
|
||||
##INITIALIZE DYNAMIC VARIABLES########
|
||||
home_amount = utils.set_home_amount()
|
||||
obstacles_coords = []
|
||||
|
||||
|
||||
#Obszar przeznaczony na płyki
|
||||
PLAY_WIDTH = (home_amount+1)*64
|
||||
PLAY_WIDTH = home_amount*CELL_SIZE
|
||||
PLAY_HEIGHT = PLAY_WIDTH
|
||||
|
||||
#Całe okno gry (z przyszłym hud'em)
|
||||
WINDOW_WIDTH = PLAY_WIDTH #+ 100
|
||||
WINDOW_HEIGHT = PLAY_HEIGHT #+ 100
|
||||
WINDOW_WIDTH = PLAY_WIDTH
|
||||
WINDOW_HEIGHT = PLAY_HEIGHT
|
||||
######################################
|
||||
|
||||
##GAMEWINDOW##########################
|
||||
GAMEWINDOW = display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
|
||||
WHITE = (255, 255, 255)
|
||||
display.set_caption('Śmieciarz WMI')
|
||||
######################################
|
||||
|
||||
#Dodawanie pól typu Grass
|
||||
for x in range(PLAY_HEIGHT//64):
|
||||
cells.append([])
|
||||
for y in range(PLAY_HEIGHT//64):
|
||||
grass = Grass(x,y)
|
||||
cells[x].append(grass)
|
||||
##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)
|
||||
|
||||
##################################################################################
|
||||
|
||||
#Losowanie domków i dodawanie je do mapy
|
||||
home_len = home_amount
|
||||
trash_count = 2
|
||||
while( home_len > 0 ):
|
||||
#Sprawdzenie, czy istnieje już domek na danej pozycji, jeżeli tak to losuj ponownie
|
||||
x, y = utils.generate_rand_coordinates(PLAY_WIDTH, PLAY_HEIGHT)
|
||||
if( type(cells[x][y]) == Grass ):
|
||||
cells[x][y] = House(x,y, 10, 10, 10)
|
||||
home_len = home_len - 1
|
||||
##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
|
||||
|
||||
while(trash_count >= 0):
|
||||
x, y = utils.generate_rand_coordinates(PLAY_WIDTH, PLAY_HEIGHT)
|
||||
if( type(cells[x][y]) == Grass ):
|
||||
cells[x][y] = Landfill(x, y, trash_count)
|
||||
trash_count -= 1
|
||||
for item in houses:
|
||||
all_sprites.add(item)
|
||||
##################################################################################
|
||||
|
||||
while(True):
|
||||
x, y = utils.generate_rand_coordinates(PLAY_WIDTH, PLAY_HEIGHT)
|
||||
if type(cells[x][y]) is Grass:
|
||||
cells[x][y] = Garbage_collector(x,y)
|
||||
##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)
|
||||
##################################################################################
|
||||
|
||||
#Dodawanie wszystkich spritow do grupy spritow
|
||||
for x in range(len(cells)):
|
||||
for y in range(len(cells[x])):
|
||||
all_sprites.add(cells[x][y])
|
||||
|
||||
#Sama gierka
|
||||
##GAME LOOP#######################################################################
|
||||
while(1):
|
||||
for e in event.get():
|
||||
if e.type == QUIT:
|
||||
@ -71,23 +87,22 @@ while(1):
|
||||
sys.exit()
|
||||
if e.type == KEYUP:
|
||||
if e.key == K_UP:
|
||||
print('up')
|
||||
gc.move('up', obstacles_coords)
|
||||
if e.key == K_DOWN:
|
||||
print('down')
|
||||
gc.move('down', obstacles_coords)
|
||||
if e.key == K_RIGHT:
|
||||
print('right')
|
||||
gc.move('right', obstacles_coords)
|
||||
if e.key == K_LEFT:
|
||||
print('left')
|
||||
|
||||
gc.move('left', obstacles_coords)
|
||||
|
||||
|
||||
all_sprites.update()
|
||||
all_sprites.draw(GAMEWINDOW)
|
||||
|
||||
#generowanie smieci
|
||||
for house in all_sprites:
|
||||
if( type(house) == House ):
|
||||
house.generate_rubbish()
|
||||
#house.check_rubbish_status()
|
||||
for item in all_sprites:
|
||||
if( type(item) == House ):
|
||||
item.generate_rubbish()
|
||||
|
||||
display.flip()
|
||||
fps_clock.tick(FPS)
|
||||
##################################################################################
|
@ -3,9 +3,14 @@ import pygame
|
||||
import sys
|
||||
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.rect = pygame.Rect(x*64,y*64, 64, 64)
|
||||
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)
|
||||
|
@ -2,14 +2,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: self.y + 1 if (self.x, self.y + 1) not in forbidden else self.y,
|
||||
"down": lambda forbidden: self.y - 1 if (self.x, self.y - 1) not in forbidden else self.y,
|
||||
"left": lambda forbidden: self.x - 1 if (self.x - 1, self.y) not in forbidden else self.x,
|
||||
"right": lambda forbidden: self.x + 1 if (self.x + 1, self.y) not in forbidden else self.x
|
||||
}
|
||||
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()
|
||||
|
||||
|
||||
|
@ -3,6 +3,6 @@ import sys
|
||||
from sprites.cell import Cell
|
||||
|
||||
class Grass(Cell):
|
||||
def __init__(self,x,y):
|
||||
def __init__(self, x, y ):
|
||||
Cell.__init__(self,x,y)
|
||||
self.image = pygame.image.load("images/grass.png")
|
||||
|
@ -3,8 +3,8 @@ import sys
|
||||
from sprites.cell import Cell
|
||||
|
||||
class Landfill(Cell):
|
||||
def __init__(self, x, y, type):
|
||||
types = ["plastic", "glass", "metal"]
|
||||
self.type = types[type]
|
||||
Cell.__init__(self,x,y)
|
||||
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))
|
||||
|
5
utils.py
5
utils.py
@ -16,5 +16,6 @@ def set_home_amount():
|
||||
print(err)
|
||||
sys.exit(2)
|
||||
|
||||
def generate_rand_coordinates(PLAY_WIDTH, PLAY_HEIGHT):
|
||||
return (random.randint(0, (PLAY_WIDTH//64)-1), random.randint(0, (PLAY_WIDTH//64)-1))
|
||||
def generate_rand_coordinates(max_x, max_y):
|
||||
return (random.randint(0, max_x), random.randint(0, (max_y)))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user