Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] image VS drawing
#11
It's very interesting and all... but it's completely OFF-TOPIC (maths for isometric 2D).
Seriously, begin your own thread please
Reply
#12
Mock grass tile and ice block.
import pygame
from types import SimpleNamespace

BRED = pygame.Color(200, 110, 110)

def darken_color(color, darken=0.75):
    c = color.hsla
    color.hsla = c[0], c[1], c[2] * darken, c[3]

def shift_points(points, xshift=0, yshift=0):
    return tuple([(p[0] + xshift, p[1] + yshift) for p in points])

def top_points(x=0, y=0, w=64, h=32):
    if w < 0:
        w = 64 + w

    if h < 0:
        h = 32 + h

    w -= x
    h -= y

    topleft     = x,            y + h * 0.5
    topright    = x + w * 0.5,  y + x * 0.5
    bottomleft  = x + w * 0.5,  y + h - x * 0.5
    bottomright = x + w,        y + h * 0.5
    return topleft, topright, bottomright, bottomleft

def side_points(top_points, height=32):
    return ((top_points[0],
             top_points[3],
            (top_points[3][0],  top_points[3][1] + height),
            (top_points[0][0],  top_points[0][1] + height)),

            (top_points[3],
             top_points[2],
            (top_points[2][0],  top_points[2][1] + height),
            (top_points[3][0],  top_points[3][1] + height)))

# Patterns
def square_pattern(surface, color):
    top = top_points(x=24, w=-24)
    top = shift_points(top, -24, 32)
    darken_color(color, 0.6)
    pygame.draw.polygon(surface, color, top)
    for point_shift in ((16, -8), (16, 0), (16, 8), (32, -8), (32, 0), (32, 8), (48, 0)):
        shift = shift_points(top, *point_shift)
        pygame.draw.polygon(surface, color, shift)

class DrawBlock:
    def __init__(self, block, data):
        self.block = block
        self.data = data

    def draw(self, surface, pos):
        self.block.draw(surface, pos, self.data)

class Grass:
    def __init__(self, color):
        self.surface = pygame.Surface((64, 64)).convert_alpha()
        self.surface.fill((0,0,0,0))

        pygame.draw.polygon(self.surface, color, [(0,48), (32,32), (64,48), (32,64)])
        square_pattern(self.surface, color)

        #pygame.draw.polygon(self.surface, BRED, [(0,48), (32,32), (66,48), (32,64)], 1)

    def draw(self, surface, pos):
        surface.blit(self.surface, pos)

class Block:
    def __init__(self, color):
        self.surface = pygame.Surface((64, 64)).convert_alpha()
        self.surface.fill((0,0,0,0))
        self.create_image(color)

    def create_image(self, color):
        top = top_points()
        left, right = side_points(top)
        # top
        pygame.draw.polygon(self.surface, color, top)

        # left
        darken_color(color)
        pygame.draw.polygon(self.surface, color, left)

        # right
        darken_color(color)
        pygame.draw.polygon(self.surface, color, right)
        #pygame.draw.polygon(self.surface, BRED, [(32,32), (0,16), (32,0), (64,16)], 1)
        #pygame.draw.polygon(self.surface, BRED, [(0,16), (32,32), (32,64), (0,48)], 1)
        #pygame.draw.polygon(self.surface, BRED, [(32,32), (64,16), (64,48), (32,64)], 1)

    def draw(self, surface, pos):
        surface.blit(self.surface, pos)

class IceBlock(Block):
    def create_image(self, color):
        top_color = pygame.Color(*color)
        Block.create_image(self, color)
        top = top_points()
        points = (top[0],
                 (top[0][0] + 8, top[0][1] - 4),
                 (top[0][0] + 4, top[0][1] + 2))

        pygame.draw.polygon(self.surface, color, points)

        points = ((top[0][0] + 16, top[0][1] - 8),
                  (top[0][0] + 20, top[0][1] - 10),
                  (top[0][0] + 18, top[0][1] + 9),
                  (top[0][0] + 16, top[0][1] + 8))

        pygame.draw.polygon(self.surface, color, points)

        points = (top[2],
                 (int(top[2][0] - 20), int(top[2][1] - 10)),
                 (int(top[2][0] - 24), int(top[2][1] + 12)))

        pygame.draw.polygon(self.surface, color, points)

        points = ((int(top[2][0] - 4), int(top[2][1] + 16)),
                  (int(top[2][0] - 10), int(top[2][1] + 10)),
                  (int(top[2][0] - 12), int(top[2][1] + 24)),
                  (int(top[2][0] - 14), int(top[2][1] + 18)),
                  (int(top[2][0] - 24), int(top[2][1] + 18)),
                  (int(top[2][0] - 24), int(top[2][1] + 34)),

                  (int(top[3][0] - 8), int(top[3][1] + 6)),
                  (int(top[3][0] - 14), int(top[3][1] + 18)),
                  (int(top[3][0] - 20), int(top[3][1] + 9)),
                  (int(top[3][0] - 24), int(top[3][1])),
        )
        for p in points:
            self.surface.set_at(p, top_color)

