0

だから私はプログラミングの方法を学び始めたばかりで、動くプラットフォームでこの小さなゲームを作ろうとしています。通常の壁/プラットフォームを機能させることはできますが、これらの動くものを機能させる方法がわかりません。次のようなトレースバックを取得し続けます。

Traceback (most recent call last):
  File "C:\Users\Jacob\Desktop\gametest.py", line 227, in <module>
    walls.update()
  File "C:\Python34\lib\site-packages\pygame\sprite.py", line 462, in update
    s.update(*args)
  File "C:\Users\Jacob\Desktop\gametest.py", line 111, in update
    hit = pygame.sprite.collide_rect(self, self.player)
  File "C:\Python34\lib\site-packages\pygame\sprite.py", line 1300, in collide_rect
    return left.rect.colliderect(right.rect)
AttributeError: 'NoneType' object has no attribute 'rect'

問題は、私が持っているこれらのコードの一部に関係していると思いますが、完全にはわかりません。

class Wall(pygame.sprite.Sprite):

    def __init__(self, width, height):
        #creates the wall
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([width, height])
        self.image.fill(blue)

        self.rect = self.image.get_rect()


######################################################
class MovingEnemy(Wall):
    change_x = 0
    change_y=0

    boundary_top = 0
    boundary_bottom = 0
    boundary_left = 0
    boundary_right = 0

    player = None
    level = None

    def update(self):
        # Move left/right
        self.rect.x += self.change_x

        # See if we hit the player
        hit = pygame.sprite.collide_rect(self, self.player)
        if hit:
            # We did hit the player. Shove the player around and
            # assume he/she won't hit anything else.

            # If we are moving right, set our right side
            # to the left side of the item we hit
            if self.change_x < 0:
                self.player.rect.right = self.rect.left
            else:
                # Otherwise if we are moving left, do the opposite.
                self.player.rect.left = self.rect.right

        # Move up/down
        self.rect.y += self.change_y

        # Check and see if we the player
        hit = pygame.sprite.collide_rect(self, self.player)
        if hit:
            # We did hit the player. Shove the player around and
            # assume he/she won't hit anything else.

            # Reset our position based on the top/bottom of the object.
            if self.change_y < 0:
                self.player.rect.bottom = self.rect.top
            else:
                self.player.rect.top = self.rect.bottom

        # Check the boundaries and see if we need to reverse
        # direction.
        if self.rect.bottom > self.boundary_bottom or self.rect.top < self.boundary_top:
            self.change_y *= -1

        cur_pos = self.rect.x - self.level.world_shift
        if cur_pos < self.boundary_left or cur_pos > self.boundary_right:
            self.change_x *= -1




wall = MovingEnemy(70,40)
wall.rect.x = 500
wall.rect.y = 400
wall.boundary_left = 250
wall.boundary_right = 800
wall.change_x = 1
walls.add(wall)

助けを受けるために正しい情報を提供したかどうかはわかりませんが、正直に答えようとしています。これを行う方法を探して何時間もインターネットを閲覧しましたが、試したことはすべてうまくいかないようです。誰かが私が持っているこのごちゃごちゃした混乱を理解し、私を助けてくれるなら、本当に感謝します.

編集: プレイヤー クラスがありますが、MovingEnemy 内のプレイヤーをそのクラスに設定する必要がありますか? それが可能かどうか、または正確に何に設定する必要があるかはわかりません。これが簡単になる場合、これが私のプレーヤークラスです。

class Player(pygame.sprite.Sprite):

    #Sets the starting speed
    change_x = 0
    change_y = 0
    walls = None


    def __init__(self, x, y):
        #creates the sprite for the player
        pygame.sprite.Sprite.__init__(self)

        #sets the size
        self.image = pygame.Surface([25,25])
        self.image.fill(green)

        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x

    def movement(self, x, y):

        self.change_x += x
        self.change_y += y

    def update(self):

        #changes the position of the player moving left and right
        self.rect.x += self.change_x



        #checks to see if the player sprite hits the wall
        collision = pygame.sprite.spritecollide(self, self.walls, False)
        for block in collision:
            # If the player hits a block while moving right, it is set back
            # to the left side of the block that was hit.
            if self.change_x > 0:
                self.rect.right = block.rect.left
            # Does the same as above, except with moving left.   
            else:
                self.rect.left = block.rect.right


        #changes the position of the player moving up and down    
        self.rect.y += self.change_y

        collision = pygame.sprite.spritecollide(self, self.walls, False)
        for block in collision:

            # Does the same as the above "for" except for moving up and down
            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom
4

2 に答える 2

1

あなたのMovingEnemyクラスには、 という属性がありますplayerplayerは から始まりNoneます。playerコードのどこでも , 以外に変更する必要はありません。Noneしたがって、プレーヤーのタイプは none またはNoneType. 呼び出すメソッドrectによって使用されるメソッドを持たないものはありません。collide_rect

于 2014-06-14T06:44:51.023 に答える
0

あなたのコンパイラは何が間違っているかを教えています。

walls.update()

まず、プログラムがwalls.updateを呼び出しますが、ここから問題が始まります。

hit = pygame.sprite.collide_rect(self, self.player)

次に、壁とプレイヤーの間のヒット検出を試みます。

File "C:\Python34\lib\site-packages\pygame\sprite.py", line 1300, in collide_rect
return left.rect.colliderect(right.rect)

これは最終的に問題を引き起こした行であり、次のように述べているため、次の手がかりがこの行にあることに注意してください。

AttributeError: 'NoneType' object has no attribute 'rect'

壁 (self) または (self.player) のタイプが None であるため、衝突を判断できる属性 'rect' がありません。ここで注意してください:

player = None

あなたは決してプレーヤーを設定しません!したがって、プレーヤーのタイプは None であり、衝突検出を行うための属性 'rect' はありません。

于 2014-06-14T06:46:49.797 に答える