1

I'm currently attempting to add walls to my game that can be destroyed by both the players laser and the aliens bombs. Currently when drawing my wall to screen, it is just a single broken up line, instead of the expected shape.

This is my first time using enumerate, and I followed along with Clear Codes Youtube video and from what I can tell my code should in theory be producing the desired shape.

What my wall currently looks like

walls.py

import pygame
from pygame.sprite import Sprite

class Wall(Sprite):
    """A class to define our wall"""

    def __init__(self, size, color, x, y):
        super().__init__()
        self.image = pygame.Surface((size, size))
        self.image.fill(color)
        self.rect =  self.image.get_rect(topleft = (x, y))

shape = [
'  xxxxxxx'
' xxxxxxxxx'
'xxxxxxxxxxx'
'xxxxxxxxxxx'
'xxxxxxxxxxx'
'xxx     xxx'
'xx       xx']

main.py

import walls

class AlienInvasion:
    """Overall class to manage game assets and behavior."""

    def __init__(self):
        """Initlize the game, and create game resources."""
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height

        pygame.display.set_caption("Alien Invasion")

        self.stats = GameStats(self)
        self.sb = Scoreboard(self)

        self.ship = Ship(self)

        self._wall_setup()
        self._create_walls(40, 800)


        self._create_groups()
        self._create_fleet()
        self._create_buttons()

   def _wall_setup(self):
        self.shape = walls.shape
        self.block_size = 6
        self.blocks = pygame.sprite.Group()


   def _create_walls(self, x_start, y_start):
        for row_index, row in enumerate(self.shape):
            for col_index, col in enumerate(row):
                if col == 'x':
                    x = x_start + col_index * self.block_size
                    y = y_start + row_index * self.block_size
                    block = walls.Wall(self.block_size, (255, 255, 255), 
                    x, y)
                    self.blocks.add(block)

   def _update_screen(self):
        """Update images on screen and flip to the new screen."""
        #fill our background with our bg_color
        self.screen.fill(self.settings.bg_color)

        # draw scoreboard to screen
        self.sb.show_score()

        #draw ship to screen
        self.ship.blitme()

        for bullet in self.bullets.sprites():
            bullet.draw_bullet()

        self.alien_bombs.update()
        self.alien_bombs.draw(self.screen)

        self.aliens.draw(self.screen)

        self.powerups.draw(self.screen)
        self.powerups.update() 
        self._check_pow_collisions()

        self.blocks.draw(self.screen)

        # draw play button if game is inactive
        if not self.stats.game_active:
            if self.stats.level == 1:
                self.play_button.draw_button()

            elif not self.stats.ships_left:
                self.game_over_button.draw_button()
                pygame.mouse.set_visible(True)

            elif self.stats.ships_left != 0:
                self.continue_button.draw_button()

        #Make the most recently drawn screen visible.
        #this clears our previous screen and updates it to a new one
        #this gives our programe smooth movemnt
        pygame.display.flip()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Zoidberg
  • 21
  • 4

1 Answers1

3

Without deeply reading the program, the core issue is likely that your shape is a single string of lines catenated together, which happens when strings are simply collected together in an argument without any separator.. while you probably intended to put commas or newlines inbetween them to form the rows of the sprite!

current:

>>> print(shape)
['  xxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx     xxxxx       xx']

Direct list of N strings (comma added)

shape = [
'  xxxxxxx  ',
' xxxxxxxxx ',
'xxxxxxxxxxx',
'xxxxxxxxxxx',
'xxxxxxxxxxx',
'xxx     xxx',
'xx       xx',]
>>> print(shape)
['  xxxxxxx  ', ' xxxxxxxxx ', 'xxxxxxxxxxx', 'xxxxxxxxxxx', 'xxxxxxxxxxx', 'xxx     xxx', 'xx       xx']

You could also do this by first creating a multiline string and then breaking it up

shape = """  xxxxxxx  
 xxxxxxxxx 
xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxx
xxx     xxx
xx       xx""".splitlines()
>>> print(shape)
['  xxxxxxx  ', ' xxxxxxxxx ', 'xxxxxxxxxxx', 'xxxxxxxxxxx', 'xxxxxxxxxxx', 'xxx     xxx', 'xx       xx']

Whether you include trailing spaces on the first two rows is up to you/your implementation

ti7
  • 16,375
  • 6
  • 40
  • 68