首页 > 解决方案 > Python - 外星人入侵 为什么我的墙没有呈现它们的形状?

问题描述

我目前正在尝试在我的游戏中添加可以被玩家激光和外星人炸弹摧毁的墙。目前,在将我的墙绘制到屏幕上时,它只是一条断线,而不是预期的形状。

这是我第一次使用枚举,我跟着 Clear Codes Youtube 视频,从我可以告诉我的代码理论上应该产生所需的形状。

我的墙目前的样子

墙.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']

主文件

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

标签: python

解决方案


如果不深入阅读该程序,核心问题很可能是您shape是一串串在一起的行,当字符串只是在没有任何分隔符的参数中简单地收集在一起时会发生这种情况..而您可能打算在它们之间放置逗号或换行符以形成精灵的行!

当前的:

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

N 个字符串的直接列表(添加逗号)

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

你也可以通过首先创建一个多行字符串然后分解它来做到这一点

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

是否在前两行包含尾随空格取决于您/您的实施


推荐阅读