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班级中,您有一个名为player. player的开始于None。您的代码中没有任何地方可以更改player为除 , 之外的任何内容None,因此 player 的类型为 none 或 a 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 回答