Dodano generowanie obiektow na siatce z plikow map

This commit is contained in:
Konrad Pierzyński 2019-04-01 12:48:11 +02:00
parent 316f80387f
commit 65b5c8074b
17 changed files with 136 additions and 10 deletions

26
DataModels/Cell.py Normal file
View File

@ -0,0 +1,26 @@
import pygame, random
from DataModels.Container import Container
from PIL import Image,ImageDraw
from config import CELL_SIZE
class Cell( pygame.sprite.Sprite ):
def __init__( self, x, y, max_rubbish, yellow = 0, green = 0, blue = 0, image_name = None):
pygame.sprite.Sprite.__init__( self )
self.image_name = image_name or type(self).__name__
self.update_rect( x,y )
self.container = Container( max_rubbish, yellow, green, blue )
self.update_image()
def update_rect( self, x, y ):
self.x, self.y = x,y
self.rect = pygame.Rect( x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE )
def update_image( self ):
image = Image.open("Resources/Images/Image_" + self.image_name + ".png" )
draw = ImageDraw.Draw(image)
draw.text( (5,5), str( self.container.status() ) )
mode, size, data = image.mode, image.size, image.tobytes()
self.image = pygame.image.frombuffer( data, size, mode )

10
DataModels/Container.py Normal file
View File

@ -0,0 +1,10 @@
class Container():
def __init__( self, max, y = 0, g = 0, b = 0 ):
self.y, self.g, self.b = y, g, b
def empty( self ):
self.y, self.g, self.b = 0,0,0
def status( self ):
return [self.y, self.g, self.b]

7
DataModels/Dump.py Normal file
View File

@ -0,0 +1,7 @@
import pygame
from DataModels.Cell import Cell
class Dump( Cell ):
def __init__( self, x, y, max_rubbish, dump_type, yellow = 0, green = 0, blue = 0 ):
Cell.__init__( self, x, y, max_rubbish, yellow, green, blue, dump_type )

8
DataModels/Grass.py Normal file
View File

@ -0,0 +1,8 @@
import pygame
from config import CELL_SIZE
class Grass( pygame.sprite.Sprite ):
def __init__( self, x, y ):
pygame.sprite.Sprite.__init__( self )
self.rect = pygame.Rect( x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE )
self.image = pygame.image.load("Resources/Images/Image_Grass.png")

6
DataModels/House.py Normal file
View File

@ -0,0 +1,6 @@
from DataModels.Cell import Cell
class House( Cell ):
def __init__( self, x, y, max_rubbish, yellow = 0, green = 0, blue = 0 ):
Cell.__init__( self, x, y, max_rubbish, yellow, green, blue )

8
DataModels/Road.py Normal file
View File

@ -0,0 +1,8 @@
import pygame
from config import CELL_SIZE
class Road( pygame.sprite.Sprite ):
def __init__( self, x, y ):
pygame.sprite.Sprite.__init__( self )
self.rect = pygame.Rect( x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE )
self.image = pygame.image.load("Resources/Images/Image_Road.png")

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,3 @@
2 2
E H
H B

Binary file not shown.

BIN
c Executable file

Binary file not shown.

11
c.c Normal file
View File

@ -0,0 +1,11 @@
#include <stdio.h>
int main( void ) {
char c = NULL;
int b = c || 5;
printf("%d\r\n", b);
return 0;
}

View File

@ -1,12 +1,12 @@
import sys, random
CELL_SIZE = 64
FPS = 60
HOME_AMOUNT = 7
GRID_HEIGHT = HOME_AMOUNT
GRID_WIDTH = GRID_HEIGHT
map = open( sys.argv[1], 'r' )
GRID_WIDTH, GRID_HEIGHT = [int(x) for x in map.readline().split()]
WINDOW_HEIGHT = GRID_HEIGHT * CELL_SIZE
WINDOW_WIDTH = GRID_WIDTH * CELL_SIZE
WINDOW_WIDTH = GRID_WIDTH * CELL_SIZE
HOUSE_CAPACITY = random.randint(1, 11)

55
main.py
View File

@ -1,10 +1,57 @@
import pygame
import pygame, sys
from config import WINDOW_HEIGHT, WINDOW_WIDTH
from random import randint
from config import WINDOW_HEIGHT, WINDOW_WIDTH, GRID_HEIGHT, GRID_WIDTH, HOUSE_CAPACITY, FPS
from DataModels.Grass import Grass
from DataModels.House import House
from DataModels.Dump import Dump
pygame_sprites = pygame.sprite.Group()
FPS_CLOCK = pygame.time.Clock()
GAME_WINDOW = display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
GAME_WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
grid = [ [] for x in range(0)]
map = open(sys.argv[1], 'r')
map.readline()
map_objects = [ [ None for y in range(0,GRID_WIDTH)] for x in range(0, GRID_HEIGHT)]
def generate( letter ):
letter_mapping = {
'E': lambda x, y: Grass(x,y),
'H': lambda x, y, max_rubbish, yellow, green, blue: House( x, y, max_rubbish, yellow, green, blue ),
'B': lambda x, y, max_rubbish, dump_type: Dump( x, y, max_rubbish, dump_type )
}
return letter_mapping[letter]
i = 0
for y in map.readlines():
for x in y.split():
x_coord = i%GRID_WIDTH
y_coord = i //GRID_HEIGHT
yellow, green, blue = [randint(0, HOUSE_CAPACITY //2),randint(0, HOUSE_CAPACITY //2),randint(0, HOUSE_CAPACITY //2)]
if x is 'E':
map_objects[x_coord][y_coord] = generate(x)(x_coord, y_coord)
elif x is 'H':
map_objects[x_coord][y_coord] = generate(x)(x_coord, y_coord, HOUSE_CAPACITY, yellow, green, blue)
elif x is 'B':
map_objects[x_coord][y_coord] = generate(x)(x_coord, y_coord, 100, "Dump_Blue")
i += 1
for line in map_objects:
for item in line:
pygame_sprites.add(item)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame_sprites.update()
pygame_sprites.draw(GAME_WINDOW)
pygame.display.flip()
FPS_CLOCK.tick(FPS)