我正在pygame中制作平台游戏。我正在为这个关卡添加暴民。我认为这与添加平台相同,但事实并非如此。我不断收到此错误: “ TypeError:'Mob'对象不可下标”。 (底部的完整错误)。谁能解决这个问题并解释?注意:我要导入很多文件,所以会有很多导入。
GridBagLayout layout = (GridBagLayout)panel;
Component[] components = panel.getComponent();
for (each component in the array)
{
GridBagConstraint gbc = layout.getConstraints( component )
if (gbc.gridX == ?? && gbc.gridY = ??)
{
// remove the component from the panel
}
}
panel.revalidate();
import pygame
from spritesheet_functions import SpriteSheet
SLIME_WALK = (52, 125, 50, 28)
class Mob(pygame.sprite.Sprite):
""" Mobs the player will encounter in the level. """
def __init__(self, sprite_sheet_data):
""" Mob constructor. Assumes constructed with
user passing in
an array of 5 numbers like what's defined at the
top of this
code. """
super().__init__()
sprite_sheet = SpriteSheet('enemies.png')
self.image = sprite_sheet.get_image(sprite_sheet_data[0],
sprite_sheet_data[1],
sprite_sheet_data[2],
sprite_sheet_data[3])
self.rect = self.image.get_rect()
import pygame
from os import path
from spritesheet_functions import SpriteSheet
import constants
import platforms
import mobs
class Level(object):
def __init__(self, player):
# Lists of sprites used in all levels. Add or remove
# lists as needed for your game.
self.platform_list = None
self.enemy_list = None
# Background image
self.background = None
# How far this world has been scrolled left/right
self.world_shift = 0
self.level_limit = -1000
self.platform_list = pygame.sprite.Group()
self.enemy_list = pygame.sprite.Group()
self.player = player
# Update everything on this level
def update(self):
""" Update everything in this level."""
self.platform_list.update()
self.enemy_list.update()
def draw(self, screen):
""" Draw everything on this level. """
# Draw the background
# We don't shift the background as much as the
sprites are shifted
# to give a feeling of depth.
screen.fill(constants.BLUE)
screen.blit(self.background.sprite_sheet,
(self.world_shift // 3, 0))
# Draw all the sprite lists that we have
self.platform_list.draw(screen)
self.enemy_list.draw(screen)
def shift_world(self, shift_x):
""" When the user moves left/right and we need
to scroll everything: """
# Keep track of the shift amount
self.world_shift += shift_x
# Go through all the sprite lists and shift
for platform in self.platform_list:
platform.rect.x += shift_x
for enemy in self.enemy_list:
enemy.rect.x += shift_x
# Create platforms and mobs for the level
class Level01(Level):
""" Definition for level 1. """
def __init__(self, player):
""" Create level 1. """
# Call the parent constructor
Level.__init__(self, player)
self.background = SpriteSheet('background_01.png')
self.background.sprite_sheet.set_colorkey(constants.WHITE)
self.level_limit = -2500
# Array with type of platform, and x, y location of the
platform.
level_blocks = [[platforms.GRASS_LEFT, 500, 500],
[platforms.GRASS_MIDDLE, 570, 500],
[platforms.GRASS_RIGHT, 640, 500],
[platforms.GRASS_LEFT, 800, 400],
[platforms.GRASS_MIDDLE, 870, 400],
[platforms.GRASS_RIGHT, 940, 400],
[platforms.GRASS_LEFT, 1000, 500],
[platforms.GRASS_MIDDLE, 1070, 500],
[platforms.GRASS_RIGHT, 1140, 500],
[platforms.STONE_PLATFORM_LEFT, 1120, 280],
[platforms.STONE_PLATFORM_MIDDLE, 1190,
280],
[platforms.STONE_PLATFORM_RIGHT, 1260,
280],
]
# Array with type of mob, and x, y location of the mob
level_enemies = [[mobs.SLIME_WALK, 100, 400]]
# Go through the array above and add platforms
for platform in level_blocks:
block = platforms.Platform(platform[0])
block.rect.x = platform[1]
block.rect.y = platform[2]
block.player = self.player
self.platform_list.add(block)
# Go through the array above and add mobs
for enemy in level_enemies:
enemy = mobs.Mob(enemy[0])
enemy.rect.x = enemy[1]
enemy.rect.y = enemy[2]
enemy.player = self.player
self.platform_list.add(enemy)
答案 0 :(得分:0)
# Array with type of mob, and x, y location of the mob
level_enemies = [mobs.SLIME_WALK, 100, 400]
# Go through the array above and add mobs
for enemy in level_enemies:
enemy = mobs.Mob(enemy[0])
enemy.rect.x = enemy[1]
enemy.rect.y = enemy[2]
enemy.player = self.player
self.platform_list.add(enemy)
在此代码块中,enemy
指的是mob.SLIME_WALK
,100
,然后是400
。它们是ints
,不能下标。
也许您打算将level_enemies
声明为:
level_enemies = [[mobs.SLIME_WALK, 100, 400]]