Initialize grid generating with basic colours.

This commit is contained in:
nlitkowski 2019-04-07 23:39:18 +02:00
parent fe7cc509fd
commit 925c328169
4 changed files with 89 additions and 31 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
.vscode/settings.json .vscode/*
*/__pycache__/*

View File

@ -1,34 +1,47 @@
import pygame import pygame as pg
import numpy as np
class Grid: class Grid:
def __init__(self, game): # define rectangles dimensions
self.game = game r_width = 20
self.length = self.game.screen_res[0]/15 r_height = 20
self.width = (self.game.screen_res[1]/15)-3 r_margin = 5
self.nodes = [[Node(self, [row, col + 3]) # define some basic colors
for row in xrange(self.length)] # TODO: change to Enum
for col in xrange(self.width)] BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
def __init__(self, cols, rows):
self.table = np.zeros(shape=(rows, cols), dtype=int)
self.cols = list(range(cols))
self.rows = list(range(rows))
class Node: def draw_map(self, screen):
def __init__(self, grid, pos): screen.fill(self.BLACK)
self.grid = grid for col in self.cols:
self.game = self.grid.game for row in self.rows:
self.draw_node(screen, row, col)
self.pos = pos def draw_node(self, screen, row, col):
self.blit_pos = [i*15 for i in self.pos] if self.table[row][col] == 0:
self.color = [0, 0, 0] color = self.GREEN
elif self.table[row][col] == 1:
color = self.RED
elif self.table[row][col] == 2:
color = self.BLUE
# rect -> (left, top, width, height)
# draw.rect(surface, color, rect, margin)
self.image = pygame.Surface((15, 15)) pg.draw.rect(screen, color,
((col * (self.r_width + self.r_margin)) + self.r_margin,
(row * (self.r_height + self.r_margin)) + self.r_margin,
self.r_width, self.r_height))
pg.display.flip()
self.rect = self.image.get_rect(topleft=self.blit_pos) def change_field(self, row, col, field_type):
self.table[row][col] = field_type
self.solid = 0
self.in_path = False
self.checked = False
def fill(self, screen):
self.image.fill(self.color)
screen.blit(self.image, self.rect)

40
UI/window.py Normal file
View File

@ -0,0 +1,40 @@
import pygame as pg
import time
from pygame.locals import *
from UI.grid import Grid
class Window():
def __init__(self, grid):
pg.init()
# setup window
pg.display.set_caption('Inteligentna śmieciarka')
self.grid = grid
# assign to variables for brevity
cols = len(self.grid.cols)
rows = len(self.grid.rows)
width = self.grid.r_width
height = self.grid.r_height
margin = self.grid.r_margin
screen_width = cols * (width + margin) + 2 * margin
screen_height = rows * (height + margin) + 2 * margin
self.screen = pg.display.set_mode([screen_width, screen_height])
self.end = False
self.clock = pg.time.Clock()
grid.change_field(0, 0, 1)
grid.change_field(19, 19, 2)
path = [(i, i) for i in range(1, 20, 1)]
self.grid.draw_map(self.screen)
for t in path:
x, y = t
self.grid.change_field(x-1, y-1, 0)
self.grid.change_field(x, y, 1)
self.grid.draw_node(self.screen, x - 1, y - 1)
self.grid.draw_node(self.screen, x, y)
pg.time.delay(500)
pg.quit()

14
main.py
View File

@ -1,11 +1,15 @@
import pygame import pygame as pg
import numpy as np
from UI.grid import Grid
from UI.window import Window
def main(): def main():
pygame.init() # initialize grid
# Define screen size tuple grid = Grid(20, 20)
size = width, height = 1200, 900 # initialize window
screen = pygame.display.set_mode(size) window = Window(grid)
if __name__ == "__main__": if __name__ == "__main__":