Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] image VS drawing
#6
(Dec-11-2018, 07:34 AM)ThePhi Wrote: A bit more than that... Even you didn't manage to create the same image as in my example (see the red line around each face of the cube? Not so easy drawing that by the program isn't it?).
Confused

Example with my poor quality red lines.
import pygame

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

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

        pygame.draw.polygon(self.surface, color, [(32,32), (64,48), (32,64), (0,48)])
        pygame.draw.polygon(self.surface, BRED, [(32,32), (64,48), (32,64), (0,48)], 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))

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

        c = color.hsla
        color.hsla = c[0], c[1], c[2] * 0.75, c[3]
        pygame.draw.polygon(self.surface, color, [(0,16), (32,32), (32,64), (0,48)])

        c = color.hsla
        color.hsla = c[0], c[1], c[2] * 0.75, c[3]
        pygame.draw.polygon(self.surface, color, [(32,32), (64,16), (64,48), (32,64)])
        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 BlockHalf:
    def __init__(self, color):
        self.surface = pygame.Surface((64, 64)).convert_alpha()
        self.surface.fill((0,0,0,0))

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

        # left
        c = color.hsla
        color.hsla = c[0], c[1], c[2] * 0.75, c[3]
        pygame.draw.polygon(self.surface, color, [(0,32), (32,48), (32,64), (0,56)])

        # right
        c = color.hsla
        color.hsla = c[0], c[1], c[2] * 0.75, c[3]
        pygame.draw.polygon(self.surface, color, [(32,48), (64,32), (64,56), (32,64)])

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

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 = [Square(pygame.Color('lawngreen')),
                       Block(pygame.Color('cyan')),
                       Block(pygame.Color('firebrick'))
        ]

        self.map = [[0, 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, 1, 0, 0, 1, 0, 0, 2, 0, 0],
                    [0, 1, 0, 0, 1, 0, 2, 2, 2, 0],
                    [0, 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()
One good thing about low quality images. You can see how fast it program runs. Then see how much slow down with high quality images. In programming everything has it cost.
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
[split] image VS drawing - by Windspar - Dec-09-2018, 10:44 PM
RE: [split] image VS drawing - by Windspar - Dec-13-2018, 06:02 PM
RE: [split] image VS drawing - by ThePhi - Dec-14-2018, 07:18 AM
RE: [split] image VS drawing - by Windspar - Dec-23-2018, 02:04 PM
RE: [split] image VS drawing - by metulburr - Dec-23-2018, 02:16 PM
RE: Simple code for isometric 2D games - by ThePhi - Dec-10-2018, 07:25 AM
RE: Simple code for isometric 2D games - by ThePhi - Dec-11-2018, 07:34 AM
RE: Simple code for isometric 2D games - by Windspar - Dec-11-2018, 01:43 PM
RE: Simple code for isometric 2D games - by ThePhi - Dec-11-2018, 06:28 PM
RE: Simple code for isometric 2D games - by ThePhi - Dec-13-2018, 07:37 AM

Forum Jump:

User Panel Messages

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