class Tree:
    def __init__(self, color, trunk_color, ground_color):
        self.tree_top = pygame.Surface((64, 64)).convert_alpha()
        self.tree_top.fill((0,0,0,0))
        self.trunk = pygame.Surface((64, 64)).convert_alpha()
        self.trunk.fill((0,0,0,0))
        self.create_image(color, trunk_color, ground_color)

    def create_image(self, color, trunk_color, ground_color):
        # Tree Top
        top, left, right = self.layer(top_points(), 24, 8)
        self.draw_polygons(self.tree_top, top, left, right, pygame.Color(*color))
        top, left, right = self.layer(top_points(8, w=-8), 16, 8)
        self.draw_polygons(self.tree_top, top, left, right, pygame.Color(*color))
        top, left, right = self.layer(top_points(16, w=-16), 8, 8)
        self.draw_polygons(self.tree_top, top, left, right, pygame.Color(*color))
        top, left, right = self.layer(top_points(24, w=-24), 0, 8)
        self.draw_polygons(self.tree_top, top, left, right, pygame.Color(*color))
        # Trunk
        top = shift_points(top_points(), yshift=32)
        pygame.draw.polygon(self.trunk, ground_color, top)
        square_pattern(self.trunk, ground_color)
        top, left, right = self.layer(top_points(23, w=-23), 0, 32)
        self.draw_polygons(self.trunk, top, left, right, trunk_color)

    def draw_polygons(self, surface, top, left, right, color):
        # top
        pygame.draw.polygon(surface, color, top)

        # left
        darken_color(color)
        pygame.draw.polygon(surface, color, left)

        # right
        darken_color(color)
        pygame.draw.polygon(surface, color, right)

    def layer(self, toptop, height, diff):
        top = shift_points(toptop, yshift=height)
        left, right = side_points(top, diff)
        return top, left, right

    def draw(self, surface, pos):
        surface.blit(self.trunk, pos)
        position = pos[0], pos[1] - 32
        surface.blit(self.tree_top, position)

class Scene:
    def __init__(self):
        self.rect = pygame.Rect(0, 0, 800, 600)
        pygame.display.set_caption('Example')
        self.surface = pygame.display.set_mode(self.rect.size)
        self.clock = pygame.time.Clock()

        self.images = [Grass(pygame.Color('lawngreen')),
                       Block(pygame.Color('cyan')),
                       Block(pygame.Color('firebrick')),
                       Tree(pygame.Color('forestgreen'), pygame.Color('brown'), pygame.Color('lawngreen')),
                       IceBlock(pygame.Color('cyan')),
                       IceBlock(pygame.Color('firebrick'))
        ]

        self.map = [[3, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 1, 0, 0, 1, 0, 2, 2, 2, 0],
                    [0, 1, 0, 0, 1, 0, 0, 2, 0, 0],
                    [0, 1, 1, 1, 1, 0, 0, 2, 0, 0],
                    [0, 4, 0, 0, 1, 0, 0, 2, 0, 3],
                    [0, 4, 0, 0, 1, 0, 2, 5, 5, 0],
                    [3, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

    def loop(self):
        self.running = True
        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False

            self.surface.fill((0,0,0))

            cx = self.rect.centerx // 2 + len(self.map) * 32 + 16
            cy = self.rect.centery // 2 - len(self.map[0]) * 4
            w, h = self.rect.size
            for row, line in enumerate(self.map):
                for col, tile in enumerate(line):
                    pos = w - (row * 32 + cx) + col * 32, (col + row) * 16 + cy
                    self.images[tile].draw(self.surface, pos)

            pygame.display.flip()
            self.clock.tick(30)

        pygame.quit()

scene = Scene()
scene.loop()
99 percent of computer problems exists between chair and keyboard.
Reply
#13
[Image: Screenshot-2018-12-14-08-10-06.png]
Post the screenshot it would be easier
Maybe you could create a database of computer-drawn images (I'm not sure that what you want but anyway)
Reply
#14
When creating images at runtime. Keeps the program package smaller.
Con will have longer load time.

New code for creating image during runtime.
Github Pygame Blocks
[Image: Block-Shape1.png]
99 percent of computer problems exists between chair and keyboard.
Reply
#15
i would definitely not do it that way, but that is quite impressive. Thumbs Up
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020