繁体   English   中英

我在pygame中创建移动平台时遇到了麻烦。 我遇到错误,有人可以帮我吗?

[英]I am having trouble creating moving platforms in pygames. I am getting an error, can anyone help me out please?

因此,我刚刚开始学习如何编程,并且一直在尝试使用移动平台来创建这款小型游戏。 我可以使常规的墙/平台正常工作,但似乎无法弄清楚如何使这些活动的墙/平台正常工作。 我不断收到如下所示的回溯:

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

MovingEnemy类中,有一个名为player的属性。 player的在开始时None 没有在代码中的任何地方将player更改为除None任何内容,因此播放器的类型为none或NoneType 没有没有方法rect它使用的collide_rect您调用方法。

您的编译器告诉您出了什么问题。

walls.update()

首先,您的程序调用wall.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”来进行碰撞检测。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM