Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] image VS drawing
#1
Simple improvement would to put images in a list or dict.
This would stop if block. When more image are added.
DISPLAYSURF.blit(tileImage[tile], (centered_x, centered_y)) #display the actual tile
My example. My math could be improve.
import pygame

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)])

    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)])

    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
            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


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 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