Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
breakout clone
#24
Bricks and BricksB are identical.

I would change BRICKS_FILES to be this:
BRICK_FILES = (("brick0.png", "brick1.png", "brick2.png"), ("bbrick0.png", "bbrick1.png", "bbrick2.png"))
Bricks can randomly pick a group of image files.

With all the image files there's not much reason for an image dictionary. I would modify the code so each Brick has their own images. This will also make it easy to write text on the bricks.
class Brick(EnhancedSprite):
    """
    A target for the ball.  After I take some number of hits I die.
    Number of hits I can take is in range 1 to 3.  Hits is randomly
    selected if not specified.

    Specify brick color using (R, G, B) format.  If color not specified
    a color is selected based on the row.
    image
    """
    group = pygame.sprite.Group()

    def __init__(self, x, y, color=None, hits=None, image_files=None):
        color = color or random.choice(BRICK_COLORS)
        hits = hits or random.choice((1, 1, 1, 2, 2, 3))
        hits = max(0, min(len(image_files, hits)))
        self.value = self.hits = hits
        image_files = image_files or random.choice(BRICK_FILES)
        self.images = [create_image(image_file, color) for image_file in image_files]
        super().__init__(self.images[self.hits-1], self.group)
        self.at(x, y)

    def __len__(self):
        """Return how many bricks remaining"""
        return len(self.group)

    def hit(self):
        """
        I was hit!  Update my appearance or die based on my hit total.
        Return score based on being hit.
        """
        self.hits -= 1
        if self.hits > 0:
            self.image = self.images[self.hits-1]
            return 0
        self.kill()
        return self.value
This simplifies the brick creation. The main function doesn't do anything special for bricks.
        while len(lives) > 0:
            for coords in BRICK_COORDS:
                 Brick(*coords)
            allsprites.add(Brick.group)
And I modified the coordinates too.
BRICK_COORDS = (
    (32, 32), (32, 64), (32, 96), (32, 128), (32, 160), (32, 192), (64, 64), (96, 96),
    (128, 64), (160, 32), (160, 64), (160, 96), (160, 128), (160, 160), (160, 192), (224, 32), 
    (224, 64), (224, 96), (224, 128), (224, 160), (224, 192), (256, 32), (256, 128), (288, 32), 
    (288, 64), (288, 96), (288, 128), (288, 160), (288, 192), (352, 32), (352, 64), (352, 96), 
    (352, 128), (352, 160), (352, 192), (384, 32), (384, 96), (384, 128), (416, 32), (416, 64), 
    (416, 96), (416, 160), (448, 192), (512, 32), (512, 64), (512, 96), (512, 128), (512, 160), 
    (512, 192), (544, 32), (544, 192), (576, 32), (576, 192), (640, 32), (640, 64), (640, 96), 
    (640, 128), (640, 160), (640, 192), (672, 32), (672, 192), (704, 32), (704, 192), (736, 32), 
    (736, 64), (736, 96), (736, 128), (736, 160), (736, 192))
You could, if you wanted, make a different brick coords list for each level. This will be cleaner if the list is at the top of the file instead of buried inside a method down near the bottom.

Note: I have not run any of this code.

I thought you wanted to put names on the bricks. Do you still need help with that? I noticed your code does not have the create_image() function that lets you write text on the sprite.
Reply


Messages In This Thread
breakout clone - by flash77 - Feb-09-2022, 07:27 PM
RE: breakout clone - by deanhystad - Feb-09-2022, 08:44 PM
RE: breakout clone - by flash77 - Feb-10-2022, 06:51 PM
RE: breakout clone - by deanhystad - Feb-13-2022, 04:17 AM
RE: breakout clone - by flash77 - Feb-14-2022, 08:40 PM
RE: breakout clone - by deanhystad - Feb-15-2022, 04:38 AM
RE: breakout clone - by deanhystad - Feb-18-2022, 11:04 PM
RE: breakout clone - by flash77 - Feb-19-2022, 05:55 PM
RE: breakout clone - by deanhystad - Feb-20-2022, 03:10 PM
RE: breakout clone - by flash77 - Feb-21-2022, 05:59 PM
RE: breakout clone - by flash77 - Feb-26-2022, 04:09 PM
RE: breakout clone - by deanhystad - Feb-26-2022, 04:12 PM
RE: breakout clone - by flash77 - Feb-26-2022, 04:31 PM
RE: breakout clone - by deanhystad - Feb-26-2022, 04:35 PM
RE: breakout clone - by flash77 - Feb-26-2022, 05:05 PM
RE: breakout clone - by deanhystad - Feb-26-2022, 06:20 PM
RE: breakout clone - by flash77 - Feb-26-2022, 08:53 PM
RE: breakout clone - by deanhystad - Feb-27-2022, 05:05 AM
RE: breakout clone - by flash77 - Feb-27-2022, 10:19 AM
RE: breakout clone - by deanhystad - Feb-27-2022, 02:41 PM
RE: breakout clone - by flash77 - Feb-27-2022, 06:41 PM
RE: breakout clone - by deanhystad - Feb-28-2022, 04:21 AM
RE: breakout clone - by flash77 - Mar-02-2022, 05:26 PM
RE: breakout clone - by deanhystad - Mar-02-2022, 08:44 PM
RE: breakout clone - by deanhystad - Mar-04-2022, 09:07 PM
RE: breakout clone - by flash77 - Mar-05-2022, 10:41 AM
RE: breakout clone - by deanhystad - Mar-05-2022, 02:50 PM
RE: breakout clone - by flash77 - Mar-05-2022, 07:11 PM
RE: breakout clone - by deanhystad - Mar-05-2022, 07:37 PM
RE: breakout clone - by flash77 - Mar-06-2022, 09:03 AM
RE: breakout clone - by deanhystad - Mar-06-2022, 04:53 PM
RE: breakout clone - by flash77 - Mar-07-2022, 06:13 PM
RE: breakout clone - by deanhystad - Mar-07-2022, 08:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] little space invaders / breakout game flash77 0 1,389 Jul-24-2024, 08:56 PM
Last Post: flash77
  breakout clone pygame flash77 2 2,656 Feb-06-2022, 06:36 PM
Last Post: flash77
  [PyGame] arkanoid / breakout clone flash77 2 5,122 Feb-04-2022, 05:42 PM
Last Post: flash77

Forum Jump:

User Panel Messages

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